4 (c) 2003 Robert Penner, all rights reserved.
5 This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.html.
7 These tweening functions provide different flavors of
8 math-based motion under a consistent API.
25 1.5 - added bounce easing
26 1.4 - added elastic and back easing
27 1.3 - tweaked the exponential easing functions to make endpoints exact
28 1.2 - inline optimizations (changing t and multiplying in one step)--thanks to Tatsuo Kato for the idea
30 Discussed in Chapter 7 of
31 Robert Penner's Programming Macromedia Flash MX
32 (including graphs of the easing equations)
34 http://www.robertpenner.com/profmx
35 http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
39 // simple linear tweening - no easing
40 // t: current time, b: beginning value, c: change in value, d: duration
41 Math.linearTween = function (t, b, c, d) {
46 ///////////// QUADRATIC EASING: t^2 ///////////////////
48 // quadratic easing in - accelerating from zero velocity
49 // t: current time, b: beginning value, c: change in value, d: duration
50 // t and d can be in frames or seconds/milliseconds
51 Math.easeInQuad = function (t, b, c, d) {
52 return c*(t/=d)*t + b;
55 // quadratic easing out - decelerating to zero velocity
56 Math.easeOutQuad = function (t, b, c, d) {
57 return -c *(t/=d)*(t-2) + b;
60 // quadratic easing in/out - acceleration until halfway, then deceleration
61 Math.easeInOutQuad = function (t, b, c, d) {
62 if ((t/=d/2) < 1) return c/2*t*t + b;
63 return -c/2 * ((--t)*(t-2) - 1) + b;
67 ///////////// CUBIC EASING: t^3 ///////////////////////
69 // cubic easing in - accelerating from zero velocity
70 // t: current time, b: beginning value, c: change in value, d: duration
71 // t and d can be frames or seconds/milliseconds
72 Math.easeInCubic = function (t, b, c, d) {
73 return c*(t/=d)*t*t + b;
76 // cubic easing out - decelerating to zero velocity
77 Math.easeOutCubic = function (t, b, c, d) {
78 return c*((t=t/d-1)*t*t + 1) + b;
81 // cubic easing in/out - acceleration until halfway, then deceleration
82 Math.easeInOutCubic = function (t, b, c, d) {
83 if ((t/=d/2) < 1) return c/2*t*t*t + b;
84 return c/2*((t-=2)*t*t + 2) + b;
88 ///////////// QUARTIC EASING: t^4 /////////////////////
90 // quartic easing in - accelerating from zero velocity
91 // t: current time, b: beginning value, c: change in value, d: duration
92 // t and d can be frames or seconds/milliseconds
93 Math.easeInQuart = function (t, b, c, d) {
94 return c*(t/=d)*t*t*t + b;
97 // quartic easing out - decelerating to zero velocity
98 Math.easeOutQuart = function (t, b, c, d) {
99 return -c * ((t=t/d-1)*t*t*t - 1) + b;
102 // quartic easing in/out - acceleration until halfway, then deceleration
103 Math.easeInOutQuart = function (t, b, c, d) {
104 if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
105 return -c/2 * ((t-=2)*t*t*t - 2) + b;
109 ///////////// QUINTIC EASING: t^5 ////////////////////
111 // quintic easing in - accelerating from zero velocity
112 // t: current time, b: beginning value, c: change in value, d: duration
113 // t and d can be frames or seconds/milliseconds
114 Math.easeInQuint = function (t, b, c, d) {
115 return c*(t/=d)*t*t*t*t + b;
118 // quintic easing out - decelerating to zero velocity
119 Math.easeOutQuint = function (t, b, c, d) {
120 return c*((t=t/d-1)*t*t*t*t + 1) + b;
123 // quintic easing in/out - acceleration until halfway, then deceleration
124 Math.easeInOutQuint = function (t, b, c, d) {
125 if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
126 return c/2*((t-=2)*t*t*t*t + 2) + b;
131 ///////////// SINUSOIDAL EASING: sin(t) ///////////////
133 // sinusoidal easing in - accelerating from zero velocity
134 // t: current time, b: beginning value, c: change in position, d: duration
135 Math.easeInSine = function (t, b, c, d) {
136 return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
139 // sinusoidal easing out - decelerating to zero velocity
140 Math.easeOutSine = function (t, b, c, d) {
141 return c * Math.sin(t/d * (Math.PI/2)) + b;
144 // sinusoidal easing in/out - accelerating until halfway, then decelerating
145 Math.easeInOutSine = function (t, b, c, d) {
146 return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
150 ///////////// EXPONENTIAL EASING: 2^t /////////////////
152 // exponential easing in - accelerating from zero velocity
153 // t: current time, b: beginning value, c: change in position, d: duration
154 Math.easeInExpo = function (t, b, c, d) {
155 return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
158 // exponential easing out - decelerating to zero velocity
159 Math.easeOutExpo = function (t, b, c, d) {
160 return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
163 // exponential easing in/out - accelerating until halfway, then decelerating
164 Math.easeInOutExpo = function (t, b, c, d) {
166 if (t==d) return b+c;
167 if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
168 return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
172 /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
174 // circular easing in - accelerating from zero velocity
175 // t: current time, b: beginning value, c: change in position, d: duration
176 Math.easeInCirc = function (t, b, c, d) {
177 return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
180 // circular easing out - decelerating to zero velocity
181 Math.easeOutCirc = function (t, b, c, d) {
182 return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
185 // circular easing in/out - acceleration until halfway, then deceleration
186 Math.easeInOutCirc = function (t, b, c, d) {
187 if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
188 return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
192 /////////// ELASTIC EASING: exponentially decaying sine wave //////////////
194 // t: current time, b: beginning value, c: change in value, d: duration, a: amplitude (optional), p: period (optional)
195 // t and d can be in frames or seconds/milliseconds
197 Math.easeInElastic = function (t, b, c, d, a, p) {
198 if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
199 if (a < Math.abs(c)) { a=c; var s=p/4; }
200 else var s = p/(2*Math.PI) * Math.asin (c/a);
201 return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
204 Math.easeOutElastic = function (t, b, c, d, a, p) {
205 if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
206 if (a < Math.abs(c)) { a=c; var s=p/4; }
207 else var s = p/(2*Math.PI) * Math.asin (c/a);
208 return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
211 Math.easeInOutElastic = function (t, b, c, d, a, p) {
212 if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
213 if (a < Math.abs(c)) { a=c; var s=p/4; }
214 else var s = p/(2*Math.PI) * Math.asin (c/a);
215 if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
216 return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
220 /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2 //////////////
222 // back easing in - backtracking slightly, then reversing direction and moving to target
223 // t: current time, b: beginning value, c: change in value, d: duration, s: overshoot amount (optional)
224 // t and d can be in frames or seconds/milliseconds
225 // s controls the amount of overshoot: higher s means greater overshoot
226 // s has a default value of 1.70158, which produces an overshoot of 10 percent
227 // s==0 produces cubic easing with no overshoot
228 Math.easeInBack = function (t, b, c, d, s) {
229 if (s == undefined) s = 1.70158;
230 return c*(t/=d)*t*((s+1)*t - s) + b;
233 // back easing out - moving towards target, overshooting it slightly, then reversing and coming back to target
234 Math.easeOutBack = function (t, b, c, d, s) {
235 if (s == undefined) s = 1.70158;
236 return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
239 // back easing in/out - backtracking slightly, then reversing direction and moving to target,
240 // then overshooting target, reversing, and finally coming back to target
241 Math.easeInOutBack = function (t, b, c, d, s) {
242 if (s == undefined) s = 1.70158;
243 if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
244 return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
248 /////////// BOUNCE EASING: exponentially decaying parabolic bounce //////////////
251 // t: current time, b: beginning value, c: change in position, d: duration
252 Math.easeInBounce = function (t, b, c, d) {
253 return c - Math.easeOutBounce (d-t, 0, c, d) + b;
257 Math.easeOutBounce = function (t, b, c, d) {
258 if ((t/=d) < (1/2.75)) {
259 return c*(7.5625*t*t) + b;
260 } else if (t < (2/2.75)) {
261 return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
262 } else if (t < (2.5/2.75)) {
263 return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
265 return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
269 // bounce easing in/out
270 Math.easeInOutBounce = function (t, b, c, d) {
271 if (t < d/2) return Math.easeInBounce (t*2, 0, c, d) * .5 + b;
272 return Math.easeOutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;
276 //trace (">> Penner easing equations loaded");