|
1 /* |
|
2 * Ease |
|
3 * Visit http://createjs.com/ for documentation, updates and examples. |
|
4 * |
|
5 * Copyright (c) 2010 gskinner.com, inc. |
|
6 * |
|
7 * Permission is hereby granted, free of charge, to any person |
|
8 * obtaining a copy of this software and associated documentation |
|
9 * files (the "Software"), to deal in the Software without |
|
10 * restriction, including without limitation the rights to use, |
|
11 * copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 * copies of the Software, and to permit persons to whom the |
|
13 * Software is furnished to do so, subject to the following |
|
14 * conditions: |
|
15 * |
|
16 * The above copyright notice and this permission notice shall be |
|
17 * included in all copies or substantial portions of the Software. |
|
18 * |
|
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
26 * OTHER DEALINGS IN THE SOFTWARE. |
|
27 */ |
|
28 |
|
29 // namespace: |
|
30 this.createjs = this.createjs||{}; |
|
31 |
|
32 (function() { |
|
33 |
|
34 // constructor: |
|
35 /** |
|
36 * The Ease class provides a collection of easing functions for use with TweenJS. |
|
37 * It does not use the standard 4 param easing signature. Instead it uses a single param |
|
38 * which indicates the current linear ratio (0 to 1) of the tween.<br/> |
|
39 * <br/> |
|
40 * Most methods on Ease can be passed directly as easing functions:<br/> |
|
41 * Tween.get(target).to({x:100}, 500, Ease.linear);<br/> |
|
42 * <br/> |
|
43 * However, methods beginning with "get" will return an easing function based on parameter values:<br/> |
|
44 * Tween.get(target).to({y:200}, 500, Ease.getPowIn(2.2));<br/> |
|
45 * <br/> |
|
46 * Equations derived from work by Robert Penner. |
|
47 * @class Ease |
|
48 * @static |
|
49 **/ |
|
50 var Ease = function() { |
|
51 throw "Ease cannot be instantiated."; |
|
52 } |
|
53 |
|
54 // public static methods: |
|
55 /** |
|
56 * @method linear |
|
57 * @static |
|
58 **/ |
|
59 Ease.linear = function(t) { return t; } |
|
60 |
|
61 /** |
|
62 * Identical to linear. |
|
63 * @method none |
|
64 * @static |
|
65 **/ |
|
66 Ease.none = Ease.linear; |
|
67 |
|
68 /** |
|
69 * Mimics the simple -100 to 100 easing in Flash Pro. |
|
70 * @method get |
|
71 * @param amount A value from -1 (ease in) to 1 (ease out) indicating the strength and direction of the ease. |
|
72 * @static |
|
73 **/ |
|
74 Ease.get = function(amount) { |
|
75 if (amount < -1) { amount = -1; } |
|
76 if (amount > 1) { amount = 1; } |
|
77 return function(t) { |
|
78 if (amount==0) { return t; } |
|
79 if (amount<0) { return t*(t*-amount+1+amount); } |
|
80 return t*((2-t)*amount+(1-amount)); |
|
81 } |
|
82 } |
|
83 |
|
84 /** |
|
85 * Configurable exponential ease. |
|
86 * @method getPowIn |
|
87 * @param pow The exponent to use (ex. 3 would return a cubic ease). |
|
88 * @static |
|
89 **/ |
|
90 Ease.getPowIn = function(pow) { |
|
91 return function(t) { |
|
92 return Math.pow(t,pow); |
|
93 } |
|
94 } |
|
95 |
|
96 |
|
97 /** |
|
98 * Configurable exponential ease. |
|
99 * @method getPowOut |
|
100 * @param pow The exponent to use (ex. 3 would return a cubic ease). |
|
101 * @static |
|
102 **/ |
|
103 Ease.getPowOut = function(pow) { |
|
104 return function(t) { |
|
105 return 1-Math.pow(1-t,pow); |
|
106 } |
|
107 } |
|
108 |
|
109 |
|
110 /** |
|
111 * Configurable exponential ease. |
|
112 * @method getPowInOut |
|
113 * @param pow The exponent to use (ex. 3 would return a cubic ease). |
|
114 * @static |
|
115 **/ |
|
116 Ease.getPowInOut = function(pow) { |
|
117 return function(t) { |
|
118 if ((t*=2)<1) return 0.5*Math.pow(t,pow); |
|
119 return 1-0.5*Math.abs(Math.pow(2-t,pow)); |
|
120 } |
|
121 } |
|
122 |
|
123 |
|
124 /** |
|
125 * @method quadIn |
|
126 * @static |
|
127 **/ |
|
128 Ease.quadIn = Ease.getPowIn(2); |
|
129 /** |
|
130 * @method quadOut |
|
131 * @static |
|
132 **/ |
|
133 Ease.quadOut = Ease.getPowOut(2); |
|
134 /** |
|
135 * @method quadInOut |
|
136 * @static |
|
137 **/ |
|
138 Ease.quadInOut = Ease.getPowInOut(2); |
|
139 |
|
140 |
|
141 /** |
|
142 * @method cubicIn |
|
143 * @static |
|
144 **/ |
|
145 Ease.cubicIn = Ease.getPowIn(3); |
|
146 /** |
|
147 * @method cubicOut |
|
148 * @static |
|
149 **/ |
|
150 Ease.cubicOut = Ease.getPowOut(3); |
|
151 /** |
|
152 * @method cubicInOut |
|
153 * @static |
|
154 **/ |
|
155 Ease.cubicInOut = Ease.getPowInOut(3); |
|
156 |
|
157 |
|
158 /** |
|
159 * @method quartIn |
|
160 * @static |
|
161 **/ |
|
162 Ease.quartIn = Ease.getPowIn(4); |
|
163 /** |
|
164 * @method quartOut |
|
165 * @static |
|
166 **/ |
|
167 Ease.quartOut = Ease.getPowOut(4); |
|
168 /** |
|
169 * @method quartInOut |
|
170 * @static |
|
171 **/ |
|
172 Ease.quartInOut = Ease.getPowInOut(4); |
|
173 |
|
174 |
|
175 /** |
|
176 * @method quintIn |
|
177 * @static |
|
178 **/ |
|
179 Ease.quintIn = Ease.getPowIn(5); |
|
180 /** |
|
181 * @method quintOut |
|
182 * @static |
|
183 **/ |
|
184 Ease.quintOut = Ease.getPowOut(5); |
|
185 /** |
|
186 * @method quintInOut |
|
187 * @static |
|
188 **/ |
|
189 Ease.quintInOut = Ease.getPowInOut(5); |
|
190 |
|
191 |
|
192 /** |
|
193 * @method sineIn |
|
194 * @static |
|
195 **/ |
|
196 Ease.sineIn = function(t) { |
|
197 return 1-Math.cos(t*Math.PI/2); |
|
198 } |
|
199 |
|
200 /** |
|
201 * @method sineOut |
|
202 * @static |
|
203 **/ |
|
204 Ease.sineOut = function(t) { |
|
205 return Math.sin(t*Math.PI/2); |
|
206 } |
|
207 |
|
208 /** |
|
209 * @method sineInOut |
|
210 * @static |
|
211 **/ |
|
212 Ease.sineInOut = function(t) { |
|
213 return -0.5*(Math.cos(Math.PI*t) - 1) |
|
214 } |
|
215 |
|
216 |
|
217 /** |
|
218 * Configurable "back in" ease. |
|
219 * @method getBackIn |
|
220 * @param amount The strength of the ease. |
|
221 * @static |
|
222 **/ |
|
223 Ease.getBackIn = function(amount) { |
|
224 return function(t) { |
|
225 return t*t*((amount+1)*t-amount); |
|
226 } |
|
227 } |
|
228 /** |
|
229 * @method backIn |
|
230 * @static |
|
231 **/ |
|
232 Ease.backIn = Ease.getBackIn(1.7); |
|
233 |
|
234 /** |
|
235 * Configurable "back out" ease. |
|
236 * @method getBackOut |
|
237 * @param amount The strength of the ease. |
|
238 * @static |
|
239 **/ |
|
240 Ease.getBackOut = function(amount) { |
|
241 return function(t) { |
|
242 return (--t*t*((amount+1)*t + amount) + 1); |
|
243 } |
|
244 } |
|
245 /** |
|
246 * @method backOut |
|
247 * @static |
|
248 **/ |
|
249 Ease.backOut = Ease.getBackOut(1.7); |
|
250 |
|
251 /** |
|
252 * Configurable "back in out" ease. |
|
253 * @method getBackInOut |
|
254 * @param amount The strength of the ease. |
|
255 * @static |
|
256 **/ |
|
257 Ease.getBackInOut = function(amount) { |
|
258 amount*=1.525; |
|
259 return function(t) { |
|
260 if ((t*=2)<1) return 0.5*(t*t*((amount+1)*t-amount)); |
|
261 return 0.5*((t-=2)*t*((amount+1)*t+amount)+2); |
|
262 } |
|
263 } |
|
264 /** |
|
265 * @method backInOut |
|
266 * @static |
|
267 **/ |
|
268 Ease.backInOut = Ease.getBackInOut(1.7); |
|
269 |
|
270 |
|
271 /** |
|
272 * @method circIn |
|
273 * @static |
|
274 **/ |
|
275 Ease.circIn = function(t) { |
|
276 return -(Math.sqrt(1-t*t)- 1); |
|
277 } |
|
278 |
|
279 /** |
|
280 * @method circOut |
|
281 * @static |
|
282 **/ |
|
283 Ease.circOut = function(t) { |
|
284 return Math.sqrt(1-(--t)*t); |
|
285 } |
|
286 |
|
287 /** |
|
288 * @method circInOut |
|
289 * @static |
|
290 **/ |
|
291 Ease.circInOut = function(t) { |
|
292 if ((t*=2) < 1) return -0.5*(Math.sqrt(1-t*t)-1); |
|
293 return 0.5*(Math.sqrt(1-(t-=2)*t)+1); |
|
294 } |
|
295 |
|
296 /** |
|
297 * @method bounceIn |
|
298 * @static |
|
299 **/ |
|
300 Ease.bounceIn = function(t) { |
|
301 return 1-Ease.bounceOut(1-t); |
|
302 } |
|
303 |
|
304 /** |
|
305 * @method bounceOut |
|
306 * @static |
|
307 **/ |
|
308 Ease.bounceOut = function(t) { |
|
309 if (t < 1/2.75) { |
|
310 return (7.5625*t*t); |
|
311 } else if (t < 2/2.75) { |
|
312 return (7.5625*(t-=1.5/2.75)*t+0.75); |
|
313 } else if (t < 2.5/2.75) { |
|
314 return (7.5625*(t-=2.25/2.75)*t+0.9375); |
|
315 } else { |
|
316 return (7.5625*(t-=2.625/2.75)*t +0.984375); |
|
317 } |
|
318 } |
|
319 |
|
320 /** |
|
321 * @method bounceInOut |
|
322 * @static |
|
323 **/ |
|
324 Ease.bounceInOut = function(t) { |
|
325 if (t<0.5) return Ease.bounceIn (t*2) * .5; |
|
326 return Ease.bounceOut(t*2-1)*0.5+0.5; |
|
327 } |
|
328 |
|
329 |
|
330 /** |
|
331 * Configurable elastic ease. |
|
332 * @method getElasticIn |
|
333 * @param amplitude |
|
334 * @param period |
|
335 * @static |
|
336 **/ |
|
337 Ease.getElasticIn = function(amplitude,period) { |
|
338 var pi2 = Math.PI*2; |
|
339 return function(t) { |
|
340 if (t==0 || t==1) return t; |
|
341 var s = period/pi2*Math.asin(1/amplitude); |
|
342 return -(amplitude*Math.pow(2,10*(t-=1))*Math.sin((t-s)*pi2/period)); |
|
343 } |
|
344 } |
|
345 /** |
|
346 * @method elasticIn |
|
347 * @static |
|
348 **/ |
|
349 Ease.elasticIn = Ease.getElasticIn(1,0.3); |
|
350 |
|
351 /** |
|
352 * Configurable elastic ease. |
|
353 * @method getElasticOut |
|
354 * @param amplitude |
|
355 * @param period |
|
356 * @static |
|
357 **/ |
|
358 Ease.getElasticOut = function(amplitude,period) { |
|
359 var pi2 = Math.PI*2; |
|
360 return function(t) { |
|
361 if (t==0 || t==1) return t; |
|
362 var s = period/pi2 * Math.asin(1/amplitude); |
|
363 return (amplitude*Math.pow(2,-10*t)*Math.sin((t-s)*pi2/period )+1); |
|
364 } |
|
365 } |
|
366 /** |
|
367 * @method elasticOut |
|
368 * @static |
|
369 **/ |
|
370 Ease.elasticOut = Ease.getElasticOut(1,0.3); |
|
371 |
|
372 /** |
|
373 * Configurable elastic ease. |
|
374 * @method getElasticInOut |
|
375 * @param amplitude |
|
376 * @param period |
|
377 * @static |
|
378 **/ |
|
379 Ease.getElasticInOut = function(amplitude,period) { |
|
380 var pi2 = Math.PI*2; |
|
381 return function(t) { |
|
382 var s = period/pi2 * Math.asin(1/amplitude); |
|
383 if ((t*=2)<1) return -0.5*(amplitude*Math.pow(2,10*(t-=1))*Math.sin( (t-s)*pi2/period )); |
|
384 return amplitude*Math.pow(2,-10*(t-=1))*Math.sin((t-s)*pi2/period)*0.5+1; |
|
385 } |
|
386 } |
|
387 /** |
|
388 * @method elasticInOut |
|
389 * @static |
|
390 **/ |
|
391 Ease.elasticInOut = Ease.getElasticInOut(1,0.3*1.5); |
|
392 |
|
393 createjs.Ease = Ease; |
|
394 }()); |