UNPKG

11.4 kBTypeScriptView Raw
1import { Point, Rectangle } from '@pixi/math';
2import { EventEmitter } from '@pixi/utils';
3import { BaseTexture } from './BaseTexture';
4import { TextureUvs } from './TextureUvs';
5import type { IPointData, ISize } from '@pixi/math';
6import type { IBaseTextureOptions, ImageSource } from './BaseTexture';
7import type { BufferResource } from './resources/BufferResource';
8import type { CanvasResource } from './resources/CanvasResource';
9import type { Resource } from './resources/Resource';
10import type { TextureMatrix } from './TextureMatrix';
11export declare 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 * Does this Texture have any frame data assigned to it?
78 *
79 * This mode is enabled automatically if no frame was passed inside constructor.
80 *
81 * In this mode texture is subscribed to baseTexture events, and fires `update` on any change.
82 *
83 * Beware, after loading or resize of baseTexture event can fired two times!
84 * If you want more control, subscribe on baseTexture itself.
85 *
86 * Any assignment of `frame` switches off `noFrame` mode.
87 * @example
88 * texture.on('update', () => {});
89 */
90 noFrame: boolean;
91 /**
92 * Anchor point that is used as default if sprite is created with this texture.
93 * Changing the `defaultAnchor` at a later point of time will not update Sprite's anchor point.
94 * @default {0,0}
95 */
96 defaultAnchor: Point;
97 /**
98 * Default width of the non-scalable border that is used if 9-slice plane is created with this texture.
99 * @since 7.2.0
100 * @see PIXI.NineSlicePlane
101 */
102 defaultBorders?: ITextureBorders;
103 /** Default TextureMatrix instance for this texture. By default, that object is not created because its heavy. */
104 uvMatrix: TextureMatrix;
105 protected _rotate: number;
106 /**
107 * Update ID is observed by sprites and TextureMatrix instances.
108 * Call updateUvs() to increment it.
109 * @protected
110 */
111 _updateID: number;
112 /**
113 * This is the area of the BaseTexture image to actually copy to the Canvas / WebGL when rendering,
114 * irrespective of the actual frame size or placement (which can be influenced by trimmed texture atlases)
115 */
116 _frame: Rectangle;
117 /**
118 * The WebGL UV data cache. Can be used as quad UV.
119 * @protected
120 */
121 _uvs: TextureUvs;
122 /**
123 * The ids under which this Texture has been added to the texture cache. This is
124 * automatically set as long as Texture.addToCache is used, but may not be set if a
125 * Texture is added directly to the TextureCache array.
126 */
127 textureCacheIds: Array<string>;
128 /**
129 * @param baseTexture - The base texture source to create the texture from
130 * @param frame - The rectangle frame of the texture to show
131 * @param orig - The area of original texture
132 * @param trim - Trimmed rectangle of original texture
133 * @param rotate - indicates how the texture was rotated by texture packer. See {@link PIXI.groupD8}
134 * @param anchor - Default anchor point used for sprite placement / rotation
135 * @param borders - Default borders used for 9-slice scaling. See {@link PIXI.NineSlicePlane}
136 */
137 constructor(baseTexture: BaseTexture<R>, frame?: Rectangle, orig?: Rectangle, trim?: Rectangle, rotate?: number, anchor?: IPointData, borders?: ITextureBorders);
138 /**
139 * Updates this texture on the gpu.
140 *
141 * Calls the TextureResource update.
142 *
143 * If you adjusted `frame` manually, please call `updateUvs()` instead.
144 */
145 update(): void;
146 /**
147 * Called when the base texture is updated
148 * @protected
149 * @param baseTexture - The base texture.
150 */
151 onBaseTextureUpdated(baseTexture: BaseTexture): void;
152 /**
153 * Destroys this texture
154 * @param [destroyBase=false] - Whether to destroy the base texture as well
155 */
156 destroy(destroyBase?: boolean): void;
157 /**
158 * Creates a new texture object that acts the same as this one.
159 * @returns - The new texture
160 */
161 clone(): Texture;
162 /**
163 * Updates the internal WebGL UV cache. Use it after you change `frame` or `trim` of the texture.
164 * Call it after changing the frame
165 */
166 updateUvs(): void;
167 /**
168 * Helper function that creates a new Texture based on the source you provide.
169 * The source can be - frame id, image url, video url, canvas element, video element, base texture
170 * @param {string|PIXI.BaseTexture|HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas} source -
171 * Source or array of sources to create texture from
172 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
173 * @param {string} [options.pixiIdPrefix=pixiid] - If a source has no id, this is the prefix of the generated id
174 * @param {boolean} [strict] - Enforce strict-mode, see {@link PIXI.settings.STRICT_TEXTURE_CACHE}.
175 * @returns {PIXI.Texture} The newly created texture
176 */
177 static from<R extends Resource = Resource, RO = any>(source: TextureSource | TextureSource[], options?: IBaseTextureOptions<RO>, strict?: boolean): Texture<R>;
178 /**
179 * Useful for loading textures via URLs. Use instead of `Texture.from` because
180 * it does a better job of handling failed URLs more effectively. This also ignores
181 * `PIXI.settings.STRICT_TEXTURE_CACHE`. Works for Videos, SVGs, Images.
182 * @param url - The remote URL or array of URLs to load.
183 * @param options - Optional options to include
184 * @returns - A Promise that resolves to a Texture.
185 */
186 static fromURL<R extends Resource = Resource, RO = any>(url: string | string[], options?: IBaseTextureOptions<RO>): Promise<Texture<R>>;
187 /**
188 * Create a new Texture with a BufferResource from a Float32Array.
189 * RGBA values are floats from 0 to 1.
190 * @param {Float32Array|Uint8Array} buffer - The optional array to use, if no data
191 * is provided, a new Float32Array is created.
192 * @param width - Width of the resource
193 * @param height - Height of the resource
194 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
195 * @returns - The resulting new BaseTexture
196 */
197 static fromBuffer(buffer: Float32Array | Uint8Array, width: number, height: number, options?: IBaseTextureOptions<ISize>): Texture<BufferResource>;
198 /**
199 * Create a texture from a source and add to the cache.
200 * @param {HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas|string} source - The input source.
201 * @param imageUrl - File name of texture, for cache and resolving resolution.
202 * @param name - Human readable name for the texture cache. If no name is
203 * specified, only `imageUrl` will be used as the cache ID.
204 * @param options
205 * @returns - Output texture
206 */
207 static fromLoader<R extends Resource = Resource>(source: ImageSource | string, imageUrl: string, name?: string, options?: IBaseTextureOptions): Promise<Texture<R>>;
208 /**
209 * Adds a Texture to the global TextureCache. This cache is shared across the whole PIXI object.
210 * @param texture - The Texture to add to the cache.
211 * @param id - The id that the Texture will be stored against.
212 */
213 static addToCache(texture: Texture, id: string): void;
214 /**
215 * Remove a Texture from the global TextureCache.
216 * @param texture - id of a Texture to be removed, or a Texture instance itself
217 * @returns - The Texture that was removed
218 */
219 static removeFromCache(texture: string | Texture): Texture | null;
220 /**
221 * Returns resolution of baseTexture
222 * @readonly
223 */
224 get resolution(): number;
225 /**
226 * The frame specifies the region of the base texture that this texture uses.
227 * Please call `updateUvs()` after you change coordinates of `frame` manually.
228 */
229 get frame(): Rectangle;
230 set frame(frame: Rectangle);
231 /**
232 * Indicates whether the texture is rotated inside the atlas
233 * set to 2 to compensate for texture packer rotation
234 * set to 6 to compensate for spine packer rotation
235 * can be used to rotate or mirror sprites
236 * See {@link PIXI.groupD8} for explanation
237 */
238 get rotate(): number;
239 set rotate(rotate: number);
240 /** The width of the Texture in pixels. */
241 get width(): number;
242 /** The height of the Texture in pixels. */
243 get height(): number;
244 /** Utility function for BaseTexture|Texture cast. */
245 castToBaseTexture(): BaseTexture;
246 private static _EMPTY;
247 private static _WHITE;
248 /** An empty texture, used often to not have to create multiple empty textures. Can not be destroyed. */
249 static get EMPTY(): Texture<Resource>;
250 /** A white texture of 16x16 size, used for graphics and other things Can not be destroyed. */
251 static get WHITE(): Texture<CanvasResource>;
252}