Before talking about the tween class, you have to mention the Bezier curve. First, the Bezier curve refers to a smooth curve drawn from any point coordinates of four positions. It is used more in drawing tools or animations, such as the pen tool in PS, the brushes in firework, and so on. Regardless of where they are applied, they are all the same in principle. Similarly, when using JS to achieve the motion effect, we can also use Bezier curve to achieve different effects, and tween.js is a well-packaged computing aid algorithm. You can enter multiple values consecutively, then use Bezier formulas to output different values, resulting in a smooth curve. Because the values on a curve are not the same, we can use the characteristics of the curve to create different effects.
Tween.js encapsulates a number of computational methods for the effects, and we can use the formulas inside or rewrite them ourselves. The following is the source code, can be used according to their own needs and additions.
//Tween ClassvarTween ={Linear:function(t,b,c,d) {returnC*T/D + b; },Quad: {easeIn:function(t,b,c,d) {returnc* (T/=d) *t + b;}, EaseOut:function(t,b,c,d) {return-c * (t/=d) * (t-2) + B;}, Easeinout:function(t,b,c,d) {if((T/=D/2) < 1)returnC/2*t*t + b;return-C/2 * ((--T) * (t-2)-1) + B;}}, Cubic: {easeIn:function(t,b,c,d) {returnc* (T/=d) *t*t + b;}, EaseOut:function(t,b,c,d) {returnc* ((t=t/d-1) *t*t + 1) + B;}, Easeinout:function(t,b,c,d) {if((T/=D/2) < 1)returnC/2*t*t*t + b;returnC/2* ((t-=2) *t*t + 2) + B;}}, quart: {easeIn:function(t,b,c,d) {returnc* (T/=d) *t*t*t + b;}, EaseOut:function(t,b,c,d) {return-C * ((t=t/d-1) *t*t*t-1) + B;}, Easeinout:function(t,b,c,d) {if((T/=D/2) < 1)returnC/2*t*t*t*t + b;return-C/2 * ((t-=2) *t*t*t-2) + B;}}, Quint: {easeIn:function(t,b,c,d) {returnc* (T/=d) *t*t*t*t + b;}, EaseOut:function(t,b,c,d) {returnc* ((t=t/d-1) *t*t*t*t + 1) + B;}, Easeinout:function(t,b,c,d) {if((T/=D/2) < 1)returnC/2*t*t*t*t*t + b;returnC/2* ((t-=2) *t*t*t*t + 2) + B;}}, Sine: {easeIn:function(t,b,c,d) {return-c * Math.Cos (T/D * (MATH.PI/2)) + C +b; }, EaseOut:function(t,b,c,d) {returnc * Math.sin (T/D * (MATH.PI/2)) +b; }, Easeinout:function(t,b,c,d) {return-C/2 * (Math.Cos (MATH.PI*T/D)-1) +b; }}, Expo: {easeIn:function(t,b,c,d) {return(t==0)? B:C * MATH.POW (2, Ten * (T/D-1)) + B;}, EaseOut:function(t,b,c,d) {return(T==d)? B+C:C * (-math.pow (2, -10 * t/d) + 1) + B;}, Easeinout:function(t,b,c,d) {if(t==0)returnb; if(T==d)returnB +C; if((T/=D/2) < 1)returnC/2 * MATH.POW (2, Ten * (t-1)) + B;returnC/2 * (-math.pow (2, -10 *--t) + 2) + B;}}, Circ: {easeIn:function(t,b,c,d) {return-C * (MATH.SQRT (1-(T/=d) *t)-1) + B;}, EaseOut:function(t,b,c,d) {returnc * MATH.SQRT (1-(t=t/d-1) *t) + B;}, Easeinout:function(t,b,c,d) {if((T/=D/2) < 1)return-C/2 * (MATH.SQRT (1-t*t)-1) + B;returnC/2 * (MATH.SQRT (1-(t-=2) *t) + 1) + B;}}, Elastic: {easeIn:function(t,b,c,d,a,p) {if(t==0)returnbif((T/=d) ==1) return b+c; if (!p) p=d*.3;if(!a | | A < Math.Abs (c)) {a=c;varS=P/4; }Else vars = p/(2*math.pi) * Math.asin (c/a); return-(A*math.pow (2,10* (t-=1)) * Math.sin ((t*d-s) * (2*MATH.PI)/p) + B;}, EaseOut:function(t,b,c,d,a,p) {if(t==0)returnbif((T/=d) ==1) return b+c; if (!p) p=d*.3;if(!a | | A < Math.Abs (c)) {a=c;varS=P/4; }Else vars = p/(2*math.pi) * Math.asin (c/a); return(A*math.pow (2,-10*t) * Math.sin ((t*d-s) * (2*MATH.PI)/p) + C + B);}, Easeinout:function(t,b,c,d,a,p) {if(t==0)returnbif((T/=D/2) ==2)returnB+c;if(!p) p=d* (. 3*1.5); if(!a | | A < Math.Abs (c)) {a=c;varS=P/4; }Else vars = p/(2*math.pi) * Math.asin (c/a); if(T < 1)return-.5* (A*math.pow (2,10* (t-=1)) * Math.sin ((t*d-s) * (2*MATH.PI)/p) + B;returnA*math.pow (2,-10* (t-=1)) * Math.sin ((t*d-s) * (2*MATH.PI)/p) *.5 + C + B;}}, back: {easeIn:function(t,b,c,d,s) {if(s = = undefined) s = 1.70158; returnc* (T/=d) *t* ((s+1) *t-s) + B;}, EaseOut:function(t,b,c,d,s) {if(s = = undefined) s = 1.70158; returnc* ((t=t/d-1) *t* ((s+1) *t + s) + 1) + B;}, Easeinout:function(t,b,c,d,s) {if(s = = undefined) s = 1.70158; if((T/=D/2) < 1)returnC/2* (t*t* (((s*= (1.525) +1) *t-s) + B;returnC/2* ((t-=2) *t* (((s*= (1.525)) +1) *t + s) + 2) + B;}}, Bounce: {easeIn:function(t,b,c,d) {returnC-tween.bounce.easeout (d-t, 0, C, D) +b; }, EaseOut:function(t,b,c,d) {if((T/=d) < (1/2.75)) { returnc* (7.5625*t*t) +b; } Else if(T < (2/2.75)) {returnc* (7.5625* (t-= (1.5/2.75)) *t +.) + B; }Else if(T < (2.5/2.75)) {returnc* (7.5625* (t-= (2.25/2.75)) *t +. 9375) + b; }Else { returnc* (7.5625* (t-= (2.625/2.75)) *t +. 984375) + b;}}, Easeinout:function(t,b,c,d) {if(T < D/2) return Tween.Bounce.easeIn (t*2, 0, C, D) *. 5 + b;Else returnTween.Bounce.easeOut (t*2-d, 0, C, D) *. 5 + c*.5 +b; } }};
This algorithm can be used in many places, if the scroll bar movement, block movement or various gradients and so on. Today, I'm going to use the object to move the demo example of how to use this auxiliary calculation class, first we have to create a timer or function, which is my usual method.
//use Tween.js to return special values to produce different effects functionTweenfn (OBJ,ATTR,VALUE,ENDFN) {varTimer =NULL; varstart = 0;//Start Position//var value = value//change value size vart = 0;//starting from 0 steps varEndt = 30;//End of Stepsclearinterval (timer); Timer= SetInterval (function() {T++; if(t>Endt) {clearinterval (timer); ENDFN&& ENDFN ();//the callback function exists and returns return; }; OBJ.STYLE[ATTR]= Tween.Cubic.easeInOut (T,start,value,endt) + "px"; },30); }
Function Description:obj, the object that the binding executes;
attr, changing the attribute value;
value, changing the size of the values;
ENDFN, executed callback function, no write-off;
start, property initial value;
T,endt, the number of steps performed can be understood as how many times the execution is completed.
function 16th in line Tween.Cubic.easeInOut (...) To invoke a method in Tween.js, you can modify the value of cubic or easeinout according to the actual requirements. I put the following list of all the methods inside:
| Linear |
Linear constant Change |
| Quad |
EaseIn
EaseOut Easeinout |
Secondary square easing |
Expo |
EaseIn
EaseOut Easeinout |
Exponential curve easing |
| Cubic |
EaseIn
EaseOut Easeinout |
Three-time Square easing |
Circ |
EaseIn
EaseOut Easeinout |
Circular curve easing |
| Quart |
EaseIn
EaseOut Easeinout |
Four-time Square easing |
Elastic |
EaseIn
EaseOut Easeinout |
Elastic telescopic easing |
| Quint |
EaseIn
EaseOut Easeinout |
Five-time Square easing |
Back |
EaseIn
EaseOut Easeinout |
Return ease |
| Sine |
EaseIn
EaseOut Easeinout |
Sinusoidal easing |
Bounce |
EaseIn
EaseOut Easeinout |
Bounce Ease |
Using tween.js algorithm to generate easing effect