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.
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".


Draw
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>
))drawis 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.framegives itswidthandheightin points, thepixelRatio, and thetimein milliseconds since it first drew. context="webgl2"handsdrawa WebGL 2 context instead of a 2D one.onPointerhears the pointer over it:down,move,up,cancel,enterandleave, 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.
Prop
Type
Set up the drawing types
Add "songstress/sandbox/canvas" to your tsconfig.json's types for the types draw is handed:
{ "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
heightis 32 to 720 points (a canvas under 32 is left out), and its width, left out, its parent's.descriptionis 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
motionisessential: a meter that is the point of the page. - A tree holds two canvases at most. A
drawthat 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
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:
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
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.
Overlays
A small box above the app's pages, drawn from your components or one of your in-app views, which the person places, hides and brings back.
Views in the app
Your own HTML and CSS drawn inside Songstress in Chromium browsers on a computer, its code in a worker beside it, and everything such a view may not do.