Texture2D
A GPU texture for the 2D engine — the image behind a Sprite or
Tilemap. The raw file bytes are decoded and uploaded natively. Not interchangeable
with the 3D Texture (material images).
At a glance
const tex = await Texture2D.load(asset('./hero.png'))
const hero = new Sprite({ texture: tex })
console.log(tex.width, tex.height) // pixel dimensionsLoading
Texture2D.load(source: string | FetchResponse | File): Promise<Texture2D>- URL string — an
asset('./file.png')path or a remotehttps://…URL; fetched, then decoded. Rejects on HTTP status ≥ 400 or a decode failure. FetchResponse— an already-fetched response (skip the extra request).File— e.g. fromCanvas.toFile()oropenFilePicker.
Texture2D.fromCanvas(canvas: Canvas): Texture2DWraps a Canvas's baked surface as a texture — synchronous (already rasterized, no decode). Its
width/height are the canvas's physical pixels (logical size × pixelRatio). Assigning a
Canvas directly to sprite.texture does this for you.
Properties & lifecycle
tex.width, tex.height // pixels (readonly)
tex.id // native texture handle (readonly)
tex.destroy(): void // free the GPU textureTextures are shared freely — many sprites can use one Texture2D. destroy() frees the GPU
resource; sprites still referencing it are undefined behavior.
Note
sampling (nearest vs linear) is not per-texture — it's a global renderer setting
chosen per scene via Scene2D's filter option.
See also
- Sprite — using a texture, frames/atlases
- Tilemap — atlas grids
- SpriteAnimation — sheet slicing into clips
- Scene2D — the global
filtersetting