@mocanvas/editor · class

TextureManager

declare class TextureManager

Owns the host's GPU textures: allocates ids, refcounts them by key, loads their pixels asynchronously and uploads them through the current RenderBackend.

acquire hands back the id straight away so a shape can be written to the engine in the same tick; the texture only appears once the load resolves, so callers keep a DOM fallback until isReady(key). A key whose load rejected resolves to 0 (no texture — the shape falls back to its plain fill).

Callers that re-derive their textures on every write (a ShapeUtil in getRenderStyle) should acquire inside withOwner, which reconciles the keys an owner holds instead of piling up references.


Methods · 15

acquire(key: string, load: TextureLoader, opts?: TextureOptions): number

Reference the texture for key, starting load the first time. Returns the host texture id, or 0 when the key previously failed to load.

constructor(options?: TextureManagerOptions)
dispose(): void
getAllOwners(): readonly string[]

Every owner holding at least one texture.

getBackend(): RenderBackend | null
getId(key: string): number

Current id for key: 0 when unknown or failed.

getInfo(key: string): TextureInfo | undefined
getOwners(key: string): readonly string[]

Owners (shape ids) currently holding key.

getState(key: string): TextureState | undefined
isReady(key: string): boolean

Whether the texture for key is uploaded and safe to draw.

release(key: string): void

Drop one reference. The texture is deleted when the last one goes.

releaseOwner(owner: string): void

Release every key an owner holds (the shape was deleted).

setBackend(backend: RenderBackend | null): void

Point the manager at the backend that owns the GPU textures. Everything already decoded is uploaded again; anything still loading uploads when it resolves. The previous backend keeps (and frees) its own GL objects.

get size(): number

Number of live texture entries.

withOwner<T>(owner: string, fn: () => T): T

Run fn with every acquire inside it attributed to owner. Keys the owner held before but did not acquire again are released, so re-deriving a shape's style neither leaks references nor keeps a stale texture alive.

mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.