Source: animate/AnimatorTimeline.js

animate/AnimatorTimeline.js

  1. const pool = [];
  2. import { Animator } from './Animator';
  3. /**
  4. * Represents a single animation play.
  5. * @class AnimatorTimeline
  6. * @memberof PIXI.animate
  7. */
  8. class AnimatorTimeline {
  9. constructor() {
  10. this._update = this.update.bind(this);
  11. this.init(null, 0, 0, false, null);
  12. }
  13. /**
  14. * The pool of timelines to use
  15. * @method PIXI.animate.AnimatorTimeline#init
  16. * @param {PIXI.animate.MovieClip} instance
  17. * @param {Number} start
  18. * @param {Number} end
  19. * @param {Boolean} loop
  20. * @param {Function} callback
  21. * @private
  22. */
  23. init(instance, start, end, loop, callback) {
  24. /**
  25. * Instance of clip to play.
  26. * @name PIXI.animate.AnimatorTimeline#instance
  27. * @type {PIXI.animate.MovieClip}
  28. * @readOnly
  29. */
  30. this.instance = instance;
  31. /**
  32. * `true` if the timeline is suppose to loop.
  33. * @name PIXI.animate.AnimatorTimeline#loop
  34. * @type {Boolean}
  35. * @readOnly
  36. */
  37. this.loop = loop;
  38. /**
  39. * Frame number of the starting farme.
  40. * @name PIXI.animate.AnimatorTimeline#start
  41. * @type {int}
  42. * @readOnly
  43. */
  44. this.start = start;
  45. /**
  46. * Frame number of the ending frame.
  47. * @name PIXI.animate.AnimatorTimeline#end
  48. * @type {int}
  49. * @readOnly
  50. */
  51. this.end = end;
  52. /**
  53. * Callback called when completed (non-looping animation).
  54. * @name PIXI.animate.AnimatorTimeline#callback
  55. * @type {Function}
  56. * @readOnly
  57. */
  58. this.callback = callback;
  59. if (instance) {
  60. //Prevent overshooting the end frame and looping back around:
  61. instance.loop = false;
  62. instance.gotoAndStop(start);
  63. instance._beforeUpdate = this._update;
  64. }
  65. }
  66. /**
  67. * Don't use after this
  68. * @method PIXI.animate.AnimatorTimeline#destroy
  69. * @private
  70. */
  71. destroy() {
  72. this.instance._beforeUpdate = null;
  73. this.init(null, 0, 0, false, null);
  74. AnimatorTimeline._pool.push(this);
  75. }
  76. /**
  77. * Is the animation complete
  78. * @method PIXI.animate.AnimatorTimeline#update
  79. * @param {PIXI.animate.MovieClip} instance
  80. * @return {Function} Callback to do after updateTimeline
  81. * @private
  82. */
  83. update(instance) {
  84. let completed;
  85. if (instance.currentFrame >= this.end) {
  86. // In case we over-shoot the current frame becuase of low FPS
  87. instance.currentFrame = this.end;
  88. if (this.loop) {
  89. // Update timeline so we get actions at the end frame
  90. instance._updateTimeline();
  91. instance.gotoAndPlay(this.start);
  92. } else {
  93. instance.stop();
  94. if (this.callback) {
  95. completed = this.callback;
  96. }
  97. this.stop(); // cleanup timeline
  98. }
  99. }
  100. return completed;
  101. }
  102. /**
  103. * Stop the animation, cannot be reused.
  104. * @method PIXI.animate.AnimatorTimeline#stop
  105. */
  106. stop() {
  107. // PIXI.animate.Animator._internalStop(this);
  108. Animator._internalStop(this);
  109. }
  110. /**
  111. * The progress from 0 to 1 of the playback.
  112. * @name PIXI.animate.AnimatorTimeline#progress
  113. * @type {Number}
  114. * @readOnly
  115. */
  116. get progress() {
  117. const progress = (this.instance.currentFrame - this.start) / (this.end - this.start);
  118. return Math.max(0, Math.min(1, progress)); // clamp
  119. }
  120. /**
  121. * The pool of timelines to use
  122. * @name PIXI.animate.AnimatorTimeline._pool
  123. * @type {Array<PIXI.animate.AnimatorTimeline>}
  124. * @static
  125. * @private
  126. */
  127. static get _pool() {
  128. return pool;
  129. }
  130. /**
  131. * Create a new timeline
  132. * @method PIXI.animate.AnimatorTimeline.create
  133. * @static
  134. * @param {PIXI.animate.MovieClip} instance
  135. * @param {Number} start
  136. * @param {Number} end
  137. * @param {Boolean} loop
  138. * @param {Function} callback
  139. * @return {PIXI.animate.AnimatorTimeline}
  140. */
  141. static create(instance, start, end, loop, callback) {
  142. var timeline;
  143. if (this._pool.length) {
  144. timeline = this._pool.pop();
  145. } else {
  146. timeline = new AnimatorTimeline();
  147. }
  148. timeline.init(instance, start, end, loop, callback);
  149. return timeline;
  150. }
  151. }
  152. module.exports = AnimatorTimeline;