# Web SDK: SonaRenderer

`SonaRenderer` is what the module exports: it makes scenes on your canvas, connections for engines that draw for themselves, and headless renderers, and holds the custom-item helpers. `defineAvatarEditor` and `SonaTrack` are exported beside it.

## SonaRendererStatic properties

| Field | Type | Meaning |
|---|---|---|
| `VERSION` | `string` | The SDK's version. Read only. |

## SonaRenderer.create()

```ts
interface SonaRendererStatic {
  create(opts: CreateOptions): Promise<SonaScene>;
}
```

Check the page's origin, open the asset bridge and start a scene on your canvas.

| Parameter | Type | Meaning |
|---|---|---|
| `opts` | [`CreateOptions`](options-and-results.md#createoptions) | Your canvas and the scene's settings. |

Returns `Promise<SonaScene>`.

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

const sona = await SonaRenderer.create({
  canvas: document.querySelector('canvas'),
  credit: 'bottom-left',
});
const avatar = sona.add(manifestBase64, { clip: 'GenericWave' });
await avatar.ready;
```
## SonaRenderer.supported()

```ts
interface SonaRendererStatic {
  supported(): boolean;
}
```

True when this browser has WebGPU or WebGL2.

Returns `boolean`.

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

if (!SonaRenderer.supported()) {
  document.body.append('This browser has neither WebGPU nor WebGL2.');
}
```

## SonaRenderer.connect()

```ts
interface SonaRendererStatic {
  connect(
    opts?: { appId?: string; host?: string },
  ): Promise<SonaConnection>;
}
```

A connection with no canvas, for engines that draw avatars themselves (the packages for three.js and Babylon.js).

| Parameter | Type | Meaning |
|---|---|---|
| `opts.appId` (optional) | `string` | The app id. Default: the one this copy was built for. |
| `opts.host` (optional) | `string` | Where assets come from. Default: the 360sona developer site. |

Returns `Promise<SonaConnection>`.

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

const connection = await SonaRenderer.connect();
const glb = await connection.glb(manifestBase64, {
  clips: ['GenericWave'],
});
```

## SonaRenderer.headless()

```ts
interface SonaRendererStatic {
  headless(
    opts?: {
      appId?: string;
      host?: string;
      renderer?: 'auto' | 'webgpu' | 'webgl2';
      antialias?: boolean | 'auto';
      faceSize?: 128 | 256 | 512;
    },
  ): Promise<SonaHeadless>;
}
```

Pictures with no canvas on the page: gamerpics, cards and frames at any size (the web half of sona_headless_*).

| Parameter | Type | Meaning |
|---|---|---|
| `opts.appId` (optional) | `string` | The app id. Default: the one this copy was built for. |
| `opts.host` (optional) | `string` | Where assets come from. Default: the 360sona developer site. |
| `opts.renderer` (optional) | `'auto' \| 'webgpu' \| 'webgl2'` | As create(): "auto", "webgpu" or "webgl2". Default "auto". |
| `opts.antialias` (optional) | `boolean \| 'auto'` | As create(). Default "auto". |
| `opts.faceSize` (optional) | `128 \| 256 \| 512` | The face sheet of each picture's avatar, in pixels. Default 512. |

Returns `Promise<SonaHeadless>`.

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

const headless = await SonaRenderer.headless();
const png = await headless.picture(manifestBase64, {
  width: 256, height: 256, preset: 'face',
});
headless.close();
save(png);   // your own: a download, an upload, an <img>
```

## SonaRenderer.checkItem()

```ts
interface SonaRendererStatic {
  checkItem(
    file: ArrayBuffer | Uint8Array,
  ): {
    ok: boolean;
    rule: number | null;
    why: string | null;
    info: CustomItemInfo | null;
  };
}
```

The shallow check of a .sonaitem (format rules 1 to 11), no network.

| Parameter | Type | Meaning |
|---|---|---|
| `file` | `ArrayBuffer \| Uint8Array` | The .sonaitem file's bytes. |

Returns `{ ok: boolean; rule: number | null; why: string | null; info: CustomItemInfo | null }`.

## SonaRenderer.newItemId()

```ts
interface SonaRendererStatic {
  newItemId(templateId: string): string;
}
```

A fresh custom id for a template: its slot and body, a custom marker, 90 random bits.

| Parameter | Type | Meaning |
|---|---|---|
| `templateId` | `string` | The stock item id of the template the custom item is drawn on. |

Returns `string`.

## SonaRenderer.isCustomId()

```ts
interface SonaRendererStatic {
  isCustomId(id: string): boolean;
}
```

True for a custom item id (the marker 8, 9, a or b at the start of the fourth group).

| Parameter | Type | Meaning |
|---|---|---|
| `id` | `string` | An item id. |

Returns `boolean`.

## SonaRenderer.pairOf()

```ts
interface SonaRendererStatic {
  pairOf(id: string): string | null;
}
```

The other body's id (man and woman swapped), or null for body 3 and for real ids.

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

Returns `string | null`.

## SonaRenderer.wear()

```ts
interface SonaRendererStatic {
  wear(
    manifestBase64: string,
    itemId: string,
    opts?: { colourway?: number },
  ): string;
}
```

The manifest with an item worn, by the editor's rules (a long-sleeved top takes off the watch). colourway needs the item loaded in a scene on this page. Returns a new manifest (base64).

| Parameter | Type | Meaning |
|---|---|---|
| `manifestBase64` | `string` | The avatar's 1,000-byte manifest, base64. |
| `itemId` | `string` | A stock or custom item id. |
| `opts.colourway` (optional) | `number` | An index into the item's colourways (scene.colourways()). Default: the item's own colours. |

Returns `string`.

## SonaRenderer.unwear()

```ts
interface SonaRendererStatic {
  unwear(manifestBase64: string, slot: string): string;
}
```

The manifest with a slot ("shirt", "headwear", ...) emptied. Returns a new manifest (base64).

| Parameter | Type | Meaning |
|---|---|---|
| `manifestBase64` | `string` | The avatar's 1,000-byte manifest, base64. |
| `slot` | `string` | A wardrobe slot, as scene.wardrobe() names its sections: "shirt", "trousers", "shoes", "costume", "headwear", "glasses", "gloves", "wristwear", "ring", "earrings" or "carryable". |

Returns `string`.

## SonaRenderer.defineAward()

```ts
interface SonaRendererStatic {
  defineAward(
    opts: {
      app: string;
      key: string;
      title: string;
      description?: string;
      item: string;
      file?: ArrayBuffer | Uint8Array | Blob;
      developerKey: string;
      host?: string;
    },
  ): Promise<Award>;
}
```

Defines an award item for your app, or changes one, with the app's developer key: for your own tools, never a shipped game. Rejects with SonaError "item" (a refused field or file), "app" (a wrong key), "limit" or "gate".

| Parameter | Type | Meaning |
|---|---|---|
| `opts.app` | `string` | Your app id. |
| `opts.key` | `string` | The award's key, 1 to 64 of a-z, 0-9, _ and -. |
| `opts.title` | `string` | The award's name, as players see it. |
| `opts.description` (optional) | `string` | A line under the title. |
| `opts.item` | `string` | A stock avatar award's item id, or your custom item's id. |
| `opts.file` (optional) | `ArrayBuffer \| Uint8Array \| Blob` | The custom item's .sonaitem, when item is a custom id. |
| `opts.developerKey` | `string` | Your app's developer key. |
| `opts.host` (optional) | `string` | The accounts server. Default: the 360sona developer site. |

Returns `Promise<Award>`.

## SonaRenderer.unlockAward()

```ts
interface SonaRendererStatic {
  unlockAward(
    opts: { key: string; playerToken: string; host?: string },
  ): Promise<{ unlocked: boolean; award: Award }>;
}
```

Gives the signed-in player an award item, which joins their 360sona wardrobe. Unlocking it twice is fine. Rejects with SonaError "not_found" (no such award), "app" (the token lacks the awards scope), "limit" or "gate".

| Parameter | Type | Meaning |
|---|---|---|
| `opts.key` | `string` | The award's key. |
| `opts.playerToken` | `string` | The player's token, with the awards scope. |
| `opts.host` (optional) | `string` | The accounts server. Default: the 360sona developer site. |

Returns `Promise<{ unlocked: boolean; award: Award }>`.

## SonaRenderer.playerAwards()

```ts
interface SonaRendererStatic {
  playerAwards(
    opts: { playerToken: string; host?: string },
  ): Promise<Award[]>;
}
```

The award items the signed-in player has earned in your app.

| Parameter | Type | Meaning |
|---|---|---|
| `opts.playerToken` | `string` | The player's token, with the awards scope. |
| `opts.host` (optional) | `string` | The accounts server. Default: the 360sona developer site. |

Returns `Promise<Award[]>`.

## SonaRenderer.awardFile()

```ts
interface SonaRendererStatic {
  awardFile(
    opts: { award: Award; playerToken: string; host?: string },
  ): Promise<Uint8Array>;
}
```

A custom award's .sonaitem, for the player who earned it; give it to scene.items.add() to wear it. Rejects with SonaError "not_found" when the player has not earned it.

| Parameter | Type | Meaning |
|---|---|---|
| `opts.award` | [`Award`](custom-items.md#award) | An award from playerAwards() that has a file. |
| `opts.playerToken` | `string` | The player's token, or your app's developer key. |
| `opts.host` (optional) | `string` | The accounts server. Default: the 360sona developer site. |

Returns `Promise<Uint8Array>`.

## SonaRenderer.defineAvatarEditor()

```ts
interface SonaRendererStatic {
  defineAvatarEditor(name?: string): SonaAvatarEditorConstructor;
}
```

Registers the avatar editor element, by default as `<sona-avatar-editor>`. The same as the defineAvatarEditor export.

| Parameter | Type | Meaning |
|---|---|---|
| `name` (optional) | `string` | The element's tag name. Default "sona-avatar-editor". |

Returns `SonaAvatarEditorConstructor`.

## VERSION

```ts
declare const VERSION: string;
```

The SDK's version, for example "1.1.0".
