Animating Clipping Paths in Silverlight 2

Clipping paths provide a way to hide sections of an object based on different geometries. You can apply rectangles, ellipses or even create your own custom paths to a target object and hide specific areas from view.

I enjoy experimenting with things from time to time and decided to try creating a sports score scroller -- similar to this one found at the top of espn.com -- to see how much could be done with pure XAML. It allows a user to scroll a set of scores left or right by moving their mouse over corresponding arrows.

While building the prototype, I realized that I needed a rectangle animation to move a rectangle around to show and hide different parts of score objects. As the user scrolls left, I would adjust the rectangle's position to show the hidden area and then hide that area again as they scroll right.

WPF provides a RectAnimation object but, unfortunately, it's not available in Silverlight. That meant I had to use points instead and then use a PointAnimation to animate them (the points are shown below in the LineSegment elements). By doing that, I can simulate a rectangle animation. Here's an example of the main XAML for the sports score scroller.

This XAML displays two JPG images containing scores and two JPG images representing arrows -- yes, all of this could be done with XAML instead of images but my goal was to get something simple working quickly -- but only shows 596 pixels total for the width of the score images. Anything to the right of that number is clipped due to the PathFigure that's defined and corresponding LineSegment elements. If you're new to PathGeomery and clipping, just imagine a rectangle starting at 0,0 that has a height of 45 and a width of 596. Everything to the right of the rectangle is clipped when the sports scroller first loads (in the image, that means everything to the right of the red right arrow is clipped).

As users mouse over one of the arrows, I need to move the Canvas holding the sports score images (cScrollerItems) to the right or left. However, I also need to move the right line of the clipping rectangle (the line made from 596,0 to 596,45) right or left to show the hidden area of the image. Fortunately, this is easy to do with a Storyboard element and a PointAnimation. Here are the animations that are called as the user mouses over the left or right arrows.

All of this works great from a prototype perspective but you can see that specific points are hard-coded into the animation elements. My goal was to do as much as possible in XAML without writing C# code, but with a little extra code it would be fairly straightforward to dynamically change the widths defined in the XAML based on the size of the images being displayed (or the width of text if that was used instead).

A lot more could be done to enhance the scroller, but it's nice to be able to create something like this with minimal code in a short period of time. An example of the scroller in action can be seen here (WMV video).

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.