The scene a drawing holds: shapes, arrows, freehand strokes, text and images
What a drawing block holds, for renderers and editors of drawings.
A drawing block (document.md) holds a scene: the shapes, arrows, freehand strokes, text and images on its canvas. A diagram block holds one too, laid out from its text. The scene is a JSON object in the block's scene field; schema/drawing.schema.json is its JSON Schema. (Markdown writes a drawing's scene in an older text form, the data form.)
The editor (@kribble/drawing), the server's SVG renderer (backend/internal/format/render/drawing_svg*.go) and every other reader draw the same scene the same way: the geometry is stored, not computed, so a renderer only has to draw what's there. kribble drawings FILE gives the reference SVG for every drawing and diagram in a note.
Scenes are stored for good, so this format only grows:
Every field added after the first version is optional, and absent means what renderers did before it existed (the defaults below).
A field is never renamed, and an existing value never changes meaning.
Readers ignore fields and element types they don't know.
{
"elements": [
{ "id": "r1", "type": "rect", "x": 20, "y": 20, "w": 160, "h": 80,
"stroke": "auto", "fill": "none", "strokeWidth": 2, "strokeStyle": "solid", "opacity": 1, "rounded": true,
"text": "Start" },
{ "id": "a1", "type": "arrow", "x": 180, "y": 60, "w": 120, "h": 0,
"stroke": "auto", "fill": "none", "strokeWidth": 2, "strokeStyle": "solid", "opacity": 1, "rounded": true,
"points": [[0, 0], [120, 0]],
"start": { "elementId": "r1", "focus": { "x": 1, "y": 0.5 }, "bindCenter": { "x": 100, "y": 60 }, "gap": 6 },
"endArrowhead": "arrow" }
],
"background": "#ffffff"
}
Field | |
|---|---|
| The elements, in stacking order: later ones are drawn on top. |
| The canvas colour (default |
| The block's height in the note, in CSS pixels (absent: the editor's default). |
Coordinates are in canvas units (CSS pixels at 100%), y pointing down. In the canonical form, a scene's object keys are sorted (document.md, Canonical form).
Every element has:
Field | Type | |
|---|---|---|
| string | Unique in the scene. |
| string | See below. |
| number | Its box: top-left corner, width, height. Lines and strokes: the origin their points are relative to, and their extent. |
| string | Outline colour: a CSS colour, or |
| string | Fill colour, or |
| number | Outline width (the editor offers 2, 4 and 8). |
| string |
|
| number | 0 to 1. |
| boolean | Rounded corners. |
And may have:
Field | Type | Default | |
|---|---|---|---|
| string | none | The label: centred in a closed shape, at the middle of a line. As the user typed it. |
| string |
| The text as the editor laid it out, with the line breaks of wrapping, when wrapping broke a line. Renderers draw this (else |
| number | 16 | The editor always writes it; 16 is what renderers fall back on for labels. |
| string |
|
|
| number | 400 | Font weight. |
| string |
| The label's alignment inside a shape: |
| number | 0 | Rotation in radians about the box's centre. Lines and freehand strokes have it baked into their points instead. |
| string |
|
|
| number | 0 | 0 clean, 1 hand-drawn, 2 very sketchy. The wobble is seeded from the element's id, so every renderer draws the same strokes (the reference: |
| string[] | none | The groups it's in, innermost first. |
| boolean | false | Can't be selected by clicking. |
| string | none | Diagram blocks: the part of the diagram text it comes from ( |
rect, ellipse and diamond are boxes with an outline. The diagram shapes are boxes too. Below, w and h are the element's size, fs its fontSize (default 16, 14 for a fragment), lh a line's height (fs × 1.2), and pad 6. The reference geometry is drawing_svg_diagrams.go and drawing_svg_language.go (the same formulas as @kribble/drawing's diagram-shapes.ts).
Shape | Outline | Text |
|---|---|---|
| A box split into sections by horizontal lines: the name (its lines × |
|
| A tab on the top left ( | Below the tab |
| A box with its top-right corner folded ( | The box |
| A box with the component icon at its top right: an 18 × 22 rectangle at ( | The box |
| A stick figure filling the box above a label band ( | Centred in the label band, which may reach 50 past each side |
| A box | The box |
| A filled ellipse | |
| An ellipse, with a filled ellipse inside it inset by | |
| An ellipse with an X across it (corner to corner at 45°) | |
| A filled box (a fork or join bar) | |
| A box with a header row ( |
|
| A head (a box | The head |
| A box filled with the paper colour | |
| A box with a pentagon tab at its top left ( |
|
| A rounded box (radius | At its top, inset by 6 |
| Caps of height | Between the caps |
| A cylinder on its side: caps of width | Between the caps |
| Six bumps through fixed points of the box ( | The middle 66% × 54% |
| A box whose bottom edge is a wave of height | Above the wave |
Some have fields of their own:
Field | On | |
|---|---|---|
| class, fragment, pool | Text cells instead of |
| class | The name in italics. |
| fragment | Where its operands are separated: y offsets from the top. |
| pool | The lanes' widths (scaled to the pool when they don't add up). |
| lifeline |
|
| lifeline | Where its dashed line breaks: from/to pairs of y offsets from the top. |
line and arrow:
Field | Type | Default | |
|---|---|---|---|
| [x, y][] | The path, relative to | |
| binding or null | none | What each end is attached to. |
| string |
|
|
| string | see below |
|
| string | none | Small labels next to each end (multiplicities, roles). |
| [x, y] | the path's middle | Where the label's centre sits, relative to |
| boolean | false | A sequence message to its own lifeline. |
| boolean | false | Laid out by a diagram's router: the points are the route. |
A binding is { elementId, focus: {x, y}, bindCenter: {x, y}, gap?, dy? }: the element the end is on, the point aimed at as a fraction of the element's box (0,0 top-left, 1,1 bottom-right), and the element's centre when it was bound. With gap (current editors), the end sits that far outside the outline and the stored points are already resolved. Without (older scenes), renderers keep the end at the focus point while the element is where it was, and on its outline once it moved. dy puts the end on a lifeline's dashed line, that far below its top.
draw: points are [x, y] or [x, y, pressure] (pressure 0 to 1). pen is the outline algorithm: absent for the original, 2 for the current one (velocity pressure, round caps, smoothed).
text: a text box. text, fontSize, fontFamily, align and weight are required. autoResize (default true) false means the text wraps at w instead of the box growing with it.
image: src is an uploaded file (../_assets/…<-code>) or a data: URL, drawn into its box.
A diagram block's scene is laid out from its source text (diagram-language.md). Its elements have keys that tie them to the text, so the layout survives edits: an element moved by hand keeps its place (the block's layout.positions) when the text changes elsewhere. Elements without a key were drawn by hand next to the diagram.
A diagram read from markdown has an empty scene, since markdown keeps only its text. Editors lay it out again with the diagram engine (@kribble/diagram, or Go's internal/diagram); readers without the engine can have the CLI do it (kribble convert --to json --layout, kribble drawings), or show the text.
Markdown writes a drawing's scene as one string, ::drawing{drawingId="…" data="…"}, and so did documents before the format's version 2. It's the scene in an older shape:
elements in any order, each with z (stacking order, higher on top) and v (a version counter);
meta: background, height, nextZ (the z the next element gets) and, for a diagram, diagram: src (the text it was laid out from) and pos (the positions of moved boxes);
an element's text as laid out (with wrapping's line breaks) and rawText as typed, instead of text and wrapped.
From the data form to a scene: elements sorted by z (ties keep their order), without z and v; text is rawText when there is one, and wrapped the old text when that differs; meta.background and meta.height move onto the scene. Back: z is the element's position from 1, v 1, meta.nextZ the count plus one. An empty string is an empty scene. The reference is kdoc.SceneFromData and SceneToData (and sceneFromData, sceneToData in @kribble/document).