Search code examples
c#wpfanimationtimereasing

'Motion easing' effect in C#


I need to increment an integer, but I want to ease the speed at which it's incremented. So say for example I have an int that is equal to 0, I would like this int to get to 100 eventually but increment progressively slower. Does anyone have experience with this?


Solution

  • hcb's answer works for 100, but a different value would require a different ease value.

    A more generalised answer would be to use a sine, which means the ease would be the same no matter what final value you wanted, or however many steps you take.

    private void EaseIn(int easeTo)
    {
         for (int n = 0; n < easeTo; n++)
         {
              double degrees = (n * 90) / easeTo;
              double easedN =  easeTo * Math.Sin(degrees * (Math.PI / 180));
    
              Console.WriteLine("Eased n = " + easedN.ToString());
         }
    }