darren david, gui geek

PennerDoubleAnimation and Animator: code-based WPF animation simplified

When I first started programming in WPF, I was frustrated by several things about the animation system:

  • a timeline in WPF is absolutely nothing like a timeline in Flash (I get it now)
  • animations in WPF are like functions applied to property values — when the animation completes, the property retains its original value and the animation stays “applied.” If you then want to change the property value manually, you need to remove the animation first. (Flash does not work this way, so it seemed odd at the time)
  • how complicated it was to pull off a simple code-based animation.
  • WPF didn’t ship with any custom easing equations, like Flash’s ubiquitous equations written by Robert Penner. The .NET 3.0 SDK now ships with some sample code that more-or-less implements reasonable facsimiles, but you still need to write several lines of code to kick off an animation. Even then, you’ve got to tweak a stack of parameters to get some of the more esoteric equations (like Bounce and Back) to work just right. Sometimes you just want it to be easy.

If you’ve come from the Flash world, you’re likely used to working with the Tween class, or better yet, a nice wrapper like Fluid’s Tweener. Either way, if you’re doing a lot of animation in code (which you are wont to do in ActionScript development), you really become accustomed to stacking up and firing off animations with one line of code.

Now that Blend has matured, I’ve become much more in tune with WPF’s Storyboards and Timelines, but I still find myself doing a lot of animation in code. I’ve been meaning to create an Penner animation class that utilized the WPF animation engine for some time now (Lee Brimelow took a first stab at the equations), and my latest project gave me an excuse to finally put together a polished package.

I had three primary goals in mind:

  • Be able to start an animation and assign an “OnComplete” handler in one line of code
  • Have those animations set the final value of the property to the “To” value of the animation, then disappear (basically, if I animate the Canvas.Left property of a Rectangle from 0 to 100, I expect the Canvas.Left property to be 100 when the animation completes, and I want to be able to assign a new value directly to that property)
  • Bring the canonical Penner easing equations to WPF, but leverage the WPF animation engine so I can use them in XAML or in code.

I created two classes, PennerDoubleAnimation, and a helper class called Animator. PennerDoubleAnimation extends DoubleAnimationBase, and provides all of the original Penner animations, plus some new “OutIn” equations created by Zeh Fernando in his MC Tween package, for a total of 41 equations. You can use this class in place of any DoubleAnimation, and it should work like a charm. However, if you really want it good, pair it with the Animator class, for one line happiness:

Animator.AnimatePenner( myControl, Canvas.LeftProperty,

   PennerDoubleAnimation.Equations.QuadEaseOut,

   0, 100, 1500, OnAnimationComplete );

Granted, it’s a long line, but it’s still easy. :)

I’m hosting this project on Google Code at http://code.google.com/p/wpf-animation/. This is my first project hosted on Google Code, so please bear with me (or better yet, let me know) if something seems amiss. You can download the source or check out the tree from Subversion.

I’ll spare you the usage details in this post; I’ve created a tester app (in the source zip) called AnimationTester to help you get your head around them.

Happy tweening!

[UPDATE] Removed direct download link.

[UPDATE] Fixed links pointing to wrong packages!

| Trackback

11 Comments so far

  1. Robby Ingebretsen May 16th, 2007 5:59 am

    This is awesome stuff. DoubleAnimation is officially of my list and PennerDoubleAnimation on it…

  2. [...] he’s been successful on those goals. Check this blog post for more [...]

  3. [ draw.logic ] WPF Animation Kit « May 16th, 2007 11:59 pm

    [...] Animation Kit May 17th, 2007 — drawk Darren David has posted a WPF animation kit to help bring it to Actionscript level of one line animation calls such as to packages like [...]

  4. Bo May 24th, 2007 4:36 am

    if I start 2 animation on the same element its like the tween is not overruled , resulting in some jerkyness ? is this possible to avoid ? I´am moving a stackpanel back and fourth using keyboard inputs ..

  5. Daniel Wabyick May 28th, 2007 11:52 am

    This is very cool Darren. One thing you should consider is refining the syntax to use a parameters object like Zeh’s Tweener library. It really makes sense as it allows you to just specify the transitions you need:

    Tweener.addTween(myMovie, {_x:-10, time:1, delay:1, transition:”linear”});

    The lib:
    http://code.google.com/p/tweener/

    Having used a number of Tween libs (and written a few), this one has my favorite syntax.

  6. notstatic.com May 30th, 2007 1:54 pm

    [...] have been thinking about the same thing because not long after my conversation with Lee, the PennerDoubleAnimation class showed up on his blog.  Very cool [...]

  7. euclidez July 2nd, 2007 1:52 pm

    Hello David, how can I animate the ScaleX from:

    I saw you use Animator.AnimatePenner(DependencyObject, DependencyProperty, …) How can I animate the ScaleTransform.

    Thanks a lot !!

  8. Darren July 10th, 2007 2:18 pm

    This is very simple. All you need is a pointer to the ScaleTransform whose properties you want to animate, for example:

    ScaleTransform st = new ScaleTransform();
    myFrameworkElement.RenderTransform = st;

    Animator.AnimatePenner(
    st,
    ScaleTransform.ScaleX,
    PennerDoubleAnimation.Equations.QuadEaseOut,
    1,
    2,
    1000,
    null);

  9. Fiscus Media September 11th, 2007 9:40 am

    This kicks ass! Setting keyframes dynamicly in Blend just doesn’t work, and the power of one line MCTween animation was the trick up my sleeve for so long. THANKS!!!!!

  10. Steven July 2nd, 2008 3:56 am

    Woo that’s great ! Thx a lot !!

  11. hs September 30th, 2008 7:49 am

    Great stuff, but the Linear Equation causes an error for me - though the other Equations work just fine.

Leave a reply

Mexico