Skip to content

Texture and TextureRegion ​

Images on the GPU and rectangles inside them.

ts
import { Texture, TextureRegion } from "willow";

Texture ​

ts
class Texture

Texture handle backed by a GPU resource. Textures can be loaded from a path or encoded bytes, or created from RGBA pixels. Direct new Texture(...) raises a TypeError.

Texture: properties ​

MemberDescription
readonly height: intTexture height in pixels.
readonly width: intTexture width in pixels.

Texture: methods ​

MemberDescription
dispose(): voidReleases the GPU resource. Idempotent and silent, like every other dispose().
update(rgba: Buffer | Uint8Array): voidReplaces all RGBA pixels of an updatable texture.

Texture: static methods ​

MemberDescription
static load(path: string, opts?: any): TextureLoads a texture from path (resolved via the engine asset roots). Pass { cache: false } to bypass the process-wide path cache.
static fromBuffer(buffer: Buffer): TextureLoads a PNG or JPEG texture from encoded bytes.
static fromPixels(width: int, height: int, rgba: Buffer | Uint8Array, updatable?: boolean): TextureCreates a texture from width * height * 4 RGBA bytes. Requires an initialized renderer, so it is unavailable in a headless console program. Set updatable to true when the pixels will later be replaced.

TextureRegion ​

ts
class TextureRegion

A rectangular sub-region of a texture with its source rectangle and pre-computed UVs. Direct new TextureRegion(...) raises a TypeError.

TextureRegion: properties ​

MemberDescription
readonly x: floatSource x within the texture, in pixels.
readonly y: floatSource y within the texture, in pixels.
readonly width: floatRegion width in pixels.
readonly height: floatRegion height in pixels.

TextureRegion: static methods ​

MemberDescription
static grid(tileset: Texture, tileWidth: int, tileHeight: int, opts?: any): Array<TextureRegion>Slices a regular grid tileset into an ordered Array<TextureRegion> (row-major, element 0 = the first cell = GID 1). opts accepts { columns?, count?, margin?, spacing? }; columns/count are derived from the texture size when omitted.
static fromRect(texture: Texture, x: float, y: float, width: float, height: float): TextureRegionCreates an axis-aligned pixel rectangle within texture.