Skip to main content

SketchCanvas

function SketchCanvas(props: SketchCanvasProps): ReactElement

A low-latency ink canvas with pens, erasers, lasso selection, zoom and undo. Touch input never reaches JavaScript; observe results through the callbacks or the CanvasController received through ref.

<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)}
/>

Props​

PropTypeDefaultDescription
toolToolBlack 4 pt penThe active tool.
drawingDrawingThe canvas's own drawingThe drawing to show and edit.
drawingPolicyDrawingPolicy'anyInput'Which touches draw.
minimumZoomnumber1Smallest zoom.
maximumZoomnumber1Largest zoom. 1 disables zooming.
contentSizeSizeThe view's own sizeScrollable content size in points.
rulerActivebooleanfalseShows the ruler.
onDrawingChange(drawing: Drawing) => voidCalled after each change of the shown drawing, including undo and redo.
onSelectionChange(selectedStrokeCount: number) => voidCalled when the number of selected strokes changes.
onViewportChange(viewport: Viewport) => voidCalled when the canvas zooms or scrolls.
onTouchDown() => voidCalled when a finger or stylus first touches the canvas, whether it then draws, pans or moves the ruler.
refRef<CanvasController>Receives the canvas's controller once the native view exists.
layout, style, other view propsNitroUI SharedViewPropsEverything a NitroUI view takes except children.

tool​

Changing tool switches the canvas's tool. See Inks and tools.

drawing​

Passing a new Drawing swaps the drawing the canvas shows and edits. Setting the prop back to undefined keeps the last drawing; it does not reset the canvas.

Behaviour​

  • With a stylus, other touches are ignored while it draws (palm rejection).
  • With drawingPolicy="stylusOnly", one finger pans; otherwise two fingers pan and pinch.
  • See Input, zoom and pan for zoom limits and the viewport.