An infinite canvas: pan and zoom, shapes, selection with resize and rotate handles, smart guides, drawing tools, cursors and comment pins.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { answerCanvas, CanvasState, canvasState } from "./ui/uicontext/components/canvas/Editor.ts"import { CanvasTool, CanvasViewport, CanvasViewportEvents } from "./ui/uicontext/components/canvas/Viewport.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { habitScreens } from "./canvas.ts"const styles = stylex.create({ stage: { blockSize: 560, inlineSize: "100%", borderRadius: 12, overflow: "hidden" } })const Message = UiSchema.TaggedEnum({ RequestedCanvas: { event: CanvasViewportEvents } })const toolOf = Schema.decodeUnknownSync(CanvasTool)// The app keeps the scene, the selection and the tool, and answers every request with// `answerCanvas`: select, change, command, undo and redo.export const ScreensCanvas = Html.component({ name: "ScreensCanvas", model: Schema.Struct({ canvas: CanvasState }), message: Message,}).make({ init: () => ({ canvas: canvasState(habitScreens) }), update: { RequestedCanvas: ($, { event }) => $.evolve({ canvas: (canvas) => answerCanvas(canvas, event, "copy") }), }, view: (h, { canvas }) => h.div.attrs(sx(styles.stage)).children(CanvasViewport(h, { props: { label: "Habit app — screens", shapes: canvas.shapes, selection: canvas.selection, tool: toolOf(canvas.tool), agentId: "canvas-demo", }, onEmit: (event) => Message.RequestedCanvas({ event }), })),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/canvas-viewportUsage
import { answerCanvas, type CanvasState } from "./ui/uicontext/components/canvas/Editor.ts"import { CanvasViewport, CanvasViewportEvents } from "./ui/uicontext/components/canvas/Viewport.ts"const Message = UiSchema.TaggedEnum({ RequestedCanvas: { event: CanvasViewportEvents } })// The app keeps the scene, the selection and the tool; `answerCanvas` applies each request with undo.export const Board = (h: Html.H<typeof Message.Type>, canvas: CanvasState) => CanvasViewport(h, { props: { label: "Flows", shapes: canvas.shapes, selection: canvas.selection, tool: "select" }, onEmit: (event) => Message.RequestedCanvas({ event }), })export const reduce = (canvas: CanvasState, event: typeof CanvasViewportEvents.Type) => answerCanvas(canvas, event, "shape")Examples
A whiteboard
Notes, ink, shapes and connectors with the board's tools, and a minimap.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { answerCanvas, CanvasState, canvasState } from "./ui/uicontext/components/canvas/Editor.ts"import { defaultDrawStyle } from "./ui/uicontext/components/canvas/StylePanel.ts"import { boardTools, ToolPalette } from "./ui/uicontext/components/canvas/ToolPalette.ts"import { CanvasTool, CanvasViewport, CanvasViewportEvents } from "./ui/uicontext/components/canvas/Viewport.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { retroBoard } from "./canvas.ts"const styles = stylex.create({ stage: { position: "relative", blockSize: 560, inlineSize: "100%", borderRadius: 12, overflow: "hidden" }, dock: { position: "absolute", insetInline: 0, bottom: 12, display: "flex", justifyContent: "center", paddingInline: 12, pointerEvents: "none" },})const Message = UiSchema.TaggedEnum({ RequestedCanvas: { event: CanvasViewportEvents }, PickedTool: { tool: CanvasTool } })const toolOf = Schema.decodeUnknownSync(CanvasTool)// Sticky notes, ink and connectors that stay attached to what they join. Press S for a note, P to// draw, Shift+L for an arrow, E to erase and K for the laser; the minimap jumps the view.export const RetroBoard = Html.component({ name: "RetroBoard", model: Schema.Struct({ canvas: CanvasState }), message: Message,}).make({ init: () => ({ canvas: canvasState(retroBoard) }), update: { RequestedCanvas: ($, { event }) => $.evolve({ canvas: (canvas) => answerCanvas(canvas, event, "board") }), PickedTool: ($, { tool }) => $.evolve({ canvas: (canvas) => ({ ...canvas, tool }) }), }, view: (h, { canvas }) => h.div.attrs(sx(styles.stage)).children( CanvasViewport(h, { props: { label: "Retro board", shapes: canvas.shapes, selection: canvas.selection, tool: toolOf(canvas.tool), drawStyle: defaultDrawStyle, minimap: true, agentId: "canvas-board" }, onEmit: (event) => Message.RequestedCanvas({ event }), }), h.div.attrs(sx(styles.dock)).children(ToolPalette(h, { tool: toolOf(canvas.tool), groups: boardTools, onPick: (tool) => Message.PickedTool({ tool }) })), ),})Review with others
Collaborators' cursors and selections, comment pins, and a canvas that only views and comments.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { CanvasViewport, CanvasViewportEvents } from "./ui/uicontext/components/canvas/Viewport.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { habitScreens } from "./canvas.ts"const styles = stylex.create({ stage: { blockSize: 480, inlineSize: "100%", borderRadius: 12, overflow: "hidden" } })const Message = UiSchema.TaggedEnum({ RequestedCanvas: { event: CanvasViewportEvents } })const peers = [ { id: "priya", name: "Priya", color: "#E5484D", cursor: { x: 640, y: 300 }, selection: ["home-card-2"] }, { id: "tom", name: "Tom", color: "#30A46C", cursor: { x: 250, y: 560 }, selection: ["welcome-title"] },]const pins = [ { id: "gap", x: 820, y: 250, author: "Priya Raman", color: "#E5484D", count: 2, preview: "Can we tighten the gap to 12?" }, { id: "copy", x: 330, y: 430, author: "Tom Okafor", color: "#30A46C", count: 1, preview: "Copy approved ✓" },]// View and comment only: people can select and measure, follow others' cursors and selections, and// open the comment pins, but not change the file.export const ReviewCanvas = Html.component({ name: "ReviewCanvas", model: Schema.Struct({ selection: Schema.Array(Schema.String), open: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ selection: [], open: null }), update: { RequestedCanvas: ($, { event }) => event._tag === "RequestedSelect" ? $.evolve({ selection: event.ids }) : event._tag === "RequestedOpenPin" ? $.evolve({ open: event.id }) : $.model, }, view: (h, { selection, open }) => h.div.attrs(sx(styles.stage)).children(CanvasViewport(h, { props: { label: "Habit app — review", shapes: habitScreens, selection, tool: "select", can: ["comment"], peers, pins, activePin: open, agentId: "canvas-review" }, onEmit: (event) => Message.RequestedCanvas({ event }), })),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | The canvas's accessible name: "Habit app — v2, Flows" |
shapesrequired | Shape[] | — | — |
selectionrequired | string[] | — | — |
toolrequired | CanvasTool | — | — |
mode | CanvasMode | — | Design edits; prototype draws and links wires; inspect only selects and measures |
can | Can(["edit", "comment"]) | — | Edit and comment by default; leave "edit" out for view and comment only |
peers | CanvasPeer[] | — | — |
pins | CanvasPin[] | — | — |
activePin | string | null | — | The open pin: drawn raised |
wires | CanvasWire[] | — | — |
drawStyle | DrawStyle | — | The style new shapes take; design defaults when absent |
background | "dots" | "plain" | — | — |
snap | boolean | on | Snap to edges, centres and equal gaps while moving and resizing; on by default |
minimap | boolean | — | — |
reveal | string | null | — | Fit a shape (its id) or everything ("all") into view. Each new value moves the camera once; add a suffix after a colon ("home:2") to ask again for the same shape. |
reportPointer | boolean | — | Report MovedPointer for presence |
idPrefix | string | — | Prefix for the ids of new shapes |
agentId | string | — | — |
Events
| Event | Payload | Description |
|---|---|---|
RequestedSelect | { ids: string[] } | Select these shapes, in order (empty clears) |
RequestedChange | { changes: CanvasChangeSchema[]; group?: string } | Apply these changes as one undo step; edits sharing a group (a burst of nudges) may merge |
RequestedCommand | { command: CanvasCommand } | — |
RequestedTool | { tool: CanvasTool } | — |
RequestedUndo | {} | — |
RequestedRedo | {} | — |
RequestedComment | { x: number; y: number; shape: string | null } | Start a comment thread at a world point, on the shape there if any |
RequestedOpenPin | { id: string } | — |
RequestedLink | { from: string; to: string } | Link a prototype hotspot to a frame |
MovedPointer | { x: number; y: number } | Where the pointer is, for presence; only with reportPointer |
LeftPointer | {} | — |
MovedCamera | { camera: Camera } | The camera settled after a pan or zoom |
Agent actions
| Action | Description |
|---|---|
PickedShapes | Select shapes by id (an empty list clears the selection) |
PickedCommand | Run a command on the selection: delete, duplicate, group, ungroup, frame, forward, backward, front, back, hide, lock, flip-x, flip-y |
AddedShape | Add a shape: kind is frame, rect, ellipse, text, sticky; x, y, w, h in canvas units; text for text and sticky notes; parent is a frame id |
NudgedSelection | Move the selection by dx, dy canvas units |
PickedZoom | Zoom: in, out, fit (everything), selection, or a factor such as 1 for 100% |