Animation.js 4.3 KB

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