ScribbleSVG

Document model

DiagramDocument shape, elements, and serialization

A diagram is a plain JSON object: DiagramDocument. Save it, load it, and both the editor and renderer produce the same scribble strokes because each element carries a deterministic seed.

Document

type DiagramDocument = {
  version: 1;
  elements: DiagramElement[];
  viewport: { x: number; y: number; zoom: number };
};

Use EMPTY_DOCUMENT from @scribblesvg/core as a starting point. Validate untrusted JSON with parseDiagramDocument / isDiagramDocument.

Element types

TypeRole
rectangleBox with optional label
circleEllipse/circle with optional label
cylinderDatabase-style cylinder
diamondDecision / flowchart diamond
iconCatalog icon keyed by iconId
textStandalone text
arrowConnector with arrowheads
lineStraight connector without heads
freedrawFreehand pencil stroke

All elements share id (UUID) and seed (Rough.js determinism). Shapes store geometry (x/y/width/height or cx/cy/radius) and optional text / fontSize. Standalone text elements may also store width / height synced from the editor’s real glyph metrics; when omitted, core falls back to measureTextSize.

Groups

Any element may carry an optional groupId. Elements that share one are a group: the editor selects, moves and scales them together.

groupId?: string; // shared by every member; omit when ungrouped

Groups are flat (no nesting), and a groupId held by only one element has no effect. Rendering ignores it, so grouping never changes how a document looks.

Connectors

Arrows and lines use canvas endpoints (startX/startY/endX/endY) plus optional bindings:

startBinding?: string; // element id, or omit for free-floating
endBinding?: string;

When bound, the editor keeps the endpoint attached to the target shape.

Freehand strokes

Pencil strokes store their points in canvas coordinates:

points: [number, number][]; // at least one; a single point is a dot

Core draws them as one smooth path: curved where the stroke flows, with sharp joins where it turns hard. Strokes have no label and connectors don't bind to them.

The points are stored as drawn, after the editor's smoothing, so a stroke renders the same everywhere and nothing is smoothed again at render time.

Icons

Icon elements store only iconId. Pass a matching icons catalog to DiagramCanvas / DiagramRenderer so artwork resolves. Unresolved ids render a dashed “Missing icon” placeholder.

Treat catalog SVG as trusted host input. Diagram JSON cannot carry SVG markup, so normal save/load is not an XSS path — do not put end-user-uploaded SVG strings into icons without sanitizing first.

Seeds

generateSeed() from core creates a new stroke seed. Keeping seeds stable across sessions is what makes the scribble look identical every time you render the same document.

Images

An image element stores a source URL or path, its box (x, y, width, height), intrinsic naturalWidth and naturalHeight, and optional alt text. Images can be bound to connectors.

On this page