Flash-style Tween/Easing Functions in C#

Easing functions make any movement look pretty. So you’re going to need them at some point.

And implementations are so widely available that you don’t have an excuse not to use them. I found this ActionScript reference to be very helpful in creating my own C# easing functions library, which I’d like to share… so here it is!

easing

First line is EaseIn, second is EaseOut, third is EaseInOut

Download

Easing.cs (4 kb – C# class)

It’s a static class and it’s framework-agnostic, completely standalone. The screenshot you see above is my TV3D test class. I won’t post the whole sample, but here’s the code if you want to see how I used it. In order to run, one would need a version of my components framework that I haven’t released yet.

I also didn’t implement each and every function that Robert Penner presents, just the ones I figured I’d use. Bounce and Elastic sound like physics to me, I don’t think I’d use easing functions to achieve that.

10 thoughts on “Flash-style Tween/Easing Functions in C#”

  1. “It’s a static class and it’s framework-agnostic, completely standalone.”

    This is not true. You use the XNA specific MathHelper class.

    1. Nope, a MathHelper static class stub is right there in the code file. I just named it the same as the XNA class so you can discard it if you do use XNA, but it doesn’t depend on anything.

      1. Hello Renaud. May you show how I should use this class? I try to include it to WindowsForms application and move my black square :)

        1. If you are moving your shape over time, then you’d do something like :

          CurrentPosition = Lerp(OldPosition, NewPosition, CurrentTime / TotalDuration)

          If you want to use my easing class, you’d do instead :

          Step = CurrentTime / TotalDuration
          EasedStep = Easing.EaseIn(Step, EasingType.Quadratic)
          CurrentPosition = Lerp(OldPosition, NewPosition, EasedStep)

          Or any other easing function over the step :)
          This is pseudocode, but should give you an idea.

  2. Hey there Renaud. I was wondering how one can get a hold of your unreleased component that will allow me to use your ported easing functions. I would love to try it out. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>