timeline.js 3.1 KB

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