1
0

vue.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // @ts-nocheck
  2. const TICK = Symbol('tick');
  3. const TICK_HANDLER = Symbol('tick-handler');
  4. const ANIMATIONS = Symbol('animations');
  5. const START_TIMES = Symbol('start-times');
  6. const PAUSE_START = Symbol('pause-start');
  7. const PAUSE_TIME = Symbol('pause-time');
  8. const _raf = typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame : function(cb: Function) {return setTimeout(cb, 1000/60)}
  9. const _caf = typeof cancelAnimationFrame !== 'undefined' ? cancelAnimationFrame: function(id: any) {clearTimeout(id)}
  10. // const TICK = 'tick';
  11. // const TICK_HANDLER = 'tick-handler';
  12. // const ANIMATIONS = 'animations';
  13. // const START_TIMES = 'start-times';
  14. // const PAUSE_START = 'pause-start';
  15. // const PAUSE_TIME = 'pause-time';
  16. // const _raf = function(callback):number|null {return setTimeout(callback, 1000/60)}
  17. // const _caf = function(id: number):void {clearTimeout(id)}
  18. export class Timeline {
  19. state: string
  20. constructor() {
  21. this.state = 'Initiated';
  22. this[ANIMATIONS] = new Set();
  23. this[START_TIMES] = new Map();
  24. }
  25. start() {
  26. if (!(this.state === 'Initiated')) return;
  27. this.state = 'Started';
  28. let startTime = Date.now();
  29. this[PAUSE_TIME] = 0;
  30. this[TICK] = () => {
  31. let now = Date.now();
  32. this[ANIMATIONS].forEach((animation) => {
  33. let t: number;
  34. if (this[START_TIMES].get(animation) < startTime) {
  35. t = now - startTime - animation.delay - this[PAUSE_TIME];
  36. } else {
  37. t = now - this[START_TIMES].get(animation) - animation.delay - this[PAUSE_TIME];
  38. }
  39. if (t > animation.duration) {
  40. this[ANIMATIONS].delete(animation);
  41. t = animation.duration;
  42. }
  43. if (t > 0) animation.run(t);
  44. })
  45. // for (let animation of this[ANIMATIONS]) {
  46. // let t: number;
  47. // console.log('animation', animation)
  48. // if (this[START_TIMES].get(animation) < startTime) {
  49. // t = now - startTime - animation.delay - this[PAUSE_TIME];
  50. // } else {
  51. // t = now - this[START_TIMES].get(animation) - animation.delay - this[PAUSE_TIME];
  52. // }
  53. // if (t > animation.duration) {
  54. // this[ANIMATIONS].delete(animation);
  55. // t = animation.duration;
  56. // }
  57. // if (t > 0) animation.run(t);
  58. // }
  59. this[TICK_HANDLER] = _raf(this[TICK]);
  60. };
  61. this[TICK]();
  62. }
  63. pause() {
  64. if (!(this.state === 'Started')) return;
  65. this.state = 'Paused';
  66. this[PAUSE_START] = Date.now();
  67. _caf(this[TICK_HANDLER]);
  68. }
  69. resume() {
  70. if (!(this.state === 'Paused')) return;
  71. this.state = 'Started';
  72. this[PAUSE_TIME] += Date.now() - this[PAUSE_START];
  73. this[TICK]();
  74. }
  75. reset() {
  76. this.pause();
  77. this.state = 'Initiated';
  78. this[PAUSE_TIME] = 0;
  79. this[PAUSE_START] = 0;
  80. this[ANIMATIONS] = new Set();
  81. this[START_TIMES] = new Map();
  82. this[TICK_HANDLER] = null;
  83. }
  84. add(animation: any, startTime?: number) {
  85. if (arguments.length < 2) startTime = Date.now();
  86. this[ANIMATIONS].add(animation);
  87. this[START_TIMES].set(animation, startTime);
  88. }
  89. }
  90. export class Animation {
  91. startValue: number
  92. endValue: number
  93. duration: number
  94. timingFunction: (t: number) => number
  95. delay: number
  96. template: (t: number) => void
  97. constructor(startValue: number, endValue: number, duration: number, delay: number, timingFunction: (t: number) => number, template: (v: number) => void) {
  98. timingFunction = timingFunction || (v => v);
  99. template = template || (v => v);
  100. this.startValue = startValue;
  101. this.endValue = endValue;
  102. this.duration = duration;
  103. this.timingFunction = timingFunction;
  104. this.delay = delay;
  105. this.template = template;
  106. }
  107. run(time: number) {
  108. let range = this.endValue - this.startValue;
  109. let progress = time / this.duration
  110. if(progress != 1) progress = this.timingFunction(progress)
  111. this.template(this.startValue + range * progress)
  112. }
  113. }