How to: Animate a Point by Using Key Frames

This example shows how to use the PointAnimationUsingKeyFrames class to animate a Point.

The following example moves an ellipse along a triangular path. The example uses the PointAnimationUsingKeyFrames class to animate the Center property of an EllipseGeometry. This animation uses three key frames in the following manner:

  1. During the first half second, uses an instance of the LinearPointKeyFrame class to move the ellipse along a path at a steady rate from its starting position. Linear key frames like LinearPointKeyFrame create a smooth linear interpolation between values.

  2. During the end of the next half second, uses an instance of the DiscretePointKeyFrame class to suddenly move the ellipse along the path to the next position. Discrete key frames like DiscretePointKeyFrame create sudden jumps between values.

  3. During the final two seconds, uses an instance of the SplinePointKeyFrame class to move the ellipse back to its starting position. Spline key frames like SplinePointKeyFrame create a variable transition between values according to the values of the KeySpline property. In this example, the animation begins slowly and speeds up exponentially toward the end of the time segment.

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

namespace Microsoft.Samples.KeyFrameExamples
    /// <summary> 
    /// This example shows how to use the PointAnimationUsingKeyFrames class 
    /// to animate the position of an object. 
    /// </summary> 
    public class PointAnimationUsingKeyFramesExample : Page
        public PointAnimationUsingKeyFramesExample()
            Title = "PointAnimationUsingKeyFrames Example";
            Background = Brushes.White;
            Margin = new Thickness(20);

            // Create a NameScope for this page so that 
            // Storyboards can be used.
            NameScope.SetNameScope(this, new NameScope());

            // Create an EllipseGeometry.
            EllipseGeometry myAnimatedEllipseGeometry =
                new EllipseGeometry(new Point(200,100), 15, 15);

            // Assign the EllipseGeometry a name so that 
            // it can be targeted by a Storyboard. 
                "MyAnimatedEllipseGeometry", myAnimatedEllipseGeometry);

            // Create a Path element to display the geometry.
            Path aPath = new Path();   
            aPath.Fill = Brushes.Blue;
            aPath.Data = myAnimatedEllipseGeometry;

            // Create a Canvas to contain the path.
            Canvas containerCanvas = new Canvas();
            containerCanvas.Width = 500;
            containerCanvas.Height = 400;

            // Create a PointAnimationUsingKeyFrames to 
            // animate the EllipseGeometry.
            PointAnimationUsingKeyFrames centerPointAnimation
                = new PointAnimationUsingKeyFrames();
            centerPointAnimation.Duration = TimeSpan.FromSeconds(5);

            // Animate from the starting position to (100,300) 
            // over the first half-second using linear 
            // interpolation.
                new LinearPointKeyFrame(
                    new Point(100, 300), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5))) // KeyTime

            // Animate from (100,300) (the value of the previous key frame)  
            // to (400,300) at 1 second using discrete interpolation. 
            // Because the interpolation is discrete, the ellipse will appear 
            // to "jump" to (400,300) at 1 second.
                new DiscretePointKeyFrame(
                    new Point(400, 300), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))) // KeyTime

            // Animate from (400,300) (the value of the previous key frame) to (200,100) 
            // over two seconds, starting at 1 second (the key time of the 
            // last key frame) and ending at 3 seconds.
                new SplinePointKeyFrame(
                    new Point(200, 100), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(3)), // KeyTime 
                    new KeySpline(0.6, 0.0, 0.9, 0.0) // KeySpline

            // Set the animation to repeat forever. 
            centerPointAnimation.RepeatBehavior = RepeatBehavior.Forever;

            // Set the animation to target the Center property 
            // of the object named "MyAnimatedEllipseGeometry".
            Storyboard.SetTargetName(centerPointAnimation, "MyAnimatedEllipseGeometry");
                centerPointAnimation, new PropertyPath(EllipseGeometry.CenterProperty));

            // Create a storyboard to apply the animation.
            Storyboard ellipseStoryboard = new Storyboard();

            // Start the storyboard when the Path loads.
            aPath.Loaded += delegate(object sender, RoutedEventArgs e)

            Content = containerCanvas;

  Background="White" Margin="20">
  <Canvas Width="400" Height="400">
    <Path Fill="Blue">

        <!-- Describes an ellipse. -->
        <EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
          Center="200,100" RadiusX="15" RadiusY="15" />
        <EventTrigger RoutedEvent="Path.Loaded">

              <!-- Animating the Center property uses 3 KeyFrames, which animate
                   the ellipse allong a triangular path. -->
                Duration="0:0:5" RepeatBehavior="Forever">

                <!-- Over the first half second, Using a LinearPointKeyFrame, the ellipse 
                     moves steadily from its starting position along the first line of the 
                     trianglar path.  -->
                  Value="100,300" />

                <!-- Using a DiscretePointKeyFrame, the ellipse suddenly changes position
                     after the first second of the animation. -->
                  Value="400,300" />

                <!-- Using a SplinePointKeyFrame, the ellipse moves back to its starting
                     position. It moves slowly at first and then speeds up. This key frame 
                     takes 2 seconds to complete. -->
                  KeySpline="0.6,0.0 0.9,0.00" 
                  Value="200,100" />

For the complete sample, see KeyFrame Animation Sample.

For consistency with other animation examples, the code versions of this example use a Storyboard object to apply the PointAnimationUsingKeyFrames. However, when applying a single animation in code, it's simpler to use the BeginAnimation method instead of using a Storyboard. For an example, see How to: Animate a Property Without Using a Storyboard.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft