@nextgensoftwares/folio-canvas
The scene engine of Folio's boards (lecture boards and whiteboards): one model of pages and objects, field-level events with exact inverses, per-user undo, recording and playback, Canvas 2D rendering with culling, SVG export through the same painters, and the selection maths app UIs use. DOM-free: it runs in workers, servers and exporters. Browser input is folio-canvas-board; collaboration is folio-canvas-collab. Guide: Boards.
ts
import { CanvasStore, createHistory, pageToSvg } from '@nextgensoftwares/folio-canvas';
const store = new CanvasStore({ version: 2, pages: [{ id: 'p1', width: 1600, height: 1000, objects: [] }] });
const history = createHistory(store);
history.commit({ type: 'create', page: 'p1', objects: [{ id: 'n1', kind: 'sticky', x: 80, y: 80, w: 200, h: 200, z: 'a0', color: '#ffe066', text: 'Hi' }] });
const svg = pageToSvg(store.page()!);Model
| Export | Description |
|---|---|
CanvasDocument, CanvasPage, PageBackground | pages (bounded with width × height, or an infinite canvas), each with its objects, ruling (blank, grid, lines, dots) and paper colour |
BaseObject, AnyObject, CanvasObject | every object has id, kind, a box (x, y, w, h, rotation radians), a stacking key z, locked, author; hosts add kinds |
StrokeObject, StickyObject, ShapeObject, ShapeType, TextObject, TextProps, TextAlign, VerticalAlign, ConnectorObject, ConnectorEnd, Arrowhead, FrameObject, ImageObject | the built-in kinds. Shapes take any ECMA-376 preset with adjust values av; strokes store points relative to their box (ow × oh) |
Camera, Viewport, Bounds, Pt | screen = (page − camera) × zoom |
keyBetween(a, b), keysBetween(a, b, n), isValidKey(k), compareZ(a, b) | fractional stacking keys: a key between any two, short however many objects go on top; ties break by id |
newId(prefix?) | collision-resistant ids |
Events, store and history
| Export | Description |
|---|---|
CanvasDocEvent, ObjectPatch | create, update (field patches; null removes), delete, batch, page-add, page-remove, page-update |
CanvasLiveEvent, CanvasViewEvent, CanvasEvent, isDocEvent, isLiveEvent | strokes being drawn (live-*), page and camera views, laser trails, cursor / cursor-leave |
CanvasStore, CanvasOrigin, CanvasListener, LiveStroke, Trail, Cursor, TRAIL_POINTS | the scene state: apply(event, origin) returns the exact inverse; listeners hear every event with its origin (local, remote, replay) |
applyDoc(store, event), patchObject(object, patch) | apply one document event; apply a field patch |
PageState, SpatialGrid, Resolve, SOLID_KINDS | one page's objects by id, in z order and in a spatial grid (query(bounds)); a page's resolve also finds the solid objects (SOLID_KINDS: shapes, notes, text, pictures) whose box meets an area (near, from a grid of its own), which elbow connectors route round |
createHistory(store, opts?), CanvasHistory, CommitOptions | per-user undo: only this client's commits; merge folds a drag into one step; steps a peer already undid are skipped |
Geometry and objects
| Export | Description |
|---|---|
emptyBounds, isEmptyBounds, unionBounds, intersects, contains, padBounds, rectBounds, boundsOfPoints | bounds helpers |
center, corners, boxBounds, inBox, localToPage, pageToLocal, edgeToward, sideToward | rotated boxes |
objectBounds, hitObject, frameContents, contentBounds | painted bounds, hit-testing with slack, what a frame carries, a page's content bounds |
strokeFromPagePoints, strokePagePoints, strokeScale | strokes between page points and box-relative points |
connectorPath, connectorBounds, ConnectorPath, sampleCubic, pointAlong | routed connectors (straight, elbow, curve; on a page elbows go round the objects they join and solid ones in their way: shapes, notes, text, pictures), their bounds, points along them |
Camera
| Export | Description |
|---|---|
ZoomLimits, DEFAULT_ZOOM_LIMITS, identityCamera, sameCamera | zoom bounds (5%–3200%) |
toPage, toScreen, visibleBounds, zoomAt, panBy, pinch, fitBounds, constrainCamera, lerpCamera | screen ⇄ page, zoom about a point, two-finger pinch, fit, keep a bounded page reachable, animate |
Recording
| Export | Description |
|---|---|
recordStore(store, opts?), CanvasRecorder, CanvasRecording, TimedEvent, drawDuration | record every document and view change with timing (strokes stamped when drawing began) |
CanvasPlayer | play a recording into a store: strokes grow as they were written; seek restores the nearest keyframe |
Rendering
| Export | Description |
|---|---|
renderScene(ctx, options), SceneOptions, SceneStats, sceneEnv | a page as the camera sees it: off-page area, paper, ruling, culled objects, live strokes, trails, selection, cursors; hidden objects are skipped and textless ones painted without text; area paints only what meets part of the view (the caller clips the rest) |
paintObject, DEFAULT_PAINTERS, Painter, PaintEnv, paintStroke, fillOutline, paintBackground | per-kind painters (hosts add kinds) |
paintLive, paintSelection, paintCursors, paintHandles, paintGuides, paintMarquee, paintConnectTarget, paintConnectorEnds, paintConnectHandles | overlays, a constant size on screen (a selected connector is highlighted along its line) |
paintTrails(trails, env, style?), LaserStyle, DEFAULT_LASER | laser trails as a comet: one tapered ribbon with a hot core and a glow, each point living tail ms (renderScene's laser option) |
Ctx, CanvasTheme, LIGHT, DARK | the canvas surface painters need; colours (paper, grid, ink, accent; a display transform for dark paper) |
traceShape, textInset, isPresetShape, loadPresetShapes, onPresetShapesLoaded, presetShapesLoaded, presetOf | native outlines; the Office preset library (its own chunk), validated per shape |
TextEngine, TextBlock, layoutText, fitText, fitTextSize, paintText, paragraphDir | text through Folio's layout (bidi, Arabic shaping, the measured faces); stickies shrink to fit; tiny text draws as bars |
textFrame(o, engine?), TextFrame, hasText, TEXT_BASE, paintObjectText, isLightColor | where an object's text goes (box, starting size, shrink, alignment; auto-width text measured), shared by the painters and the board's text editor; text on a sticky or a light fill keeps its colour on dark paper |
pageToSvg(page, options?), SvgExportOptions, SvgContext, SvgContextOptions | a page as SVG through the same painters |
Interaction
| Export | Description |
|---|---|
SelectionFrame, selectionFrame, HandleId, handlePoints, hitHandle, ROTATE_OFFSET, inFrame | selection frames and their resize and rotate handles |
movingSet, movePatches, resizePatches, rotatePatches, pointerAngle, normalizeAngle | move (frames carry their contents), resize in rotated axes or as a group, rotate about the centre |
alignPatches(page, ids, mode), distributePatches(page, ids, axis), expandGroups(page, ids), AlignMode | line up or evenly space a selection (visual boxes; a group as one; a lone object on a bounded page lines up with the page; frames carry their contents); ids grown to whole groups (objects sharing group) |
objectAt, objectsIn, snapTargetsNear | the topmost object under a point (pick limits which count), marquee selection, smart-guide targets |
snapMove, snapAngle, resizeFrame, Guide, GapGuide, Handle, SnapTargets | from folio-interaction |
EraseMode, eraseEvent, strokesAlong | erase whole strokes or only what the eraser touched |
connectTarget(page, x, y, tol, snap?), ConnectTarget, SIDE_ANCHORS, canConnect, connectorEnds, hitConnectorEnd, connectHandles(o, gap) | where a connector end dropped at a point attaches (an object, pinned to a side centre within snap, else its nearest edge; or a free point); a connector's drawn ends and which one is under a point; where a selected object's connect handles sit |