Today’s design guidelines state that all animated movement inside your application should contain so called easing. You can read this section from google material design. If your UI-framework does not contain implementations for standard set of easings you can create your own.
Here is place where you can check out animated plottings for basic easing functions. And here you can find source code for them in several languages (JS,Java,Lua,C#,C++,C).
C++ code for elastic easing (from here):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
float Elastic::easeIn (float t,float b , float c, float d) { if (t==0) return b; if ((t/=d)==1) return b+c; float p=d*.3f; float a=c; float s=p/4; float postFix =a*pow(2,10*(t-=1)); // this is a fix, again, with post-increment operators return -(postFix * sin((t*d-s)*(2*PI)/p )) + b; } float Elastic::easeOut(float t,float b , float c, float d) { if (t==0) return b; if ((t/=d)==1) return b+c; float p=d*.3f; float a=c; float s=p/4; return (a*pow(2,-10*t) * sin( (t*d-s)*(2*PI)/p ) + c + b); } float Elastic::easeInOut(float t,float b , float c, float d) { if (t==0) return b; if ((t/=d/2)==2) return b+c; float p=d*(.3f*1.5f); float a=c; float s=p/4; if (t < 1) { float postFix =a*pow(2,10*(t-=1)); // postIncrement is evil return -.5f*(postFix* sin( (t*d-s)*(2*PI)/p )) + b; } float postFix = a*pow(2,-10*(t-=1)); // postIncrement is evil return postFix * sin( (t*d-s)*(2*PI)/p )*.5f + c + b; } |
Input parameters here:
- t – time elapsed from start of animation
- b – start value
- c – value change
- d – duration of animation
There is nice online tool – Easing function generator. It generates polynomial functions as approximation. For example, elastic easing can be approximated:
1 2 3 4 5 |
function(t:Number, b:Number, c:Number, d:Number):Number { var ts:Number=(t/=d)*t; var tc:Number=ts*t; return b+c*(33*tc*ts + -106*ts*ts + 126*tc + -67*ts + 15*t); } |
In my framework i use easing formulas in more simple way. Instead of four parameters i use one! Easing can be described as time transformation f(t) -> t, which can be applied to multiple values at the same time. Also this gives a bit more clean formulas. Input time is in range [0.0..1.0], but output can exceed these values.
Here is an example for CircOut easing:
1 2 3 4 |
class MVAnimationEasing_CircOut : public MVAnimationEasing { public: inline virtual double ease(double t) final { t -= 1; return sqrt(1 - t*t); }; }; |
And base animation class could contain something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
double getProgress() { double progress = (currentTime - startTime)/(endTime - startTime); if (currentTime < startTime) progress = 0.0; if (currentTime > endTime) progress = 1.0; if (isInverted) progress = 1.0 - progress; progress = easing.ease(progress); return progress; } |
So the point is that easing is pretty easy.
ps. Don’t make your animations too long to annoy your users. Don’t make too many animations everywhere to defocus your users. Make animations so your users can better understand your UI.