# Drawings

> 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](../specification/the-document-format.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](/files/attachments/01a13ff9-1810-7049-a315-f5155b75d737/drawing.schema.json) is its JSON Schema. (Markdown writes a drawing's scene in an older text form, the [data 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.

## Compatibility

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.

## A scene

```json
{
  "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 |  |
| --- | --- |
| `elements` | The elements, in stacking order: later ones are drawn on top. |
| `background` | The canvas colour (default `#ffffff`; renderers in a dark theme may ignore it). |
| `height` | 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](../specification/the-document-format.md)).

## Elements

Every element has:

| Field | Type |  |
| --- | --- | --- |
| `id` | string | Unique in the scene. |
| `type` | string | See below. |
| `x`, `y`, `w`, `h` | number | Its box: top-left corner, width, height. Lines and strokes: the origin their points are relative to, and their extent. |
| `stroke` | string | Outline colour: a CSS colour, or `auto` for the theme's ink (near-black on light, near-white on dark). |
| `fill` | string | Fill colour, or `none`. |
| `strokeWidth` | number | Outline width (the editor offers 2, 4 and 8). |
| `strokeStyle` | string | `solid`, `dashed` or `dotted`. |
| `opacity` | number | 0 to 1. |
| `rounded` | boolean | Rounded corners. |

And may have:

| Field | Type | Default |  |
| --- | --- | --- | --- |
| `text` | string | none | The label: centred in a closed shape, at the middle of a line. As the user typed it. |
| `wrapped` | string | `text` | The text as the editor laid it out, with the line breaks of wrapping, when wrapping broke a line. Renderers draw this (else `text`) line by line, splitting on `\n` only, without measuring: line *n* sits at `fontSize × 1.2 × n` from the first. Editors that change an element's text or size wrap it again. |
| `fontSize` | number | 16 | The editor always writes it; 16 is what renderers fall back on for labels. |
| `fontFamily` | string | `sans` | `sans` (Inter), `mono` (JetBrains Mono) or `serif` (Georgia). Renderers without the font use the platform's sans-serif, monospace or serif face; since text isn't measured, only the glyphs' widths change. |
| `weight` | number | 400 | Font weight. |
| `align`, `valign` | string | `center`, `middle` | The label's alignment inside a shape: `left`/`center`/`right`, `top`/`middle`/`bottom`. |
| `angle` | number | 0 | Rotation in radians about the box's centre. Lines and freehand strokes have it baked into their points instead. |
| `fillStyle` | string | `solid` | `solid`, `hachure` or `cross-hatch`. |
| `roughness` | 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: `drawing_svg_rough.go`, rough.js's algorithm with mulberry32 seeded by the FNV-1a hash of the id). |
| `groupIds` | string\[\] | none | The groups it's in, innermost first. |
| `locked` | boolean | false | Can't be selected by clicking. |
| `key` | string | none | Diagram blocks: the part of the diagram text it comes from (`n:<name>`, `e:<from>→<to>#<k>`, …). |

### Shapes

`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 |
| --- | --- | --- |
| `uml-class` | A box split into sections by horizontal lines: the name (its lines × `lh` + 2 `pad`), attributes and operations (their lines × `lh` + 2 `pad`, or 2 `pad` + 4 when empty); the last section takes any height left over | `cells`: the name centred, bold (600), italic when `abstract` or the name is `«abstract»`; the others left and top |
| `uml-package` | A tab on the top left (`max(0.4 w, min(40, w))` wide, `lh + 8` tall) over a box below it | Below the tab |
| `uml-note` | A box with its top-right corner folded (`min(16, w/4, h/4)`) | The box |
| `uml-component` | A box with the component icon at its top right: an 18 × 22 rectangle at (`w − 24`, 8), and two 12 × 5 tabs at (`w − 30`, 12) and (`w − 30`, 21) | The box |
| `uml-actor` | A stick figure filling the box above a label band (`lh + 12`): head of radius 0.12 of the figure's height, arms at 0.36, hips at 0.62 | Centred in the label band, which may reach 50 past each side |
| `uml-boundary` | A box | The box |
| `act-initial` | A filled ellipse |  |
| `act-final` | An ellipse, with a filled ellipse inside it inset by `0.22 × min(w, h)` |  |
| `act-flowfinal` | An ellipse with an X across it (corner to corner at 45°) |  |
| `act-bar` | A filled box (a fork or join bar) |  |
| `act-pool` | A box with a header row (`lh + 14`) and vertical lines between lanes; lane widths are `laneSizes` scaled to `w` (equal when absent or of the wrong count) | `cells`: lane names, centred in the header, bold |
| `seq-lifeline` | A head (a box `w` × 40, or a stick figure 80 tall when `head` is `actor`), and a dashed line down the middle from the head to `h`, broken at `gaps` | The head |
| `seq-activation` | A box filled with the paper colour |  |
| `seq-fragment` | A box with a pentagon tab at its top left (`max(44, len(operator) × fs × 0.62 + 22)` wide, `lh + 10` tall, its corner cut by 8), and dashed lines across at `dividers` | `cells`: the operator in the tab (bold), the first guard right of it, the others just below their dividers |
| `group` | A rounded box (radius `min(12, w/4, h/4)`) | At its top, inset by 6 |
| `cylinder` | Caps of height `min(14, 0.12 h, 0.2 w)`: the top ellipse in full, the bottom one's front half | Between the caps |
| `queue` | A cylinder on its side: caps of width `min(14, 0.12 w, 0.2 h)` | Between the caps |
| `cloud` | Six bumps through fixed points of the box (`cloudBumps` in the reference) | The middle 66% × 54% |
| `document` | A box whose bottom edge is a wave of height `min(10, 0.15 h)` | Above the wave |

Some have fields of their own:

| Field | On |  |
| --- | --- | --- |
| `cells` | class, fragment, pool | Text cells instead of `text`: a class's name, attributes and operations; a fragment's operator and guards; a pool's lane names. |
| `abstract` | class | The name in italics. |
| `dividers` | fragment | Where its operands are separated: y offsets from the top. |
| `laneSizes` | pool | The lanes' widths (scaled to the pool when they don't add up). |
| `head` | lifeline | `box` (default) or `actor`. |
| `gaps` | lifeline | Where its dashed line breaks: from/to pairs of y offsets from the top. |

### Lines and arrows

`line` and `arrow`:

| Field | Type | Default |  |
| --- | --- | --- | --- |
| `points` | \[x, y\]\[\] |  | The path, relative to `x`/`y`. Renderers draw these: bound ends are already resolved. |
| `start`, `end` | binding or null | none | What each end is attached to. |
| `route` | string | `straight` | `straight`, `curved` (through the points) or `elbow` (right angles). |
| `startArrowhead`, `endArrowhead` | string | see below | `none`, `arrow`, `bar`, `dot`, `circle_outline`, `triangle`, `triangle_outline`, `diamond`, `diamond_outline`. Absent: none, except an arrow's end, which is `triangle`. |
| `startLabel`, `endLabel` | string | none | Small labels next to each end (multiplicities, roles). |
| `labelAt` | \[x, y\] | the path's middle | Where the label's centre sits, relative to `x`/`y`. |
| `selfLoop` | boolean | false | A sequence message to its own lifeline. |
| `routed` | 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.

### Freehand

`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

`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.

### Images

`image`: `src` is an uploaded file (`/files/attachments/…`) or a `data:` URL, drawn into its box.

## Diagram blocks

A `diagram` block's scene is laid out from its `source` text ([diagram-language.md](../specification/the-diagram-language.md)). Its elements have `key`s 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.

## The data form

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`).
