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