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

TypeScript
Texture2D.load(asset('./hero.png')).then(tex => {
  const hero = new Sprite({ texture: tex })
  console.log(tex.width, tex.height)      // pixel dimensions
})

Loading

TypeScript
Texture2D.load(source: string | FetchResponse | File): Promise<Texture2D>
  • URL string — an asset('./file.png') path or a remote https://… 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. from Canvas.toFile() or openFilePicker.
TypeScript
Texture2D.fromCanvas(canvas: Canvas): Texture2D
canvas.texture(): Texture2D                      // the canvas's own cached one

Wraps 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). fromCanvas makes a new texture on every call; canvas.texture() creates one once and returns it after, and that one is what canvas.update() refreshes. Assigning a Canvas directly to sprite.texture uses canvas.texture() for you.

Note

a project's type-check currently rejects Texture2D.fromCanvas(canvas) — the parameter type names an internal member (an SDK typing defect); the call runs. canvas.texture() type-checks.

Properties & lifecycle

TypeScript
tex.width, tex.height    // pixels (readonly)
tex.id                   // native texture handle (readonly)
tex.destroy(): void      // free the GPU texture

Textures are shared freely — many sprites can use one Texture2D. Nothing frees a texture by itself: it lives until destroy(), even once nothing references it. After destroy() the sprites and tilemaps still using it draw nothing.

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