# Web SDK: SonaScene

A scene is many avatars in one canvas and one WebGPU or WebGL2 context, from `SonaRenderer.create()`. The examples call it `sona`.

## SonaScene properties

| Field | Type | Meaning |
|---|---|---|
| `canvas` | `HTMLCanvasElement` | The canvas the scene draws into. Read only. |
| `appId` | `string` | The app id the scene runs under. Read only. |
| `camera` | `SonaCamera` | The camera. Read only. |
| `avatars` | `readonly SonaAvatar[]` | Every avatar in the scene, in the order they were added. Read only. |
| `running` | `boolean` | True while the auto loop runs. Read only. |
| `items` | `SonaItems` | Custom items loaded into this scene. Read only. |
| `renderer` | `'webgpu' \| 'webgl2'` | "webgpu" or "webgl2": what this scene draws with. Read only. |
| `background` | `[number, number, number, number]` | The clear colour, premultiplied RGBA 0 to 1. |

## scene.add()

```ts
interface SonaScene {
  add(manifestBase64: string, opts?: AddOptions): SonaAvatar;
}
```

Queue an avatar. Returns at once; it builds over the next frames and appears when ready. Throws SonaError("limit") past maxAvatars.

| Parameter | Type | Meaning |
|---|---|---|
| `manifestBase64` | `string` | The avatar's 1,000-byte manifest, base64. |
| `opts` (optional) | [`AddOptions`](options-and-results.md#addoptions) | Where it stands and what it plays. |

Returns `SonaAvatar`.

```js
import { SonaRenderer } from './sona-renderer.js';

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
const a = sona.add(manifestBase64, {
  position: [-0.5, 0, 0], turn: 15, clip: 'GenericClap',
});
const b = sona.add(otherManifest, {
  position: [0.5, 0, 0], turn: -15, clip: 'GenericWave',
});
sona.camera.preset('crowd');
```
## scene.remove()

```ts
interface SonaScene {
  remove(avatar: SonaAvatar): void;
}
```

Take an avatar out and free what only it used.

| Parameter | Type | Meaning |
|---|---|---|
| `avatar` | [`SonaAvatar`](sonaavatar.md#sonaavatar) | An avatar of this scene. |

## scene.clear()

```ts
interface SonaScene {
  clear(): void;
}
```

Remove every avatar.

## scene.frame()

```ts
interface SonaScene {
  frame(timeMs?: number): Promise<FrameStats>;
}
```

Draw one frame at timeMs (performance.now() if omitted). For loop: "manual".

| Parameter | Type | Meaning |
|---|---|---|
| `timeMs` (optional) | `number` | The frame's time in milliseconds, which clips and eases follow. Default performance.now(). |

Returns `Promise<FrameStats>`.

## scene.settle()

```ts
interface SonaScene {
  settle(
    avatars?: SonaAvatar | SonaAvatar[],
    opts?: { seconds?: number },
  ): Promise<SonaAvatar[]>;
}
```

Resolves when the avatars (default all) are ready or failed. Avatars build only while frames are drawn: with loop "manual" this draws frames while it waits, where awaiting `avatar.ready` alone would wait for ever. Rejects with code "not_ready" after `seconds` (default 120).

| Parameter | Type | Meaning |
|---|---|---|
| `avatars` (optional) | `SonaAvatar \| SonaAvatar[]` | The avatars to wait for. Default: every avatar in the scene. |
| `opts.seconds` (optional) | `number` | How long to wait before rejecting. Default 120. |

Returns `Promise<SonaAvatar[]>`.

## scene.start()

```ts
interface SonaScene {
  start(): void;
}
```

Turn the auto loop on.

## scene.stop()

```ts
interface SonaScene {
  stop(): void;
}
```

Turn the auto loop off.

## scene.resize()

```ts
interface SonaScene {
  resize(): void;
}
```

Re-measure the canvas. Also automatic through a ResizeObserver.

## scene.snapshot()

```ts
interface SonaScene {
  snapshot(
    opts?: {
      type?: 'image/png' | 'image/webp' | 'image/jpeg';
      quality?: number;
      credit?: boolean;
    },
  ): Promise<Blob>;
}
```

A picture of the next frame, drawn and read back in one task. The credit is written into the file's metadata; credit: true also draws it in the corner.

| Parameter | Type | Meaning |
|---|---|---|
| `opts.type` (optional) | `'image/png' \| 'image/webp' \| 'image/jpeg'` | The picture's format. Default "image/png". |
| `opts.quality` (optional) | `number` | 0 to 1, for WebP and JPEG. Default: the browser's own. |
| `opts.credit` (optional) | `boolean` | Draw the badge in the picture's corner too. Default false. |

Returns `Promise<Blob>`.

```js
import { SonaRenderer } from './sona-renderer.js';

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
const avatar = sona.add(manifestBase64, { clip: 'GenericWave' });
await avatar.ready;
const png = await sona.snapshot({ credit: true });
const img = new Image();
img.src = URL.createObjectURL(png);
```

## scene.glb()

```ts
interface SonaScene {
  glb(
    manifestBase64: string,
    opts?: { clips?: ClipKey[]; textures?: boolean },
  ): Promise<ArrayBuffer>;
}
```

The avatar as a .glb, the same file the 360sona runtime exports, with the same credit inside. Runs a full build at face size 512 on the calling thread. Custom items it draws are named, with their creators, in the file's copyright. The SDK Licence (S5) allows this file only for the person whose avatar it is, with the credit left in; it is not a way to collect items or animations.

| Parameter | Type | Meaning |
|---|---|---|
| `manifestBase64` | `string` | The avatar's 1,000-byte manifest, base64. |
| `opts.clips` (optional) | `ClipKey[]` | The clips to put in the file. Default: every clip but "prop". |
| `opts.textures` (optional) | `boolean` | Embed the textures. Default true. |

Returns `Promise<ArrayBuffer>`.

## scene.clips()

```ts
interface SonaScene {
  clips(): ClipInfo[];
}
```

Every clip this SDK can play.

Returns `ClipInfo[]`.

```js
import { SonaRenderer } from './sona-renderer.js';

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
for (const c of sona.clips()) console.log(c.key, c.caption);
```

## scene.samples()

```ts
interface SonaScene {
  samples(): Promise<{ title: string; manifest: string }[]>;
}
```

Sample avatars from 360sona, for demos and benchmarks.

Returns `Promise<{ title: string; manifest: string }[]>`.

## scene.wardrobe()

```ts
interface SonaScene {
  wardrobe(): Promise<readonly WardrobeItem[]>;
}
```

The wardrobe for pickers: every item that may be offered, named, with its editor section. Loaded once per scene, through the bridge.

Returns `Promise<readonly WardrobeItem[]>`.

## scene.colours()

```ts
interface SonaScene {
  colours(): Record<string, string[]>;
}
```

The swatches of each palette slot (skin, hair, mouth, eyes, eyebrows, eyeShadow, facialHair, facialFeature), as #rrggbb.

Returns `Record<string, string[]>`.

## scene.colourways()

```ts
interface SonaScene {
  colourways(itemId: string): string[][] | null;
}
```

An item's own colourways, each three #rrggbb, once the item is loaded (worn by an avatar here, or added with items); null before, or when it has none. Pass the index to avatar.wear(id, { colourway }).

| Parameter | Type | Meaning |
|---|---|---|
| `itemId` | `string` | A stock or custom item id. |

Returns `string[][] | null`.

## scene.thumbnails()

```ts
interface SonaScene {
  thumbnails(ids: string[]): Promise<(Uint8Array | null)[]>;
}
```

Item pictures: PNG, JPEG or WebP bytes in the order asked, null where an item has none. At most 6000 different ids per page.

| Parameter | Type | Meaning |
|---|---|---|
| `ids` | `string[]` | Item ids, at most 6000 different ones a page. |

Returns `Promise<(Uint8Array | null)[]>`.

## scene.benchmark()

```ts
interface SonaScene {
  benchmark(
    opts?: {
      counts?: number[];
      seconds?: number;
      distinct?: number;
      onStep?: (r: BenchStep) => void;
    },
  ): Promise<BenchStep[]>;
}
```

Run the benchmark: adds sample avatars at each count (replicating `distinct` samples, default 12), waits until they are ready, measures, removes them.

| Parameter | Type | Meaning |
|---|---|---|
| `opts.counts` (optional) | `number[]` | The avatar counts to measure, in order; a count over maxAvatars is skipped. Default [10, 25, 50, 100, 200]. |
| `opts.seconds` (optional) | `number` | Seconds measured at each count. Default 4. |
| `opts.distinct` (optional) | `number` | Different sample avatars, repeated to make up each count. Default 12. |
| `opts.onStep` (optional) | `(r: BenchStep) => void` | Called with each row as it is measured. |

Returns `Promise<BenchStep[]>`.

## scene.stats()

```ts
interface SonaScene {
  stats(): SceneStats;
}
```

Rolling numbers over the last two seconds.

Returns `SceneStats`.

## scene.on()

```ts
interface SonaScene {
  on(event: 'ready', fn: (a: SonaAvatar) => void): () => void;
  on(
    event: 'error',
    fn: (e: SonaError, a?: SonaAvatar) => void,
  ): () => void;
  on(event: 'frame', fn: (s: FrameStats) => void): () => void;
}
```

- `'ready'`: An avatar appeared. Returns a function that removes the listener.
- `'error'`: Something failed. Returns a function that removes the listener.
- `'frame'`: A frame was drawn. Returns a function that removes the listener.

```js
import { SonaRenderer } from './sona-renderer.js';

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
sona.on('ready', (a) => console.log('drawn', a.id));
sona.on('error', (e) => console.log(e.code, e.fix));
sona.add(manifestBase64);
```

## scene.destroy()

```ts
interface SonaScene {
  destroy(): void;
}
```

Free everything: GPU objects, the bridge frame, the badge, listeners.
