Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.tsdraw.com/llms.txt

Use this file to discover all available pages before exploring further.

The @tsdraw/react package uses several internal hooks to manage canvas state. These are not exported as public API, but understanding them helps when building custom integrations.

useTsdrawCanvasController

The primary hook that wires up the editor, canvas element, event listeners, and rendering loop. It returns:
FieldTypeDescription
canvasRefRefObject<HTMLCanvasElement>Ref to attach to the canvas element
editorEditorThe core editor instance
currentToolToolIdCurrently active tool
drawStyleDrawStyleStateCurrent color, dash, fill, size
canUndobooleanWhether undo is available
canRedobooleanWhether redo is available
setTool(id: ToolId) => voidSwitch tools
applyDrawStyle(partial) => voidUpdate draw style
undo() => voidUndo last action
redo() => voidRedo last action

Persistence hooks

When persistenceKey is set, the component internally manages:
  • IndexedDB storage via TsdrawLocalIndexedDb — saves document and session snapshots
  • Cross-tab sync via BroadcastChannel — other tabs with the same key receive updates
  • Session ID via getOrCreateSessionId — tracks the browser session for conflict resolution

Rendering loop

The canvas controller sets up a requestAnimationFrame loop that:
  1. Reads the current viewport and shapes from the editor
  2. Calls the renderer to draw the background and shapes
  3. Draws selection and tool overlays on top
The loop runs continuously while the component is mounted and pauses when the tab is hidden.