index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /// <reference path="./global.d.ts" />
  2. import type { ExtensionMetadata } from '@pixi/extensions';
  3. /**
  4. * A Ticker class that runs an update loop that other objects listen to.
  5. *
  6. * This class is composed around listeners meant for execution on the next requested animation frame.
  7. * Animation frames are requested only when necessary, e.g. When the ticker is started and the emitter has listeners.
  8. * @class
  9. * @memberof PIXI
  10. */
  11. export declare class Ticker {
  12. /** The private shared ticker instance */
  13. private static _shared;
  14. /** The private system ticker instance */
  15. private static _system;
  16. /**
  17. * Whether or not this ticker should invoke the method
  18. * {@link PIXI.Ticker#start} automatically
  19. * when a listener is added.
  20. */
  21. autoStart: boolean;
  22. /**
  23. * Scalar time value from last frame to this frame.
  24. * This value is capped by setting {@link PIXI.Ticker#minFPS}
  25. * and is scaled with {@link PIXI.Ticker#speed}.
  26. * **Note:** The cap may be exceeded by scaling.
  27. */
  28. deltaTime: number;
  29. /**
  30. * Scaler time elapsed in milliseconds from last frame to this frame.
  31. * This value is capped by setting {@link PIXI.Ticker#minFPS}
  32. * and is scaled with {@link PIXI.Ticker#speed}.
  33. * **Note:** The cap may be exceeded by scaling.
  34. * If the platform supports DOMHighResTimeStamp,
  35. * this value will have a precision of 1 µs.
  36. * Defaults to target frame time
  37. * @default 16.66
  38. */
  39. deltaMS: number;
  40. /**
  41. * Time elapsed in milliseconds from last frame to this frame.
  42. * Opposed to what the scalar {@link PIXI.Ticker#deltaTime}
  43. * is based, this value is neither capped nor scaled.
  44. * If the platform supports DOMHighResTimeStamp,
  45. * this value will have a precision of 1 µs.
  46. * Defaults to target frame time
  47. * @default 16.66
  48. */
  49. elapsedMS: number;
  50. /**
  51. * The last time {@link PIXI.Ticker#update} was invoked.
  52. * This value is also reset internally outside of invoking
  53. * update, but only when a new animation frame is requested.
  54. * If the platform supports DOMHighResTimeStamp,
  55. * this value will have a precision of 1 µs.
  56. */
  57. lastTime: number;
  58. /**
  59. * Factor of current {@link PIXI.Ticker#deltaTime}.
  60. * @example
  61. * // Scales ticker.deltaTime to what would be
  62. * // the equivalent of approximately 120 FPS
  63. * ticker.speed = 2;
  64. */
  65. speed: number;
  66. /**
  67. * Whether or not this ticker has been started.
  68. * `true` if {@link PIXI.Ticker#start} has been called.
  69. * `false` if {@link PIXI.Ticker#stop} has been called.
  70. * While `false`, this value may change to `true` in the
  71. * event of {@link PIXI.Ticker#autoStart} being `true`
  72. * and a listener is added.
  73. */
  74. started: boolean;
  75. /** The first listener. All new listeners added are chained on this. */
  76. private _head;
  77. /** Internal current frame request ID */
  78. private _requestId;
  79. /**
  80. * Internal value managed by minFPS property setter and getter.
  81. * This is the maximum allowed milliseconds between updates.
  82. */
  83. private _maxElapsedMS;
  84. /**
  85. * Internal value managed by minFPS property setter and getter.
  86. * This is the minimum allowed milliseconds between updates.
  87. */
  88. private _minElapsedMS;
  89. /** If enabled, deleting is disabled.*/
  90. private _protected;
  91. /** The last time keyframe was executed. Maintains a relatively fixed interval with the previous value. */
  92. private _lastFrame;
  93. /**
  94. * Internal tick method bound to ticker instance.
  95. * This is because in early 2015, Function.bind
  96. * is still 60% slower in high performance scenarios.
  97. * Also separating frame requests from update method
  98. * so listeners may be called at any time and with
  99. * any animation API, just invoke ticker.update(time).
  100. * @param time - Time since last tick.
  101. */
  102. private _tick;
  103. constructor();
  104. /**
  105. * Conditionally requests a new animation frame.
  106. * If a frame has not already been requested, and if the internal
  107. * emitter has listeners, a new frame is requested.
  108. * @private
  109. */
  110. private _requestIfNeeded;
  111. /**
  112. * Conditionally cancels a pending animation frame.
  113. * @private
  114. */
  115. private _cancelIfNeeded;
  116. /**
  117. * Conditionally requests a new animation frame.
  118. * If the ticker has been started it checks if a frame has not already
  119. * been requested, and if the internal emitter has listeners. If these
  120. * conditions are met, a new frame is requested. If the ticker has not
  121. * been started, but autoStart is `true`, then the ticker starts now,
  122. * and continues with the previous conditions to request a new frame.
  123. * @private
  124. */
  125. private _startIfPossible;
  126. /**
  127. * Register a handler for tick events. Calls continuously unless
  128. * it is removed or the ticker is stopped.
  129. * @param fn - The listener function to be added for updates
  130. * @param context - The listener context
  131. * @param {number} [priority=PIXI.UPDATE_PRIORITY.NORMAL] - The priority for emitting
  132. * @returns This instance of a ticker
  133. */
  134. add<T = any>(fn: TickerCallback<T>, context?: T, priority?: UPDATE_PRIORITY): this;
  135. /**
  136. * Add a handler for the tick event which is only execute once.
  137. * @param fn - The listener function to be added for one update
  138. * @param context - The listener context
  139. * @param {number} [priority=PIXI.UPDATE_PRIORITY.NORMAL] - The priority for emitting
  140. * @returns This instance of a ticker
  141. */
  142. addOnce<T = any>(fn: TickerCallback<T>, context?: T, priority?: UPDATE_PRIORITY): this;
  143. /**
  144. * Internally adds the event handler so that it can be sorted by priority.
  145. * Priority allows certain handler (user, AnimatedSprite, Interaction) to be run
  146. * before the rendering.
  147. * @private
  148. * @param listener - Current listener being added.
  149. * @returns This instance of a ticker
  150. */
  151. private _addListener;
  152. /**
  153. * Removes any handlers matching the function and context parameters.
  154. * If no handlers are left after removing, then it cancels the animation frame.
  155. * @param fn - The listener function to be removed
  156. * @param context - The listener context to be removed
  157. * @returns This instance of a ticker
  158. */
  159. remove<T = any>(fn: TickerCallback<T>, context?: T): this;
  160. /**
  161. * The number of listeners on this ticker, calculated by walking through linked list
  162. * @readonly
  163. * @member {number}
  164. */
  165. get count(): number;
  166. /** Starts the ticker. If the ticker has listeners a new animation frame is requested at this point. */
  167. start(): void;
  168. /** Stops the ticker. If the ticker has requested an animation frame it is canceled at this point. */
  169. stop(): void;
  170. /** Destroy the ticker and don't use after this. Calling this method removes all references to internal events. */
  171. destroy(): void;
  172. /**
  173. * Triggers an update. An update entails setting the
  174. * current {@link PIXI.Ticker#elapsedMS},
  175. * the current {@link PIXI.Ticker#deltaTime},
  176. * invoking all listeners with current deltaTime,
  177. * and then finally setting {@link PIXI.Ticker#lastTime}
  178. * with the value of currentTime that was provided.
  179. * This method will be called automatically by animation
  180. * frame callbacks if the ticker instance has been started
  181. * and listeners are added.
  182. * @param {number} [currentTime=performance.now()] - the current time of execution
  183. */
  184. update(currentTime?: number): void;
  185. /**
  186. * The frames per second at which this ticker is running.
  187. * The default is approximately 60 in most modern browsers.
  188. * **Note:** This does not factor in the value of
  189. * {@link PIXI.Ticker#speed}, which is specific
  190. * to scaling {@link PIXI.Ticker#deltaTime}.
  191. * @member {number}
  192. * @readonly
  193. */
  194. get FPS(): number;
  195. /**
  196. * Manages the maximum amount of milliseconds allowed to
  197. * elapse between invoking {@link PIXI.Ticker#update}.
  198. * This value is used to cap {@link PIXI.Ticker#deltaTime},
  199. * but does not effect the measured value of {@link PIXI.Ticker#FPS}.
  200. * When setting this property it is clamped to a value between
  201. * `0` and `PIXI.settings.TARGET_FPMS * 1000`.
  202. * @member {number}
  203. * @default 10
  204. */
  205. get minFPS(): number;
  206. set minFPS(fps: number);
  207. /**
  208. * Manages the minimum amount of milliseconds required to
  209. * elapse between invoking {@link PIXI.Ticker#update}.
  210. * This will effect the measured value of {@link PIXI.Ticker#FPS}.
  211. * If it is set to `0`, then there is no limit; PixiJS will render as many frames as it can.
  212. * Otherwise it will be at least `minFPS`
  213. * @member {number}
  214. * @default 0
  215. */
  216. get maxFPS(): number;
  217. set maxFPS(fps: number);
  218. /**
  219. * The shared ticker instance used by {@link PIXI.AnimatedSprite} and by
  220. * {@link PIXI.VideoResource} to update animation frames / video textures.
  221. *
  222. * It may also be used by {@link PIXI.Application} if created with the `sharedTicker` option property set to true.
  223. *
  224. * The property {@link PIXI.Ticker#autoStart} is set to `true` for this instance.
  225. * Please follow the examples for usage, including how to opt-out of auto-starting the shared ticker.
  226. * @example
  227. * let ticker = PIXI.Ticker.shared;
  228. * // Set this to prevent starting this ticker when listeners are added.
  229. * // By default this is true only for the PIXI.Ticker.shared instance.
  230. * ticker.autoStart = false;
  231. * // FYI, call this to ensure the ticker is stopped. It should be stopped
  232. * // if you have not attempted to render anything yet.
  233. * ticker.stop();
  234. * // Call this when you are ready for a running shared ticker.
  235. * ticker.start();
  236. * @example
  237. * // You may use the shared ticker to render...
  238. * let renderer = PIXI.autoDetectRenderer();
  239. * let stage = new PIXI.Container();
  240. * document.body.appendChild(renderer.view);
  241. * ticker.add(function (time) {
  242. * renderer.render(stage);
  243. * });
  244. * @example
  245. * // Or you can just update it manually.
  246. * ticker.autoStart = false;
  247. * ticker.stop();
  248. * function animate(time) {
  249. * ticker.update(time);
  250. * renderer.render(stage);
  251. * requestAnimationFrame(animate);
  252. * }
  253. * animate(performance.now());
  254. * @member {PIXI.Ticker}
  255. * @static
  256. */
  257. static get shared(): Ticker;
  258. /**
  259. * The system ticker instance used by {@link PIXI.InteractionManager} and by
  260. * {@link PIXI.BasePrepare} for core timing functionality that shouldn't usually need to be paused,
  261. * unlike the `shared` ticker which drives visual animations and rendering which may want to be paused.
  262. *
  263. * The property {@link PIXI.Ticker#autoStart} is set to `true` for this instance.
  264. * @member {PIXI.Ticker}
  265. * @static
  266. */
  267. static get system(): Ticker;
  268. }
  269. export declare type TickerCallback<T> = (this: T, dt: number) => any;
  270. /**
  271. * Middleware for for Application Ticker.
  272. * @example
  273. * import {TickerPlugin} from '@pixi/ticker';
  274. * import {Application} from '@pixi/app';
  275. * import {extensions} from '@pixi/extensions';
  276. * extensions.add(TickerPlugin);
  277. * @class
  278. * @memberof PIXI
  279. */
  280. export declare class TickerPlugin {
  281. /** @ignore */
  282. static extension: ExtensionMetadata;
  283. static start: () => void;
  284. static stop: () => void;
  285. static _ticker: Ticker;
  286. static ticker: Ticker;
  287. /**
  288. * Initialize the plugin with scope of application instance
  289. * @static
  290. * @private
  291. * @param {object} [options] - See application options
  292. */
  293. static init(options?: GlobalMixins.IApplicationOptions): void;
  294. /**
  295. * Clean up the ticker, scoped to application.
  296. * @static
  297. * @private
  298. */
  299. static destroy(): void;
  300. }
  301. /**
  302. * Represents the update priorities used by internal PIXI classes when registered with
  303. * the {@link PIXI.Ticker} object. Higher priority items are updated first and lower
  304. * priority items, such as render, should go later.
  305. * @static
  306. * @constant
  307. * @name UPDATE_PRIORITY
  308. * @memberof PIXI
  309. * @enum {number}
  310. * @property {number} [INTERACTION=50] Highest priority, used for {@link PIXI.InteractionManager}
  311. * @property {number} [HIGH=25] High priority updating, {@link PIXI.VideoBaseTexture} and {@link PIXI.AnimatedSprite}
  312. * @property {number} [NORMAL=0] Default priority for ticker events, see {@link PIXI.Ticker#add}.
  313. * @property {number} [LOW=-25] Low priority used for {@link PIXI.Application} rendering.
  314. * @property {number} [UTILITY=-50] Lowest priority used for {@link PIXI.BasePrepare} utility.
  315. */
  316. export declare enum UPDATE_PRIORITY {
  317. INTERACTION = 50,
  318. HIGH = 25,
  319. NORMAL = 0,
  320. LOW = -25,
  321. UTILITY = -50
  322. }
  323. export { }