ticker.js 29 KB

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