# Web SDK: SonaAvatar

One avatar in a scene, from `scene.add()`. It is built over the next frames and shows when `ready` resolves.

## SonaAvatar properties

| Field | Type | Meaning |
|---|---|---|
| `id` | `number` | A number unique on this page. Read only. |
| `manifest` | `string` | The manifest it wears (base64): the one it was added with, as changed by wear and unwear. Read only. |
| `state` | `'queued' \| 'loading' \| 'ready' \| 'failed' \| 'removed'` | Where it is in its life. Read only. |
| `ready` | `Promise<SonaAvatar>` | Resolves when it is on screen; rejects with a SonaError if it cannot be built. Read only. |
| `position` | `Vec3` | Its position. Read only. |
| `turn` | `number` | Its turn in degrees. Read only. |
| `scale` | `number` | Its scale. Read only. |
| `clip` | `ClipKey \| null` | What it was last asked to play. Read only. |
| `missingItems` | `readonly string[]` | Custom ids this avatar wears that are not loaded (or cannot be built), as last built. Required slots show the body's default garment meanwhile. Read only. |

## avatar.play()

```ts
interface SonaAvatar {
  play(
    clip: ClipKey,
    opts?: { loop?: boolean; speed?: number; from?: number },
  ): Promise<void>;
}
```

Play a clip. Called before ready, it is remembered and applied when the avatar appears.

| Parameter | Type | Meaning |
|---|---|---|
| `clip` | `ClipKey` | A key from scene.clips(), a raw clip id, "prop", "idle" or "still". |
| `opts.loop` (optional) | `boolean` | Loop the clip. Default true ("idle" always loops). |
| `opts.speed` (optional) | `number` | Playback speed, 0 or more; 1 is the clip's own. Default 1. |
| `opts.from` (optional) | `number` | Seconds into the clip to start from, up to its length. Default 0. |

Returns `Promise<void>`.

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

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
const avatar = sona.add(manifestBase64, { clip: 'GenericStand5' });
await avatar.ready;
await avatar.play('GenericCelebration', { loop: false });
```
## avatar.stop()

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

Hold the current pose.

## avatar.move()

```ts
interface SonaAvatar {
  move(to: Placement, opts?: { over?: number }): void;
}
```

Move, turn or scale.

| Parameter | Type | Meaning |
|---|---|---|
| `to` | [`Placement`](options-and-results.md#placement) | Where to go: position (metres), turn (degrees about Y) and scale. What it leaves out stays as it is. |
| `opts.over` (optional) | `number` | Seconds to ease there. Default 0 (at once). |

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

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
const avatar = sona.add(manifestBase64);
await avatar.ready;
avatar.move({ turn: 60 }, { over: 1 });
```
## avatar.setVisible()

```ts
interface SonaAvatar {
  setVisible(visible: boolean): void;
}
```

Show or hide it.

| Parameter | Type | Meaning |
|---|---|---|
| `visible` | `boolean` | false hides it; it keeps its place and its clip. |

## avatar.bounds()

```ts
interface SonaAvatar {
  bounds(): { center: Vec3; radius: number };
}
```

Its bounding sphere in world space.

Returns `{ center: Vec3; radius: number }`.

## avatar.remove()

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

Take it out of its scene.

## avatar.wear()

```ts
interface SonaAvatar {
  wear(itemId: string, opts?: { colourway?: number }): Promise<void>;
}
```

Wears an item (stock or custom) and rebuilds. The old look stays on screen until the new one is ready. colourway: an index into the item's colourways.

| Parameter | Type | Meaning |
|---|---|---|
| `itemId` | `string` | A stock or custom item id; a custom one is loaded first with scene.items.add(). |
| `opts.colourway` (optional) | `number` | An index into the item's colourways (scene.colourways()). Default: the item's own colours. |

Returns `Promise<void>`.

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

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
const avatar = sona.add(manifestBase64);
await avatar.ready;
await avatar.wear('00001000-c6f4-2253-caa5-9f694d5309cf');
avatar.play('prop');
// room for the prop: its clip swings it above the head
sona.camera.orbit({
  yaw: 0, pitch: 5, distance: 4.5, target: [0, 0.95, 0],
});
```
## avatar.unwear()

```ts
interface SonaAvatar {
  unwear(slot: string): Promise<void>;
}
```

Empties a slot ("shirt", "headwear", ...) and rebuilds.

| Parameter | Type | Meaning |
|---|---|---|
| `slot` | `string` | A wardrobe slot, as scene.wardrobe() names its sections: "shirt", "trousers", "shoes", "costume", "headwear", "glasses", "gloves", "wristwear", "ring", "earrings" or "carryable". |

Returns `Promise<void>`.

## avatar.update()

```ts
interface SonaAvatar {
  update(manifestBase64: string): Promise<void>;
}
```

Changes to another manifest and rebuilds. The old look stays on screen until the new one is ready.

| Parameter | Type | Meaning |
|---|---|---|
| `manifestBase64` | `string` | The new manifest, base64. |

Returns `Promise<void>`.

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

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
const avatar = sona.add(manifestBase64, { clip: 'GenericStand5' });
await avatar.ready;
await avatar.update(otherManifest);
```
