Core PixiJS WebGL rendering library providing essential 2D graphics infrastructure and hardware-accelerated rendering capabilities.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Comprehensive texture management system supporting multiple resource types, texture atlases, GPU texture optimization, and various image formats. The texture system handles all aspects of GPU texture management including uploading, caching, and resource management.
Main texture class that represents a region of a BaseTexture, providing UV coordinates, frames, and transformation data for rendering.
/**
* Texture represents a region of a BaseTexture for rendering
* Handles UV coordinates, frames, and texture transformations
*/
class Texture<R = Resource> extends EventEmitter {
/** Empty texture instance for fallback rendering */
static EMPTY: Texture;
/** White 1x1 texture for solid color rendering */
static WHITE: Texture;
/** The base texture containing the image data */
baseTexture: BaseTexture<R>;
/** Original texture rectangle before any transformations */
orig: Rectangle;
/** Trimmed rectangle if texture was trimmed */
trim: Rectangle;
/** Current frame rectangle defining the texture region */
frame: Rectangle;
/** Whether texture is valid and ready for rendering */
valid: boolean;
/** Whether this texture has no frame (uses full BaseTexture) */
noFrame: boolean;
/** Default anchor point for sprites using this texture */
defaultAnchor: Point;
/** UV transformation matrix */
uvMatrix: TextureMatrix;
/** Rotation of texture in increments of π/2 */
rotate: number;
/**
* Create a new Texture
* @param baseTexture - Base texture containing image data
* @param frame - Rectangle frame within the base texture
* @param orig - Original rectangle before trimming
* @param trim - Trimmed rectangle
* @param rotate - Rotation in increments of π/2
* @param anchor - Default anchor point
*/
constructor(
baseTexture: BaseTexture,
frame?: Rectangle,
orig?: Rectangle,
trim?: Rectangle,
rotate?: number,
anchor?: IPointData
);
/** Update UV coordinates based on current frame */
updateUvs(): void;
/** Create a copy of this texture */
clone(): Texture;
/** Update texture when BaseTexture changes */
update(): void;
/**
* Destroy texture and optionally its BaseTexture
* @param destroyBase - Also destroy the BaseTexture
*/
destroy(destroyBase?: boolean): void;
/**
* Create texture from various sources
* @param source - Image source (URL, ImageElement, Canvas, etc.)
* @param options - BaseTexture creation options
* @param strict - Strict mode for error handling
*/
static from(source: TextureSource, options?: IBaseTextureOptions, strict?: boolean): Texture;
/**
* Create texture from URL with promise-based loading
* @param url - Image URL to load
* @param options - BaseTexture options
*/
static fromURL(url: string, options?: IBaseTextureOptions): Promise<Texture>;
/**
* Create texture from typed array buffer
* @param buffer - Pixel data buffer
* @param width - Texture width
* @param height - Texture height
* @param options - BaseTexture options
*/
static fromBuffer(
buffer: Float32Array | Uint8Array,
width: number,
height: number,
options?: IBaseTextureOptions
): Texture;
/**
* Create texture from loaded image element
* @param source - HTML image or canvas element
* @param imageUrl - Original image URL for caching
* @param name - Cache name
* @param options - BaseTexture options
*/
static fromLoader(
source: HTMLImageElement | HTMLCanvasElement,
imageUrl: string,
name?: string,
options?: IBaseTextureOptions
): Promise<Texture>;
/**
* Add texture to global cache
* @param texture - Texture to cache
* @param id - Cache identifier
*/
static addToCache(texture: Texture, id: string): void;
/**
* Remove texture from global cache
* @param texture - Texture or cache ID to remove
*/
static removeFromCache(texture: string | Texture): Texture | null;
}
type TextureSource = string | BaseTexture | ImageSource;
type ImageSource = HTMLImageElement | HTMLVideoElement | ImageBitmap | HTMLCanvasElement;Base texture class that manages the actual GPU texture resource, handles uploading pixel data to the GPU and manages texture parameters.
/**
* BaseTexture manages the GPU texture resource and pixel data
* Handles texture uploading, caching, and GPU resource management
*/
class BaseTexture<R = Resource> extends EventEmitter {
/** Texture width in pixels */
width: number;
/** Texture height in pixels */
height: number;
/** Texture resolution/density */
resolution: number;
/** Alpha handling mode */
alphaMode?: ALPHA_MODES;
/** Anisotropic filtering level (1-16) */
anisotropicLevel?: number;
/** Pixel format (RGBA, RGB, etc.) */
format?: FORMATS;
/** Data type (UNSIGNED_BYTE, FLOAT, etc.) */
type?: TYPES;
/** WebGL texture target (TEXTURE_2D, TEXTURE_CUBE_MAP, etc.) */
target?: TARGETS;
/** Texture wrapping mode for UV coordinates */
wrapMode: WRAP_MODES;
/** Scaling mode for minification/magnification */
scaleMode: SCALE_MODES;
/** Mipmap generation mode */
mipmap: MIPMAP_MODES;
/** The underlying resource containing image data */
resource: R;
/** Whether texture is valid and uploaded to GPU */
valid: boolean;
/** Whether texture has been destroyed */
destroyed: boolean;
/** Texture cache ID */
cacheId: string;
/** Whether texture was loaded from cache */
isPowerOfTwo: boolean;
/** Internal WebGL texture cache */
_glTextures: {[key: number]: GLTexture};
/**
* Create a new BaseTexture
* @param resource - Image resource or source
* @param options - Texture creation options
*/
constructor(resource?: R | ImageSource | string, options?: IBaseTextureOptions<RO>);
/** Update texture when resource changes */
update(): void;
/**
* Set texture scaling and mipmap modes
* @param scaleMode - Scaling mode for filtering
* @param mipmap - Mipmap generation mode
*/
setStyle(scaleMode?: SCALE_MODES, mipmap?: MIPMAP_MODES): this;
/**
* Set desired texture size and resolution
* @param desiredWidth - Target width
* @param desiredHeight - Target height
* @param resolution - Texture resolution
*/
setSize(desiredWidth: number, desiredHeight: number, resolution?: number): this;
/**
* Set real pixel size of texture
* @param realWidth - Actual pixel width
* @param realHeight - Actual pixel height
* @param resolution - Texture resolution
*/
setRealSize(realWidth: number, realHeight: number, resolution?: number): this;
/**
* Set the resource for this BaseTexture
* @param resource - New image resource
*/
setResource(resource: R): this;
/** Destroy texture and cleanup GPU resources */
destroy(): void;
/**
* Create BaseTexture from image source
* @param source - Image source
* @param options - Creation options
* @param strict - Strict error handling
*/
static from<R = Resource, RO = any>(
source: ImageSource | string,
options?: IBaseTextureOptions<RO>,
strict?: boolean
): BaseTexture<R>;
/**
* Create BaseTexture from canvas element
* @param canvas - HTML canvas element
* @param options - Creation options
*/
static fromCanvas(canvas: HTMLCanvasElement, options?: IBaseTextureOptions): BaseTexture<CanvasResource>;
/**
* Add BaseTexture to cache
* @param baseTexture - Texture to cache
* @param id - Cache identifier
*/
static addToCache(baseTexture: BaseTexture, id: string): void;
/**
* Remove BaseTexture from cache
* @param baseTexture - Texture or ID to remove
*/
static removeFromCache(baseTexture: string | BaseTexture): BaseTexture | null;
}
interface IBaseTextureOptions<RO = any> {
/** Alpha handling mode */
alphaMode?: ALPHA_MODES;
/** Mipmap generation mode */
mipmap?: MIPMAP_MODES;
/** Anisotropic filtering level */
anisotropicLevel?: number;
/** Scaling mode for filtering */
scaleMode?: SCALE_MODES;
/** Texture wrapping mode */
wrapMode?: WRAP_MODES;
/** Pixel format */
format?: FORMATS;
/** Data type */
type?: TYPES;
/** WebGL texture target */
target?: TARGETS;
/** Texture resolution */
resolution?: number;
/** Resource-specific options */
resourceOptions?: RO;
/** Texture width override */
width?: number;
/** Texture height override */
height?: number;
}UV transformation matrix for handling texture coordinate transformations, rotations, and mappings.
/**
* TextureMatrix handles UV coordinate transformations
* Manages texture coordinate mapping and transformations
*/
class TextureMatrix {
/** UV transformation matrix */
mapCoord: Matrix;
/** UV clamping matrix */
uClampFrame: Matrix;
/** UV wrapping offset matrix */
uClampOffset: Matrix;
/** Associated texture */
_texture: Texture;
/** Whether matrix needs updating */
_updateID: number;
/**
* Create a new TextureMatrix
* @param texture - Texture to create matrix for
* @param clampMargin - Clamping margin for UV coordinates
*/
constructor(texture: Texture, clampMargin?: number);
/**
* Update the matrix when texture changes
* @param forceUpdate - Force matrix recalculation
*/
update(forceUpdate?: boolean): boolean;
/**
* Multiply UV coordinates by this matrix
* @param uvs - UV coordinate array to transform
* @param out - Output array for transformed coordinates
*/
multiplyUvs(uvs: Float32Array, out?: Float32Array): Float32Array;
}Various resource types for different image sources and data formats.
/**
* Base resource class for texture data sources
*/
abstract class Resource extends EventEmitter {
/** Resource width */
width: number;
/** Resource height */
height: number;
/** Whether resource is valid */
valid: boolean;
/** Whether resource has been destroyed */
destroyed: boolean;
/** Internal format */
internal: boolean;
/**
* Create new resource
* @param width - Resource width
* @param height - Resource height
*/
constructor(width?: number, height?: number);
/** Upload resource to GPU texture */
upload(renderer: Renderer, baseTexture: BaseTexture, glTexture: GLTexture): boolean;
/** Update resource */
update(): void;
/** Dispose of resource */
dispose(): void;
/** Destroy resource */
destroy(): void;
}
/**
* Resource for HTML image elements
*/
class ImageResource extends Resource {
/** Source image element */
source: HTMLImageElement;
/** Image URL */
url: string;
constructor(source: HTMLImageElement | string, options?: IImageResourceOptions);
/** Load image from URL */
load(): Promise<ImageResource>;
}
/**
* Resource for HTML canvas elements
*/
class CanvasResource extends Resource {
/** Source canvas element */
source: HTMLCanvasElement;
constructor(source: HTMLCanvasElement);
}
/**
* Resource for HTML video elements
*/
class VideoResource extends Resource {
/** Source video element */
source: HTMLVideoElement;
/** Auto-play flag */
autoPlay: boolean;
/** Video update FPS */
updateFPS: number;
constructor(source: HTMLVideoElement | string, options?: IVideoResourceOptions);
}
/**
* Resource for ImageBitmap objects
*/
class ImageBitmapResource extends Resource {
/** Source ImageBitmap */
source: ImageBitmap;
constructor(source: ImageBitmap);
}
/**
* Resource for typed array buffers
*/
class BufferResource extends Resource {
/** Source data buffer */
data: Float32Array | Uint8Array | Uint32Array;
constructor(source: Float32Array | Uint8Array | Uint32Array, options: IBufferResourceOptions);
}Usage Examples:
import { Texture, BaseTexture, TextureMatrix } from "@pixi/core";
// Create texture from image URL
const texture = Texture.from('/path/to/image.png');
// Create texture from canvas
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 100, 100);
const canvasTexture = Texture.from(canvas);
// Create texture from buffer data
const buffer = new Uint8Array(64 * 64 * 4); // RGBA data
buffer.fill(255); // White texture
const bufferTexture = Texture.fromBuffer(buffer, 64, 64);
// Create texture with options
const texture = Texture.from('/path/to/image.png', {
scaleMode: SCALE_MODES.NEAREST,
wrapMode: WRAP_MODES.REPEAT,
mipmap: MIPMAP_MODES.ON
});
// Use texture frames for sprite sheets
const baseTexture = BaseTexture.from('/path/to/spritesheet.png');
const frameTexture = new Texture(
baseTexture,
new Rectangle(0, 0, 32, 32) // 32x32 frame at top-left
);
// Handle texture loading
Texture.fromURL('/path/to/image.png').then((texture) => {
console.log('Texture loaded:', texture.width, texture.height);
});
// Texture caching
Texture.addToCache(texture, 'my-texture');
const cachedTexture = Texture.from('my-texture');
// UV transformations
const uvMatrix = new TextureMatrix(texture);
const uvs = new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]);
const transformedUvs = uvMatrix.multiplyUvs(uvs);interface IImageResourceOptions {
/** Cross-origin mode */
crossorigin?: boolean | string;
/** Auto-load flag */
autoLoad?: boolean;
/** Create ImageBitmap flag */
createBitmap?: boolean;
}
interface IVideoResourceOptions {
/** Auto-update flag */
autoUpdate?: boolean;
/** Auto-play flag */
autoPlay?: boolean;
/** Update FPS rate */
updateFPS?: number;
/** Cross-origin mode */
crossorigin?: boolean | string;
}
interface IBufferResourceOptions {
/** Texture width */
width: number;
/** Texture height */
height: number;
/** Pixel format */
format?: FORMATS;
/** Data type */
type?: TYPES;
}
enum ALPHA_MODES {
NPM = 0,
UNPACK = 1,
PMA = 2,
NO_PREMULTIPLIED_ALPHA = 0,
PREMULTIPLY_ON_UPLOAD = 1,
PREMULTIPLIED_ALPHA = 2
}
enum SCALE_MODES {
NEAREST = 0,
LINEAR = 1
}
enum WRAP_MODES {
CLAMP = 0,
REPEAT = 1,
MIRRORED_REPEAT = 2
}
enum MIPMAP_MODES {
OFF = 0,
POW2 = 1,
ON = 2,
ON_MANUAL = 3
}
enum FORMATS {
RGBA = 6408,
RGB = 6407,
ALPHA = 6406,
LUMINANCE = 6409,
LUMINANCE_ALPHA = 6410,
DEPTH_COMPONENT = 6402,
DEPTH_STENCIL = 34041
}
enum TYPES {
UNSIGNED_BYTE = 5121,
UNSIGNED_SHORT = 5123,
UNSIGNED_SHORT_5_6_5 = 33635,
UNSIGNED_SHORT_4_4_4_4 = 32819,
UNSIGNED_SHORT_5_5_5_1 = 32820,
FLOAT = 5126,
HALF_FLOAT = 36193
}Install with Tessl CLI
npx tessl i tessl/npm-pixi--core