@mocanvas/editor · class
declare class ScribbleManager extends EditorManagerThe 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 | undefinedOffer 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 | undefinedAdd a point to the current session. A no-op with no session.
addScribble(options?: ScribbleSessionOptions, id?: string): ScribbleItemStart a new scribble and return it. Its id addresses it afterwards.
addScribbleToSession(options?: ScribbleSessionOptions): ScribbleItemAdd 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(): voidEnd the session and remove every scribble immediately, fade included.
complete(id: string): voidRemove a scribble immediately, without fading it.
constructor(editor: Editor)extendSession(options?: ScribbleSessionOptions): ScribbleItem | undefinedKeep 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(): booleanWhether 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(): booleanWhether startSession has run and the session is still open.
reset(): voidDrop every scribble at once.
startSession(options?: ScribbleSessionOptions): ScribbleItemBegin 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 | undefinedAsk a scribble to finish: it stops growing and fades out.
stopSession(): voidLet the session's scribble fade out, and close the session.
tick(elapsed: number): voidAdvance 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.