UNPKG

12.3 kBTypeScriptView Raw
1import { Point, Rectangle } from '@pixi/math';
2import { EventEmitter } from '@pixi/utils';
3import { BaseTexture } from './BaseTexture';
4import { TextureUvs } from './TextureUvs';
5import type { IPointData } from '@pixi/math';
6import type { IBaseTextureOptions, ImageSource } from './BaseTexture';
7import type { BufferResource, BufferType, IBufferResourceOptions } from './resources/BufferResource';
8import type { CanvasResource } from './resources/CanvasResource';
9import type { Resource } from './resources/Resource';
10import type { TextureMatrix } from './TextureMatrix';
11export type TextureSource = string | BaseTexture | ImageSource;
12/**
13 * Stores the width of the non-scalable borders, for example when used with {@link PIXI.NineSlicePlane} texture.
14 * @memberof PIXI
15 * @since 7.2.0
16 */
17export interface ITextureBorders {
18 /** left border in pixels */
19 left: number;
20 /** top border in pixels */
21 top: number;
22 /** right border in pixels */
23 right: number;
24 /** bottom border in pixels */
25 bottom: number;
26}
27export interface Texture extends GlobalMixins.Texture, EventEmitter {
28}
29/**
30 * A texture stores the information that represents an image or part of an image.
31 *
32 * It cannot be added to the display list directly; instead use it as the texture for a Sprite.
33 * If no frame is provided for a texture, then the whole image is used.
34 *
35 * You can directly create a texture from an image and then reuse it multiple times like this :
36 *
37 * ```js
38 * import { Sprite, Texture } from 'pixi.js';
39 *
40 * const texture = Texture.from('assets/image.png');
41 * const sprite1 = new Sprite(texture);
42 * const sprite2 = new Sprite(texture);
43 * ```
44 *
45 * If you didnt pass the texture frame to constructor, it enables `noFrame` mode:
46 * it subscribes on baseTexture events, it automatically resizes at the same time as baseTexture.
47 *
48 * Textures made from SVGs, loaded or not, cannot be used before the file finishes processing.
49 * You can check for this by checking the sprite's _textureID property.
50 *
51 * ```js
52 * import { Sprite, Texture } from 'pixi.js';
53 *
54 * const texture = Texture.from('assets/image.svg');
55 * const sprite1 = new Sprite(texture);
56 * // sprite1._textureID should not be undefined if the texture has finished processing the SVG file
57 * ```
58 *
59 * You can use a ticker or rAF to ensure your sprites load the finished textures after processing.
60 * See issue [#3085]{@link https://github.com/pixijs/pixijs/issues/3085}.
61 * @memberof PIXI
62 * @typeParam R - The BaseTexture's Resource type.
63 */
64export declare class Texture<R extends Resource = Resource> extends EventEmitter {
65 /** The base texture that this texture uses. */
66 baseTexture: BaseTexture<R>;
67 /** This is the area of original texture, before it was put in atlas. */
68 orig: Rectangle;
69 /**
70 * This is the trimmed area of original texture, before it was put in atlas
71 * Please call `updateUvs()` after you change coordinates of `trim` manually.
72 */
73 trim: Rectangle;
74 /** This will let the renderer know if the texture is valid. If it's not then it cannot be rendered. */
75 valid: boolean;
76 /**
77 * Has the texture been destroyed?
78 * @readonly
79 */
80 destroyed: boolean;
81 /**
82 * Does this Texture have any frame data assigned to it?
83 *
84 * This mode is enabled automatically if no frame was passed inside constructor.
85 *
86 * In this mode texture is subscribed to baseTexture events, and fires `update` on any change.
87 *
88 * Beware, after loading or resize of baseTexture event can fired two times!
89 * If you want more control, subscribe on baseTexture itself.
90 *
91 * Any assignment of `frame` switches off `noFrame` mode.
92 * @example
93 * texture.on('update', () => {});
94 */
95 noFrame: boolean;
96 /**
97 * Anchor point that is used as default if sprite is created with this texture.
98 * Changing the `defaultAnchor` at a later point of time will not update Sprite's anchor point.
99 * @default {0,0}
100 */
101 defaultAnchor: Point;
102 /**
103 * Default width of the non-scalable border that is used if 9-slice plane is created with this texture.
104 * @since 7.2.0
105 * @see PIXI.NineSlicePlane
106 */
107 defaultBorders?: ITextureBorders;
108 /** Default TextureMatrix instance for this texture. By default, that object is not created because its heavy. */
109 uvMatrix: TextureMatrix;
110 protected _rotate: number;
111 /**
112 * Update ID is observed by sprites and TextureMatrix instances.
113 * Call updateUvs() to increment it.
114 * @protected
115 */
116 _updateID: number;
117 /**
118 * This is the area of the BaseTexture image to actually copy to the Canvas / WebGL when rendering,
119 * irrespective of the actual frame size or placement (which can be influenced by trimmed texture atlases)
120 */
121 _frame: Rectangle;
122 /**
123 * The WebGL UV data cache. Can be used as quad UV.
124 * @protected
125 */
126 _uvs: TextureUvs;
127 /**
128 * The ids under which this Texture has been added to the texture cache. This is
129 * automatically set as long as Texture.addToCache is used, but may not be set if a
130 * Texture is added directly to the TextureCache array.
131 */
132 textureCacheIds: Array<string>;
133 /**
134 * @param baseTexture - The base texture source to create the texture from
135 * @param frame - The rectangle frame of the texture to show
136 * @param orig - The area of original texture
137 * @param trim - Trimmed rectangle of original texture
138 * @param rotate - indicates how the texture was rotated by texture packer. See {@link PIXI.groupD8}
139 * @param anchor - Default anchor point used for sprite placement / rotation
140 * @param borders - Default borders used for 9-slice scaling. See {@link PIXI.NineSlicePlane}
141 */
142 constructor(baseTexture: BaseTexture<R>, frame?: Rectangle, orig?: Rectangle, trim?: Rectangle, rotate?: number, anchor?: IPointData, borders?: ITextureBorders);
143 /**
144 * Updates this texture on the gpu.
145 *
146 * Calls the TextureResource update.
147 *
148 * If you adjusted `frame` manually, please call `updateUvs()` instead.
149 */
150 update(): void;
151 /**
152 * Called when the base texture is updated
153 * @protected
154 * @param baseTexture - The base texture.
155 */
156 onBaseTextureUpdated(baseTexture: BaseTexture): void;
157 /**
158 * Destroys this texture
159 * @param [destroyBase=false] - Whether to destroy the base texture as well
160 * @fires PIXI.Texture#destroyed
161 */
162 destroy(destroyBase?: boolean): void;
163 /**
164 * Creates a new texture object that acts the same as this one.
165 * @returns - The new texture
166 */
167 clone(): Texture;
168 /**
169 * Updates the internal WebGL UV cache. Use it after you change `frame` or `trim` of the texture.
170 * Call it after changing the frame
171 */
172 updateUvs(): void;
173 /**
174 * Helper function that creates a new Texture based on the source you provide.
175 * The source can be - frame id, image url, video url, canvas element, video element, base texture
176 * @param {string|PIXI.BaseTexture|HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas} source -
177 * Source or array of sources to create texture from
178 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
179 * @param {string} [options.pixiIdPrefix=pixiid] - If a source has no id, this is the prefix of the generated id
180 * @param {boolean} [strict] - Enforce strict-mode, see {@link PIXI.settings.STRICT_TEXTURE_CACHE}.
181 * @returns {PIXI.Texture} The newly created texture
182 */
183 static from<R extends Resource = Resource, RO = any>(source: TextureSource | TextureSource[], options?: IBaseTextureOptions<RO>, strict?: boolean): Texture<R>;
184 /**
185 * Useful for loading textures via URLs. Use instead of `Texture.from` because
186 * it does a better job of handling failed URLs more effectively. This also ignores
187 * `PIXI.settings.STRICT_TEXTURE_CACHE`. Works for Videos, SVGs, Images.
188 * @param url - The remote URL or array of URLs to load.
189 * @param options - Optional options to include
190 * @returns - A Promise that resolves to a Texture.
191 */
192 static fromURL<R extends Resource = Resource, RO = any>(url: string | string[], options?: IBaseTextureOptions<RO>): Promise<Texture<R>>;
193 /**
194 * Create a new Texture with a BufferResource from a typed array.
195 * @param buffer - The optional array to use. If no data is provided, a new Float32Array is created.
196 * @param width - Width of the resource
197 * @param height - Height of the resource
198 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
199 * Default properties are different from the constructor's defaults.
200 * @param {PIXI.FORMATS} [options.format] - The format is not given, the type is inferred from the
201 * type of the buffer: `RGBA` if Float32Array, Int8Array, Uint8Array, or Uint8ClampedArray,
202 * otherwise `RGBA_INTEGER`.
203 * @param {PIXI.TYPES} [options.type] - The type is not given, the type is inferred from the
204 * type of the buffer. Maps Float32Array to `FLOAT`, Int32Array to `INT`, Uint32Array to
205 * `UNSIGNED_INT`, Int16Array to `SHORT`, Uint16Array to `UNSIGNED_SHORT`, Int8Array to `BYTE`,
206 * Uint8Array/Uint8ClampedArray to `UNSIGNED_BYTE`.
207 * @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.NPM]
208 * @param {PIXI.SCALE_MODES} [options.scaleMode=PIXI.SCALE_MODES.NEAREST]
209 * @returns - The resulting new BaseTexture
210 */
211 static fromBuffer(buffer: BufferType, width: number, height: number, options?: IBaseTextureOptions<IBufferResourceOptions>): Texture<BufferResource>;
212 /**
213 * Create a texture from a source and add to the cache.
214 * @param {HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas|string} source - The input source.
215 * @param imageUrl - File name of texture, for cache and resolving resolution.
216 * @param name - Human readable name for the texture cache. If no name is
217 * specified, only `imageUrl` will be used as the cache ID.
218 * @param options
219 * @returns - Output texture
220 */
221 static fromLoader<R extends Resource = Resource>(source: ImageSource | string, imageUrl: string, name?: string, options?: IBaseTextureOptions): Promise<Texture<R>>;
222 /**
223 * Adds a Texture to the global TextureCache. This cache is shared across the whole PIXI object.
224 * @param texture - The Texture to add to the cache.
225 * @param id - The id that the Texture will be stored against.
226 */
227 static addToCache(texture: Texture, id: string): void;
228 /**
229 * Remove a Texture from the global TextureCache.
230 * @param texture - id of a Texture to be removed, or a Texture instance itself
231 * @returns - The Texture that was removed
232 */
233 static removeFromCache(texture: string | Texture): Texture | null;
234 /**
235 * Returns resolution of baseTexture
236 * @readonly
237 */
238 get resolution(): number;
239 /**
240 * The frame specifies the region of the base texture that this texture uses.
241 * Please call `updateUvs()` after you change coordinates of `frame` manually.
242 */
243 get frame(): Rectangle;
244 set frame(frame: Rectangle);
245 /**
246 * Indicates whether the texture is rotated inside the atlas
247 * set to 2 to compensate for texture packer rotation
248 * set to 6 to compensate for spine packer rotation
249 * can be used to rotate or mirror sprites
250 * See {@link PIXI.groupD8} for explanation
251 */
252 get rotate(): number;
253 set rotate(rotate: number);
254 /** The width of the Texture in pixels. */
255 get width(): number;
256 /** The height of the Texture in pixels. */
257 get height(): number;
258 /** Utility function for BaseTexture|Texture cast. */
259 castToBaseTexture(): BaseTexture;
260 private static _EMPTY;
261 private static _WHITE;
262 /** An empty texture, used often to not have to create multiple empty textures. Can not be destroyed. */
263 static get EMPTY(): Texture<Resource>;
264 /** A white texture of 16x16 size, used for graphics and other things Can not be destroyed. */
265 static get WHITE(): Texture<CanvasResource>;
266}