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     customEasing: -1,
 32     linear: 0,
 33 
 34     sineEaseIn: 1,
 35     sineEaseOut: 2,
 36     sineEaseInOut: 3,
 37 
 38     quadEaseIn: 4,
 39     quadEaseOut: 5,
 40     quadEaseInOut: 6,
 41 
 42     cubicEaseIn: 7,
 43     cubicEaseOut: 8,
 44     cubicEaseInOut: 9,
 45 
 46     quartEaseIn: 10,
 47     quartEaseOut: 11,
 48     quartEaseInOut: 12,
 49 
 50     quintEaseIn: 13,
 51     quintEaseOut: 14,
 52     quintEaseInOut: 15,
 53 
 54     expoEaseIn: 16,
 55     expoEaseOut: 17,
 56     expoEaseInOut: 18,
 57 
 58     circEaseIn: 19,
 59     eircEaseOut: 20,
 60     circEaseInOut: 21,
 61 
 62     elasticEaseIn: 22,
 63     elasticEaseOut: 23,
 64     elasticEaseInOut: 24,
 65 
 66     backEaseIn: 25,
 67     backEaseOut: 26,
 68     backEaseInOut: 27,
 69 
 70     bounceEaseIn: 28,
 71     bounceEaseOut: 29,
 72     bounceEaseInOut: 30,
 73 
 74     tweenEasingMax: 10000
 75 };
 76 
 77 ccs.TweenFunction = ccs.TweenFunction || ccs.Class.extend({});
 78 
 79 ccs.M_PI_X_2 = Math.PI * 2;
 80 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.customEasing:
 88             delta = this.customEase(time, easingParam);
 89             break;
 90         case ccs.TweenType.linear:
 91             delta = this.linear(time);
 92             break;
 93         case ccs.TweenType.sineEaseIn:
 94             delta = this.sineEaseIn(time);
 95             break;
 96         case ccs.TweenType.sineEaseOut:
 97             delta = this.sineEaseOut(time);
 98             break;
 99         case ccs.TweenType.sineEaseInOut:
