1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 /**
 27  * TweenType
 28  * @type Object
 29  */
 30 ccs.TweenType = {
 31     CUSTOM_EASING: -1,
 32     LINEAR: 0,
 33 
 34     SINE_EASEIN: 1,
 35     SINE_EASEOUT: 2,
 36     SINE_EASEINOUT: 3,
 37 
 38     QUAD_EASEIN: 4,
 39     QUAD_EASEOUT: 5,
 40     QUAD_EASEINOUT: 6,
 41 
 42     CUBIC_EASEIN: 7,
 43     CUBIC_EASEOUT: 8,
 44     CUBIC_EASEINOUT: 9,
 45 
 46     QUART_EASEIN: 10,
 47     QUART_EASEOUT: 11,
 48     QUART_EASEINOUT: 12,
 49 
 50     QUINT_EASEIN: 13,
 51     QUINT_EASEOUT: 14,
 52     QUINT_EASEINOUT: 15,
 53 
 54     EXPO_EASEIN: 16,
 55     EXPO_EASEOUT: 17,
 56     EXPO_EASEINOUT: 18,
 57 
 58     CIRC_EASEIN: 19,
 59     CIRC_EASEOUT: 20,
 60     CIRC_EASEINOUT: 21,
 61 
 62     ELASTIC_EASEIN: 22,
 63     ELASTIC_EASEOUT: 23,
 64     ELASTIC_EASEINOUT: 24,
 65 
 66     BACK_EASEIN: 25,
 67     BACK_EASEOUT: 26,
 68     BACK_EASEINOUT: 27,
 69 
 70     BOUNCE_EASEIN: 28,
 71     BOUNCE_EASEOUT: 29,
 72     BOUNCE_EASEINOUT: 30,
 73 
 74     TWEEN_EASING_MAX: 10000
 75 };
 76 
 77 ccs.TweenFunction = ccs.TweenFunction || ccs.Class.extend({});
 78 
 79 ccs.DOUBLE_PI = ccs.M_PI_X_2 = Math.PI * 2;
 80 ccs.HALF_PI = ccs.M_PI_2 = Math.PI / 2;
 81 ccs.M_PI = Math.PI;
 82 
 83 ccs.TweenFunction.tweenTo = function (time, type, easingParam) {
 84     var delta = 0;
 85 
 86     switch (type) {
 87         case ccs.TweenType.CUSTOM_EASING:
 88             delta = this.customEase(time, easingParam);
 89             break;
 90         case ccs.TweenType.LINEAR:
 91             delta = this.linear(time);
 92             break;
 93         case ccs.TweenType.SINE_EASEIN:
 94             delta = this.sineEaseIn(time);
 95             break;
 96         case ccs.TweenType.SINE_EASEOUT:
 97             delta = this.sineEaseOut(time);
 98             break;
 99         case ccs.TweenType.SINE_EASEINOUT:
100             delta = this.sineEaseInOut(time);
101             break;
102 
103         case ccs.TweenType.QUAD_EASEIN:
104             delta = this.quadEaseIn(time);
105             break;
106         case ccs.TweenType.QUAD_EASEOUT:
107             delta = this.quadEaseOut(time);
108             break;
109         case ccs.TweenType.QUAD_EASEINOUT:
110             delta = this.quadEaseInOut(time);
111             break;
112 
113         case ccs.TweenType.CUBIC_EASEIN:
114             delta = this.cubicEaseIn(time);
115             break;
116         case ccs.TweenType.CUBIC_EASEOUT:
117             delta = this.cubicEaseOut(time);
118             break;
119         case ccs.TweenType.CUBIC_EASEINOUT:
120             delta = this.cubicEaseInOut(time);
121             break;
122 
123         case ccs.TweenType.QUART_EASEIN:
124             delta = this.quartEaseIn(time);
125             break;
126         case ccs.TweenType.QUART_EASEOUT:
127             delta = this.quartEaseOut(time);
128             break;
129         case ccs.TweenType.QUART_EASEINOUT:
130             delta = this.quartEaseInOut(time);
131             break;
132 
133         case ccs.TweenType.QUINT_EASEIN:
134             delta = this.quintEaseIn(time);
135             break;
136         case ccs.TweenType.QUINT_EASEOUT:
137             delta = this.quintEaseOut(time);
138             break;
139         case ccs.TweenType.QUINT_EASEINOUT:
140             delta = this.quintEaseInOut(time);
141             break;
142 
143         case ccs.TweenType.EXPO_EASEIN:
144             delta = this.expoEaseIn(time);
145             break;
146         case ccs.TweenType.EXPO_EASEOUT:
147             delta = this.expoEaseOut(time);
148             break;
149         case ccs.TweenType.EXPO_EASEINOUT:
150             delta = this.expoEaseInOut(time);
151             break;
152 
153         case ccs.TweenType.CIRC_EASEIN:
154             delta = this.circEaseIn(time);
155             break;
156         case ccs.TweenType.CIRC_EASEOUT:
157             delta = this.circEaseOut(time);
158             break;
159         case ccs.TweenType.CIRC_EASEINOUT:
160             delta = this.circEaseInOut(time);
161             break;
162 
163         case ccs.TweenType.ELASTIC_EASEIN:
164             var period = 0.3;
165             if(null != easingParam && easingParam.length > 0){
166                 period = easingParam[0];
167             }
168             delta = this.elasticEaseIn(time, period);
169             break;
170         case ccs.TweenType.ELASTIC_EASEOUT:
171             var period = 0.3;
172             if(null != easingParam && easingParam.length > 0){
173                 period = easingParam[0];
174             }
175             delta = this.elasticEaseOut(time, period);
176             break;
177         case ccs.TweenType.ELASTIC_EASEINOUT:
178             var period = 0.3;
179             if(null != easingParam && easingParam.length > 0){
180                 period = easingParam[0];
181             }
182             delta = this.elasticEaseInOut(time, period);
183             break;
184 
185         case ccs.TweenType.BACK_EASEIN:
186             delta = this.backEaseIn(time);
187             break;
188         case ccs.TweenType.BACK_EASEOUT:
189             delta = this.backEaseOut(time);
190             break;
191         case ccs.TweenType.BACK_EASEINOUT:
192             delta = this.backEaseInOut(time);
193             break;
194 
195         case ccs.TweenType.BOUNCE_EASEIN:
196             delta = this.bounceEaseIn(time);
197             break;
198         case ccs.TweenType.BOUNCE_EASEOUT:
199             delta = this.bounceEaseOut(time);
200             break;
201         case ccs.TweenType.BOUNCE_EASEINOUT:
202             delta = this.bounceEaseInOut(time);
203             break;
204 
205         default:
206             delta = this.sineEaseInOut(time);
207             break;
208     }
209 
210     return delta;
211 };
212 
213 
214 // Linear
215 ccs.TweenFunction.linear = function (time) {
216     return time;
217 };
218 
219 
220 // Sine Ease
221 ccs.TweenFunction.sineEaseIn = function (time) {
222     return -1 * Math.cos(time * ccs.HALF_PI) + 1;
223 };
224 ccs.TweenFunction.sineEaseOut = function (time) {
225     return Math.sin(time * ccs.HALF_PI);
226 };
227 ccs.TweenFunction.sineEaseInOut = function (time) {
228     return -0.5 * (Math.cos(ccs.M_PI * time) - 1);
229 };
230 
231 
232 // Quad Ease
233 ccs.TweenFunction.quadEaseIn = function (time) {
234     return time * time;
235 };
236 ccs.TweenFunction.quadEaseOut = function (time) {
237     return -1 * time * (time - 2);
238 };
239 ccs.TweenFunction.quadEaseInOut = function (time) {
240     time = time * 2;
241     if (time < 1)
242         return 0.5 * time * time;
243     --time;
244     return -0.5 * (time * (time - 2) - 1);
245 };
246 
247 
248 // Cubic Ease
249 ccs.TweenFunction.cubicEaseIn = function (time) {
250     return time * time * time;
251 };
252 ccs.TweenFunction.cubicEaseOut = function (time) {
253     time -= 1;
254     return (time * time * time + 1);
255 };
256 ccs.TweenFunction.cubicEaseInOut = function (time) {
257     time = time * 2;
258     if (time < 1)
259         return 0.5 * time * time * time;
260     time -= 2;
261     return 0.5 * (time * time * time + 2);
262 };
263 
264 
265 // Quart Ease
266 ccs.TweenFunction.quartEaseIn = function (time) {
267     return time * time * time * time;
268 };
269 ccs.TweenFunction.quartEaseOut = function (time) {
270     time -= 1;
271     return -(time * time * time * time - 1);
272 };
273 ccs.TweenFunction.quartEaseInOut = function (time) {
274     time = time * 2;
275     if (time < 1)
276         return 0.5 * time * time * time * time;
277     time -= 2;
278     return -0.5 * (time * time * time * time - 2);
279 };
280 
281 
282 // Quint Ease
283 ccs.TweenFunction.quintEaseIn = function (time) {
284     return time * time * time * time * time;
285 };
286 ccs.TweenFunction.quintEaseOut = function (time) {
287     time -= 1;
288     return (time * time * time * time * time + 1);
289 };
290 ccs.TweenFunction.quintEaseInOut = function (time) {
291     time = time * 2;
292     if (time < 1)
293         return 0.5 * time * time * time * time * time;
294     time -= 2;
295     return 0.5 * (time * time * time * time * time + 2);
296 };
297 
298 
299 // Expo Ease
300 ccs.TweenFunction.expoEaseIn = function (time) {
301     return time === 0 ? 0 : Math.pow(2, 10 * (time - 1)) - 0.001;
302 };
303 ccs.TweenFunction.expoEaseOut = function (time) {
304     return time === 1 ? 1 : (-Math.pow(2, -10 * time) + 1);
305 };
306 ccs.TweenFunction.expoEaseInOut = function (time) {
307     time /= 0.5;
308     if (time < 1) {
309         time = 0.5 * Math.pow(2, 10 * (time - 1));
310     }
311     else {
312         time = 0.5 * (-Math.pow(2, -10 * (time - 1)) + 2);
313     }
314 
315     return time;
316 };
317 
318 
319 // Circ Ease
320 ccs.TweenFunction.circEaseIn = function (time) {
321     return -1 * (Math.sqrt(1 - time * time) - 1);
322 };
323 ccs.TweenFunction.circEaseOut = function (time) {
324     time = time - 1;
325     return Math.sqrt(1 - time * time);
326 };
327 ccs.TweenFunction.circEaseInOut = function (time) {
328     time = time * 2;
329     if (time < 1)
330         return -0.5 * (Math.sqrt(1 - time * time) - 1);
331     time -= 2;
332     return 0.5 * (Math.sqrt(1 - time * time) + 1);
333 };
334 
335 
336 // Elastic Ease
337 ccs.TweenFunction.elasticEaseIn = function (time, easingParam) {
338     var period = 0.3;
339 
340     if (easingParam.length > 0) {
341         period = easingParam[0];
342     }
343 
344     var newT = 0;
345     if (time === 0 || time === 1) {
346         newT = time;
347     }
348     else {
349         var s = period / 4;
350         time = time - 1;
351         newT = -Math.pow(2, 10 * time) * Math.sin((time - s) * ccs.DOUBLE_PI / period);
352     }
353 
354     return newT;
355 };
356 ccs.TweenFunction.elasticEaseOut = function (time, easingParam) {
357     var period = 0.3;
358 
359     if (easingParam.length > 0) {
360         period = easingParam[0];
361     }
362 
363     var newT = 0;
364     if (time === 0 || time === 1) {
365         newT = time;
366     }
367     else {
368         var s = period / 4;
369         newT = Math.pow(2, -10 * time) * Math.sin((time - s) * ccs.DOUBLE_PI / period) + 1;
370     }
371 
372     return newT;
373 };
374 ccs.TweenFunction.elasticEaseInOut = function (time, easingParam) {
375     var period = 0.3;
376 
377     if (easingParam.length > 0) {
378         period = easingParam[0];
379     }
380 
381     var newT = 0;
382     if (time === 0 || time === 1) {
383         newT = time;
384     }
385     else {
386         time = time * 2;
387         if (!period) {
388             period = 0.3 * 1.5;
389         }
390 
391         var s = period / 4;
392 
393         time = time - 1;
394         if (time < 0) {
395             newT = -0.5 * Math.pow(2, 10 * time) * Math.sin((time - s) * ccs.DOUBLE_PI / period);
396         } else {
397             newT = Math.pow(2, -10 * time) * Math.sin((time - s) * ccs.DOUBLE_PI / period) * 0.5 + 1;
398         }
399     }
400     return newT;
401 };
402 
403 
404 // Back Ease
405 ccs.TweenFunction.backEaseIn = function (time) {
406     var overshoot = 1.70158;
407     return time * time * ((overshoot + 1) * time - overshoot);
408 };
409 ccs.TweenFunction.backEaseOut = function (time) {
410     var overshoot = 1.70158;
411 
412     time = time - 1;
413     return time * time * ((overshoot + 1) * time + overshoot) + 1;
414 };
415 ccs.TweenFunction.backEaseInOut = function (time) {
416     var overshoot = 1.70158 * 1.525;
417 
418     time = time * 2;
419     if (time < 1) {
420         return (time * time * ((overshoot + 1) * time - overshoot)) / 2;
421     }
422     else {
423         time = time - 2;
424         return (time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1;
425     }
426 };
427 
428 
429 // Bounce Ease
430 ccs.bounceTime = function (time) {
431     if (time < 1 / 2.75) {
432         return 7.5625 * time * time;
433     } else if (time < 2 / 2.75) {
434         time -= 1.5 / 2.75;
435         return 7.5625 * time * time + 0.75;
436     } else if (time < 2.5 / 2.75) {
437         time -= 2.25 / 2.75;
438         return 7.5625 * time * time + 0.9375;
439     }
440 
441     time -= 2.625 / 2.75;
442     return 7.5625 * time * time + 0.984375;
443 };
444 ccs.TweenFunction.bounceEaseIn = function (time) {
445     return 1 - ccs.bounceTime(1 - time);
446 };
447 
448 ccs.TweenFunction.bounceEaseOut = function (time) {
449     return ccs.bounceTime(time);
450 };
451 
452 ccs.TweenFunction.bounceEaseInOut = function (time) {
453     var newT = 0;
454     if (time < 0.5) {
455         time = time * 2;
456         newT = (1 - ccs.bounceTime(1 - time)) * 0.5;
457     } else {
458         newT = ccs.bounceTime(time * 2 - 1) * 0.5 + 0.5;
459     }
460 
461     return newT;
462 };
463 
464 
465 // Custom Ease
466 ccs.TweenFunction.customEase = function (time, easingParam) {
467     if (easingParam.length > 0) {
468         var tt = 1 - time;
469         return easingParam[1] * tt * tt * tt + 3 * easingParam[3] * time * tt * tt + 3 * easingParam[5] * time * time * tt + easingParam[7] * time * time * time;
470     }
471     return time;
472 };
473 
474 ccs.TweenFunction.easeIn = function(time, rate){
475     return Math.pow(time, rate);
476 };
477 
478 ccs.TweenFunction.easeOut = function(time, rate){
479     return Math.pow(time, 1 / rate);
480 };
481 
482 ccs.TweenFunction.easeInOut = function(time, rate){
483     time *= 2;
484     if(time < 1){
485         return 0.5 * Math.pow(time, rate);
486     }else{
487         return 1 - 0.5 * Math.pow(2 - time, rate);
488     }
489 };
490 
491 ccs.TweenFunction.quadraticIn = function(time){
492     return Math.pow(time, 2);
493 };
494 
495 ccs.TweenFunction.quadraticOut = function(time){
496     return -time * (time - 2);
497 };
498 
499 ccs.TweenFunction.bezieratFunction = function(a, b, c, d, t){
500     return (Math.pow(1-t,3) * a + 3*t*(Math.pow(1-t,2))*b + 3*Math.pow(t,2)*(1-t)*c + Math.pow(t,3)*d );
501 };