Skip to main content

Quick start

1. Show a canvas​

A Drawing holds the strokes. Create one per document and pass it to SketchCanvas:

import { useRef, useState } from 'react'
import { Drawing, SketchCanvas, type CanvasController } from 'nitro-ui-sketch'

export function Notes() {
const [drawing] = useState(() => new Drawing())
const canvas = useRef<CanvasController>(null)
return (
<SketchCanvas
ref={canvas}
layout={{ flex: 1 }}
drawing={drawing}
tool={{ type: 'ink', ink: 'pen', color: 0x1e88e5ff, width: 4 }}
maximumZoom={8}
onDrawingChange={(changed) => console.log(changed.strokeCount)}
/>
)
}

Without drawing, the canvas edits its own empty drawing, reachable as canvas.current.drawing. Without tool, it draws with a black 4 pt pen. maximumZoom defaults to 1, which disables pinch zoom.

2. Add the tool picker​

Keep the tool in state and share it between the canvas and the picker. Passing drawing to the picker shows its undo and redo buttons.

import { useState } from 'react'
import { Drawing, SketchCanvas, ToolPicker, type Tool } from 'nitro-ui-sketch'
import { View } from 'nitro-ui-renderer'

export function Notes() {
const [drawing] = useState(() => new Drawing())
const [tool, setTool] = useState<Tool>({ type: 'ink', ink: 'pen', color: 0x000000ff, width: 4 })
return (
<View layout={{ flex: 1 }}>
<SketchCanvas layout={{ flex: 1 }} drawing={drawing} tool={tool} />
<ToolPicker
tool={tool}
onToolChange={setTool}
drawing={drawing}
layout={{ position: 'absolute', left: 0, right: 0, bottom: 32 }}
/>
</View>
)
}

3. Close the options card on canvas touches​

PencilKit closes its width and colour card when you start drawing. Do the same by passing the picker's handle to the canvas's onTouchDown:

const picker = useRef<ToolPickerHandle | null>(null)

<SketchCanvas tool={tool} onTouchDown={() => picker.current?.dismissOptions()} layout={{ flex: 1 }} />
<ToolPicker ref={picker} tool={tool} onToolChange={setTool} />

4. Save and export​

const data = drawing.toData() // ArrayBuffer, compact binary
const restored = Drawing.fromData(data)

const png = await drawing.toImage({ format: 'png', quality: 100, scale: 2 })
const svg = await drawing.toSvg({})

See Save and export for the options.

Next steps​