ticker.js 26 KB

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