timeline.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { requestAnimationFrame } from '../util/requestAnimationFrame';
  2. var clock = typeof performance === 'object' && performance.now ? performance : Date;
  3. class Timeline {
  4. constructor() {
  5. this.anims = [];
  6. this.time = null;
  7. this.playing = false;
  8. this.canvas = [];
  9. }
  10. play() {
  11. var self = this;
  12. self.time = clock.now();
  13. self.playing = true;
  14. function step() {
  15. if (self.playing) {
  16. requestAnimationFrame(step);
  17. self.update();
  18. }
  19. }
  20. requestAnimationFrame(step);
  21. }
  22. stop() {
  23. this.playing = false;
  24. this.time = null;
  25. this.canvas = [];
  26. }
  27. pushAnim(animInfo) {
  28. // 先启动动画
  29. if (!this.playing) {
  30. this.play();
  31. }
  32. var {
  33. delay,
  34. duration
  35. } = animInfo;
  36. var startTime = this.time + delay;
  37. var endTime = startTime + duration;
  38. animInfo.startTime = startTime;
  39. animInfo.endTime = endTime;
  40. this.anims.push(animInfo);
  41. }
  42. update() {
  43. var currentTime = clock.now();
  44. this.canvas = [];
  45. if (!this.anims.length) {
  46. this.stop();
  47. return;
  48. }
  49. for (var i = 0; i < this.anims.length; i++) {
  50. var propertyAnim = this.anims[i];
  51. if (currentTime < propertyAnim.startTime || propertyAnim.hasEnded) {
  52. continue;
  53. }
  54. var shape = propertyAnim.shape; // shape
  55. if (shape.get('destroyed')) {
  56. this.anims.splice(i, 1);
  57. i--;
  58. continue;
  59. }
  60. var {
  61. startState,
  62. endState,
  63. interpolate,
  64. duration
  65. } = propertyAnim;
  66. if (currentTime >= propertyAnim.startTime && !propertyAnim.hasStarted) {
  67. propertyAnim.hasStarted = true;
  68. if (propertyAnim.onStart) {
  69. propertyAnim.onStart();
  70. }
  71. }
  72. var t = (currentTime - propertyAnim.startTime) / duration;
  73. t = Math.max(0, Math.min(t, 1));
  74. t = propertyAnim.easing(t);
  75. if (propertyAnim.onFrame) {
  76. propertyAnim.onFrame(t);
  77. } else {
  78. for (var key in interpolate) {
  79. var diff = interpolate[key];
  80. var value = diff(t);
  81. var newValue = void 0;
  82. if (key === 'points') {
  83. newValue = [];
  84. var aLen = Math.max(startState.points.length, endState.points.length);
  85. for (var j = 0; j < aLen; j += 2) {
  86. newValue.push({
  87. x: value[j],
  88. y: value[j + 1]
  89. });
  90. }
  91. } else {
  92. newValue = value;
  93. }
  94. shape._attrs.attrs[key] = newValue;
  95. shape._attrs.bbox = null; // should clear calculated bbox
  96. }
  97. }
  98. var canvas = shape.get('canvas');
  99. if (this.canvas.indexOf(canvas) === -1) {
  100. this.canvas.push(canvas);
  101. }
  102. if (propertyAnim.onUpdate) {
  103. propertyAnim.onUpdate(t);
  104. }
  105. if (currentTime >= propertyAnim.endTime && !propertyAnim.hasEnded) {
  106. propertyAnim.hasEnded = true;
  107. if (propertyAnim.onEnd) {
  108. propertyAnim.onEnd();
  109. }
  110. }
  111. if (t === 1) {
  112. // end
  113. this.anims.splice(i, 1);
  114. i--;
  115. }
  116. }
  117. this.canvas.map(function (c) {
  118. c.draw();
  119. return c;
  120. });
  121. this.time = clock.now();
  122. }
  123. }
  124. export default Timeline;