Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

SilverLight FAQ Part 2 (Animations and Transformations)

Rate me:
Please Sign up or sign in to vote.
4.13/5 (6 votes)
10 Aug 2010CPOL7 min read 55.9K   54   4
Animations and Transformations in Silverlight
In this tutorial, you will learn about animation basics like timelines and storyboard. The article moves ahead to talk about different animations supported and the tutorial ends with a simple rectangle animation.

Update: Other SilverLight FAQ's links are moved to the bottom of this article.

Image 1

Video demonstration One Way, Two Way and One Time Bindings using Silverlight

Table of Contents

Introduction and Goal

This FAQ is completed dedicated to animations and transformations using SilverLight. The tutorial starts with animation basics like timelines and storyboard. Later, the article moves ahead to talk about different animations supported and we finally end the tutorial with a simple rectangle animation.

Some News About Silverlight Which Is Floating Around

I know I need to concentrate on technical for this article, but still I found something interesting to share about achievements and news about Silverlight , just thought it should be shared. If you are still thinking Silverlight is not worth putting effort in, then below is some news which can excite you:

  • 300 Million Installations of Silverlight, not bad as compared to Flash which is now there for years. We are not trying to underestimate Flash but the start of Flash was definitely not so good.
  • Ooooh Silverlight covered the last Olympics and NBC
  • 3000 developers and designers behind this initiative
  • Netfix CEO redhastings joins on the Microsoft board of directors

Still not convinced, read the detailed report at the end of this article here. Ok just so that I am not biased, I have also covered some bad news of Silverlight.

What is the Definition of Animation from Silverlight Perspective?

Animation is modification of a value over a period of time. Due to the modification of value over a period of time, it creates an illusion of animation. For instance, you can place a circle and modify the ‘Canvas.Right’ property to create an animation which moves the circle to the right.

Image 2

Figure: Canvas right direction

What is a Timeline in Silverlight?

Time represents a segment / unit of time on which the animation will move. The unit of time can be in seconds, minutes or hours. The unit depends on the animation nature. ‘System.Windows.Media.Animation.Timeline’ class represents the time line.

What are the Different Kinds of Animation Supported by Silverlight?

As said previously, animation is all about a start value and the moving towards some new value and thus providing illusion. Silverlight uses three properties for it: ‘From’, ‘To’ and ‘By’. ‘From’ specifies the start of the animation and ‘To’ specifies till where the animation should run. ‘By’ is relative animation. When we specify the ‘From’ and “By’ animation progresses from the value specified by the ‘From’ property to the value specified by the sum of the ‘From’ and ‘By’ properties.

Image 3

Figure: Different kind of animations

Now again using ‘From’, ‘By’ and ‘To’, you can have linear animation or you can have non-linear animation. In linear animation, everything is straight forward while non-linear animation changes as per animation needs.

Image 4

Figure: Linear and Non-linear

Can You Explain doubleanimation , coloranimation and pointanimation?

As discussed in the previous question, Silverlight animation is all about specifying ‘From’ ,’To’ and ‘By’ value for a property. Now the property can be a simple double value, can be color or can be a point. Silverlight has categorized these properties into three sections as explained below:

  • DoubleAnimation’ uses properties with double value, for instance Rectangle.Height or width. You can specify double values by using 'From', 'To' and 'By'.
  • PointAnimation’ uses point value, i.e., X, Y values for line segments and curves.
  • ColorAnimation’ helps to alter the value of color value of an object.

What is a Storyboard?

Storyboard is sequence of snapshots / sketches which depict changes over a period of time. You can visualize storyboard as a time line which we have discussed in one of our previous questions. Storyboard has collection of animations grouped together. The collection of animation is because storyboard uses sketches to depict changes over a period of time.

For instance, you can see there are four sketches used in the below animation to depict clash of two arrows with a boom boom at the end. So basically, storyboard will have collection of four animation objects which will be shown rapidly over a period of time.

Image 5

Figure: Arrow Sketches

Can We See a Simple Silverlight Animation to Just Get Started?

Let’s create a simple animation shown below. We will create a rectangle object whose height will increase in an animated manner. You can see from the below figure how the animation will look like. We will execute this animation using ‘DoubleAnimation’ object.

Image 6

Figure: Rectangle height animation

So the first step is to define the rectangle object. Below is the XAML code snippet which defines the rectangle object with height and width equal to 100 and chocolate background.

XML
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle x:Name="RectAnimated" Fill="Chocolate" Stroke="Black"
Width="100" Height="100">

</Rectangle>
</Grid>

