Animation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { __extends } from "tslib";
  2. import Eventful from '../core/Eventful.js';
  3. import requestAnimationFrame from './requestAnimationFrame.js';
  4. import Animator from './Animator.js';
  5. export function getTime() {
  6. return new Date().getTime();
  7. }
  8. var Animation = (function (_super) {
  9. __extends(Animation, _super);
  10. function Animation(opts) {
  11. var _this = _super.call(this) || this;
  12. _this._running = false;
  13. _this._time = 0;
  14. _this._pausedTime = 0;
  15. _this._pauseStart = 0;
  16. _this._paused = false;
  17. opts = opts || {};
  18. _this.stage = opts.stage || {};
  19. return _this;
  20. }
  21. Animation.prototype.addClip = function (clip) {
  22. if (clip.animation) {
  23. this.removeClip(clip);
  24. }
  25. if (!this._head) {
  26. this._head = this._tail = clip;
  27. }
  28. else {
  29. this._tail.next = clip;
  30. clip.prev = this._tail;
  31. clip.next = null;
  32. this._tail = clip;
  33. }
  34. clip.animation = this;
  35. };
  36. Animation.prototype.addAnimator = function (animator) {
  37. animator.animation = this;
  38. var clip = animator.getClip();
  39. if (clip) {
  40. this.addClip(clip);
  41. }
  42. };
  43. Animation.prototype.removeClip = function (clip) {
  44. if (!clip.animation) {
  45. return;
  46. }
  47. var prev = clip.prev;
  48. var next = clip.next;
  49. if (prev) {
  50. prev.next = next;
  51. }
  52. else {
  53. this._head = next;
  54. }
  55. if (next) {
  56. next.prev = prev;
  57. }
  58. else {
  59. this._tail = prev;
  60. }
  61. clip.next = clip.prev = clip.animation = null;
  62. };
  63. Animation.prototype.removeAnimator = function (animator) {
  64. var clip = animator.getClip();
  65. if (clip) {
  66. this.removeClip(clip);
  67. }
  68. animator.animation = null;
  69. };
  70. Animation.prototype.update = function (notTriggerFrameAndStageUpdate) {
  71. var time = getTime() - this._pausedTime;
  72. var delta = time - this._time;
  73. var clip = this._head;
  74. while (clip) {
  75. var nextClip = clip.next;
  76. var finished = clip.step(time, delta);
  77. if (finished) {
  78. clip.ondestroy();
  79. this.removeClip(clip);
  80. clip = nextClip;
  81. }
  82. else {
  83. clip = nextClip;
  84. }
  85. }
  86. this._time = time;
  87. if (!notTriggerFrameAndStageUpdate) {
  88. this.trigger('frame', delta);
  89. this.stage.update && this.stage.update();
  90. }
  91. };
  92. Animation.prototype._startLoop = function () {
  93. var self = this;
  94. this._running = true;
  95. function step() {
  96. if (self._running) {
  97. requestAnimationFrame(step);
  98. !self._paused && self.update();
  99. }
  100. }
  101. requestAnimationFrame(step);
  102. };
  103. Animation.prototype.start = function () {
  104. if (this._running) {
  105. return;
  106. }
  107. this._time = getTime();
  108. this._pausedTime = 0;
  109. this._startLoop();
  110. };
  111. Animation.prototype.stop = function () {
  112. this._running = false;
  113. };
  114. Animation.prototype.pause = function () {
  115. if (!this._paused) {
  116. this._pauseStart = getTime();
  117. this._paused = true;
  118. }
  119. };
  120. Animation.prototype.resume = function () {
  121. if (this._paused) {
  122. this._pausedTime += getTime() - this._pauseStart;
  123. this._paused = false;
  124. }
  125. };
  126. Animation.prototype.clear = function () {
  127. var clip = this._head;
  128. while (clip) {
  129. var nextClip = clip.next;
  130. clip.prev = clip.next = clip.animation = null;
  131. clip = nextClip;
  132. }
  133. this._head = this._tail = null;
  134. };
  135. Animation.prototype.isFinished = function () {
  136. return this._head == null;
  137. };
  138. Animation.prototype.animate = function (target, options) {
  139. options = options || {};
  140. this.start();
  141. var animator = new Animator(target, options.loop);
  142. this.addAnimator(animator);
  143. return animator;
  144. };
  145. return Animation;
  146. }(Eventful));
  147. export default Animation;