Page view counter

Did You Know... There Are Four Basic Animation Mechanisms?

Silverlight offers you four ways to move an object from here to there

From/To Animation

  1. Linear Key Frame Animation
  2. Discrete Key Frame Animation
  3. Spline Key Frame Animation

From - To Animations

(From there to here, from here to there, funny things are everywhere)

From/To animation is the simplest to understand and thus to implement. You create a Storyboard and within that you create a DoubleAnimation object (you use a DoubleAnimation because the value you are going to change is of type Double).

One example of a value you might set would be the location (e.g., Canvas.Left), or you might set the Opacity of an object. In either case, you start at some value (From) and you end up at some other value (To) and you get from one value to the other over a period of time (the duration).

Here are the properties you'll set for your DoubleAnimation

  • Name - the name of the Animation object so that you can refer to it from your code
  • Duration - the period over which you want the animation to run - make the period short and the animation will run quickly.
  • From - the starting value
  • To - The ending value
  • Storyboard.TargetName - the name of the object to animate
  • Storyboard.TargetProperty - the property in the animated object whose value will change

As an example, you can create a simple square, and then move it from its initial position across the Silverlight control using a From/To Double animation by creating a Storyboard in the Canvas.Resource and then obtaining a reference to that Storyboard in the onload event handler in the code-behind and calling Begin on the storyboard.

Scene.xaml

<Canvas xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Canvas.Resources>
        <Storyboard x:Name="MoveSquareStoryBoard" >
            <DoubleAnimation x:Name="Animate" Duration="00:00:02.00" 
                      From = "100" To = "400"       
                      Storyboard.TargetName="mySquare" 
                      Storyboard.TargetProperty="(Canvas.Left)" />
        </Storyboard>
    </Canvas.Resources>

  <Rectangle
    Name ="mySquare"
    Height="100"
    Width="100"
    Fill="Blue"
    Stroke="Black"
    StrokeThickness="3"
    Canvas.Left="100"
    Canvas.Top ="20"/>

</Canvas>

Scene.Xaml.js

if (!window.Animations)
    window.Animations = {};

Animations.Scene = function() 
{
}

Animations.Scene.prototype =
{
    handleLoad: function(plugIn, userContext, rootElement) 
    {
        var storyboard = plugIn.content.FindName("MoveSquareStoryBoard");
        storyboard.Begin();
    }
}

All of which is pretty straight forward. You can readily see how changing the target property from Canvas.Left to opacity will cause the square to stop sliding across the control and instead cause it to fade out (it would be good programming practice to change the name of the story board to reflect its new purpose; I've intentionally not done so here to show how little must change. I've not even added an opacity property to the square!),

   <Canvas.Resources>
       <Storyboard x:Name="MoveSquareStoryBoard" >
           <DoubleAnimation x:Name="Animate" Duration="00:00:02.00" 
                     From = "1" To = ".25"       
                     Storyboard.TargetName="mySquare" 
                     Storyboard.TargetProperty="Opacity" />
       </Storyboard>
   </Canvas.Resources>

 <Rectangle
   Name ="mySquare"
   Height="100"
   Width="100"
   Fill="Blue"
   Stroke="Black"
   StrokeThickness="3"
   Canvas.Left="100"
   Canvas.Top ="20"
   />

Note that Opacity is measured from 1 (fully opaque) to 0 (fully transparent) and we move down to .25 (1/4 opaque) which explains why we use a double and not an integer for the animation. The result is a dramatic fading away of the square,

Opacity

I will describe the key-frame Animations in another Tip of the Day.

kick it on DotNetKicks.com
Published Monday, January 21, 2008 10:31 AM by jesseliberty

Comments

# From-To Animation in Silverlight

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Monday, January 21, 2008 10:34 AM by DotNetKicks.com

# re: Did You Know... There Are Four Basic Animation Mechanisms?

Hi Jesse,

I've done a ton of animation over the years, cartooning in Flash, and am very interested in how frames and key frames will evolve in Silverlight. Also very interested if there will be any Developer tools to help us calculate points or if we will need to do the math. Thanks.

Monday, January 21, 2008 10:50 AM by wisecarver

# re: Did You Know... There Are Four Basic Animation Mechanisms?

>>Also very interested if there will be any Developer tools to help us calculate points or if we will need to do the math.<<

Well, of course, Expression Blend will do all that for you in a WYSIWYG environment; is that what you had in mind?

Take a look here: www.microsoft.com/.../overview.aspx click on Tools for Creativity and then click on Keyframe Animation as a start.

Monday, January 21, 2008 11:02 AM by jesseliberty

# re: Did You Know... There Are Four Basic Animation Mechanisms?

Thanks Jesse. I'm currently in VS 2005 Pro and 2008 Express, have not tried Blend yet.

Monday, January 21, 2008 11:33 AM by wisecarver

# re: Did You Know... There Are Four Basic Animation Mechanisms?

i have never made an animation on silverlight yet, but to see that article im really happy that it could be more easier way with silverlight.

Tuesday, January 22, 2008 4:53 AM by NasirHassan063

# re: Did You Know... There Are Four Basic Animation Mechanisms?

Jesse, the Silverlight Share the Love site is very well done...Please share it on the main Silverlight page. ;-)

www.microsoft.com/.../default.html

Tuesday, January 22, 2008 8:47 AM by wisecarver

# Silverlight Cream for January 23, 2008 -2 -- #178

Tim Sneath discusses the Update capability just released for SL 1.0, Shawn Wildermuth gives us a *very

Wednesday, January 23, 2008 3:31 PM by Community Blogs

# Did You Know... There are 3 Versions of Keyframe Animation?

This is, as you may have guessed, the follow up to the previous Tip of the Day on Animation.In today&#39;s

Friday, January 25, 2008 11:48 AM by Jesse Liberty - Silverlight Geek

# Did You Know... There are 3 Versions of Keyframe Animation?

Despite the fact that my animations showed up on my blog when I wrote this they are not showing up now

Friday, January 25, 2008 12:13 PM by Blogs