Skip to main content

CanvasController

class CanvasController

Controls one mounted canvas: its drawing, tool, zoom and selection. Get it through SketchCanvas's ref:

const canvas = useRef<CanvasController>(null)
<SketchCanvas ref={canvas} layout={{ flex: 1 }} />
warning

Calls throw once the controller's canvas is unmounted.

Overview​

MemberKind
drawingProperty
drawingPolicyProperty
minimumZoom, maximumZoomProperties
contentSizeProperty
rulerActiveProperty
viewportRead-only
selectedStrokeCountRead-only
setInkingToolMethod
setEraserToolMethod
setLassoToolMethod
setViewportMethod
getSelectionMethod
setSelectionMethod
deleteSelectionMethod
duplicateSelectionMethod
setSelectionColorMethod
addOnSelectionChangeListenerListener
addOnViewportChangeListenerListener
addOnTouchDownListenerListener

The properties mirror SketchCanvas's props. If you also pass the prop, a later change of the prop overwrites the value you set here.

drawing​

drawing: Drawing

The drawing shown and edited by the canvas. Only drawings created by nitro-ui-sketch are accepted.

drawingPolicy​

drawingPolicy: DrawingPolicy

Which touches draw. Default 'anyInput'. See Drawing policy.

minimumZoom, maximumZoom​

minimumZoom: number
maximumZoom: number

Zoom limits, both default 1; a maximumZoom of 1 disables zooming. They must satisfy 0 < minimumZoom <= maximumZoom.

contentSize​

contentSize?: Size

Scrollable content size in points; undefined uses the view's own size. Must be finite and not negative.

rulerActive​

rulerActive: boolean

Shows the ruler, a straightedge over the canvas. Strokes started on it or near an edge follow that edge. Default false. See Ruler.

viewport​

readonly viewport: Viewport

The visible part of the content.

selectedStrokeCount​

readonly selectedStrokeCount: number

Number of selected strokes.

setInkingTool​

setInkingTool(ink: Ink): void

Draws with ink. Throws when ink.width is not above zero.

setEraserTool​

setEraserTool(mode: EraserMode, width: number): void

Erases with a circle of diameter width content points.

setLassoTool​

setLassoTool(): void

Selects strokes with a lasso.

setViewport​

setViewport(viewport: Viewport): void

Scrolls and zooms. The value is clamped to the zoom limits and the content size. Throws on non-finite values.

getSelection​

getSelection(): Stroke[]

The selected strokes, back to front.

setSelection​

setSelection(strokes: Stroke[]): void

Selects strokes of the canvas's drawing; others are ignored.

deleteSelection​

deleteSelection(): void

Removes the selected strokes as one undo step.

duplicateSelection​

duplicateSelection(): void

Copies the selection, offset by 16 points, and selects the copies.

setSelectionColor​

setSelectionColor(color: number): void

Recolours the selection (0xRRGGBBAA) as one undo step.

addOnSelectionChangeListener​

addOnSelectionChangeListener(listener: (selectedStrokeCount: number) => void): ListenerSubscription

Called when the number of selected strokes changes. SketchCanvas's onSelectionChange uses it.

addOnViewportChangeListener​

addOnViewportChangeListener(listener: (viewport: Viewport) => void): ListenerSubscription

Called when the viewport changes. SketchCanvas's onViewportChange uses it.

addOnTouchDownListener​

addOnTouchDownListener(listener: () => void): ListenerSubscription

Called when a finger or stylus first touches the canvas, whether it then draws, pans or moves the ruler. SketchCanvas's onTouchDown uses it.

Each listener method returns a ListenerSubscription; call remove() to stop listening.