100             delta = this.sineEaseInOut(time);
101             break;
102 
103         case ccs.TweenType.quadEaseIn:
104             delta = this.quadEaseIn(time);
105             break;
106         case ccs.TweenType.quadEaseOut:
107             delta = this.quadEaseOut(time);
108             break;
109         case ccs.TweenType.quadEaseInOut:
110             delta = this.quadEaseInOut(time);
111             break;
112 
113         case ccs.TweenType.cubicEaseIn:
114             delta = this.cubicEaseIn(time);
115             break;
116         case ccs.TweenType.cubicEaseOut:
117             delta = this.cubicEaseOut(time);
118             break;
119         case ccs.TweenType.cubicEaseInOut:
120             delta = this.cubicEaseInOut(time);
121             break;
122 
123         case ccs.TweenType.quartEaseIn:
124             delta = this.quartEaseIn(time);
125             break;
126         case ccs.TweenType.quartEaseOut:
127             delta = this.quartEaseOut(time);
128             break;
129         case ccs.TweenType.quartEaseInOut:
130             delta = this.quartEaseInOut(time);
131             break;
132 
133         case ccs.TweenType.quintEaseIn:
134             delta = this.quintEaseIn(time);
135             break;
136         case ccs.TweenType.quintEaseOut:
137             delta = this.quintEaseOut(time);
138             break;
139         case ccs.TweenType.quintEaseInOut:
140             delta = this.quintEaseInOut(time);
141             break;
142 
143         case ccs.TweenType.expoEaseIn:
144             delta = this.expoEaseIn(time);
145             break;
146         case ccs.TweenType.expoEaseOut:
147             delta = this.expoEaseOut(time);
148             break;
149         case ccs.TweenType.expoEaseInOut:
150             delta = this.expoEaseInOut(time);
151             break;
152 
153         case ccs.TweenType.circEaseIn:
154             delta = this.circEaseIn(time);
155             break;
156         case ccs.TweenType.eircEaseOut:
157             delta = this.circEaseOut(time);
158             break;
159         case ccs.TweenType.circEaseInOut:
160             delta = this.circEaseInOut(time);
161             break;
162 
163         case ccs.TweenType.elasticEaseIn:
164             delta = this.elasticEaseIn(time, easingParam);
165             break;
166         case ccs.TweenType.elasticEaseOut:
167             delta = this.elasticEaseOut(time, easingParam);
168             break;
169         case ccs.TweenType.elasticEaseInOut:
170             delta = this.elasticEaseInOut(time, easingParam);
171             break;
172 
173         case ccs.TweenType.backEaseIn:
174             delta = this.backEaseIn(time);
175             break;
176         case ccs.TweenType.backEaseOut:
177             delta = this.backEaseOut(time);
178             break;
179         case ccs.TweenType.backEaseInOut:
180             delta = this.backEaseInOut(time);
181             break;
182 
183         case ccs.TweenType.bounceEaseIn:
184             delta = this.bounceEaseIn(time);
185             break;
186         case ccs.TweenType.bounceEaseOut:
187             delta = this.bounceEaseOut(time);
188             break;
189         case ccs.TweenType.bounceEaseInOut:
190             delta = this.bounceEaseInOut(time);
191             break;
192 
193         default:
194             delta = this.sineEaseInOut(time);
195             break;
196     }
197 
198     return delta;
199 };
200 
201 
202 // Linear
203 ccs.TweenFunction.linear = function (time) {
204     return time;
205 };
206 
207 
208 // Sine Ease
209 ccs.TweenFunction.sineEaseIn = function (time) {
210     return -1 * Math.cos(time * ccs.M_PI_2) + 1;
211 };
212 ccs.TweenFunction.sineEaseOut = function (time) {
213     return Math.sin(time * ccs.M_PI_2);
214 };
215 ccs.TweenFunction.sineEaseInOut = function (time) {
216     return -0.5 * (Math.cos(ccs.M_PI * time) - 1);
217 };
218 
219 
220 // Quad Ease
221 ccs.TweenFunction.quadEaseIn = function (time) {
222     return time * time;
223 };
224 ccs.TweenFunction.quadEaseOut = function (time) {
225     return -1 * time * (time - 2);
226 };
227 ccs.TweenFunction.quadEaseInOut = function (time) {
228     time = time * 2;
229     if (time < 1)
230         return 0.5 * time * time;
231     --time;
232     return -0.5 * (time * (time - 2) - 1);
233 };
234 
235 
236 // Cubic Ease
237 ccs.TweenFunction.cubicEaseIn = function (time) {
238     return time * time * time;
239 };
240 ccs.TweenFunction.cubicEaseOut = function (time) {
241     time -= 1;
242     return (time * time * time + 1);
243 };
244 ccs.TweenFunction.cubicEaseInOut = function (time) {
245     time = time * 2;
246     if (time < 1)
247         return 0.5 * time * time * time;
248     time -= 2;
249     return 0.5 * (time * time * time + 2);
250 };
251 
252 
253 // Quart Ease
254 ccs.TweenFunction.quartEaseIn = function (time) {
255     return time * time * time * time;
256 };
257 ccs.TweenFunction.quartEaseOut = function (time) {
258     time -= 1;
259     return -(time * time * time * time - 1);
260 };
261 ccs.TweenFunction.quartEaseInOut = function (time) {
262     time = time * 2;
263     if (time < 1)
264         return 0.5 * time * time * time * time;
265     time -= 2;
266     return -0.5 * (time * time * time * time - 2);
267 };
268 
269 
270 // Quint Ease
271 ccs.TweenFunction.quintEaseIn = function (time) {
272     return time * time * time * time * time;
273 };
274 ccs.TweenFunction.quintEaseOut = function (time) {
275     time -= 1;
276     return (time * time * time * time * time + 1);
277 };
278 ccs.TweenFunction.quintEaseInOut = function (time) {
279     time = time * 2;
280     if (time < 1)
281         return 0.5 * time * time * time * time * time;
282     time -= 2;
283     return 0.5 * (time * time * time * time * time + 2);
284 };
285 
286 
287 // Expo Ease
288 ccs.TweenFunction.expoEaseIn = function (time) {
289     return time == 0 ? 0 : Math.pow(2, 10 * (time - 1)) - 0.001;
290 };
291 ccs.TweenFunction.expoEaseOut = function (time) {
292     return time == 1 ? 1 : (-Math.pow(2, -10 * time) + 1);
293 };
294 ccs.TweenFunction.expoEaseInOut = function (time) {
295     time /= 0.5;
296     if (time < 1) {
297         time = 0.5 * Math.pow(2, 10 * (time - 1));
298     }
299     else {
300         time = 0.5 * (-Math.pow(2, -10 * (time - 1)) + 2);
301     }
302 
303     return time;
304 };
305 
306 
307 // Circ Ease
308 ccs.TweenFunction.circEaseIn = function (time) {
309     return -1 * (Math.sqrt(1 - time * time) - 1);
310 };
311 ccs.TweenFunction.circEaseOut = function (time) {
312     time = time - 1;
313     return Math.sqrt(1 - time * time);
314 };
315 ccs.TweenFunction.circEaseInOut = function (time) {
316     time = time * 2;
317     if (time < 1)
318         return -0.5 * (Math.sqrt(1 - time * time) - 1);
319     time -= 2;
320     return 0.5 * (Math.sqrt(1 - time * time) + 1);
321 };
322 
323 
324 // Elastic Ease
325 ccs.TweenFunction.elasticEaseIn = function (time, easingParam) {
326     var period = 0.3;
327 
328     if (easingParam.length > 0) {
329         period = easingParam[0];
330     }
331 
332     var newT = 0;
333     if (time == 0 || time == 1) {
334         newT = time;
335     }
336     else {
337         var s = period / 4;
338         time = time - 1;
339         newT = -Math.pow(2, 10 * time) * Math.sin((time - s) * ccs.M_PI_X_2 / period);
340     }
341 
342     return newT;
343 };
344 ccs.TweenFunction.elasticEaseOut = function (time, easingParam) {
345     var period = 0.3;
346 
347     if (easingParam.length > 0) {
348         period = easingParam[0];
349     }
350 
351     var newT = 0;
352     if (time == 0 || time == 1) {
353         newT = time;
354     }
355     else {
356         var s = period / 4;
357         newT = Math.pow(2, -10 * time) * Math.sin((time - s) * ccs.M_PI_X_2 / period) + 1;
358     }
359 
360     return newT;
361 };
362 ccs.TweenFunction.elasticEaseInOut = function (time, easingParam) {
363     var period = 0.3;
364 
365     if (easingParam.length > 0) {
366         period = easingParam[0];
367     }
368 
369     var newT = 0;
370     if (time == 0 || time == 1) {
371         newT = time;
372     }
373     else {
374         time = time * 2;
375         if (!period) {
376             period = 0.3 * 1.5;
377         }
378 
379         var s = period / 4;
380 
381         time = time - 1;
382         if (time < 0) {
383             newT = -0.5 * Math.pow(2, 10 * time) * Math.sin((time - s) * ccs.M_PI_X_2 / period);
384         }
385         else {
386             newT = Math.pow(2, -10 * time) * Math.sin((time - s) * ccs.M_PI_X_2 / period) * 0.5 + 1;
387         }
388     }
389     return newT;
390 };
391 
392 
393 // Back Ease
394 ccs.TweenFunction.backEaseIn = function (time) {
395     var overshoot = 1.70158;
396     return time * time * ((overshoot + 1) * time - overshoot);
397 };
398 ccs.TweenFunction.backEaseOut = function (time) {
399     var overshoot = 1.70158;
400 
401     time = time - 1;
402     return time * time * ((overshoot + 1) * time + overshoot) + 1;
403 };
404 ccs.TweenFunction.backEaseInOut = function (time) {
405     var overshoot = 1.70158 * 1.525;
406 
407     time = time * 2;
408     if (time < 1) {
409         return (time * time * ((overshoot + 1) * time - overshoot)) / 2;
410     }
411     else {
412         time = time - 2;
413         return (time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1;
414     }
415 };
416 
417 
418 // Bounce Ease
419 ccs.bounceTime = function (time) {
420     if (time < 1 / 2.75) {
421         return 7.5625 * time * time;
422     } else if (time < 2 / 2.75) {
423         time -= 1.5 / 2.75;
424         return 7.5625 * time * time + 0.75;
425     } else if (time < 2.5 / 2.75) {
426         time -= 2.25 / 2.75;
427         return 7.5625 * time * time + 0.9375;
428     }
429 
430     time -= 2.625 / 2.75;
431     return 7.5625 * time * time + 0.984375;
432 };
433 ccs.TweenFunction.bounceEaseIn = function (time) {
434     return 1 - ccs.bounceTime(1 - time);
435 };
436 
437 ccs.TweenFunction.bounceEaseOut = function (time) {
438     return ccs.bounceTime(time);
439 };
440 
441 ccs.TweenFunction.bounceEaseInOut = function (time) {
442     var newT = 0;
443     if (time < 0.5) {
444         time = time * 2;
445         newT = (1 - ccs.bounceTime(1 - time)) * 0.5;
446     }
447     else {
448         newT = ccs.bounceTime(time * 2 - 1) * 0.5 + 0.5;
449     }
450 
451     return newT;
452 };
453 
454 
455 // Custom Ease
456 ccs.TweenFunction.customEase = function (time, easingParam) {
457     if (easingParam.length > 0) {
458         var tt = 1 - time;
459         return easingParam[1] * tt * tt * tt + 3 * easingParam[3] * time * tt * tt + 3 * easingParam[5] * time * time * tt + easingParam[7] * time * time * time;
460     }
461     return time;
462 };