]> mj.ucw.cz Git - gallery.git/blob - gal2/highslide/easing_equations.js
Gallery2: Experiments with HighSlide
[gallery.git] / gal2 / highslide / easing_equations.js
1 /*
2   Easing Equations v1.5
3   May 1, 2003
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.  
6   
7   These tweening functions provide different flavors of 
8   math-based motion under a consistent API. 
9   
10   Types of easing:
11   
12           Linear
13           Quadratic
14           Cubic
15           Quartic
16           Quintic
17           Sinusoidal
18           Exponential
19           Circular
20           Elastic
21           Back
22           Bounce
23
24   Changes:
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
29   
30   Discussed in Chapter 7 of 
31   Robert Penner's Programming Macromedia Flash MX
32   (including graphs of the easing equations)
33   
34   http://www.robertpenner.com/profmx
35   http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
36 */
37
38
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) {
42         return c*t/d + b;
43 };
44
45
46  ///////////// QUADRATIC EASING: t^2 ///////////////////
47
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;
53 };
54
55 // quadratic easing out - decelerating to zero velocity
56 Math.easeOutQuad = function (t, b, c, d) {
57         return -c *(t/=d)*(t-2) + b;
58 };
59
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;
64 };
65
66
67  ///////////// CUBIC EASING: t^3 ///////////////////////
68
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;
74 };
75
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;
79 };
80
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;
85 };
86
87
88  ///////////// QUARTIC EASING: t^4 /////////////////////
89
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;
95 };
96
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;
100 };
101
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;
106 };
107
108
109  ///////////// QUINTIC EASING: t^5  ////////////////////
110
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;
116 };
117
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;
121 };
122
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;
127 };
128
129
130
131  ///////////// SINUSOIDAL EASING: sin(t) ///////////////
132
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;
137 };
138
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;
142 };
143
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;
147 };
148
149
150  ///////////// EXPONENTIAL EASING: 2^t /////////////////
151
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;
156 };
157
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;
161 };
162
163 // exponential easing in/out - accelerating until halfway, then decelerating
164 Math.easeInOutExpo = function (t, b, c, d) {
165         if (t==0) return b;
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;
169 };
170
171
172  /////////// CIRCULAR EASING: sqrt(1-t^2) //////////////
173
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;
178 };
179
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;
183 };
184
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;
189 };
190
191
192  /////////// ELASTIC EASING: exponentially decaying sine wave  //////////////
193
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
196
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;
202 };
203
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;
209 };
210
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;
217 };
218
219
220  /////////// BACK EASING: overshooting cubic easing: (s+1)*t^3 - s*t^2  //////////////
221
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;
231 };
232
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;
237 };
238
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;
245 };
246
247
248  /////////// BOUNCE EASING: exponentially decaying parabolic bounce  //////////////
249
250 // bounce easing in
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;
254 };
255
256 // bounce easing out
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;
264         } else {
265                 return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
266         }
267 };
268
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;
273 };
274
275
276 //trace (">> Penner easing equations loaded");
277
278
279
280
281
282