Skip to main content

Drawing

class Drawing

An ordered list of strokes with undo history. Works without a canvas: build strokes, serialize them and export them headless.

warning

Mutate a drawing on the JavaScript thread only.

Overview​

MemberKind
constructorConstructor
fromDataStatic
strokeCountRead-only
boundsRead-only
canUndo, canRedoRead-only
getStrokesMethod
addStrokeMethod
removeStrokeMethod
clearMethod
undoMethod
redoMethod
copyMethod
toDataMethod
toImageAsync
toSvgAsync
toPdfAsync
addOnChangeListenerListener

constructor​

new Drawing()

Creates an empty drawing.

fromData​

static fromData(data: ArrayBuffer): Drawing

Restores a drawing from toData output. Throws on invalid, truncated or unsupported data.

strokeCount​

readonly strokeCount: number

Number of strokes.

bounds​

readonly bounds: Rect

Union of all stroke bounds; a zero rect when the drawing is empty.

canUndo, canRedo​

readonly canUndo: boolean
readonly canRedo: boolean

Whether undo or redo would change the drawing.

getStrokes​

getStrokes(): Stroke[]

The strokes, from back to front.

addStroke​

addStroke(ink: Ink, points: StrokePoint[]): Stroke

Appends a stroke and returns it. Throws when points is empty or holds non-finite values.

removeStroke​

removeStroke(stroke: Stroke): boolean

Removes a stroke. Returns false if it is not in this drawing.

clear​

clear(): void

Removes every stroke as one undo step.

undo​

undo(): boolean

Reverts the last change. Returns false when there is nothing to undo. Keeps 100 steps.

redo​

redo(): boolean

Reapplies the last undone change. Returns false when there is nothing to redo.

copy​

copy(): Drawing

Returns a drawing with the same strokes, an empty history and no listeners.

toData​

toData(): ArrayBuffer

Serializes the strokes into a compact binary format. Stroke ids are not saved.

toImage​

toImage(options: ImageExportOptions): Promise<ArrayBuffer>

Renders the strokes to PNG or JPEG bytes off the JavaScript thread. See ImageExportOptions.

toSvg​

toSvg(options: VectorExportOptions): Promise<string>

Renders the strokes to an SVG document off the JavaScript thread. See VectorExportOptions.

toPdf​

toPdf(options: VectorExportOptions): Promise<ArrayBuffer>

Renders the strokes to a one-page PDF off the JavaScript thread.

addOnChangeListener​

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

Called after each change, including undo and redo. Returns a ListenerSubscription.

useLayoutEffect(() => {
const subscription = drawing.addOnChangeListener(() => setCount(drawing.strokeCount))
return () => subscription.remove()
}, [drawing])