As a second step, we need to define when the animation will be triggered. You can see from the below code snippet we have defined that the storyboard will be invoked when the rectangle object is loaded.

XML
<Grid x:Name="LayoutRoot" Background="White"> 
<Rectangle x:Name="RectAnimated" Fill="Chocolate" Stroke="Black"
Width="100" Height="100">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>

</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Grid>

Finally, we have put in the ‘DoubleAnimation’ object which uses the ‘Height’ as the target property which will be animated ‘100’ value to ‘300’ value in 5 seconds. Also note that target name is the rectangle object ‘RectAnimated’. We have also define ‘AutoReverse’ as ‘true’ which means once it reaches ‘300’, it will restart from ‘100’.

XML
<Grid x:Name="LayoutRoot" Background="White"> 
<Rectangle x:Name="RectAnimated" Fill="Chocolate" Stroke="Black"
Width="100" Height="100">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard x:Name="myStoryboard">
<DoubleAnimation
Storyboard.TargetName="RectAnimated"
Storyboard.TargetProperty="Height"
From="100" By="300" Duration="0:0:5" 
AutoReverse="True" 
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Grid>

What are the Different Ways in which Silverlight Does Transformation?

There are times when you want to transform your object in various ways. For example, you would like to tilt the object in 45 degree; you would like to skew the object or rotate the object. Below are some important transformation examples which you can achieve using Silverlight.

Below is a simple example which uses ‘RotateTransform’ to tilt the text at 45 degree.
XML
<TextBlock HorizontalAlignment="Center" 
Text="Text 
rotated by 45 degree">
<TextBlock.RenderTransform>
<RotateTransform Angle="45" />
</TextBlock.RenderTransform>
</TextBlock>

Image 7

S

Below is a simple example which uses ‘ScaleTransform’ to scale the text to ‘2’.
XML
<TextBlock VerticalAlignment="Center"
 HorizontalAlignment="Center" 
Text="Text scaled with 2">
<TextBlock.RenderTransform>
<ScaleTransform ScaleX="2"/> 
</TextBlock.RenderTransform>
</TextBlock>

Image 8

Below is a simple example which uses ‘RenderTransform’ to position your text at a particular X and Y position.
XML
<TextBlock VerticalAlignment="Center" 
HorizontalAlignment="Center" Text="Text 
with X/Y values">
<TextBlock.RenderTransform>
<TranslateTransform X="-100" Y="-100"/>
</TextBlock.RenderTransform>
</TextBlock>
Image 9
In case you want skew your object, below is a simple XAML code snippet which skews your rectangle object at 45 degree.
XML
<Rectangle Fill="Chocolate" 
Stroke="Black" Width="100" Height="100">
<Rectangle.RenderTransform>
<SkewTransform AngleX="45"/>
</Rectangle.RenderTransform>
</Rectangle>

Image 10

There situations when you would like to apply two or more transformation types on an object. In those scenarios, you can use ‘TransformGroup’ to apply multiple transformation.

Below is a code snippet which shows ‘SkewTransform’ and ‘RotateTransform’ applied in a group to the rectangle object.

XML
<Rectangle Fill="Chocolate" 
Stroke="Black" Width="100" Height="100">
<Rectangle.RenderTransform>
<TransformGroup>

<SkewTransform AngleX="45"/>
<RotateTransform Angle="45"/>

</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>

Image 11

Silverlight vs Flash Good News and Bad News

Some Bad News

Other Silverlight FAQ

Silverlight FAQ Part 1: This tutorial has 21 basic FAQs which will help you understand WPF, XAML, help you build your first Silverlight application and also explains the overall Silverlight architecture.

SilverLight FAQ's Part 3: This article discusses 12 FAQs which revolve around bindings, layouts, consuming WCF services and how to connect to database through Silverlight. The article first starts with bindings and discusses about one way, two way and one time bindings.

History

  • 1st June, 2009: Initial version

For Further reading do watch the below Interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
QuestionHow did you get your eventtrigger to run in Silverlight? Pin
dbrenth18-Aug-10 4:45
dbrenth18-Aug-10 4:45 
AnswerRe: How did you get your eventtrigger to run in Silverlight? Pin
Shivprasad koirala18-Aug-10 7:11
Shivprasad koirala18-Aug-10 7:11 
Generalbad link Pin
AndrusM8-Jun-09 9:04
AndrusM8-Jun-09 9:04 
GeneralRe: bad link Pin
ADLER19-Oct-09 8:14
ADLER19-Oct-09 8:14 
The link works!

_____________________________
The force of .NET is with me!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.