index.d.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /// <reference path="./global.d.ts" />
  2. import type { Dict } from '@pixi/utils';
  3. import type { ExtensionMetadata } from '@pixi/core';
  4. import type { IBaseTextureOptions } from '@pixi/core';
  5. import type { Texture } from '@pixi/core';
  6. /**
  7. * Application plugin for supporting loader option. Installing the LoaderPlugin
  8. * is not necessary if using **pixi.js** or **pixi.js-legacy**.
  9. * @example
  10. * import {AppLoaderPlugin} from '@pixi/loaders';
  11. * import {extensions} from '@pixi/core';
  12. * extensions.add(AppLoaderPlugin);
  13. * @memberof PIXI
  14. */
  15. export declare class AppLoaderPlugin {
  16. /** @ignore */
  17. static extension: ExtensionMetadata;
  18. /**
  19. * Loader instance to help with asset loading.
  20. * @memberof PIXI.Application#
  21. * @readonly
  22. */
  23. static loader: Loader;
  24. /**
  25. * Called on application constructor
  26. * @param options
  27. * @private
  28. */
  29. static init(options?: GlobalMixins.IApplicationOptions): void;
  30. /**
  31. * Called when application destroyed
  32. * @private
  33. */
  34. static destroy(): void;
  35. }
  36. /**
  37. * Options for a call to `.add()`.
  38. * @see Loader#add
  39. * @property {string} name - The name of the resource to load, if not passed the url is used.
  40. * @property {string} key - Alias for `name`.
  41. * @property {string} url - The url for this resource, relative to the baseUrl of this loader.
  42. * @property {string|boolean} crossOrigin - Is this request cross-origin? Default is to determine automatically.
  43. * @property {number} [timeout=0] - A timeout in milliseconds for the load. If the load takes longer
  44. * than this time it is cancelled and the load is considered a failure. If this value is
  45. * set to `0` then there is no explicit timeout.
  46. * @property {LoaderResource.LOAD_TYPE} [loadType=LoaderResource.LOAD_TYPE.XHR] - How should this resource be loaded?
  47. * @property {LoaderResource.XHR_RESPONSE_TYPE} [xhrType=LoaderResource.XHR_RESPONSE_TYPE.DEFAULT] - How should the data
  48. * being loaded be interpreted when using XHR?
  49. * @property {LoaderResource.OnCompleteSignal} onComplete - Callback to add an an onComplete signal istener.
  50. * @property {LoaderResource.OnCompleteSignal} callback - Alias for `onComplete`.
  51. * @property {IResourceMetadata} metadata - Extra configuration for middleware and the Resource object.
  52. */
  53. export declare interface IAddOptions {
  54. name?: string;
  55. key?: string;
  56. url?: string;
  57. crossOrigin?: string | boolean;
  58. timeout?: number;
  59. parentResource?: LoaderResource;
  60. loadType?: LoaderResource.LOAD_TYPE;
  61. xhrType?: LoaderResource.XHR_RESPONSE_TYPE;
  62. onComplete?: LoaderResource.OnCompleteSignal;
  63. callback?: LoaderResource.OnCompleteSignal;
  64. metadata?: IResourceMetadata;
  65. }
  66. export declare interface ILoaderAdd {
  67. (this: Loader, name: string, url: string, callback?: LoaderResource.OnCompleteSignal): Loader;
  68. (this: Loader, name: string, url: string, options?: IAddOptions, callback?: LoaderResource.OnCompleteSignal): Loader;
  69. (this: Loader, url: string, callback?: LoaderResource.OnCompleteSignal): Loader;
  70. (this: Loader, url: string, options?: IAddOptions, callback?: LoaderResource.OnCompleteSignal): Loader;
  71. (this: Loader, options: IAddOptions, callback?: LoaderResource.OnCompleteSignal): Loader;
  72. (this: Loader, resources: (IAddOptions | string)[], callback?: LoaderResource.OnCompleteSignal): Loader;
  73. }
  74. export declare type ILoaderMiddleware = (resource: LoaderResource, next: (...args: any[]) => void) => void;
  75. /**
  76. * Plugin to be installed for handling specific Loader resources.
  77. * @property {Function} add - Function to call immediate after registering plugin.
  78. * @property {Function} pre - Middleware function to run before load, the
  79. * arguments for this are `(resource, next)`
  80. * @property {Function} use - Middleware function to run after load, the
  81. * arguments for this are `(resource, next)`
  82. */
  83. export declare interface ILoaderPlugin {
  84. /** Function to call immediate after registering plugin. */
  85. add?(): void;
  86. /**
  87. * Middleware function to run before load
  88. * @param {LoaderResource} resource - resource
  89. * @param {LoaderResource} next - next middleware
  90. */
  91. pre?(resource: LoaderResource, next: (...args: any[]) => void): void;
  92. /**
  93. * Middleware function to run after load
  94. * @param {LoaderResource} resource - resource
  95. * @param {LoaderResource} next - next middleware
  96. */
  97. use?(resource: LoaderResource, next: (...args: any[]) => void): void;
  98. }
  99. /** @deprecated - Use LoaderResource instead */
  100. export declare type ILoaderResource = LoaderResource;
  101. /**
  102. * Metadata for loader resource. It is very messy way to pass options for loader middlewares
  103. *
  104. * Can be extended in `GlobalMixins.IResourceMetadata`
  105. * @memberof PIXI
  106. */
  107. export declare interface IResourceMetadata extends GlobalMixins.IResourceMetadata, IBaseTextureOptions {
  108. /** The element to use for loading, instead of creating one. */
  109. loadElement?: HTMLImageElement | HTMLAudioElement | HTMLVideoElement;
  110. /**
  111. * Skips adding source(s) to the load element. This
  112. * is useful if you want to pass in a `loadElement` that you already added load sources to.
  113. */
  114. skipSource?: boolean;
  115. /**
  116. * The mime type to use for the source element
  117. * of a video/audio elment. If the urls are an array, you can pass this as an array as well
  118. * where each index is the mime type to use for the corresponding url index.
  119. */
  120. mimeType?: string | string[];
  121. /**
  122. * Used by BitmapFonts, Spritesheet and CompressedTextures as the options to used for
  123. * metadata when loading the child image.
  124. */
  125. imageMetadata?: IResourceMetadata;
  126. }
  127. /**
  128. * The new loader, forked from Resource Loader by Chad Engler: https://github.com/englercj/resource-loader
  129. *
  130. * ```js
  131. * const loader = PIXI.Loader.shared; // PixiJS exposes a premade instance for you to use.
  132. * // or
  133. * const loader = new PIXI.Loader(); // You can also create your own if you want
  134. *
  135. * const sprites = {};
  136. *
  137. * // Chainable `add` to enqueue a resource
  138. * loader.add('bunny', 'data/bunny.png')
  139. * .add('spaceship', 'assets/spritesheet.json');
  140. * loader.add('scoreFont', 'assets/score.fnt');
  141. *
  142. * // Chainable `pre` to add a middleware that runs for each resource, *before* loading that resource.
  143. * // This is useful to implement custom caching modules (using filesystem, indexeddb, memory, etc).
  144. * loader.pre(cachingMiddleware);
  145. *
  146. * // Chainable `use` to add a middleware that runs for each resource, *after* loading that resource.
  147. * // This is useful to implement custom parsing modules (like spritesheet parsers, spine parser, etc).
  148. * loader.use(parsingMiddleware);
  149. *
  150. * // The `load` method loads the queue of resources, and calls the passed in callback called once all
  151. * // resources have loaded.
  152. * loader.load((loader, resources) => {
  153. * // resources is an object where the key is the name of the resource loaded and the value is the resource object.
  154. * // They have a couple default properties:
  155. * // - `url`: The URL that the resource was loaded from
  156. * // - `error`: The error that happened when trying to load (if any)
  157. * // - `data`: The raw data that was loaded
  158. * // also may contain other properties based on the middleware that runs.
  159. * sprites.bunny = new PIXI.TilingSprite(resources.bunny.texture);
  160. * sprites.spaceship = new PIXI.TilingSprite(resources.spaceship.texture);
  161. * sprites.scoreFont = new PIXI.TilingSprite(resources.scoreFont.texture);
  162. * });
  163. *
  164. * // throughout the process multiple signals can be dispatched.
  165. * loader.onProgress.add(() => {}); // called once per loaded/errored file
  166. * loader.onError.add(() => {}); // called once per errored file
  167. * loader.onLoad.add(() => {}); // called once per loaded file
  168. * loader.onComplete.add(() => {}); // called once when the queued resources all load.
  169. * ```
  170. * @memberof PIXI
  171. */
  172. export declare class Loader {
  173. /** The base url for all resources loaded by this loader. */
  174. baseUrl: string;
  175. /** The progress percent of the loader going through the queue. */
  176. progress: number;
  177. /** Loading state of the loader, true if it is currently loading resources. */
  178. loading: boolean;
  179. /**
  180. * A querystring to append to every URL added to the loader.
  181. *
  182. * This should be a valid query string *without* the question-mark (`?`). The loader will
  183. * also *not* escape values for you. Make sure to escape your parameters with
  184. * [`encodeURIComponent`](https://mdn.io/encodeURIComponent) before assigning this property.
  185. * @example
  186. * const loader = new Loader();
  187. *
  188. * loader.defaultQueryString = 'user=me&password=secret';
  189. *
  190. * // This will request 'image.png?user=me&password=secret'
  191. * loader.add('image.png').load();
  192. *
  193. * loader.reset();
  194. *
  195. * // This will request 'image.png?v=1&user=me&password=secret'
  196. * loader.add('iamge.png?v=1').load();
  197. */
  198. defaultQueryString: string;
  199. /** The middleware to run before loading each resource. */
  200. private _beforeMiddleware;
  201. /** The middleware to run after loading each resource. */
  202. private _afterMiddleware;
  203. /** The tracks the resources we are currently completing parsing for. */
  204. private _resourcesParsing;
  205. /**
  206. * The `_loadResource` function bound with this object context.
  207. * @param r - The resource to load
  208. * @param d - The dequeue function
  209. */
  210. private _boundLoadResource;
  211. /** The resources waiting to be loaded. */
  212. private _queue;
  213. /** All the resources for this loader keyed by name. */
  214. resources: Dict<LoaderResource>;
  215. /** Dispatched once per loaded or errored resource. */
  216. onProgress: Signal<Loader.OnProgressSignal>;
  217. /** Dispatched once per errored resource. */
  218. onError: Signal<Loader.OnErrorSignal>;
  219. /** Dispatched once per loaded resource. */
  220. onLoad: Signal<Loader.OnLoadSignal>;
  221. /** Dispatched when the loader begins to process the queue. */
  222. onStart: Signal<Loader.OnStartSignal>;
  223. /** Dispatched when the queued resources all load. */
  224. onComplete: Signal<Loader.OnCompleteSignal>;
  225. /**
  226. * @param baseUrl - The base url for all resources loaded by this loader.
  227. * @param concurrency - The number of resources to load concurrently.
  228. */
  229. constructor(baseUrl?: string, concurrency?: number);
  230. /**
  231. * Adds a resource (or multiple resources) to the loader queue.
  232. *
  233. * This function can take a wide variety of different parameters. The only thing that is always
  234. * required the url to load. All the following will work:
  235. *
  236. * ```js
  237. * loader
  238. * // normal param syntax
  239. * .add('key', 'http://...', function () {})
  240. * .add('http://...', function () {})
  241. * .add('http://...')
  242. *
  243. * // object syntax
  244. * .add({
  245. * name: 'key2',
  246. * url: 'http://...'
  247. * }, function () {})
  248. * .add({
  249. * url: 'http://...'
  250. * }, function () {})
  251. * .add({
  252. * name: 'key3',
  253. * url: 'http://...'
  254. * onComplete: function () {}
  255. * })
  256. * .add({
  257. * url: 'https://...',
  258. * onComplete: function () {},
  259. * crossOrigin: true
  260. * })
  261. *
  262. * // you can also pass an array of objects or urls or both
  263. * .add([
  264. * { name: 'key4', url: 'http://...', onComplete: function () {} },
  265. * { url: 'http://...', onComplete: function () {} },
  266. * 'http://...'
  267. * ])
  268. *
  269. * // and you can use both params and options
  270. * .add('key', 'http://...', { crossOrigin: true }, function () {})
  271. * .add('http://...', { crossOrigin: true }, function () {});
  272. * ```
  273. */
  274. add: ILoaderAdd;
  275. /**
  276. * Same as add, params have strict order
  277. * @private
  278. * @param name - The name of the resource to load.
  279. * @param url - The url for this resource, relative to the baseUrl of this loader.
  280. * @param options - The options for the load.
  281. * @param callback - Function to call when this specific resource completes loading.
  282. * @returns The loader itself.
  283. */
  284. protected _add(name: string, url: string, options: IAddOptions, callback?: LoaderResource.OnCompleteSignal): this;
  285. /**
  286. * Sets up a middleware function that will run *before* the
  287. * resource is loaded.
  288. * @param fn - The middleware function to register.
  289. * @returns The loader itself.
  290. */
  291. pre(fn: ILoaderMiddleware): this;
  292. /**
  293. * Sets up a middleware function that will run *after* the
  294. * resource is loaded.
  295. * @param fn - The middleware function to register.
  296. * @returns The loader itself.
  297. */
  298. use(fn: ILoaderMiddleware): this;
  299. /**
  300. * Resets the queue of the loader to prepare for a new load.
  301. * @returns The loader itself.
  302. */
  303. reset(): this;
  304. /**
  305. * Starts loading the queued resources.
  306. * @param cb - Optional callback that will be bound to the `complete` event.
  307. * @returns The loader itself.
  308. */
  309. load(cb?: Loader.OnCompleteSignal): this;
  310. /**
  311. * The number of resources to load concurrently.
  312. * @default 10
  313. */
  314. get concurrency(): number;
  315. set concurrency(concurrency: number);
  316. /**
  317. * Prepares a url for usage based on the configuration of this object
  318. * @param url - The url to prepare.
  319. * @returns The prepared url.
  320. */
  321. private _prepareUrl;
  322. /**
  323. * Loads a single resource.
  324. * @param resource - The resource to load.
  325. * @param dequeue - The function to call when we need to dequeue this item.
  326. */
  327. private _loadResource;
  328. /** Called once loading has started. */
  329. private _onStart;
  330. /** Called once each resource has loaded. */
  331. private _onComplete;
  332. /**
  333. * Called each time a resources is loaded.
  334. * @param resource - The resource that was loaded
  335. */
  336. private _onLoad;
  337. static _plugins: Array<ILoaderPlugin>;
  338. private static _shared;
  339. /**
  340. * If this loader cannot be destroyed.
  341. * @default false
  342. */
  343. private _protected;
  344. /** Destroy the loader, removes references. */
  345. destroy(): void;
  346. /** A premade instance of the loader that can be used to load resources. */
  347. static get shared(): Loader;
  348. /**
  349. * Use the {@link PIXI.extensions.add} API to register plugins.
  350. * @deprecated since 6.5.0
  351. * @param plugin - The plugin to add
  352. * @returns Reference to PIXI.Loader for chaining
  353. */
  354. static registerPlugin(plugin: ILoaderPlugin): typeof Loader;
  355. }
  356. export declare namespace Loader {
  357. /**
  358. * When the resource starts to load.
  359. * @param resource - The resource that the event happened on.
  360. */
  361. export type OnStartSignal = (loader: Loader) => void;
  362. /**
  363. * When the progress changes the loader and resource are dispatched.
  364. * @param loader - The loader the progress is advancing on.
  365. * @param resource - The resource that has completed or failed to cause the progress to advance.
  366. */
  367. export type OnProgressSignal = (loader: Loader, resource: LoaderResource) => void;
  368. /**
  369. * When a load completes without error the loader and resource are dispatched.
  370. * @param loader - The loader that has started loading resources.
  371. * @param resource - The resource that has completed.
  372. */
  373. export type OnLoadSignal = (loader: Loader, resource: LoaderResource) => void;
  374. /**
  375. * When the loader starts loading resources it dispatches this callback.
  376. * @param loader - The loader that has started loading resources.
  377. */
  378. export type OnCompleteSignal = (loader: Loader, resources: Dict<LoaderResource>) => void;
  379. /**
  380. * When an error occurs the loader and resource are dispatched.
  381. * @param loader - The loader the error happened in.
  382. * @param resource - The resource that caused the error.
  383. */
  384. export type OnErrorSignal = (error: Error, loader: Loader, resource: LoaderResource) => void;
  385. }
  386. export declare interface LoaderResource extends GlobalMixins.LoaderResource, GlobalMixins.ILoaderResource {
  387. }
  388. /**
  389. * Manages the state and loading of a resource and all child resources.
  390. *
  391. * Can be extended in `GlobalMixins.LoaderResource`.
  392. * @memberof PIXI
  393. */
  394. export declare class LoaderResource {
  395. /**
  396. * Texture reference for loading images and other textures.
  397. * @type {PIXI.Texture}
  398. */
  399. texture?: Texture;
  400. /** used by parsing middleware */
  401. blob?: Blob;
  402. /**
  403. * The name of this resource.
  404. * @readonly
  405. * @type {string}
  406. */
  407. readonly name: string;
  408. /**
  409. * The url used to load this resource.
  410. * @readonly
  411. * @type {string}
  412. */
  413. readonly url: string;
  414. /**
  415. * The extension used to load this resource.
  416. * @readonly
  417. * @type {string}
  418. */
  419. readonly extension: string;
  420. /** The data that was loaded by the resource. */
  421. data: any;
  422. /** Is this request cross-origin? If unset, determined automatically. */
  423. crossOrigin: string | boolean;
  424. /**
  425. * A timeout in milliseconds for the load. If the load takes longer than this time
  426. * it is cancelled and the load is considered a failure. If this value is set to `0`
  427. * then there is no explicit timeout.
  428. * @type {number}
  429. */
  430. timeout: number;
  431. /**
  432. * The method of loading to use for this resource.
  433. * @type {PIXI.LoaderResource.LOAD_TYPE}
  434. */
  435. loadType: LoaderResource.LOAD_TYPE;
  436. /**
  437. * The type used to load the resource via XHR. If unset, determined automatically.
  438. * @member {string}
  439. */
  440. xhrType: string;
  441. /**
  442. * Extra info for middleware, and controlling specifics about how the resource loads.
  443. *
  444. * Note that if you pass in a `loadElement`, the Resource class takes ownership of it.
  445. * Meaning it will modify it as it sees fit.
  446. * @type {PIXI.IResourceMetadata}
  447. */
  448. metadata: IResourceMetadata;
  449. /**
  450. * The error that occurred while loading (if any).
  451. * @readonly
  452. * @member {Error}
  453. */
  454. error: Error;
  455. /**
  456. * The XHR object that was used to load this resource. This is only set
  457. * when `loadType` is `LoaderResource.LOAD_TYPE.XHR`.
  458. * @readonly
  459. */
  460. xhr: XMLHttpRequest;
  461. private xdr;
  462. /**
  463. * The child resources this resource owns.
  464. * @type {PIXI.LoaderResource[]}
  465. */
  466. readonly children: LoaderResource[];
  467. /**
  468. * The resource type.
  469. * @readonly
  470. * @type {PIXI.LoaderResource.TYPE}
  471. */
  472. type: LoaderResource.TYPE;
  473. /**
  474. * The progress chunk owned by this resource.
  475. * @readonly
  476. * @member {number}
  477. */
  478. progressChunk: number;
  479. /**
  480. * Dispatched when the resource beings to load.
  481. *
  482. * The callback looks like {@link LoaderResource.OnStartSignal}.
  483. * @type {PIXI.Signal}
  484. */
  485. onStart: Signal<LoaderResource.OnStartSignal>;
  486. /**
  487. * Dispatched each time progress of this resource load updates.
  488. * Not all resources types and loader systems can support this event
  489. * so sometimes it may not be available. If the resource
  490. * is being loaded on a modern browser, using XHR, and the remote server
  491. * properly sets Content-Length headers, then this will be available.
  492. *
  493. * The callback looks like {@link LoaderResource.OnProgressSignal}.
  494. * @type {PIXI.Signal}
  495. */
  496. onProgress: Signal<LoaderResource.OnProgressSignal>;
  497. /**
  498. * Dispatched once this resource has loaded, if there was an error it will
  499. * be in the `error` property.
  500. *
  501. * The callback looks like {@link LoaderResource.OnCompleteSignal}.
  502. * @type {PIXI.Signal}
  503. */
  504. onComplete: Signal<LoaderResource.OnCompleteSignal>;
  505. /**
  506. * Dispatched after this resource has had all the *after* middleware run on it.
  507. *
  508. * The callback looks like {@link LoaderResource.OnCompleteSignal}.
  509. * @type {PIXI.Signal}
  510. */
  511. onAfterMiddleware: Signal<LoaderResource.OnCompleteSignal>;
  512. /**
  513. * The state flags of this resource.
  514. * @private
  515. * @member {number}
  516. */
  517. private _flags;
  518. /**
  519. * The `dequeue` method that will be used a storage place for the async queue dequeue method
  520. * used privately by the loader.
  521. * @private
  522. * @member {Function}
  523. */
  524. _dequeue: any;
  525. /**
  526. * Used a storage place for the on load binding used privately by the loader.
  527. * @private
  528. * @member {Function}
  529. */
  530. _onLoadBinding: any;
  531. /**
  532. * The timer for element loads to check if they timeout.
  533. * @private
  534. */
  535. private _elementTimer;
  536. /**
  537. * The `complete` function bound to this resource's context.
  538. * @private
  539. * @type {Function}
  540. */
  541. private _boundComplete;
  542. /**
  543. * The `_onError` function bound to this resource's context.
  544. * @private
  545. * @type {Function}
  546. */
  547. private _boundOnError;
  548. /**
  549. * The `_onProgress` function bound to this resource's context.
  550. * @private
  551. * @type {Function}
  552. */
  553. private _boundOnProgress;
  554. /**
  555. * The `_onTimeout` function bound to this resource's context.
  556. * @private
  557. * @type {Function}
  558. */
  559. private _boundOnTimeout;
  560. private _boundXhrOnError;
  561. private _boundXhrOnTimeout;
  562. private _boundXhrOnAbort;
  563. private _boundXhrOnLoad;
  564. /**
  565. * Sets the load type to be used for a specific extension.
  566. * @static
  567. * @param {string} extname - The extension to set the type for, e.g. "png" or "fnt"
  568. * @param {PIXI.LoaderResource.LOAD_TYPE} loadType - The load type to set it to.
  569. */
  570. static setExtensionLoadType(extname: string, loadType: LoaderResource.LOAD_TYPE): void;
  571. /**
  572. * Sets the load type to be used for a specific extension.
  573. * @static
  574. * @param {string} extname - The extension to set the type for, e.g. "png" or "fnt"
  575. * @param {PIXI.LoaderResource.XHR_RESPONSE_TYPE} xhrType - The xhr type to set it to.
  576. */
  577. static setExtensionXhrType(extname: string, xhrType: LoaderResource.XHR_RESPONSE_TYPE): void;
  578. /**
  579. * @param {string} name - The name of the resource to load.
  580. * @param {string|string[]} url - The url for this resource, for audio/video loads you can pass
  581. * an array of sources.
  582. * @param {object} [options] - The options for the load.
  583. * @param {string|boolean} [options.crossOrigin] - Is this request cross-origin? Default is to
  584. * determine automatically.
  585. * @param {number} [options.timeout=0] - A timeout in milliseconds for the load. If the load takes
  586. * longer than this time it is cancelled and the load is considered a failure. If this value is
  587. * set to `0` then there is no explicit timeout.
  588. * @param {PIXI.LoaderResource.LOAD_TYPE} [options.loadType=LOAD_TYPE.XHR] - How should this resource
  589. * be loaded?
  590. * @param {PIXI.LoaderResource.XHR_RESPONSE_TYPE} [options.xhrType=XHR_RESPONSE_TYPE.DEFAULT] - How
  591. * should the data being loaded be interpreted when using XHR?
  592. * @param {PIXI.LoaderResource.IMetadata} [options.metadata] - Extra configuration for middleware
  593. * and the Resource object.
  594. */
  595. constructor(name: string, url: string | string[], options?: {
  596. crossOrigin?: string | boolean;
  597. timeout?: number;
  598. loadType?: LoaderResource.LOAD_TYPE;
  599. xhrType?: LoaderResource.XHR_RESPONSE_TYPE;
  600. metadata?: IResourceMetadata;
  601. });
  602. /**
  603. * When the resource starts to load.
  604. * @memberof PIXI.LoaderResource
  605. * @callback OnStartSignal@callback OnStartSignal
  606. * @param {PIXI.Resource} resource - The resource that the event happened on.
  607. */
  608. /**
  609. * When the resource reports loading progress.
  610. * @memberof PIXI.LoaderResource
  611. * @callback OnProgressSignal@callback OnProgressSignal
  612. * @param {PIXI.Resource} resource - The resource that the event happened on.
  613. * @param {number} percentage - The progress of the load in the range [0, 1].
  614. */
  615. /**
  616. * When the resource finishes loading.
  617. * @memberof PIXI.LoaderResource
  618. * @callback OnCompleteSignal@callback OnCompleteSignal
  619. * @param {PIXI.Resource} resource - The resource that the event happened on.
  620. */
  621. /**
  622. * @memberof PIXI.LoaderResource
  623. * @typedef {object} IMetadata@typedef {object} IMetadata
  624. * @property {HTMLImageElement|HTMLAudioElement|HTMLVideoElement} [loadElement=null] - The
  625. * element to use for loading, instead of creating one.
  626. * @property {boolean} [skipSource=false] - Skips adding source(s) to the load element. This
  627. * is useful if you want to pass in a `loadElement` that you already added load sources to.
  628. * @property {string|string[]} [mimeType] - The mime type to use for the source element
  629. * of a video/audio elment. If the urls are an array, you can pass this as an array as well
  630. * where each index is the mime type to use for the corresponding url index.
  631. */
  632. /**
  633. * Stores whether or not this url is a data url.
  634. * @readonly
  635. * @member {boolean}
  636. */
  637. get isDataUrl(): boolean;
  638. /**
  639. * Describes if this resource has finished loading. Is true when the resource has completely
  640. * loaded.
  641. * @readonly
  642. * @member {boolean}
  643. */
  644. get isComplete(): boolean;
  645. /**
  646. * Describes if this resource is currently loading. Is true when the resource starts loading,
  647. * and is false again when complete.
  648. * @readonly
  649. * @member {boolean}
  650. */
  651. get isLoading(): boolean;
  652. /** Marks the resource as complete. */
  653. complete(): void;
  654. /**
  655. * Aborts the loading of this resource, with an optional message.
  656. * @param {string} message - The message to use for the error
  657. */
  658. abort(message: string): void;
  659. /**
  660. * Kicks off loading of this resource. This method is asynchronous.
  661. * @param {PIXI.LoaderResource.OnCompleteSignal} [cb] - Optional callback to call once the resource is loaded.
  662. */
  663. load(cb?: LoaderResource.OnCompleteSignal): void;
  664. /**
  665. * Checks if the flag is set.
  666. * @param flag - The flag to check.
  667. * @returns True if the flag is set.
  668. */
  669. private _hasFlag;
  670. /**
  671. * (Un)Sets the flag.
  672. * @param flag - The flag to (un)set.
  673. * @param value - Whether to set or (un)set the flag.
  674. */
  675. private _setFlag;
  676. /** Clears all the events from the underlying loading source. */
  677. private _clearEvents;
  678. /** Finalizes the load. */
  679. private _finish;
  680. /**
  681. * Loads this resources using an element that has a single source,
  682. * like an HTMLImageElement.
  683. * @private
  684. * @param type - The type of element to use.
  685. */
  686. _loadElement(type: string): void;
  687. /**
  688. * Loads this resources using an element that has multiple sources,
  689. * like an HTMLAudioElement or HTMLVideoElement.
  690. * @param type - The type of element to use.
  691. */
  692. private _loadSourceElement;
  693. /** Loads this resources using an XMLHttpRequest. */
  694. private _loadXhr;
  695. /** Loads this resources using an XDomainRequest. This is here because we need to support IE9 (gross). */
  696. private _loadXdr;
  697. /**
  698. * Creates a source used in loading via an element.
  699. * @param type - The element type (video or audio).
  700. * @param url - The source URL to load from.
  701. * @param [mime] - The mime type of the video
  702. * @returns The source element.
  703. */
  704. private _createSource;
  705. /**
  706. * Called if a load errors out.
  707. * @param event - The error event from the element that emits it.
  708. */
  709. private _onError;
  710. /**
  711. * Called if a load progress event fires for an element or xhr/xdr.
  712. * @param event - Progress event.
  713. */
  714. private _onProgress;
  715. /** Called if a timeout event fires for an element. */
  716. private _onTimeout;
  717. /** Called if an error event fires for xhr/xdr. */
  718. private _xhrOnError;
  719. /** Called if an error event fires for xhr/xdr. */
  720. private _xhrOnTimeout;
  721. /** Called if an abort event fires for xhr/xdr. */
  722. private _xhrOnAbort;
  723. /** Called when data successfully loads from an xhr/xdr request. */
  724. private _xhrOnLoad;
  725. /**
  726. * Sets the `crossOrigin` property for this resource based on if the url
  727. * for this resource is cross-origin. If crossOrigin was manually set, this
  728. * function does nothing.
  729. * @private
  730. * @param url - The url to test.
  731. * @param [loc=globalThis.location] - The location object to test against.
  732. * @returns The crossOrigin value to use (or empty string for none).
  733. */
  734. _determineCrossOrigin(url: string, loc?: any): string;
  735. /**
  736. * Determines the responseType of an XHR request based on the extension of the
  737. * resource being loaded.
  738. * @private
  739. * @returns {PIXI.LoaderResource.XHR_RESPONSE_TYPE} The responseType to use.
  740. */
  741. private _determineXhrType;
  742. /**
  743. * Determines the loadType of a resource based on the extension of the
  744. * resource being loaded.
  745. * @private
  746. * @returns {PIXI.LoaderResource.LOAD_TYPE} The loadType to use.
  747. */
  748. private _determineLoadType;
  749. /**
  750. * Extracts the extension (sans '.') of the file being loaded by the resource.
  751. * @param [url] - url to parse, `this.url` by default.
  752. * @returns The extension.
  753. */
  754. private _getExtension;
  755. /**
  756. * Determines the mime type of an XHR request based on the responseType of
  757. * resource being loaded.
  758. * @param type - The type to get a mime type for.
  759. * @private
  760. * @returns The mime type to use.
  761. */
  762. _getMimeFromXhrType(type: LoaderResource.XHR_RESPONSE_TYPE): string;
  763. }
  764. export declare namespace LoaderResource {
  765. /**
  766. * When the resource starts to load.
  767. * @memberof PIXI.LoaderResource
  768. * @callback OnStartSignal@callback OnStartSignal
  769. * @param {PIXI.Resource} resource - The resource that the event happened on.
  770. */
  771. export type OnStartSignal = (resource: LoaderResource) => void;
  772. /**
  773. * When the resource reports loading progress.
  774. * @memberof PIXI.LoaderResource
  775. * @callback OnProgressSignal@callback OnProgressSignal
  776. * @param {PIXI.Resource} resource - The resource that the event happened on.
  777. * @param {number} percentage - The progress of the load in the range [0, 1].
  778. */
  779. export type OnProgressSignal = (resource: LoaderResource, percentage: number) => void;
  780. /**
  781. * When the resource finishes loading.
  782. * @memberof PIXI.LoaderResource
  783. * @callback OnCompleteSignal@callback OnCompleteSignal
  784. * @param {PIXI.Resource} resource - The resource that the event happened on.
  785. */
  786. export type OnCompleteSignal = (resource: LoaderResource) => void;
  787. /**
  788. * The types of resources a resource could represent.
  789. * @static
  790. * @readonly
  791. * @enum {number}
  792. * @memberof PIXI.LoaderResource
  793. */
  794. export enum STATUS_FLAGS {
  795. /** None */
  796. NONE = 0,
  797. /** Data URL */
  798. DATA_URL = 1,
  799. /** Complete */
  800. COMPLETE = 2,
  801. /** Loading */
  802. LOADING = 4
  803. }
  804. /**
  805. * The types of resources a resource could represent.
  806. * @static
  807. * @readonly
  808. * @enum {number}
  809. * @memberof PIXI.LoaderResource
  810. */
  811. export enum TYPE {
  812. /** Unknown */
  813. UNKNOWN = 0,
  814. /** JSON */
  815. JSON = 1,
  816. /** XML */
  817. XML = 2,
  818. /** Image */
  819. IMAGE = 3,
  820. /** Audio */
  821. AUDIO = 4,
  822. /** Video */
  823. VIDEO = 5,
  824. /** Plain text */
  825. TEXT = 6
  826. }
  827. /**
  828. * The types of loading a resource can use.
  829. * @static
  830. * @readonly
  831. * @enum {number}
  832. * @memberof PIXI.LoaderResource
  833. */
  834. export enum LOAD_TYPE {
  835. /** Uses XMLHttpRequest to load the resource. */
  836. XHR = 1,
  837. /** Uses an `Image` object to load the resource. */
  838. IMAGE = 2,
  839. /** Uses an `Audio` object to load the resource. */
  840. AUDIO = 3,
  841. /** Uses a `Video` object to load the resource. */
  842. VIDEO = 4
  843. }
  844. /**
  845. * The XHR ready states, used internally.
  846. * @static
  847. * @readonly
  848. * @enum {string}
  849. * @memberof PIXI.LoaderResource
  850. */
  851. export enum XHR_RESPONSE_TYPE {
  852. /** string */
  853. DEFAULT = "text",
  854. /** ArrayBuffer */
  855. BUFFER = "arraybuffer",
  856. /** Blob */
  857. BLOB = "blob",
  858. /** Document */
  859. DOCUMENT = "document",
  860. /** Object */
  861. JSON = "json",
  862. /** String */
  863. TEXT = "text"
  864. }
  865. const _loadTypeMap: Dict<number>;
  866. const _xhrTypeMap: Dict<XHR_RESPONSE_TYPE>;
  867. const EMPTY_GIF = "data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
  868. }
  869. /**
  870. * @memberof PIXI
  871. */
  872. declare class Signal<CbType = (...args: any) => void> {
  873. _head: SignalBinding<CbType>;
  874. _tail: SignalBinding<CbType>;
  875. /**
  876. * MiniSignal constructor.
  877. * @example
  878. * let mySignal = new Signal();
  879. * let binding = mySignal.add(onSignal);
  880. * mySignal.dispatch('foo', 'bar');
  881. * mySignal.detach(binding);
  882. */
  883. constructor();
  884. /**
  885. * Return an array of attached SignalBinding.
  886. * @param {boolean} [exists=false] - We only need to know if there are handlers.
  887. * @returns {PIXI.SignalBinding[] | boolean} Array of attached SignalBinding or Boolean if called with exists = true
  888. * @api public
  889. */
  890. handlers(exists?: boolean): Array<SignalBinding<CbType>> | boolean;
  891. /**
  892. * Return true if node is a SignalBinding attached to this MiniSignal
  893. * @param {PIXI.SignalBinding} node - Node to check.
  894. * @returns {boolean} True if node is attache to mini-signal
  895. */
  896. has(node: SignalBinding<CbType>): boolean;
  897. /**
  898. * Dispaches a signal to all registered listeners.
  899. * @param {...any} args
  900. * @returns {boolean} Indication if we've emitted an event.
  901. */
  902. dispatch(...args: any[]): boolean;
  903. /**
  904. * Register a new listener.
  905. * @param {Function} fn - Callback function.
  906. * @param {object} [thisArg] - The context of the callback function.
  907. * @returns {PIXI.SignalBinding} The SignalBinding node that was added.
  908. */
  909. add(fn: CbType, thisArg?: any): SignalBinding<CbType>;
  910. /**
  911. * Register a new listener that will be executed only once.
  912. * @param {Function} fn - Callback function.
  913. * @param {object} [thisArg] - The context of the callback function.
  914. * @returns {PIXI.SignalBinding} The SignalBinding node that was added.
  915. */
  916. once(fn: CbType, thisArg?: any): SignalBinding<CbType>;
  917. /**
  918. * Remove binding object.
  919. * @param {PIXI.SignalBinding} node - The binding node that will be removed.
  920. * @returns {Signal} The instance on which this method was called.
  921. @api public */
  922. detach(node: SignalBinding<CbType>): this;
  923. /**
  924. * Detach all listeners.
  925. * @returns {Signal} The instance on which this method was called.
  926. */
  927. detachAll(): this;
  928. }
  929. /**
  930. * @memberof PIXI
  931. */
  932. declare class SignalBinding<CbType> {
  933. _fn: any;
  934. _once: boolean;
  935. _next: SignalBinding<CbType>;
  936. _prev: SignalBinding<CbType>;
  937. _owner: Signal<CbType>;
  938. _thisArg: any;
  939. /**
  940. * SignalBinding constructor.
  941. * @constructs SignalBinding
  942. * @param {Function} fn - Event handler to be called.
  943. * @param {boolean} [once=false] - Should this listener be removed after dispatch
  944. * @param {object} [thisArg] - The context of the callback function.
  945. * @api private
  946. */
  947. constructor(fn: CbType, once: boolean, thisArg: any);
  948. detach(): boolean;
  949. }
  950. /**
  951. * Loader plugin for handling Texture resources.
  952. * @memberof PIXI
  953. */
  954. export declare class TextureLoader {
  955. /** @ignore */
  956. static extension: ExtensionMetadata;
  957. /** Handle SVG elements a text, render with SVGResource. */
  958. static add(): void;
  959. /**
  960. * Called after a resource is loaded.
  961. * @see PIXI.Loader.loaderMiddleware
  962. * @param resource
  963. * @param {Function} next
  964. */
  965. static use(resource: LoaderResource, next: (...args: any[]) => void): void;
  966. }
  967. export { }