Setting up storyboard animations and manipulating properties of an object in the timeline either in Flash or Blend is a really good visual way to get a feel for how your animation is behaving as it’s being created but there’s a lot of situations where this falls short, either isn’t flexible enough, or can be too complex to achieve the effects required. A scenario I’ve hit in a current Silverlight project is creating a simple elastic effect on a dynamically created user control.
Flash/Flex developers have a number of tools available to them to make animation effects easy, programmable and procedural. Libraries like Tweener, TweenLite and Fuse enable developers to create animation transitions at runtime through code.
A tween library is must have in any developers toolkit so after searching around a bit and realising that there’s nothing widely available in Silverlight yet, I decided to port the Tweener Actionscript library to C# and Silverlight 1.1.
This post gives a high level run through of how to use the library and presents three demos that show the features of the library. By all means contact me with suggestions for improvement or effects that you’re trying to achieve. I haven’t covered all the features in Tweener yet, but the basics are there and it’s now opened up a whole new range of opportunities for development in Silverlight that hopefully you can get something out of.
Firstly a bit of credit to those who have done the ground work already. The awesome Zeh Fernando and Nate Chatellier created the Actionscript versions of Tweener. Yuichi Tateno created the javascript version JSTweener, and Harvey posted source on the Silverlight forum with the beginnings of a Silverlight tweener.
Robert Penner created a whole range of easing equations that have become a standard in transitions, especially in Flash. A graphic representation of each transition is shown here.
XAML supports basic KeySpline animation that lets you define the control points of a transition which gives you control over the acceleration of an easein/easeout transition. My previous post Keyspline animation in Silverlight shows how these control points affect the KeySpline animation.
The main limitations in using these KeySpline animations we’re faced with are:
- Overshoot, undershoot, elastic and bounce type transitions aren’t supported
- Preset KeySpline paths (ie. control point combinations) can’t easily be called by name. In Blend or in code I can’t easily select an “EaseOutReallyReallyFast” vs “EaseOutMondayMorningFast” transition.
Let’s take a quick look at what each of the Penner transition functions looks like with the first sample. You’ll need Silverlight 1.1 for this. This is the same moving box sample that Yuichi Tateno put together to demonstrate JSTweener. It’s a good first comparison of what’s needed to use the library in Silverlight.

The code for this sample is available on Codeplex. You’ll need to download the agTweener.zip file and TweenerBlock.zip.
In this sample we define a rectangle in Page.xaml
<Rectangle x:Name=“Block“ Width=“50“ Height=“50“ Fill=“#F37427“ Opacity=“0.8“/>
In the Page_Loaded handler we call a function that starts off the tween
Tweener.addTween(Block, p, new TweenEvent(Move2), null);
The important point to note is that there is no StoryBoard or DoubleAnimation XAML required at all in Page.xaml. The dynamic creation of this is handled within the Tweener library.
The rectangle to animate is passed as a parameter to the addTween method (can be any FrameworkElement) and the tween parameters are also passed in. The following code block shows how the tween parameters are set.
//set the target tween parameters
TweenParameter p = new TweenParameter();
p.transition = (TransitionType)Enum.Parse(typeof(TransitionType), _transitionFunction, true);
p.LeftProperty = PATH_TOP_RIGHT_X;
p.TopProperty = PATH_TOP_RIGHT_Y;
p.time = _time;
Tweener.addTween(Block, p, new TweenEvent(Move2), null);
The x,y coordinates for the target position of the block at the end of it’s motion are set in the p.LeftProperty and p.TopProperty properties. The time for the overall transition is set in p.time and the transition function to use is also set.
The four paths around the rectangle are set up as four separate tweens with the LeftProperty and TopProperty target values as the four corners of the rectangle. Just like the actionsript Tweener library, you can pass in a delegate to a function to call when the current tween is completed. In the code sample above we want the Move2 function called when the tween is finished. In Move2 we set the next end point and call:
Tweener.addTween(Block, p, new TweenEvent(Move3), null);
That’s pretty much it for the basics of Tweener and this sample. Now we’ll dig a bit deeper and go through the full features of what you can do with this version.
In the Flash version of Tweener you can tween any property of a movieclip by passing in a named array of property values
eg, Actionscript code
Tweener.addTween( target, { scaleX:1.5, scaleY:1.5, time:1, transition:”easeoutelastic”, onComplete:all_done } );
In this case the scaleX and scaleY properties on target will be tweened. This is a really flexible way of doing things. Sure it’s a bit prone to design time errors, but that’s the tradeoff. We don’t have the equivalent of named arrays in C#, so the next best thing is to create a parameter class and pass in values to the addTween method this way.
The TweenParameter class currently has the following properties for FrameworkElements. This represents all the properties that can have a tween transition applied to them.
|
LeftProperty
|
The Canvas.LeftProperty of the FrameworkElement
|
|
TopProperty
|
The Canvas.TopProperty of the FrameworkElement
|
|
Width
|
The Width of the FrameworkElement
|
|
Height
|
The Height of the FrameworkElement
|
|
X
|
The X property of a TranslateTransform
|
|
Y
|
The Y property of a TranslateTransform
|
|
ScaleX
|
The ScaleX property of a ScaleTransform
|
|
ScaleY
|
The ScaleY property of a ScaleTransform
|
|
SkewAngleX
|
The AngleX property of a SkewTransform
|
|
SkewAngleY
|
The AngleY property of a SkewTransform
|
|
RotateAngle
|
The Angle property of a RotateTransform
|
|
Opacity
|
The Opacity of the FrameworkElement
|
|
ZIndex
|
The Canvas.ZIndex of the FrameworkElement
|
It’s time to bring back Dooby to demonstrate this. In my previous KeySpline Animation post I used LiquidBoy’s Dooby in the sandpit to try out different keyspline animations. He’s back in the next sample to play around with some Tweener parameters on each of the transforms.

