Tween.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Easing } from "./Easing.js";
  2. export class TweenManager {
  3. constructor() {
  4. this.clear()
  5. }
  6. add(tween) {
  7. tween.setId(this.tweenIdIndex++)
  8. this.activeTweens.push(tween);
  9. return tween.getId();
  10. }
  11. remove(tween) {
  12. this.activeTweens.splice(this.activeTweens.findIndex(t => t == tween), 1);
  13. }
  14. cancel(tweenId) {
  15. let tweenIndex = this.activeTweens.findIndex(t => t.getId() == tweenId);
  16. if(tweenIndex != -1) {
  17. this.activeTweens.splice(tweenIndex, 1)
  18. }
  19. }
  20. update() {
  21. let completedTweens = [];
  22. for(let i = 0; i < this.activeTweens.length; i++) {
  23. let tween = this.activeTweens[i];
  24. tween.update();
  25. if(tween.isComplete()) {
  26. completedTweens.push(tween);
  27. }
  28. }
  29. for(let i = 0; i < completedTweens.length; i++) {
  30. let tween = completedTweens[i];
  31. tween.onFinish();
  32. this.remove(tween);
  33. }
  34. }
  35. clear() {
  36. this.activeTweens = [];
  37. this.tweenIdIndex = 0;
  38. }
  39. }
  40. export class Tween {
  41. constructor(objToTween, targetProperties, duration, easing = Easing.Linear.EaseNone, onFinish = () => {}) {
  42. this.objectToTween = objToTween;
  43. this.startProperties = {};
  44. this.targetProperties = JSON.parse(JSON.stringify(targetProperties));
  45. this.saveInitialValues(this.startProperties, objToTween, targetProperties);
  46. this.startTime = new Date().getTime()
  47. this.endTime = this.startTime + duration
  48. this.easing = easing;
  49. this.onFinish = onFinish;
  50. this.id = -1;
  51. }
  52. saveInitialValues(propertiesObj, obj, targetProperties) {
  53. for(let property in targetProperties) {
  54. let startValue = obj[property];
  55. if(typeof startValue == "object") {
  56. propertiesObj[property] = {};
  57. this.saveInitialValues(propertiesObj[property], obj[property], targetProperties[property]);
  58. continue;
  59. }
  60. propertiesObj[property] = startValue;
  61. }
  62. }
  63. update() {
  64. let currentTime = new Date().getTime();
  65. let runTime = currentTime - this.startTime;
  66. let duration = this.endTime - this.startTime;
  67. let percentageComplete = Math.min(1, runTime / duration);
  68. let valueToUpdate = this.easing(percentageComplete);
  69. this.updateProperties(this.objectToTween, this.startProperties, this.targetProperties, valueToUpdate);
  70. }
  71. updateProperties(obj, objStart, objTarget, valueToUpdate) {
  72. for(let property in objStart) {
  73. let startValue = objStart[property];
  74. if(typeof startValue == "object") {
  75. this.updateProperties(obj[property], objStart[property], objTarget[property], valueToUpdate);
  76. continue;
  77. }
  78. let endValue = objTarget[property];
  79. let delta = endValue - startValue;
  80. obj[property] = startValue + (delta * valueToUpdate);
  81. }
  82. }
  83. setId(newId){
  84. this.id = newId
  85. }
  86. getId() {
  87. return this.id
  88. }
  89. isComplete() {
  90. return this.endTime <= new Date().getTime()
  91. }
  92. }