@mocanvas/editor · class

ScribbleManager

declare class ScribbleManager extends EditorManager

The trails a laser pointer, an eraser or a select brush leaves behind.

A scribble is not a shape: it is not in the document, it is not undoable, and it disappears on its own. It lives on the instance record so that anything reading the store — the renderer, a minimap, a presence encoder — sees it without a separate channel, and so a collaborator's scribble arrives the same way a local one is made.

There are two ways in. The **session** API (startSession, addPointToSession, stopSession) is for a tool driving one continuous gesture: it owns a single scribble and does not have to carry its id around. The **direct** API (addScribble, addPoint, stop) is for anything managing several at once — five collaborators' lasers, for instance.

Nothing animates until tick is called; the editor's frame loop drives it, and a test can drive it by hand.


Methods · 16

addPoint(id: string, x: number, y: number, z?: number): ScribbleItem | undefined

Offer a point to a scribble. It is appended on the next tick, not now: a pointer move can fire many times per frame, and a scribble that grew on every one of them would be a different shape at 120Hz than at 60.

addPointToSession(x: number, y: number, z?: number): ScribbleItem | undefined

Add a point to the current session. A no-op with no session.

addScribble(options?: ScribbleSessionOptions, id?: string): ScribbleItem

Start a new scribble and return it. Its id addresses it afterwards.

addScribbleToSession(options?: ScribbleSessionOptions): ScribbleItem

Add a *second* scribble that shares the session's lifetime.

A laser drawn with two fingers, or a gesture that leaves both a trail and a highlight, needs more than one stroke to end together. Everything started this way is cleared by clearSession.

clearSession(): void

End the session and remove every scribble immediately, fade included.

complete(id: string): void

Remove a scribble immediately, without fading it.

constructor(editor: Editor)
extendSession(options?: ScribbleSessionOptions): ScribbleItem | undefined

Keep the session open but restart its stroke — the gesture continues, the trail behind it does not. Used when a drag crosses a boundary that should break the line.

getItems(): ScribbleItem[]

Every live scribble, in the order they were started.

hasPendingWork(): boolean

Whether anything here still needs frames.

A host's frame loop asks this to decide whether to schedule another one. It is deliberately "is there a scribble at all" rather than "is there anything visible to redraw": a point offered through addPoint is held in next and writes nothing to the store, so a loop that parked itself because the picture had settled would never wake up to commit it, and the trail would stop dead under a moving pointer.

isSessionActive(): boolean

Whether startSession has run and the session is still open.

reset(): void

Drop every scribble at once.

startSession(options?: ScribbleSessionOptions): ScribbleItem

Begin the one scribble this manager will treat as current. An existing session is stopped first, so a tool that forgot to close one cannot leak it.

stop(id: string): ScribbleItem | undefined

Ask a scribble to finish: it stops growing and fades out.

stopSession(): void

Let the session's scribble fade out, and close the session.

tick(elapsed: number): void

Advance every scribble by elapsed milliseconds: append the point offered since the last tick, burn off the delay, and shed points from the tail once it has run out. Scribbles that have shed everything are removed.

mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.