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 21 January 2008 10:31 AM by jesseliberty

Comments

# DotNetKicks.com said on 21 January, 2008 10:34 AM

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

# wisecarver said on 21 January, 2008 10:50 AM

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.

# jesseliberty said on 21 January, 2008 11:02 AM

>>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.

# wisecarver said on 21 January, 2008 11:33 AM

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

# NasirHassan063 said on 22 January, 2008 04:53 AM

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.

# wisecarver said on 22 January, 2008 08:47 AM

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

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

# Community Blogs said on 23 January, 2008 03:31 PM

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

# Jesse Liberty - Silverlight Geek said on 25 January, 2008 11:48 AM

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

# Blogs said on 25 January, 2008 12:13 PM

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

Search

Go

This Blog

News

     
     
    What's New In Silverlight 3
    A Frequently Updated WikiDoc .
    Last Update: July 10
     
    AgOpenSource
    From Design to implementation
     
    Better Videos
    Diary of an experiment in extreme post-production
     
    Quick Bits
    Too big for Twitter, Too Small for the blog
     
    New to Silverlight?
    Click here to get started.

Syndication