Again, the code for this sample is available on Codeplex. You’ll need to download the agTweener.zip file and TweenerDooby.zip.
Click the “Tween” button see Dooby transition from the reference position in the top left of the screen to the target position that you can define by either dragging Dooby to a new position, or type in the values in the form at the right.
I didn’t go as far as putting scale, rotate and skew controls around Dooby, so those properties are easy enough to just set directly in the HTML form.
I’ve also added in a nice little transition progress chart for each easing function that shows how the target values are being updated over time. The code was pretty simple in the end, check it out in the attachment.
When refreshing the page you may occassionally get a Javascript run time error. It appears to be something to do with the order in which the Silverlight object loads and wiring up the events here
_host.Content.Tweener.DoobyUpdatedEvent = firedFromScriptableEvent;
I haven’t figured that one out yet, so if you see it, just close your browser and load the sample again.
The last sample for this post demonstrates the event model around calling delegates on the onStart, onUpdate and onComplete events, and also using the delay property to set a delay before the tween begins. This sample is based on one of the original Tweener samples, ported across from Actionscript to C#.

20 blocks are dynamically created and added to the parent Canvas. Each block gets a random color from a predefined color palette.
A set of TweenParameter objects are created and the properties of each tween set.
open_tween.ScaleX = 1.5;
open_tween.ScaleY = 1.5;
open_tween.time = 0.5;
open_tween.RotateAngle = 90;
open_tween.Opacity = 0.3;
open_tween.transition = TransitionType.easeOutElastic;
We then attach event handlers to the MouseEnter, MouseLeave and MouseLeftButtonUp events as follows.
// Add some event listeners
b.MouseEnter += new MouseEventHandler(pop_open);
b.MouseLeave += new EventHandler(pop_close);
b.MouseLeftButtonUp += new MouseEventHandler(select_box);
The pop_open casts the sender to a Button class and then passes that the the addTween method to perform the selected tween on the target object.
private void pop_open(Object sender, EventArgs e)
{
Button target = (Button)sender; // Get the current button
object[] open_params = { “button open”, target.GetValue(Canvas.LeftProperty)};
Tweener.addTween(target.Box, open_tween, new TweenEvent(UpdateTotalTweens), null, null, null, new TweenEvent(UpdateTotalTweens), open_params);
The UpdateTotalTweens method is created as a delegate function for the onStart and onComplete events. When the tween starts we want to update the total count, and we it completes we also want to update that value. An object array can also be passed as parameters for each of these tween events and returned as a parameter array to the delegate function.
private void UpdateTotalTweens(params object[] args)
{
string msg;
if (args == null)
msg = “”;
else
msg = (string)args[0] + ” “ + args[1].ToString();
this.TotalTweens.Text = “Total active tweens: “ + Tweener.TweenCount + “. Parameter on update: “ + msg;
}
That’s the start of what should hopefully be a useful toolkit for Silveright designers and developers. The similarity to the Tweener interface should make it easier for those coming across from Flash/Flex.
A version compatible with Silverlight 2.0 will be available right after MIX thanks to LiquidBoy’s connections and colloboration on this.
In the mean time let me know what else you’d like to see in here, or if you’d like to contribute to the project at CodePlex.
MC