Skip to content

Class: SSSurface ​

Defined in: ssplayer-webgl/src/SSSurface.ts:67

A WebGL context's worth of SpriteStudio state, and any number of SSPlayers drawn into it.

This is the unit the browser's WebGL context cap applies to: contexts scale with surfaces, not with players. One surface can carry a whole scene.

Three modes, chosen by what you construct it with and how you drive it:

  • owned — pass a <canvas>. The surface creates the context and owns the rAF ticker, the per-frame clear, resize(), context-loss recovery and visibility gating.
  • borrowed — pass an existing WebGLRenderingContext. The surface touches none of the above: the host drives update() / draw() and owns the frame.
  • overlay — an owned surface whose canvas covers the viewport, with each player's SSPlayer.box driven from a DOM element's rect. Built on top of the above; see SSOverlay.
ts
await SSSurface.initialize();
const surface = new SSSurface(canvas, { autoStart: true });
const player = surface.createPlayer();
await player.load('chara.ssab');
player.setAnimation('idle');
player.play();

Constructors ​

Constructor ​

new SSSurface(target, options?): SSSurface

Defined in: ssplayer-webgl/src/SSSurface.ts:134

Parameters ​

target ​

WebGLRenderingContext | WebGL2RenderingContext | HTMLCanvasElement

options? ​

SSSurfaceOptions = {}

Returns ​

SSSurface

Properties ​

canvas ​

readonly canvas: HTMLCanvasElement

Defined in: ssplayer-webgl/src/SSSurface.ts:82


gl ​

readonly gl: WebGLRenderingContext | WebGL2RenderingContext

Defined in: ssplayer-webgl/src/SSSurface.ts:81


options ​

options: SSSurfaceOptions = {}

Defined in: ssplayer-webgl/src/SSSurface.ts:136


owned ​

readonly owned: boolean

Defined in: ssplayer-webgl/src/SSSurface.ts:91

False for a borrowed context: the host owns the frame, the clear and the canvas.


renderer ​

readonly renderer: SSWebGLRenderer

Defined in: ssplayer-webgl/src/SSSurface.ts:83


sharedOverlay ​

sharedOverlay: boolean = false

Defined in: ssplayer-webgl/src/SSSurface.ts:127

Internal

Set by SSOverlay on the surface it draws every element's player on. Those players sit over the page, which is their backdrop though the surface cannot read it, so several of them there are as normal as one alone.


textureCache ​

readonly textureCache: SSWebGLTextureCache

Defined in: ssplayer-webgl/src/SSSurface.ts:89

One GPU upload per texture URL across every player here. A WebGLTexture belongs to its context, so this is exactly the right scope — and it is why a cast of the same character costs one atlas rather than one each.

Accessors ​

children ​

Get Signature ​

get children(): readonly SSPlayer[]

Defined in: ssplayer-webgl/src/SSSurface.ts:218

Players in draw order (front of the array draws first).

Returns ​

readonly SSPlayer[]


drawingBufferSize ​

Get Signature ​

get drawingBufferSize(): object

Defined in: ssplayer-webgl/src/SSSurface.ts:191

Drawing-buffer size in device pixels — the space player boxes live in.

Returns ​

object

height ​

height: number

width ​

width: number

Methods ​

add() ​

add(player): void

Defined in: ssplayer-webgl/src/SSSurface.ts:205

Add a player. Called for you by the SSPlayer constructor. Idempotent.

Parameters ​

player ​

SSPlayer

Returns ​

void


contextChange() ​

contextChange(): void

Defined in: ssplayer-webgl/src/SSSurface.ts:503

Internal

Let a borrowed host tell us its context was recreated. Restores the renderer only, not textures.

Returns ​

void


createPlayer() ​

createPlayer(options?): SSPlayer

Defined in: ssplayer-webgl/src/SSSurface.ts:198

Construct a player on this surface and add it.

Parameters ​

options? ​

SSPlayerOptions

Returns ​

SSPlayer


destroy() ​

destroy(): void

Defined in: ssplayer-webgl/src/SSSurface.ts:514

Destroy the surface and everything it owns: the renderer's GL objects, plus every player still attached (and therefore their WASM allocations and textures).

Returns ​

void


draw() ​

draw(): void

Defined in: ssplayer-webgl/src/SSSurface.ts:257

Clear (owned only) and draw every player in order.

A borrowed surface never clears: the host's framebuffer already holds its own scene, and wiping it would erase the host's frame.

Returns ​

void


invalidateOrder() ​

invalidateOrder(): void

Defined in: ssplayer-webgl/src/SSSurface.ts:224

Call after changing any player's order so the next draw re-sorts.

Returns ​

void


remove() ​

remove(player): void

Defined in: ssplayer-webgl/src/SSSurface.ts:212

Remove a player without destroying it. Idempotent.

Parameters ​

player ​

SSPlayer

Returns ​

void


resize() ​

resize(cssW, cssH, dpr?): void

Defined in: ssplayer-webgl/src/SSSurface.ts:443

Size the drawing buffer to cssW × cssH at the device pixel ratio, clamped by maxDPR to bound fill-rate on HiDPI.

Only the backing store is written — CSS is the host's business. Size the canvas element yourself (a stylesheet, a layout, style.width); a library that writes inline styles fights whatever the host already set.

Players tracking the whole surface follow this automatically.

Parameters ​

cssW ​

number

cssH ​

number

dpr? ​

number

Returns ​

void


restoreContext() ​

restoreContext(): Promise<void>

Defined in: ssplayer-webgl/src/SSSurface.ts:478

Rebuild GPU state after the context is restored: the renderer's GL objects (programs / buffers / VAO) synchronously, then every player's textures from their retained sources. Only after textures are back do we clear the lost flag and resume — so we never draw a frame with missing textures.

On a shared surface this is one operation for all players, which also means one loss blanks every player until it completes.

Returns ​

Promise<void>


startTicker() ​

startTicker(): void

Defined in: ssplayer-webgl/src/SSSurface.ts:359

Returns ​

void


stopTicker() ​

stopTicker(): void

Defined in: ssplayer-webgl/src/SSSurface.ts:366

Returns ​

void


update() ​

update(deltaMs): void

Defined in: ssplayer-webgl/src/SSSurface.ts:242

Advance every player's timeline. Owned surfaces call this from their ticker.

Parameters ​

deltaMs ​

number

Returns ​

void


initialize() ​

static initialize(wasm?): Promise<void>

Defined in: ssplayer-webgl/src/SSSurface.ts:68

Parameters ​

wasm? ​

string | ArrayBuffer | Uint8Array<ArrayBufferLike> | URL

Returns ​

Promise<void>