介绍
这段时间研究了微信小游戏,由于白鹭自身的Tween动画效率较差,就尝试使用第三方库
于是在网上找到了GreenSock库,但不清楚是小游戏不兼容还是别的什么原因,折腾好久也没弄上
最后实在没办法,自己写了个Tween库
摘取网虫虫第一个字母W,于是名字就出来啦 WTween~~
查看动画演示:缓动示例演示
动画算法来自:TextFx——Ease Function(缓动函数)
感谢@liuyonggen 提供ts语言技术支持
包含动画
1 2 3 4 5 6 7 8 9 10 11 |
Linear, QuadEaseOut, QuadEaseIn, QuadEaseInOut, QuadEaseOutIn, ExpoEaseOut, ExpoEaseIn, ExpoEaseInOut, ExpoEaseOutIn, CubicEaseOut, CubicEaseIn, CubicEaseInOut, CubicEaseOutIn, QuartEaseOut, QuartEaseIn, QuartEaseInOut, QuartEaseOutIn, QuintEaseOut, QuintEaseIn, QuintEaseInOut, QuintEaseOutIn, CircEaseOut, CircEaseIn, CircEaseInOut, CircEaseOutIn, SineEaseOut, SineEaseIn, SineEaseInOut, SineEaseOutIn, ElasticEaseOut, ElasticEaseIn, ElasticEaseInOut, ElasticEaseOutIn, BounceEaseOut, BounceEaseIn, BounceEaseInOut, BounceEaseOutIn, BackEaseOut, BackEaseIn, BackEaseInOut, BackEaseOutIn |
效果展示
源码
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * 创建场景界面 * Create scene interface */ protected createGameScene(): void { for(var i = 0; i < 5; i++) { let icon: egret.Bitmap = this.createBitmapByName("egret_icon_png"); this.addChild(icon); icon.x = 26 + i * 120; icon.y = 33; WTween.Play(new WTweener() .get(icon, -1) .to({y:800, alpha:0.5}, 2000, WTweenType.BounceEaseOut) .call(()=>{ console.log("回调1", new Date()); }) .wait(1000) .call(()=>{ console.log("回调2", new Date()); })); } } |
动画库文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 |
enum WTweenType { Linear, QuadEaseOut, QuadEaseIn, QuadEaseInOut, QuadEaseOutIn, ExpoEaseOut, ExpoEaseIn, ExpoEaseInOut, ExpoEaseOutIn, CubicEaseOut, CubicEaseIn, CubicEaseInOut, CubicEaseOutIn, QuartEaseOut, QuartEaseIn, QuartEaseInOut, QuartEaseOutIn, QuintEaseOut, QuintEaseIn, QuintEaseInOut, QuintEaseOutIn, CircEaseOut, CircEaseIn, CircEaseInOut, CircEaseOutIn, SineEaseOut, SineEaseIn, SineEaseInOut, SineEaseOutIn, ElasticEaseOut, ElasticEaseIn, ElasticEaseInOut, ElasticEaseOutIn, BounceEaseOut, BounceEaseIn, BounceEaseInOut, BounceEaseOutIn, BackEaseOut, BackEaseIn, BackEaseInOut, BackEaseOutIn } class WTweener { public stepBackups = []; public step = []; public target : any; public loop : number; // -1 无限循环;0 执行一次 public index : number = 0; /// <param name="target">动画目标</param> /// <param name="loop">循环次数 -1 无限循环;0 执行一次</param> public get(target : any, loop : number = 0) { this.target = target; this.loop = loop; return this; } /// <param name="params">参数 格式为:{x:10,y:20}</param> /// <param name="time">动画时长</param> /// <param name="ease">缓动效果类型</param> public to(params : any, time : number, ease : WTweenType = WTweenType.Linear) { var obj : any = {}; obj.type = "to"; obj.startValues = Tools.Copy(params); obj.endValues = params; obj.endTime = time; obj.currentTime = 0; obj.ease = ease; obj.index = this.index++; if(obj.index == 0) { for(var key in obj.startValues) { obj.startValues[key] = this.target[key]; } } this.step.push(obj); if(this.loop != 0 && this.loop != 1) this.stepBackups.push(Tools.Copy(obj)); return this; } /// <param name="func">方法</param> public call(func) { var obj : any = {}; obj.type = "call"; obj.func = func; this.step.push(obj); if(this.loop != 0 && this.loop != 1) this.stepBackups.push(Tools.Copy(obj)); return this; } /// <param name="time">动画时长</param> public wait(time : number) { var obj : any = {}; obj.type = "wait"; obj.endTime = time; obj.currentTime = 0; this.step.push(obj); if(this.loop != 0 && this.loop != 1) this.stepBackups.push(Tools.Copy(obj)); return this; } } class WTween { private static tweenTable : Array<WTweener> = new Array(); /// 在Main.ts中初始化后调用该方法给WTween添加心跳 public static Init() { egret.startTick(this.Update,this); } public static Play(wTweener : WTweener) { this.tweenTable.push(wTweener); return wTweener; } public static Stop(wTweener : WTweener) { var index = -1; for(var i = 0; i < this.tweenTable.length; i++) { if(this.tweenTable[i] == wTweener) { index = i; break; } } if(index != -1) this.tweenTable.splice(index, 1); } private static timeOnEnterFrame:number = 0; private static Update(timeStamp:number) { var now = timeStamp; var time = this.timeOnEnterFrame; var pass = now - time; this.timeOnEnterFrame = now; for(var i = 0; i < this.tweenTable.length; i++) { switch(this.tweenTable[i].step[0]["type"]) { case "to": if(this.tweenTable[i].step[0]["currentTime"] == 0) { if(this.tweenTable[i].step[0]["index"] != 0) { for(var key in this.tweenTable[i].step[0]["startValues"]) { this.tweenTable[i].step[0]["startValues"][key] = this.tweenTable[i].target[key]; this.tweenTable[i].stepBackups.forEach((value,index,array)=>{ if(value["index"] != null && value["index"] == this.tweenTable[i].step[0]["index"]) this.tweenTable[i].stepBackups[index]["startValues"][key] = this.tweenTable[i].target[key]; }) } } } if(this.tweenTable[i].step[0]["currentTime"] + pass < this.tweenTable[i].step[0]["endTime"]) { this.tweenTable[i].step[0]["currentTime"] += pass; this.DoTween(this.tweenTable[i].target, this.tweenTable[i].step[0]) } else { for(var key in this.tweenTable[i].step[0]["endValues"]) this.tweenTable[i].target[key] = this.tweenTable[i].step[0]["endValues"][key]; this.tweenTable[i].step.shift(); } break; case "call": if(this.tweenTable[i].step[0]["func"] != null) this.tweenTable[i].step[0]["func"].call(); this.tweenTable[i].step.shift(); break; case "wait": if(this.tweenTable[i].step[0]["currentTime"] < this.tweenTable[i].step[0]["endTime"]) this.tweenTable[i].step[0]["currentTime"] += pass; else this.tweenTable[i].step.shift(); break; } if(this.tweenTable[i].step.length == 0) { if(this.tweenTable[i].loop > 1 || this.tweenTable[i].loop == -1) { if(this.tweenTable[i].loop > 1) this.tweenTable[i].loop--; this.tweenTable[i].step = Tools.Copy(this.tweenTable[i].stepBackups); for(var j = 0; j < this.tweenTable[i].stepBackups.length; j++) { if(this.tweenTable[i].stepBackups[j]["type"] == "to") { for(var key in this.tweenTable[i].stepBackups[j]["startValues"]) { this.tweenTable[i].target[key] = this.tweenTable[i].stepBackups[j]["startValues"][key]; } break; } } } else this.Stop(this.tweenTable[i]); } } return true; } private static DoTween(target : any, obj : any) { for(var key in obj["endValues"]) { var currentValue = 0; switch(obj["ease"]) { case WTweenType.Linear: currentValue = this.Linear(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BackEaseIn: currentValue = this.BackEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BackEaseInOut: currentValue = this.BackEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BackEaseOut: currentValue = this.BackEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BackEaseOutIn: currentValue = this.BackEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BounceEaseIn: currentValue = this.BounceEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BounceEaseInOut: currentValue = this.BounceEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BounceEaseOut: currentValue = this.BounceEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.BounceEaseOutIn: currentValue = this.BounceEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CircEaseIn: currentValue = this.CircEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CircEaseInOut: currentValue = this.CircEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CircEaseOut: currentValue = this.CircEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CircEaseOutIn: currentValue = this.CircEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CubicEaseIn: currentValue = this.CubicEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CubicEaseInOut: currentValue = this.CubicEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CubicEaseOut: currentValue = this.CubicEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.CubicEaseOutIn: currentValue = this.CubicEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ElasticEaseIn: currentValue = this.ElasticEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ElasticEaseInOut: currentValue = this.ElasticEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ElasticEaseOut: currentValue = this.ElasticEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ElasticEaseOutIn: currentValue = this.ElasticEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ExpoEaseIn: currentValue = this.ExpoEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ExpoEaseInOut: currentValue = this.ExpoEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ExpoEaseOut: currentValue = this.ExpoEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.ExpoEaseOutIn: currentValue = this.ExpoEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuadEaseIn: currentValue = this.QuadEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuadEaseInOut: currentValue = this.QuadEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuadEaseOut: currentValue = this.QuadEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuadEaseOutIn: currentValue = this.QuadEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuartEaseIn: currentValue = this.QuartEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuartEaseInOut: currentValue = this.QuartEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuartEaseOut: currentValue = this.QuartEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuartEaseOutIn: currentValue = this.QuartEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuintEaseIn: currentValue = this.QuintEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuintEaseInOut: currentValue = this.QuintEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuintEaseOut: currentValue = this.QuintEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.QuintEaseOutIn: currentValue = this.QuintEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.SineEaseIn: currentValue = this.SineEaseIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.SineEaseInOut: currentValue = this.SineEaseInOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.SineEaseOut: currentValue = this.SineEaseOut(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; case WTweenType.SineEaseOutIn: currentValue = this.SineEaseOutIn(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; default: currentValue = this.Linear(obj["currentTime"], 0, obj["endValues"][key] - obj["startValues"][key], obj["endTime"]); break; } target[key] = currentValue + obj["startValues"][key]; } } /// <summary> /// Easing equation function for a simple linear tweening, with no easing. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static Linear(t : number, b : number, c : number, d : number) { return c * t / d + b; } /// <summary> /// Easing equation function for an exponential (2^t) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ExpoEaseOut(t : number, b : number, c : number, d : number) { return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b; } /// <summary> /// Easing equation function for an exponential (2^t) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ExpoEaseIn(t : number, b : number, c : number, d : number) { return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b; } /// <summary> /// Easing equation function for an exponential (2^t) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ExpoEaseInOut(t : number, b : number, c : number, d : number) { if (t == 0) return b; if (t == d) return b + c; if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b; } /// <summary> /// Easing equation function for an exponential (2^t) easing out/in: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ExpoEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.ExpoEaseOut(t * 2, b, c / 2, d); return this.ExpoEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a circular (sqrt(1-t^2)) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CircEaseOut(t : number, b : number, c : number, d : number) { return c * Math.sqrt(1 - (t = t / d - 1) * t) + b; } /// <summary> /// Easing equation function for a circular (sqrt(1-t^2)) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CircEaseIn(t : number, b : number, c : number, d : number) { return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b; } /// <summary> /// Easing equation function for a circular (sqrt(1-t^2)) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CircEaseInOut(t : number, b : number, c : number, d : number) { if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b; return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b; } /// <summary> /// Easing equation function for a circular (sqrt(1-t^2)) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CircEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.CircEaseOut(t * 2, b, c / 2, d); return this.CircEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a quadratic (t^2) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuadEaseOut(t : number, b : number, c : number, d : number) { return -c * (t /= d) * (t - 2) + b; } /// <summary> /// Easing equation function for a quadratic (t^2) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuadEaseIn(t : number, b : number, c : number, d : number) { return c * (t /= d) * t + b; } /// <summary> /// Easing equation function for a quadratic (t^2) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuadEaseInOut(t : number, b : number, c : number, d : number) { if ((t /= d / 2) < 1) return c / 2 * t * t + b; return -c / 2 * ((--t) * (t - 2) - 1) + b; } /// <summary> /// Easing equation function for a quadratic (t^2) easing out/in: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuadEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.QuadEaseOut(t * 2, b, c / 2, d); return this.QuadEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a sinusoidal (sin(t)) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static SineEaseOut(t : number, b : number, c : number, d : number) { return c * Math.sin(t / d * (Math.PI / 2)) + b; } /// <summary> /// Easing equation function for a sinusoidal (sin(t)) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static SineEaseIn(t : number, b : number, c : number, d : number) { return -c * Math.cos(t / d * (Math.PI / 2)) + c + b; } /// <summary> /// Easing equation function for a sinusoidal (sin(t)) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static SineEaseInOut(t : number, b : number, c : number, d : number) { if ((t /= d / 2) < 1) return c / 2 * (Math.sin(Math.PI * t / 2)) + b; return -c / 2 * (Math.cos(Math.PI * --t / 2) - 2) + b; } /// <summary> /// Easing equation function for a sinusoidal (sin(t)) easing in/out: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static SineEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.SineEaseOut(t * 2, b, c / 2, d); return this.SineEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a cubic (t^3) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CubicEaseOut(t : number, b : number, c : number, d : number) { return c * ((t = t / d - 1) * t * t + 1) + b; } /// <summary> /// Easing equation function for a cubic (t^3) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CubicEaseIn(t : number, b : number, c : number, d : number) { return c * (t /= d) * t * t + b; } /// <summary> /// Easing equation function for a cubic (t^3) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CubicEaseInOut(t : number, b : number, c : number, d : number) { if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; return c / 2 * ((t -= 2) * t * t + 2) + b; } /// <summary> /// Easing equation function for a cubic (t^3) easing out/in: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static CubicEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.CubicEaseOut(t * 2, b, c / 2, d); return this.CubicEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a quartic (t^4) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuartEaseOut(t : number, b : number, c : number, d : number) { return -c * ((t = t / d - 1) * t * t * t - 1) + b; } /// <summary> /// Easing equation function for a quartic (t^4) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuartEaseIn(t : number, b : number, c : number, d : number) { return c * (t /= d) * t * t * t + b; } /// <summary> /// Easing equation function for a quartic (t^4) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuartEaseInOut(t : number, b : number, c : number, d : number) { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b; return -c / 2 * ((t -= 2) * t * t * t - 2) + b; } /// <summary> /// Easing equation function for a quartic (t^4) easing out/in: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuartEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.QuartEaseOut(t * 2, b, c / 2, d); return this.QuartEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a quintic (t^5) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuintEaseOut(t : number, b : number, c : number, d : number) { return c * ((t = t / d - 1) * t * t * t * t + 1) + b; } /// <summary> /// Easing equation function for a quintic (t^5) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuintEaseIn(t : number, b : number, c : number, d : number) { return c * (t /= d) * t * t * t * t + b; } /// <summary> /// Easing equation function for a quintic (t^5) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuintEaseInOut(t : number, b : number, c : number, d : number) { if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b; return c / 2 * ((t -= 2) * t * t * t * t + 2) + b; } /// <summary> /// Easing equation function for a quintic (t^5) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static QuintEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.QuintEaseOut(t * 2, b, c / 2, d); return this.QuintEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for an elastic (exponentially decaying sine wave) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ElasticEaseOut(t : number, b : number, c : number, d : number) { if ((t /= d) == 1) return b + c; var p = d * 0.3; var s = p / 4; return (c * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b); } /// <summary> /// Easing equation function for an elastic (exponentially decaying sine wave) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ElasticEaseIn(t : number, b : number, c : number, d : number) { if ((t /= d) == 1) return b + c; var p = d * 0.3; var s = p / 4; return -(c * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; } /// <summary> /// Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ElasticEaseInOut(t : number, b : number, c : number, d : number) { if ((t /= d / 2) == 2) return b + c; var p = d * (0.3 * 1.5); var s = p / 4; if (t < 1) return -0.5 * (c * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; return c * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * 0.5 + c + b; } /// <summary> /// Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static ElasticEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.ElasticEaseOut(t * 2, b, c / 2, d); return this.ElasticEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BounceEaseOut(t : number, b : number, c : number, d : number) { if ((t /= d) < (1 / 2.75)) return c * (7.5625 * t * t) + b; else if (t < (2 / 2.75)) return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b; else if (t < (2.5 / 2.75)) return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b; else return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b; } /// <summary> /// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BounceEaseIn(t : number, b : number, c : number, d : number) { return c - this.BounceEaseOut(d - t, 0, c, d) + b; } /// <summary> /// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BounceEaseInOut(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.BounceEaseIn(t * 2, 0, c, d) * 0.5 + b; else return this.BounceEaseOut(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b; } /// <summary> /// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BounceEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.BounceEaseOut(t * 2, b, c / 2, d); return this.BounceEaseIn((t * 2) - d, b + c / 2, c / 2, d); } /// <summary> /// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: /// decelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BackEaseOut(t : number, b : number, c : number, d : number) { return c * ((t = t / d - 1) * t * ((1.70158 + 1) * t + 1.70158) + 1) + b; } /// <summary> /// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: /// accelerating from zero velocity. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BackEaseIn(t : number, b : number, c : number, d : number) { return c * (t /= d) * t * ((1.70158 + 1) * t - 1.70158) + b; } /// <summary> /// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: /// acceleration until halfway, then deceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BackEaseInOut(t : number, b : number, c : number, d : number) { var s = 1.70158; if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b; return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b; } /// <summary> /// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: /// deceleration until halfway, then acceleration. /// </summary> /// <param name="t">Current time in seconds.</param> /// <param name="b">Starting value.</param> /// <param name="c">Final value.</param> /// <param name="d">Duration of animation.</param> /// <returns>The correct value.</returns> public static BackEaseOutIn(t : number, b : number, c : number, d : number) { if (t < d / 2) return this.BackEaseOut(t * 2, b, c / 2, d); return this.BackEaseIn((t * 2) - d, b + c / 2, c / 2, d); } } |
数组及Json深度拷贝脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Tools{ public static Copy (o) { if (o instanceof Array) { var a = []; for (var i = 0; i < o.length; ++i) { a[i] = Tools.Copy(o[i]); } return a; } else if (o instanceof Function) { var b = o; return b; } else if (o instanceof Object) { var c = {} for (var j in o) { c[j] = Tools.Copy(o[j]); } return c; } else { return o; } } } |
给WTween添加心跳
1 2 3 4 5 6 |
class Main extends eui.UILayer { protected createChildren(): void { super.createChildren(); WTween.Init(); |
经常使用白鹭自带Tween动画的会发现 调用方式是跟Tween差不多的~~
本文为作者原创,如需转载请联系作者。
项目使用版本:EgretEngine5.1.5 GitHub下载地址:
https://github.com/654306663/WTween-For-Egret
- 本文固定链接: http://www.u3d8.com/?p=1571
- 转载请注明: 网虫虫 在 u3d8.com 发表过