# The Canvas

> Pixels of your own beside Songstress's parts, drawn from your client code's worker in 2D or WebGL 2, with the pointer forwarded and drawing stopped while it is out of sight.

Page: https://songstress.app/docs/develop/canvas/

`Canvas` is the one place an extension draws pixels of its own: a visualiser, a meter, a little scene beside
Songstress's parts, never in place of one. It is a component of client code. Its `draw(context, frame)` runs in your
extension's worker, on a 2D context or a WebGL 2 one, and each frame reaches the app as a picture. Ask for
`"songstress": "^0.1.12"`.

<Shot name="web/ext-canvas" alt="A page of client code: a count, and two canvases its worker draws, a dot and a square" />

## Draw [#draw]

```tsx title="src/dot.tsx"
import { songstress } from 'songstress/client'
import { Canvas, Section } from 'songstress/ui'

songstress.ui.pages.register('dot', () => (
  <Section title="A dot">
    <Canvas
      height={80}
      description="A dot that crosses the canvas"
      draw={(context, frame) => {
        context.fillStyle = '#e50043'
        context.fillRect(((frame.time % 2000) / 2000) * frame.width, frame.height / 2 - 4, 8, 8)
      }}
    />
  </Section>
))
```

- `draw` is called for each frame the app shows, at most 60 a second, and never while the canvas is out of sight
  (scrolled away, a tab behind, the app in the background).
- The canvas is clear at the start of every `draw`. `frame` gives its `width` and `height` in points, the
  `pixelRatio`, and the `time` in milliseconds since it first drew.
- `context="webgl2"` hands `draw` a WebGL 2 context instead of a 2D one.
- `onPointer` hears the pointer over it: `down`, `move`, `up`, `cancel`, `enter` and `leave`, each with its place in
  points.

Inside a canvas, and only there, a colour is yours to choose. Read the theme from `onAppearanceChange` (API 0.1.14)
if your picture should follow dark and light.

A `Canvas`'s props: a 2D canvas (the default) or a WebGL 2 one, and the
function that draws each frame. The canvas is clear at the start of every
`draw`: draw the whole picture each time.

| Field | Type | Description |
|---|---|---|
| `grow?` | `boolean \| undefined` | In a horizontal stack, it takes the room left over. |
| `height` | `number` | Its height, in points, from 32 to 720: a canvas has no content to size itself by. |
| `width?` | `number \| undefined` | Its width, in points, from 32 to 720. Left out, across its parent. |
| `motion?` | `"decorative" \| "essential" \| undefined` | `essential` keeps it moving under reduced motion; left out, it is decoration and stops there. |
| `description` | `string` | What it shows, for people who do not see it. |
| `onPointer?` | `((pointer: CanvasPointer) => void) \| undefined` | Told of the pointer over it; without it the canvas takes no pointer, and a press falls to what is under it. |
| `context?` | `"2d" \| "webgl2" \| undefined` | Left out, "2d". WebGL 2. |
| `draw` | `((context: OffscreenCanvasRenderingContext2D, frame: CanvasFrame) => void) \| ((context: WebGL2RenderingContext, frame: CanvasFrame) => void)` | Draws one frame, in points (the context is already scaled to the device's pixels). Draws one frame, in the drawing buffer's pixels (`frame.pixelRatio` of them a point). |

## Set up the drawing types [#set-up-the-drawing-types]

Add `"songstress/sandbox/canvas"` to your `tsconfig.json`'s `types` for the types `draw` is handed:

```json title="tsconfig.json"
{ "compilerOptions": { "lib": ["ES2023"], "types": ["songstress/sandbox", "songstress/sandbox/canvas"] } }
```

It is TypeScript's library of a worker, so it also names a worker's other globals, `fetch` among them, which the
sandbox does not give, and a `types` entry holds for every file of the project: once it is there, a `fetch` anywhere
compiles and fails only when it runs. Keep the files that draw in a project of their own (a second `tsconfig.json`
that includes them), or remember that the sandbox has no `fetch`.

## The rules [#the-rules]

- `height` is 32 to 720 points (a canvas under 32 is left out), and its width, left out, its parent's.
  `description` is what a screen reader says.
- When the person asks for reduced motion a canvas is decoration and stops (it draws a frame when it appears, after
  each render and when its size changes), unless its `motion` is `essential`: a meter that is the point of the page.
- A tree holds two canvases at most. A `draw` that throws three times in a row is called no more until the page
  renders again.
- The server refuses a canvas: it is client code's alone. A canvas cannot draw a member's file, since its code never
  holds the bytes: a member's own picture plays as a `Sprite`.

## Where it draws [#where-it-draws]

The web, and the macOS desktop from 13.3, draw a canvas. The phones and an older macOS show a line saying where it can
be seen. Read `client.capabilities.canvas` in your renderer and draw something else where it is false:

```tsx title="src/meter.tsx"
import { songstress } from 'songstress/client'
import { Canvas, Text } from 'songstress/ui'

songstress.ui.panels.register('meter', ({ client }) =>
  client.capabilities.canvas ? (
    <Canvas height={48} description="How loud it plays" draw={(context, frame) => context.fillRect(0, 0, frame.width / 2, frame.height)} />
  ) : (
    <Text tone="muted">The meter shows on the web and on a Mac.</Text>
  ),
)
```

## Test it [#test-it]

`page.draw(name, context, frame)` calls a canvas's `draw` once, with a context of the test's own (a recorder of its
calls, say), and `pointer(name, { kind, x, y })` tells its `onPointer` of the pointer as the app does. See
[Test and debug](/docs/develop/testing/#mount-a-component).
