# Widget SDK v2 — Canvas widgets

Use v2 for bespoke effects such as fireworks, character animations, flowing
backgrounds, visualizers, custom scoreboards, or a completely custom UI. The
render function can draw any layout and visuals with Canvas 2D or WebGL 2.
Use v1 for the ready-made card, strip and plain layouts.

## Manifest

Set `version: 2` and put a single JavaScript function expression in `program`.
Keep the source as a separate JavaScript file while building. Serialize it as a
JSON string when writing `widget.json`. TypeScript authors should compile to
JavaScript first. See [canvas-runtime.d.ts](https://animstream.com/widget-sdk/agent-kit/canvas-runtime.d.ts)
for the render context and [widget.schema.json](https://animstream.com/widget-sdk/agent-kit/widget.schema.json)
for the accepted manifest fields.

```js
function render({ctx, width, height, time, event}) {
  ctx.clearRect(0, 0, width, height);
  ctx.fillStyle = "#91f2ff";
  ctx.beginPath();
  ctx.arc(width / 2, height / 2, 20 + Math.sin(time * 4) * 8, 0, Math.PI * 2);
  ctx.fill();
  ctx.fillText(event.user || "Hello", 12, 22);
}
```

| Field | Meaning |
| --- | --- |
| `mode: "alert"` | Plays when a Studio Flow triggers. `durationSeconds` is 1–30. Studio manages queueing and completion. |
| `mode: "live"` | Runs as a persistent source. Use `counters` to draw session totals. `metric`, `goal` and `showProgress` provide the same defaults as v1. The program owns what gets drawn. |
| `renderer` | Optional `"2d"` (default) or `"webgl2"`. In WebGL set the viewport on resize and clear buffers before each draw. |
| `fps` | Optional 12–60 frames per second, default 30. Keep each call fast enough for the frame interval. |
| `images` | Optional map of names to `{ mime, data }`. Use PNG, JPEG or WebP with raw base64. Each decoded image must fit within 2048 × 2048. Draw them through `images[name]`. |
| `program` | One synchronous function expression, up to 32,768 characters. No DOM or Node APIs. |
| Total request | At most 256 KiB, including manifest and embedded images. |

## Frame input

- `ctx`, `canvas`, `width`, `height` give you the rendering context, an OffscreenCanvas and its current pixel dimensions. The surface can resize while running and is capped at 1920 pixels on either axis.
- `time` and `delta` are seconds. `progress` is 0–1 for an alert and `null` for a persistent source. Studio may seek directly to a time for a still preview. Use time-based math and a seeded random function for effects that can seek.
- `event` contains `user`, `gift`, `count`, `coins` and `level` strings for Flow alerts. Fields may be empty. Convert numeric strings defensively.
- `counters` holds the available session numbers keyed by `gifts`, `coins`, `likes`, `follows`, `shares`, `comments`, `joins` and `subscribes`.
- `params` contains editable `headline`, `detail` and optional `goal`. For live widgets, `metric` and `showProgress` are also provided.
- `state` is an initially empty object retained between render calls for the same playback. It resets on replay or seek. `images` contains decoded assets. No DOM, external network access, account credentials or arbitrary Studio APIs are available to the program.

The render worker runs separately from the document. Return synchronously and
draw into the supplied context. Use a transparent background if the scene
should show through. Use a Flow audio action when an effect needs sound.

## Fireworks example

[Download fireworks.json](https://animstream.com/widget-sdk/agent-kit/examples/fireworks.json)
or read the [editable JavaScript](https://animstream.com/widget-sdk/agent-kit/examples/fireworks.js).
This example uses timed bursts, gravity, fading trails, additive glow and the
viewer's name. The number of sparks responds to the event count. It covers
portrait and landscape surfaces using relative coordinates.

Preview this file in the Widget SDK playground. Render it at multiple sizes,
check a full alert cycle and run it in the Flow Wizard with a test trigger.
For a live widget, change the test counters and verify that the displayed
values update without restarting playback. Follow the same Vendor Studio review
flow as v1.
