This documentation is archived and is not being maintained.

How to: Animate an Object Along a Path (Matrix Animation with Offset Accumulation)

This example shows how to use the MatrixAnimationUsingPath class to animate an object along a path and have that animation accumulate its offset values as it repeats.


The following example uses the MatrixAnimationUsingPath object to animate the Matrix property of a MatrixTransform applied to a button. As a result, a button moves along a curved path.

In addition, the example sets the IsOffsetCumulative property to true, which causes the offset of the animated matrix to accumulate as the animation repeats. Because the offset accumulates, the button moves farther across the screen when the animation repeats, rather than resetting to the starting position.

  xmlns:x="" Margin="20"
  <Canvas Width="400" Height="400">

    <!-- The Button that is animated across the screen by animating
         the MatrixTransform applied to the button. -->
    <Button MinWidth="100" Content="A Button">
        <MatrixTransform x:Name="myMatrixTransform">
          <MatrixTransform.Matrix >
            <Matrix />
        <EventTrigger RoutedEvent="Button.Click">
          <BeginStoryboard Name="Foo">
                    Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" 
                    PresentationOptions:Freeze="True" />

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SDKSample

    /// <summary>
    /// Shows how to use the MatrixAnimationUsingPath.IsOffsetCumulative
    /// property to make a MatrixAnimatioinUsingPath accumulate
    /// its values when it repeats.
    /// </summary>
    public class MatrixAnimationUsingPathExampleOffsetCumulative : Page

        public MatrixAnimationUsingPathExampleOffsetCumulative()
            this.Margin = new Thickness(20);

            // Create a NameScope for the page so that
            // we can use Storyboards.
            NameScope.SetNameScope(this, new NameScope());

            // Create a button.
            Button aButton = new Button();
            aButton.MinWidth = 100;
            aButton.Content = "A Button";

            // Create a MatrixTransform. This transform
            // will be used to move the button.
            MatrixTransform buttonMatrixTransform = new MatrixTransform();
            aButton.RenderTransform = buttonMatrixTransform;

            // Register the transform's name with the page
            // so that it can be targeted by a Storyboard.
            this.RegisterName("ButtonMatrixTransform", buttonMatrixTransform);

            // Create a Canvas to contain the button
            // and add it to the page.
            // Although this example uses a Canvas,
            // any type of panel will work.
            Canvas mainPanel = new Canvas();
            mainPanel.Width = 400;
            mainPanel.Height = 400;
            this.Content = mainPanel;

            // Create the animation path.
            PathGeometry animationPath = new PathGeometry();
            PathFigure pFigure = new PathFigure();
            pFigure.StartPoint = new Point(10, 100);
            PolyBezierSegment pBezierSegment = new PolyBezierSegment();
            pBezierSegment.Points.Add(new Point(35, 0));
            pBezierSegment.Points.Add(new Point(135, 0));
            pBezierSegment.Points.Add(new Point(160, 100));
            pBezierSegment.Points.Add(new Point(180, 190));
            pBezierSegment.Points.Add(new Point(285, 200));
            pBezierSegment.Points.Add(new Point(310, 100));

            // Freeze the PathGeometry for performance benefits.

            // Create a MatrixAnimationUsingPath to move the
            // button along the path by animating
            // its MatrixTransform.
            MatrixAnimationUsingPath matrixAnimation =
                new MatrixAnimationUsingPath();
            matrixAnimation.PathGeometry = animationPath;

            // Set IsOffsetCumulative to true so that the animation
            // values accumulate when its repeats. 
            matrixAnimation.IsOffsetCumulative = true;
            matrixAnimation.Duration = TimeSpan.FromSeconds(5);
            matrixAnimation.RepeatBehavior = new RepeatBehavior(2);

            // Set the animation to target the Matrix property
            // of the MatrixTransform named "ButtonMatrixTransform".
            Storyboard.SetTargetName(matrixAnimation, "ButtonMatrixTransform");
                new PropertyPath(MatrixTransform.MatrixProperty));
            // Create a Storyboard to contain and apply the animation.
            Storyboard pathAnimationStoryboard = new Storyboard();

            // Start the animation when the button is clicked.
            aButton.Click += delegate(object sender, RoutedEventArgs e)
                // Start the storyboard.


Note that, although the IsOffsetCumulative property causes offset values to accumulate over repetitions, it doesn't cause rotation values to accumulate. To make rotation values accumulate, set the animation's DoesRotateWithTangent and IsAngleCumulative properties to true.

For the complete sample, see Path Animation Sample. For an example showing how to animate a Matrix value along a path without offset accumulation, see How to: Animate an Object Along a Path (Matrix Animation).

See Also