ticker.mjs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*!
  2. * @pixi/ticker - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/ticker is licensed under the MIT License.
  6. * http://www.opensource.org/licenses/mit-license
  7. */
  8. import { settings } from '@pixi/settings';
  9. import { ExtensionType } from '@pixi/extensions';
  10. /**
  11. * Target frames per millisecond.
  12. * @static
  13. * @name TARGET_FPMS
  14. * @memberof PIXI.settings
  15. * @type {number}
  16. * @default 0.06
  17. */
  18. settings.TARGET_FPMS = 0.06;
  19. /**
  20. * Represents the update priorities used by internal PIXI classes when registered with
  21. * the {@link PIXI.Ticker} object. Higher priority items are updated first and lower
  22. * priority items, such as render, should go later.
  23. * @static
  24. * @constant
  25. * @name UPDATE_PRIORITY
  26. * @memberof PIXI
  27. * @enum {number}
  28. * @property {number} [INTERACTION=50] Highest priority, used for {@link PIXI.InteractionManager}
  29. * @property {number} [HIGH=25] High priority updating, {@link PIXI.VideoBaseTexture} and {@link PIXI.AnimatedSprite}
  30. * @property {number} [NORMAL=0] Default priority for ticker events, see {@link PIXI.Ticker#add}.
  31. * @property {number} [LOW=-25] Low priority used for {@link PIXI.Application} rendering.
  32. * @property {number} [UTILITY=-50] Lowest priority used for {@link PIXI.BasePrepare} utility.
  33. */
  34. var UPDATE_PRIORITY;
  35. (function (UPDATE_PRIORITY) {
  36. UPDATE_PRIORITY[UPDATE_PRIORITY["INTERACTION"] = 50] = "INTERACTION";
  37. UPDATE_PRIORITY[UPDATE_PRIORITY["HIGH"] = 25] = "HIGH";
  38. UPDATE_PRIORITY[UPDATE_PRIORITY["NORMAL"] = 0] = "NORMAL";
  39. UPDATE_PRIORITY[UPDATE_PRIORITY["LOW"] = -25] = "LOW";
  40. UPDATE_PRIORITY[UPDATE_PRIORITY["UTILITY"] = -50] = "UTILITY";
  41. })(UPDATE_PRIORITY || (UPDATE_PRIORITY = {}));
  42. /**
  43. * Internal class for handling the priority sorting of ticker handlers.
  44. * @private
  45. * @class
  46. * @memberof PIXI
  47. */
  48. var TickerListener = /** @class */ (function () {
  49. /**
  50. * Constructor
  51. * @private
  52. * @param fn - The listener function to be added for one update
  53. * @param context - The listener context
  54. * @param priority - The priority for emitting
  55. * @param once - If the handler should fire once
  56. */
  57. function TickerListener(fn, context, priority, once) {
  58. if (context === void 0) { context = null; }
  59. if (priority === void 0) { priority = 0; }
  60. if (once === void 0) { once = false; }
  61. /** The next item in chain. */
  62. this.next = null;
  63. /** The previous item in chain. */
  64. this.previous = null;
  65. /** `true` if this listener has been destroyed already. */
  66. this._destroyed = false;
  67. this.fn = fn;
  68. this.context = context;
  69. this.priority = priority;
  70. this.once = once;
  71. }
  72. /**
  73. * Simple compare function to figure out if a function and context match.
  74. * @private
  75. * @param fn - The listener function to be added for one update
  76. * @param context - The listener context
  77. * @returns `true` if the listener match the arguments
  78. */
  79. TickerListener.prototype.match = function (fn, context) {
  80. if (context === void 0) { context = null; }
  81. return this.fn === fn && this.context === context;
  82. };
  83. /**
  84. * Emit by calling the current function.
  85. * @private
  86. * @param deltaTime - time since the last emit.
  87. * @returns Next ticker
  88. */
  89. TickerListener.prototype.emit = function (deltaTime) {
  90. if (this.fn) {
  91. if (this.context) {
  92. this.fn.call(this.context, deltaTime);
  93. }
  94. else {
  95. this.fn(deltaTime);
  96. }
  97. }
  98. var redirect = this.next;
  99. if (this.once) {
  100. this.destroy(true);
  101. }
  102. // Soft-destroying should remove
  103. // the next reference
  104. if (this._destroyed) {
  105. this.next = null;
  106. }
  107. return redirect;
  108. };
  109. /**
  110. * Connect to the list.
  111. * @private
  112. * @param previous - Input node, previous listener
  113. */
  114. TickerListener.prototype.connect = function (previous) {
  115. this.previous = previous;
  116. if (previous.next) {
  117. previous.next.previous = this;
  118. }
  119. this.next = previous.next;
  120. previous.next = this;
  121. };
  122. /**
  123. * Destroy and don't use after this.
  124. * @private
  125. * @param hard - `true` to remove the `next` reference, this
  126. * is considered a hard destroy. Soft destroy maintains the next reference.
  127. * @returns The listener to redirect while emitting or removing.
  128. */
  129. TickerListener.prototype.destroy = function (hard) {
  130. if (hard === void 0) { hard = false; }
  131. this._destroyed = true;
  132. this.fn = null;
  133. this.context = null;
  134. // Disconnect, hook up next and previous
  135. if (this.previous) {
  136. this.previous.next = this.next;
  137. }
  138. if (this.next) {
  139. this.next.previous = this.previous;
  140. }
  141. // Redirect to the next item
  142. var redirect = this.next;
  143. // Remove references
  144. this.next = hard ? null : redirect;
  145. this.previous = null;
  146. return redirect;
  147. };
  148. return TickerListener;
  149. }());
  150. /**
  151. * A Ticker class that runs an update loop that other objects listen to.
  152. *
  153. * This class is composed around listeners meant for execution on the next requested animation frame.
  154. * Animation frames are requested only when necessary, e.g. When the ticker is started and the emitter has listeners.
  155. * @class
  156. * @memberof PIXI
  157. */
  158. var Ticker = /** @class */ (function () {
  159. function Ticker() {
  160. var _this = this;
  161. /**
  162. * Whether or not this ticker should invoke the method
  163. * {@link PIXI.Ticker#start} automatically
  164. * when a listener is added.
  165. */
  166. this.autoStart = false;
  167. /**
  168. * Scalar time value from last frame to this frame.
  169. * This value is capped by setting {@link PIXI.Ticker#minFPS}
  170. * and is scaled with {@link PIXI.Ticker#speed}.
  171. * **Note:** The cap may be exceeded by scaling.
  172. */
  173. this.deltaTime = 1;
  174. /**
  175. * The last time {@link PIXI.Ticker#update} was invoked.
  176. * This value is also reset internally outside of invoking
  177. * update, but only when a new animation frame is requested.
  178. * If the platform supports DOMHighResTimeStamp,
  179. * this value will have a precision of 1 µs.
  180. */
  181. this.lastTime = -1;
  182. /**
  183. * Factor of current {@link PIXI.Ticker#deltaTime}.
  184. * @example
  185. * // Scales ticker.deltaTime to what would be
  186. * // the equivalent of approximately 120 FPS
  187. * ticker.speed = 2;
  188. */
  189. this.speed = 1;
  190. /**
  191. * Whether or not this ticker has been started.
  192. * `true` if {@link PIXI.Ticker#start} has been called.
  193. * `false` if {@link PIXI.Ticker#stop} has been called.
  194. * While `false`, this value may change to `true` in the
  195. * event of {@link PIXI.Ticker#autoStart} being `true`
  196. * and a listener is added.
  197. */
  198. this.started = false;
  199. /** Internal current frame request ID */
  200. this._requestId = null;
  201. /**
  202. * Internal value managed by minFPS property setter and getter.
  203. * This is the maximum allowed milliseconds between updates.
  204. */
  205. this._maxElapsedMS = 100;
  206. /**
  207. * Internal value managed by minFPS property setter and getter.
  208. * This is the minimum allowed milliseconds between updates.
  209. */
  210. this._minElapsedMS = 0;
  211. /** If enabled, deleting is disabled.*/
  212. this._protected = false;
  213. /** The last time keyframe was executed. Maintains a relatively fixed interval with the previous value. */
  214. this._lastFrame = -1;
  215. this._head = new TickerListener(null, null, Infinity);
  216. this.deltaMS = 1 / settings.TARGET_FPMS;
  217. this.elapsedMS = 1 / settings.TARGET_FPMS;
  218. this._tick = function (time) {
  219. _this._requestId = null;
  220. if (_this.started) {
  221. // Invoke listeners now
  222. _this.update(time);
  223. // Listener side effects may have modified ticker state.
  224. if (_this.started && _this._requestId === null && _this._head.next) {
  225. _this._requestId = requestAnimationFrame(_this._tick);
  226. }
  227. }
  228. };
  229. }
  230. /**
  231. * Conditionally requests a new animation frame.
  232. * If a frame has not already been requested, and if the internal
  233. * emitter has listeners, a new frame is requested.
  234. * @private
  235. */
  236. Ticker.prototype._requestIfNeeded = function () {
  237. if (this._requestId === null && this._head.next) {
  238. // ensure callbacks get correct delta
  239. this.lastTime = performance.now();
  240. this._lastFrame = this.lastTime;
  241. this._requestId = requestAnimationFrame(this._tick);
  242. }
  243. };
  244. /**
  245. * Conditionally cancels a pending animation frame.
  246. * @private
  247. */
  248. Ticker.prototype._cancelIfNeeded = function () {
  249. if (this._requestId !== null) {
  250. cancelAnimationFrame(this._requestId);
  251. this._requestId = null;
  252. }
  253. };
  254. /**
  255. * Conditionally requests a new animation frame.
  256. * If the ticker has been started it checks if a frame has not already
  257. * been requested, and if the internal emitter has listeners. If these
  258. * conditions are met, a new frame is requested. If the ticker has not
  259. * been started, but autoStart is `true`, then the ticker starts now,
  260. * and continues with the previous conditions to request a new frame.
  261. * @private
  262. */
  263. Ticker.prototype._startIfPossible = function () {
  264. if (this.started) {
  265. this._requestIfNeeded();
  266. }
  267. else if (this.autoStart) {
  268. this.start();
  269. }
  270. };
  271. /**
  272. * Register a handler for tick events. Calls continuously unless
  273. * it is removed or the ticker is stopped.
  274. * @param fn - The listener function to be added for updates
  275. * @param context - The listener context
  276. * @param {number} [priority=PIXI.UPDATE_PRIORITY.NORMAL] - The priority for emitting
  277. * @returns This instance of a ticker
  278. */
  279. Ticker.prototype.add = function (fn, context, priority) {
  280. if (priority === void 0) { priority = UPDATE_PRIORITY.NORMAL; }
  281. return this._addListener(new TickerListener(fn, context, priority));
  282. };
  283. /**
  284. * Add a handler for the tick event which is only execute once.
  285. * @param fn - The listener function to be added for one update
  286. * @param context - The listener context
  287. * @param {number} [priority=PIXI.UPDATE_PRIORITY.NORMAL] - The priority for emitting
  288. * @returns This instance of a ticker
  289. */
  290. Ticker.prototype.addOnce = function (fn, context, priority) {
  291. if (priority === void 0) { priority = UPDATE_PRIORITY.NORMAL; }
  292. return this._addListener(new TickerListener(fn, context, priority, true));
  293. };
  294. /**
  295. * Internally adds the event handler so that it can be sorted by priority.
  296. * Priority allows certain handler (user, AnimatedSprite, Interaction) to be run
  297. * before the rendering.
  298. * @private
  299. * @param listener - Current listener being added.
  300. * @returns This instance of a ticker
  301. */
  302. Ticker.prototype._addListener = function (listener) {
  303. // For attaching to head
  304. var current = this._head.next;
  305. var previous = this._head;
  306. // Add the first item
  307. if (!current) {
  308. listener.connect(previous);
  309. }
  310. else {
  311. // Go from highest to lowest priority
  312. while (current) {
  313. if (listener.priority > current.priority) {
  314. listener.connect(previous);
  315. break;
  316. }
  317. previous = current;
  318. current = current.next;
  319. }
  320. // Not yet connected
  321. if (!listener.previous) {
  322. listener.connect(previous);
  323. }
  324. }
  325. this._startIfPossible();
  326. return this;
  327. };
  328. /**
  329. * Removes any handlers matching the function and context parameters.
  330. * If no handlers are left after removing, then it cancels the animation frame.
  331. * @param fn - The listener function to be removed
  332. * @param context - The listener context to be removed
  333. * @returns This instance of a ticker
  334. */
  335. Ticker.prototype.remove = function (fn, context) {
  336. var listener = this._head.next;
  337. while (listener) {
  338. // We found a match, lets remove it
  339. // no break to delete all possible matches
  340. // incase a listener was added 2+ times
  341. if (listener.match(fn, context)) {
  342. listener = listener.destroy();
  343. }
  344. else {
  345. listener = listener.next;
  346. }
  347. }
  348. if (!this._head.next) {
  349. this._cancelIfNeeded();
  350. }
  351. return this;
  352. };
  353. Object.defineProperty(Ticker.prototype, "count", {
  354. /**
  355. * The number of listeners on this ticker, calculated by walking through linked list
  356. * @readonly
  357. * @member {number}
  358. */
  359. get: function () {
  360. if (!this._head) {
  361. return 0;
  362. }
  363. var count = 0;
  364. var current = this._head;
  365. while ((current = current.next)) {
  366. count++;
  367. }
  368. return count;
  369. },
  370. enumerable: false,
  371. configurable: true
  372. });
  373. /** Starts the ticker. If the ticker has listeners a new animation frame is requested at this point. */
  374. Ticker.prototype.start = function () {
  375. if (!this.started) {
  376. this.started = true;
  377. this._requestIfNeeded();
  378. }
  379. };
  380. /** Stops the ticker. If the ticker has requested an animation frame it is canceled at this point. */
  381. Ticker.prototype.stop = function () {
  382. if (this.started) {
  383. this.started = false;
  384. this._cancelIfNeeded();
  385. }
  386. };
  387. /** Destroy the ticker and don't use after this. Calling this method removes all references to internal events. */
  388. Ticker.prototype.destroy = function () {
  389. if (!this._protected) {
  390. this.stop();
  391. var listener = this._head.next;
  392. while (listener) {
  393. listener = listener.destroy(true);
  394. }
  395. this._head.destroy();
  396. this._head = null;
  397. }
  398. };
  399. /**
  400. * Triggers an update. An update entails setting the
  401. * current {@link PIXI.Ticker#elapsedMS},
  402. * the current {@link PIXI.Ticker#deltaTime},
  403. * invoking all listeners with current deltaTime,
  404. * and then finally setting {@link PIXI.Ticker#lastTime}
  405. * with the value of currentTime that was provided.
  406. * This method will be called automatically by animation
  407. * frame callbacks if the ticker instance has been started
  408. * and listeners are added.
  409. * @param {number} [currentTime=performance.now()] - the current time of execution
  410. */
  411. Ticker.prototype.update = function (currentTime) {
  412. if (currentTime === void 0) { currentTime = performance.now(); }
  413. var elapsedMS;
  414. // If the difference in time is zero or negative, we ignore most of the work done here.
  415. // If there is no valid difference, then should be no reason to let anyone know about it.
  416. // A zero delta, is exactly that, nothing should update.
  417. //
  418. // The difference in time can be negative, and no this does not mean time traveling.
  419. // This can be the result of a race condition between when an animation frame is requested
  420. // on the current JavaScript engine event loop, and when the ticker's start method is invoked
  421. // (which invokes the internal _requestIfNeeded method). If a frame is requested before
  422. // _requestIfNeeded is invoked, then the callback for the animation frame the ticker requests,
  423. // can receive a time argument that can be less than the lastTime value that was set within
  424. // _requestIfNeeded. This difference is in microseconds, but this is enough to cause problems.
  425. //
  426. // This check covers this browser engine timing issue, as well as if consumers pass an invalid
  427. // currentTime value. This may happen if consumers opt-out of the autoStart, and update themselves.
  428. if (currentTime > this.lastTime) {
  429. // Save uncapped elapsedMS for measurement
  430. elapsedMS = this.elapsedMS = currentTime - this.lastTime;
  431. // cap the milliseconds elapsed used for deltaTime
  432. if (elapsedMS > this._maxElapsedMS) {
  433. elapsedMS = this._maxElapsedMS;
  434. }
  435. elapsedMS *= this.speed;
  436. // If not enough time has passed, exit the function.
  437. // Get ready for next frame by setting _lastFrame, but based on _minElapsedMS
  438. // adjustment to ensure a relatively stable interval.
  439. if (this._minElapsedMS) {
  440. var delta = currentTime - this._lastFrame | 0;
  441. if (delta < this._minElapsedMS) {
  442. return;
  443. }
  444. this._lastFrame = currentTime - (delta % this._minElapsedMS);
  445. }
  446. this.deltaMS = elapsedMS;
  447. this.deltaTime = this.deltaMS * settings.TARGET_FPMS;
  448. // Cache a local reference, in-case ticker is destroyed
  449. // during the emit, we can still check for head.next
  450. var head = this._head;
  451. // Invoke listeners added to internal emitter
  452. var listener = head.next;
  453. while (listener) {
  454. listener = listener.emit(this.deltaTime);
  455. }
  456. if (!head.next) {
  457. this._cancelIfNeeded();
  458. }
  459. }
  460. else {
  461. this.deltaTime = this.deltaMS = this.elapsedMS = 0;
  462. }
  463. this.lastTime = currentTime;
  464. };
  465. Object.defineProperty(Ticker.prototype, "FPS", {
  466. /**
  467. * The frames per second at which this ticker is running.
  468. * The default is approximately 60 in most modern browsers.
  469. * **Note:** This does not factor in the value of
  470. * {@link PIXI.Ticker#speed}, which is specific
  471. * to scaling {@link PIXI.Ticker#deltaTime}.
  472. * @member {number}
  473. * @readonly
  474. */
  475. get: function () {
  476. return 1000 / this.elapsedMS;
  477. },
  478. enumerable: false,
  479. configurable: true
  480. });
  481. Object.defineProperty(Ticker.prototype, "minFPS", {
  482. /**
  483. * Manages the maximum amount of milliseconds allowed to
  484. * elapse between invoking {@link PIXI.Ticker#update}.
  485. * This value is used to cap {@link PIXI.Ticker#deltaTime},
  486. * but does not effect the measured value of {@link PIXI.Ticker#FPS}.
  487. * When setting this property it is clamped to a value between
  488. * `0` and `PIXI.settings.TARGET_FPMS * 1000`.
  489. * @member {number}
  490. * @default 10
  491. */
  492. get: function () {
  493. return 1000 / this._maxElapsedMS;
  494. },
  495. set: function (fps) {
  496. // Minimum must be below the maxFPS
  497. var minFPS = Math.min(this.maxFPS, fps);
  498. // Must be at least 0, but below 1 / settings.TARGET_FPMS
  499. var minFPMS = Math.min(Math.max(0, minFPS) / 1000, settings.TARGET_FPMS);
  500. this._maxElapsedMS = 1 / minFPMS;
  501. },
  502. enumerable: false,
  503. configurable: true
  504. });
  505. Object.defineProperty(Ticker.prototype, "maxFPS", {
  506. /**
  507. * Manages the minimum amount of milliseconds required to
  508. * elapse between invoking {@link PIXI.Ticker#update}.
  509. * This will effect the measured value of {@link PIXI.Ticker#FPS}.
  510. * If it is set to `0`, then there is no limit; PixiJS will render as many frames as it can.
  511. * Otherwise it will be at least `minFPS`
  512. * @member {number}
  513. * @default 0
  514. */
  515. get: function () {
  516. if (this._minElapsedMS) {
  517. return Math.round(1000 / this._minElapsedMS);
  518. }
  519. return 0;
  520. },
  521. set: function (fps) {
  522. if (fps === 0) {
  523. this._minElapsedMS = 0;
  524. }
  525. else {
  526. // Max must be at least the minFPS
  527. var maxFPS = Math.max(this.minFPS, fps);
  528. this._minElapsedMS = 1 / (maxFPS / 1000);
  529. }
  530. },
  531. enumerable: false,
  532. configurable: true
  533. });
  534. Object.defineProperty(Ticker, "shared", {
  535. /**
  536. * The shared ticker instance used by {@link PIXI.AnimatedSprite} and by
  537. * {@link PIXI.VideoResource} to update animation frames / video textures.
  538. *
  539. * It may also be used by {@link PIXI.Application} if created with the `sharedTicker` option property set to true.
  540. *
  541. * The property {@link PIXI.Ticker#autoStart} is set to `true` for this instance.
  542. * Please follow the examples for usage, including how to opt-out of auto-starting the shared ticker.
  543. * @example
  544. * let ticker = PIXI.Ticker.shared;
  545. * // Set this to prevent starting this ticker when listeners are added.
  546. * // By default this is true only for the PIXI.Ticker.shared instance.
  547. * ticker.autoStart = false;
  548. * // FYI, call this to ensure the ticker is stopped. It should be stopped
  549. * // if you have not attempted to render anything yet.
  550. * ticker.stop();
  551. * // Call this when you are ready for a running shared ticker.
  552. * ticker.start();
  553. * @example
  554. * // You may use the shared ticker to render...
  555. * let renderer = PIXI.autoDetectRenderer();
  556. * let stage = new PIXI.Container();
  557. * document.body.appendChild(renderer.view);
  558. * ticker.add(function (time) {
  559. * renderer.render(stage);
  560. * });
  561. * @example
  562. * // Or you can just update it manually.
  563. * ticker.autoStart = false;
  564. * ticker.stop();
  565. * function animate(time) {
  566. * ticker.update(time);
  567. * renderer.render(stage);
  568. * requestAnimationFrame(animate);
  569. * }
  570. * animate(performance.now());
  571. * @member {PIXI.Ticker}
  572. * @static
  573. */
  574. get: function () {
  575. if (!Ticker._shared) {
  576. var shared = Ticker._shared = new Ticker();
  577. shared.autoStart = true;
  578. shared._protected = true;
  579. }
  580. return Ticker._shared;
  581. },
  582. enumerable: false,
  583. configurable: true
  584. });
  585. Object.defineProperty(Ticker, "system", {
  586. /**
  587. * The system ticker instance used by {@link PIXI.InteractionManager} and by
  588. * {@link PIXI.BasePrepare} for core timing functionality that shouldn't usually need to be paused,
  589. * unlike the `shared` ticker which drives visual animations and rendering which may want to be paused.
  590. *
  591. * The property {@link PIXI.Ticker#autoStart} is set to `true` for this instance.
  592. * @member {PIXI.Ticker}
  593. * @static
  594. */
  595. get: function () {
  596. if (!Ticker._system) {
  597. var system = Ticker._system = new Ticker();
  598. system.autoStart = true;
  599. system._protected = true;
  600. }
  601. return Ticker._system;
  602. },
  603. enumerable: false,
  604. configurable: true
  605. });
  606. return Ticker;
  607. }());
  608. /**
  609. * Middleware for for Application Ticker.
  610. * @example
  611. * import {TickerPlugin} from '@pixi/ticker';
  612. * import {Application} from '@pixi/app';
  613. * import {extensions} from '@pixi/extensions';
  614. * extensions.add(TickerPlugin);
  615. * @class
  616. * @memberof PIXI
  617. */
  618. var TickerPlugin = /** @class */ (function () {
  619. function TickerPlugin() {
  620. }
  621. /**
  622. * Initialize the plugin with scope of application instance
  623. * @static
  624. * @private
  625. * @param {object} [options] - See application options
  626. */
  627. TickerPlugin.init = function (options) {
  628. var _this = this;
  629. // Set default
  630. options = Object.assign({
  631. autoStart: true,
  632. sharedTicker: false,
  633. }, options);
  634. // Create ticker setter
  635. Object.defineProperty(this, 'ticker', {
  636. set: function (ticker) {
  637. if (this._ticker) {
  638. this._ticker.remove(this.render, this);
  639. }
  640. this._ticker = ticker;
  641. if (ticker) {
  642. ticker.add(this.render, this, UPDATE_PRIORITY.LOW);
  643. }
  644. },
  645. get: function () {
  646. return this._ticker;
  647. },
  648. });
  649. /**
  650. * Convenience method for stopping the render.
  651. * @method
  652. * @memberof PIXI.Application
  653. * @instance
  654. */
  655. this.stop = function () {
  656. _this._ticker.stop();
  657. };
  658. /**
  659. * Convenience method for starting the render.
  660. * @method
  661. * @memberof PIXI.Application
  662. * @instance
  663. */
  664. this.start = function () {
  665. _this._ticker.start();
  666. };
  667. /**
  668. * Internal reference to the ticker.
  669. * @type {PIXI.Ticker}
  670. * @name _ticker
  671. * @memberof PIXI.Application#
  672. * @private
  673. */
  674. this._ticker = null;
  675. /**
  676. * Ticker for doing render updates.
  677. * @type {PIXI.Ticker}
  678. * @name ticker
  679. * @memberof PIXI.Application#
  680. * @default PIXI.Ticker.shared
  681. */
  682. this.ticker = options.sharedTicker ? Ticker.shared : new Ticker();
  683. // Start the rendering
  684. if (options.autoStart) {
  685. this.start();
  686. }
  687. };
  688. /**
  689. * Clean up the ticker, scoped to application.
  690. * @static
  691. * @private
  692. */
  693. TickerPlugin.destroy = function () {
  694. if (this._ticker) {
  695. var oldTicker = this._ticker;
  696. this.ticker = null;
  697. oldTicker.destroy();
  698. }
  699. };
  700. /** @ignore */
  701. TickerPlugin.extension = ExtensionType.Application;
  702. return TickerPlugin;
  703. }());
  704. export { Ticker, TickerPlugin, UPDATE_PRIORITY };
  705. //# sourceMappingURL=ticker.mjs.map