index.d.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /// <reference path="./global.d.ts" />
  2. import { BaseTexture } from '@pixi/core';
  3. import type { Dict } from '@pixi/utils';
  4. import type { ExtensionMetadata } from '@pixi/core';
  5. import type { IPointData } from '@pixi/math';
  6. import { LoaderResource } from '@pixi/loaders';
  7. import { Texture } from '@pixi/core';
  8. /** Atlas format. */
  9. export declare interface ISpritesheetData {
  10. frames: Dict<ISpritesheetFrameData>;
  11. animations?: Dict<string[]>;
  12. meta: {
  13. scale: string;
  14. related_multi_packs?: string[];
  15. };
  16. }
  17. /** Represents the JSON data for a spritesheet atlas. */
  18. export declare interface ISpritesheetFrameData {
  19. frame: {
  20. x: number;
  21. y: number;
  22. w: number;
  23. h: number;
  24. };
  25. trimmed?: boolean;
  26. rotated?: boolean;
  27. sourceSize?: {
  28. w: number;
  29. h: number;
  30. };
  31. spriteSourceSize?: {
  32. x: number;
  33. y: number;
  34. };
  35. anchor?: IPointData;
  36. }
  37. /**
  38. * Utility class for maintaining reference to a collection
  39. * of Textures on a single Spritesheet.
  40. *
  41. * To access a sprite sheet from your code you may pass its JSON data file to Pixi's loader:
  42. *
  43. * ```js
  44. * PIXI.Loader.shared.add("images/spritesheet.json").load(setup);
  45. *
  46. * function setup() {
  47. * let sheet = PIXI.Loader.shared.resources["images/spritesheet.json"].spritesheet;
  48. * ...
  49. * }
  50. * ```
  51. *
  52. * Alternately, you may circumvent the loader by instantiating the Spritesheet directly:
  53. * ```js
  54. * const sheet = new PIXI.Spritesheet(texture, spritesheetData);
  55. * await sheet.parse();
  56. * console.log('Spritesheet ready to use!');
  57. * ```
  58. *
  59. * With the `sheet.textures` you can create Sprite objects,`sheet.animations` can be used to create an AnimatedSprite.
  60. *
  61. * Sprite sheets can be packed using tools like {@link https://codeandweb.com/texturepacker|TexturePacker},
  62. * {@link https://renderhjs.net/shoebox/|Shoebox} or {@link https://github.com/krzysztof-o/spritesheet.js|Spritesheet.js}.
  63. * Default anchor points (see {@link PIXI.Texture#defaultAnchor}) and grouping of animation sprites are currently only
  64. * supported by TexturePacker.
  65. * @memberof PIXI
  66. */
  67. export declare class Spritesheet {
  68. /** The maximum number of Textures to build per process. */
  69. static readonly BATCH_SIZE = 1000;
  70. /** For multi-packed spritesheets, this contains a reference to all the other spritesheets it depends on. */
  71. linkedSheets: Spritesheet[];
  72. /** Reference to ths source texture. */
  73. baseTexture: BaseTexture;
  74. /**
  75. * A map containing all textures of the sprite sheet.
  76. * Can be used to create a {@link PIXI.Sprite|Sprite}:
  77. * ```js
  78. * new PIXI.Sprite(sheet.textures["image.png"]);
  79. * ```
  80. */
  81. textures: Dict<Texture>;
  82. /**
  83. * A map containing the textures for each animation.
  84. * Can be used to create an {@link PIXI.AnimatedSprite|AnimatedSprite}:
  85. * ```js
  86. * new PIXI.AnimatedSprite(sheet.animations["anim_name"])
  87. * ```
  88. */
  89. animations: Dict<Texture[]>;
  90. /**
  91. * Reference to the original JSON data.
  92. * @type {object}
  93. */
  94. data: ISpritesheetData;
  95. /** The resolution of the spritesheet. */
  96. resolution: number;
  97. /**
  98. * Reference to original source image from the Loader. This reference is retained so we
  99. * can destroy the Texture later on. It is never used internally.
  100. */
  101. private _texture;
  102. /**
  103. * Map of spritesheet frames.
  104. * @type {object}
  105. */
  106. private _frames;
  107. /** Collection of frame names. */
  108. private _frameKeys;
  109. /** Current batch index being processed. */
  110. private _batchIndex;
  111. /**
  112. * Callback when parse is completed.
  113. * @type {Function}
  114. */
  115. private _callback;
  116. /**
  117. * @param texture - Reference to the source BaseTexture object.
  118. * @param {object} data - Spritesheet image data.
  119. * @param resolutionFilename - The filename to consider when determining
  120. * the resolution of the spritesheet. If not provided, the imageUrl will
  121. * be used on the BaseTexture.
  122. */
  123. constructor(texture: BaseTexture | Texture, data: ISpritesheetData, resolutionFilename?: string);
  124. /**
  125. * Generate the resolution from the filename or fallback
  126. * to the meta.scale field of the JSON data.
  127. * @param resolutionFilename - The filename to use for resolving
  128. * the default resolution.
  129. * @returns Resolution to use for spritesheet.
  130. */
  131. private _updateResolution;
  132. /**
  133. * Parser spritesheet from loaded data. This is done asynchronously
  134. * to prevent creating too many Texture within a single process.
  135. * @method PIXI.Spritesheet#parse
  136. */
  137. parse(): Promise<Dict<Texture>>;
  138. /**
  139. * Please use the Promise-based version of this function.
  140. * @method PIXI.Spritesheet#parse
  141. * @deprecated since version 6.5.0
  142. * @param {Function} callback - Callback when complete returns
  143. * a map of the Textures for this spritesheet.
  144. */
  145. parse(callback?: (textures?: Dict<Texture>) => void): void;
  146. /**
  147. * Process a batch of frames
  148. * @param initialFrameIndex - The index of frame to start.
  149. */
  150. private _processFrames;
  151. /** Parse animations config. */
  152. private _processAnimations;
  153. /** The parse has completed. */
  154. private _parseComplete;
  155. /** Begin the next batch of textures. */
  156. private _nextBatch;
  157. /**
  158. * Destroy Spritesheet and don't use after this.
  159. * @param {boolean} [destroyBase=false] - Whether to destroy the base texture as well
  160. */
  161. destroy(destroyBase?: boolean): void;
  162. }
  163. /**
  164. * {@link PIXI.Loader} middleware for loading texture atlases that have been created with
  165. * TexturePacker or similar JSON-based spritesheet.
  166. *
  167. * This middleware automatically generates Texture resources.
  168. *
  169. * If you're using Webpack or other bundlers and plan on bundling the atlas' JSON,
  170. * use the {@link PIXI.Spritesheet} class to directly parse the JSON.
  171. *
  172. * The Loader's image Resource name is automatically appended with `"_image"`.
  173. * If a Resource with this name is already loaded, the Loader will skip parsing the
  174. * Spritesheet. The code below will generate an internal Loader Resource called `"myatlas_image"`.
  175. * @example
  176. * loader.add('myatlas', 'path/to/myatlas.json');
  177. * loader.load(() => {
  178. * loader.resources.myatlas; // atlas JSON resource
  179. * loader.resources.myatlas_image; // atlas Image resource
  180. * });
  181. * @memberof PIXI
  182. */
  183. export declare class SpritesheetLoader {
  184. /** @ignore */
  185. static extension: ExtensionMetadata;
  186. /**
  187. * Called after a resource is loaded.
  188. * @see PIXI.Loader.loaderMiddleware
  189. * @param resource
  190. * @param next
  191. */
  192. static use(resource: LoaderResource, next: (...args: unknown[]) => void): void;
  193. /**
  194. * Get the spritesheets root path
  195. * @param resource - Resource to check path
  196. * @param baseUrl - Base root url
  197. */
  198. static getResourcePath(resource: LoaderResource, baseUrl: string): string;
  199. }
  200. export { }