Skip to content

Drawing ​

@adecore/drawing stores hand-drawn shapes, arrows, freehand strokes, text and sticky notes as plain JSON. It has the code an editor needs around that JSON: hit tests and resizing, paths for a painter, an SVG export, edits by id and a reading of the drawing as lines of text. It needs no DOM, so a backend exports a stored drawing the same way a page draws it.

tsx
import { readingOrder, toSvg } from '@adecore/drawing';
import type { DrawingRoughness } from '@adecore/drawing/protocol';
import { Segmented } from '@adecore/ui';
import { useMemo, useState } from 'react';
import { THEME_EDGE, THEME_PALETTE, THEME_PAPER } from '../shared/canvas-theme.ts';
import { SKETCH } from '../shared/sketch.ts';

type Roughness = 'architect' | 'artist' | 'cartoonist';

const ROUGHNESS: Record<Roughness, DrawingRoughness> = { architect: 0, artist: 1, cartoonist: 2 };

export default function DrawingExportDemo() {
    const [roughness, setRoughness] = useState<Roughness>('artist');
    const elements = useMemo(() => SKETCH.map((element) => ({ ...element, roughness: ROUGHNESS[roughness] })), [roughness]);
    const svg = toSvg(elements, { palette: THEME_PALETTE, paper: THEME_PAPER, edge: THEME_EDGE });

    return (
        <div className="flex w-full flex-col gap-4">
            <Segmented<Roughness>
                label="Roughness"
                value={roughness}
                onValueChange={setRoughness}
                options={[
                    { id: 'architect', label: 'Architect' },
                    { id: 'artist', label: 'Artist' },
                    { id: 'cartoonist', label: 'Cartoonist' }
                ]}
                className="self-start"
            />
            <div className="[&_svg]:h-auto [&_svg]:w-full" dangerouslySetInnerHTML={{ __html: svg }} />
            <ol className="flex flex-col gap-1 font-mono text-code text-text-muted">
                {readingOrder(elements).map((line) => (
                    <li key={line}>{line}</li>
                ))}
            </ol>
        </div>
    );
}

The demo passes one drawing to toSvg and readingOrder. Each shape stores a seed, so it wobbles the same way on every render.

What it does ​

  • Validates a drawing file with Zod schemas, on an entry point that loads nothing else.
  • Applies an edit by id from a second writer, such as an agent, all or nothing.
  • Rebases unsaved edits on a newer document and names the elements both sides changed.
  • Finds the element under the pointer, the elements in a marquee and the new box of a resize.
  • Gives the paths of one element for a canvas of your own, and writes a whole drawing as SVG.
  • Wraps and measures text, and turns a drawing into lines an agent can read.

What it leaves to the app ​

The package keeps no state and registers no listener. The app owns:

  • Storage and conflicts. A document carries a rev; the app compares it with the stored one on save and writes rev + 1.
  • Input: gestures, selection, undo, and the transform from screen pixels to world units.
  • Theme and fonts. A drawing names palette colors and fonts, and the app says what they are when it paints or exports.

@adecore/diagram builds on this package for graphs that are written rather than drawn.

Where to next ​

  • Getting started creates a drawing, changes it and exports it.
  • Documents describes the file and its seven element kinds.
  • Edits and rebasing covers writers other than the editor on screen.
  • Geometry covers bounds, hit tests, marquee selection and resizing.
  • Paths and SVG covers painting one element and exporting a whole drawing.
  • Text covers wrapping and measuring text.
  • Reading order turns a drawing into lines of text.
  • Reference lists the limits and every export.

The package is licensed under FSL-1.1-MIT.

Licenses and third-party notices are listed in each package.