The Artefact Animator library is based on the same concept as Tweener for Flash, which provides an easy way to do procedural animations in WPF and Silverlight.
I found some time last night to put together this little Xbox Live avatar mosaic and was very pleased with the results.
If you take a look at the prototype, you can see there’s a few things you can do:
- Drag the grid splitter, resize the window or change the padding slider to cause the tiles to reposition.
- Type a new Xbox Live Gamertag in the textbox and TAB out to cause the tiles to fade out and fade back in with a new image
- Roll over a tile and it will scale down until you roll off of it
You can download the source to see the full code but I’ll just highlight the areas where the Artefact Animator is used below.
Download the Library
The first thing you need to do is download a copy of the library from the codeplex site. Jesse has also posted documentation which will help you understand how to use the library. Once you have the library downloaded make sure to add a reference to your project.
Position the Avatar Tiles
When the application loads, AvatarTile controls are dynamically added to the main Canvas called LayoutRoot. Once the SizeChanged event of the Canvas is fired the updateTileLayout method is called. Calling the method at this point ensures the ActualWidth property of the Canvas is available and the tile layout will be repositioned if the browser window is resized or the grid splitter is moved.
Then we loop through the children of the Canvas and based on the index of the tile an easing function is added using the code below:
The element parameter defines which AvatarTile to animate.
AnimationTypes is an enum tied to different properties you can choose to animate.
The curX and curY parameters define the new value of the animated property.
The time parameter defines how long the animation should take to get to the new value.
The delay parameter defines how long before the animation should begin.
The ease parameter is a PercentHandler class instance used to define the type of easing function. The full list of Ease Functions is posted, along with how to create custom ease functions.
When determining the curX and curY values in the updateTileLayout method, padding is also taken into consideration. The slider sets the padding based on its position. After the slider is moved, the updateTileLayout method is called to reflect the change in padding.
Fade Out, New Image, Fade In
After a new value is entered into the textbox and the LostFocus event is fired, the AvatarTiles are faded out to reset the brush. This was a test of the available Events of the EaseObject.
After looping through the children and animating the Opacity of each element, a reference to the return value of the AddEase method is stored. This way we can add an event handler to the Completed event of the EaseObject instance. The event handler will then reset the image brush and animate the tiles back into view using the code below:
Mouse over action for the Tiles
And finally, taking advantage of some of the Shortcut functions built in, we’ll scale the AvatarTile based on the mouse
over events. In the AvatarTile class, the first step is to use the handy NormalizeTransformGroup method which ensures the needed Transform objects are in place.
Once the library is upgraded to Silverlight 4, this step could become obsolete by leveraging the new CompositeTransform class.
In the Loaded event:
Then on Mouse Enter:
And to remove the effect on Mouse Leave:
And that’s it! Everything is hooked up and ready to run.
I really like the direction Jesse is headed with the library by making procedural animations easier to write in Silverlight and WPF. He’s mentioned the library is in its early stages and would love to see more people use it and provide feedback.
One of the things I found is that after changing the gamertag and fading out the tiles, the repositioning animations got very slow. I’m not sure if that’s colliding easing objects or what, but there’s some performance work that could be done there.
The other issue is that due to the nature of procedural animations there is no design-time experience. The best thing about Storyboards is that you can create them visually in Blend and preview them without having to run the application. It’d be interesting to see if there was some way to tackle this problem. Not sure what to suggest yet.
What do you think? Give the Artefact Animator a try and send your feedback to Jesse.