@mocanvas/mocanvas 1.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,11 +5,96 @@ tools and UI, on top of a Rust + WebAssembly engine that does geometry,
5
5
  hit-testing, culling and tessellation and writes GPU buffers straight into
6
6
  linear memory.
7
7
 
8
- - Repository, docs and issues: <https://github.com/SYMBIO/mocanvas>
9
- - Benchmarks against the library it is shaped after:
10
- [docs/BENCHMARK.md](https://github.com/SYMBIO/mocanvas/blob/main/docs/BENCHMARK.md)
11
- - Coming from tldraw:
12
- [docs/MIGRATION.md](https://github.com/SYMBIO/mocanvas/blob/main/docs/MIGRATION.md)
8
+ ## Why this exists
9
+
10
+ We built products on [tldraw](https://tldraw.dev), and it is a genuinely
11
+ excellent library the API model in this package is shaped after it because we
12
+ think they got that design right. What we ran out of was headroom: our canvases
13
+ grew past the point where a DOM-and-SVG renderer kept up, and the work we needed
14
+ to move off the main thread had nowhere to go.
15
+
16
+ So mocanvas is that same shape of API with a different engine underneath, and it
17
+ is built so you can come across without rewriting your app:
18
+
19
+ - **The API is the one you know.** `Editor`, `ShapeUtil`, `StateNode`, records
20
+ with `shape:` / `page:` ids, `createShapes`, `zoomToFit`, `.tldr` files.
21
+ - **Every symbol tldraw's reference documents exists here under the same name.**
22
+ 1,420 of 1,420 — see *Compatibility with tldraw* below for what that does and
23
+ does not promise.
24
+ - **`@mocanvas/compat` gives you the `TL`-prefixed spellings**, so a migration
25
+ can change import paths first and rename later. `MIGRATION.md`, shipped in
26
+ this package, walks through it.
27
+
28
+ ## Performance
29
+
30
+ The engine is the point. Geometry, hit-testing, spatial indexing, culling and
31
+ tessellation run in WebAssembly and write GPU vertex buffers straight into
32
+ linear memory; the renderer is WebGL2 today and WebGPU behind the same
33
+ interface next.
34
+
35
+ Creating shapes, median milliseconds — lower is better:
36
+
37
+ | Scene | mocanvas | tldraw 5.4 | |
38
+ | :--- | ---: | ---: | ---: |
39
+ | 1,000 geo shapes | 15.1 ms | 37.8 ms | **2.5× faster** |
40
+ | 5,000 geo shapes | 40.1 ms | 120.0 ms | **3.0× faster** |
41
+ | 20,000 geo shapes | 134.2 ms | 433.2 ms | **3.2× faster** |
42
+ | 1,000 mixed | 25.7 ms | 76.0 ms | **3.0× faster** |
43
+ | 5,000 mixed | 67.8 ms | 241.5 ms | **3.6× faster** |
44
+ | 20,000 mixed | 249.4 ms | 753.7 ms | **3.0× faster** |
45
+
46
+ Interaction at scale, dragging a selection, median milliseconds per frame:
47
+
48
+ | Scene | mocanvas | tldraw 5.4 | |
49
+ | :--- | ---: | ---: | ---: |
50
+ | 1,000 geo shapes | 33.0 ms | 25.1 ms | tldraw ahead |
51
+ | 5,000 geo shapes | 116.6 ms | 150.0 ms | **1.3× faster** |
52
+ | 20,000 geo shapes | 400.0 ms | 858.4 ms | **2.1× faster** |
53
+ | 20,000 mixed | 841.6 ms | 1,124.9 ms | **1.3× faster** |
54
+
55
+ The shape of it is that mocanvas scales: it wins by ~3× on building a scene at
56
+ every size, and the interaction gap opens as the scene grows. On small scenes
57
+ tldraw's renderer is ahead.
58
+
59
+ **Read these numbers with their caveats.** They were measured on 2026-09-04 on an
60
+ Apple M3 Pro in headless Chromium **with no hardware GPU** — Chromium fell back
61
+ to software rasterisation, which penalises mocanvas's WebGL2 renderer far more
62
+ than it penalises tldraw's DOM one, so the interaction numbers above are close to
63
+ a worst case for mocanvas. Both libraries ran on default settings, and this is
64
+ our own benchmark of our own library. `BENCHMARK.md`, shipped in this package,
65
+ has the method, the full tables and a longer list of caveats — including a
66
+ rendering-fidelity comparison against tldraw at 99.2% interior IoU.
67
+
68
+ ## Compatibility with tldraw
69
+
70
+ The API is shaped after **tldraw 5.4**, and every symbol its public reference
71
+ documents exists here under the same name — 1,420 of 1,420, across `tldraw`,
72
+ `@tldraw/editor`, `@tldraw/tlschema`, `@tldraw/store`, `@tldraw/state`,
73
+ `@tldraw/state-react` and `@tldraw/validate`. `@mocanvas/compat` carries the
74
+ `TL`-prefixed spellings so an existing codebase can switch imports first and
75
+ rename later.
76
+
77
+ Names are not behaviour, and the honest number is lower: across the symbols both
78
+ libraries share, 86.4% of `@tldraw/editor`'s members are present. `Vec`, `Box`
79
+ and `Mat` are complete; `Editor` has 304 of its 313 members and `ShapeUtil` 52 of
80
+ 77, the gap there being mostly the `can*` predicates.
81
+
82
+ Four documented packages are not implemented. `@tldraw/mermaid` and
83
+ `@tldraw/driver` are wanted and unbuilt. `@tldraw/sync` and `@tldraw/sync-core`
84
+ are a deliberate difference: mocanvas has multiplayer — `@mocanvas/sync`, with
85
+ its own transport and a field-level CRDT — but not tldraw's sync protocol, so an
86
+ app keeping its existing tldraw sync *server* would have to move. Licensing and
87
+ watermark checks, `useSyncDemo` and the asset CDN defaults are tied to services
88
+ tldraw runs and are not coming; each has a seam to supply your own.
89
+
90
+ `COMPAT.md` in this package has the measured breakdown and names what is missing.
91
+
92
+ ## Guides, shipped in this package
93
+
94
+ `COMPAT.md` for what exists of the tldraw API, `MIGRATION.md` for moving an app
95
+ across, `CUSTOM_SHAPES.md` for writing a shape util and its tool, `BENCHMARK.md`
96
+ for the performance and fidelity method, `ARCHITECTURE.md` for how the engine
97
+ works, and `CLEAN_ROOM.md` for how this library was built.
13
98
 
14
99
  ## Install
15
100
 
@@ -109,6 +194,37 @@ Two optional notes:
109
194
  | `@mocanvas/sync` | multiplayer: record diffs, presence, cursors |
110
195
  | `@mocanvas/compat` | `TL`-prefixed aliases for a tldraw migration |
111
196
 
197
+ ## Clean room
198
+
199
+ **mocanvas has never been built by reading tldraw's source** — not the
200
+ repository, not a copy in `node_modules`, not a fork. It is written from first
201
+ principles against the public API reference and guides on tldraw.dev and sample
202
+ `.tldr` documents, and its compatibility is *measured* against that same
203
+ reference. Names are not copyrightable expression (*Google v. Oracle*, 2021);
204
+ the implementation behind them is our own original work. `CLEAN_ROOM.md` in
205
+ this package is the full policy.
206
+
207
+ mocanvas is not affiliated with or endorsed by tldraw.
208
+
112
209
  ## License
113
210
 
114
- MIT
211
+ **Source-available, not open source.** Free to use for:
212
+
213
+ - personal, non-commercial projects;
214
+ - non-profit organisations;
215
+ - development, evaluation, testing and staging — including inside a for-profit
216
+ company, so you can try it and build against it before committing;
217
+ - teaching and academic research.
218
+
219
+ **Shipping it in a commercial product, service or website needs a written
220
+ agreement with us.** That includes anything sold, anything that earns revenue
221
+ directly or through advertising, and internal tools running a for-profit
222
+ business.
223
+
224
+ To arrange one, or if you are unsure which side of the line you are on, write to
225
+ **mocanvas@symbio.agency** — we would rather answer the question than have you
226
+ guess.
227
+
228
+ The full terms are in `LICENSE`, shipped in this package. Versions released
229
+ earlier under MIT stay available under MIT, on the terms they were released
230
+ with.
package/UI.md ADDED
@@ -0,0 +1,256 @@
1
+ # The default UI
2
+
3
+ `<Mocanvas />` ships a small, opinionated interface: a toolbar, a zoom bar, a
4
+ style panel, an optional statistics chip, and the chrome the editor draws on the
5
+ canvas itself. All of it is optional and all of it is themeable from CSS.
6
+
7
+ Source lives in `packages/mocanvas/src/ui/`:
8
+
9
+ | File | What it holds |
10
+ | --- | --- |
11
+ | `DefaultUi.tsx` | The toolbar, the zoom bar, the stats chip, and the `DefaultUi` wrapper |
12
+ | `StylePanel.tsx` | The style panel and `getStylePanelSections`, the rule for which rows appear |
13
+ | `icons.tsx` | The whole icon set, plus the geo icons generated from canvas geometry |
14
+ | `overlays.tsx` | The shared tooltip and popover layers, and `placeNear` |
15
+ | `ui.css` | Every design token and every rule |
16
+ | `useKeyboardShortcuts.ts` | The default key bindings |
17
+
18
+ ## Design tokens
19
+
20
+ Tokens are declared on `.mocanvas` (the editor container), on `.mocanvas-panel`,
21
+ and on `.mocanvas-layer` (the floating tooltip and popover, which sit outside the
22
+ panels in the DOM). Override any of them on `.mocanvas`, on a wrapper, or on a
23
+ single panel.
24
+
25
+ ### Surfaces and colour
26
+
27
+ | Token | Light | Dark | Used for |
28
+ | --- | --- | --- | --- |
29
+ | `--mocanvas-ui-panel` | `#ffffff` | `#1b1d22` | Panel, popover and picker background |
30
+ | `--mocanvas-ui-panel-border` | `#d6d9e0` | `#3c404a` | Panel hairline, dividers, group separators |
31
+ | `--mocanvas-ui-control` | `rgba(16,24,40,.06)` | `rgba(255,255,255,.08)` | Recessed background of a segmented button |
32
+ | `--mocanvas-ui-text` | `#16181d` | `#eceef3` | Icon and label colour |
33
+ | `--mocanvas-ui-muted` | `#5c6070` | `#a4a9b4` | Row labels, stats text |
34
+ | `--mocanvas-ui-accent` | `#2563eb` | `#6ea8fe` | Selected control, focus ring |
35
+ | `--mocanvas-ui-accent-fg` | `#ffffff` | `#0e1116` | Icon on an accent fill |
36
+ | `--mocanvas-ui-accent-soft` | 12% accent | 18% accent | Hover on a segmented button, open disclosure |
37
+ | `--mocanvas-ui-hover` | 6% ink | 9% white | Hover on a bare button |
38
+ | `--mocanvas-ui-active` | 13% ink | 18% white | Pointer-down on a bare button |
39
+ | `--mocanvas-ui-shadow` | — | — | Panel elevation |
40
+ | `--mocanvas-ui-tip-bg` / `-fg` / `-muted` | — | — | Tooltip pill, its label, its shortcut |
41
+
42
+ Every text-on-background pair is at or above 4.5:1 in both palettes. Measured on
43
+ the shipped values, the tightest are `accent` on a segmented control (4.58 light,
44
+ 5.54 dark) and `muted` on the panel (6.25 light, 7.16 dark). Disabled controls
45
+ render at 35% opacity, which WCAG exempts.
46
+
47
+ ### Metrics
48
+
49
+ | Token | Value | Meaning |
50
+ | --- | --- | --- |
51
+ | `--mocanvas-ui-btn` | `40px` | Every hit target — buttons, swatches, popover cells |
52
+ | `--mocanvas-ui-icon` | `20px` | Icon size inside a button |
53
+ | `--mocanvas-ui-gap` | `2px` | Gap between adjacent controls |
54
+ | `--mocanvas-ui-pad` | `4px` | Padding of a bar |
55
+ | `--mocanvas-ui-pad-lg` | `10px` | Padding of the style panel, gap between its groups |
56
+ | `--mocanvas-ui-inset` | `12px` | Distance from a panel to the viewport edge |
57
+ | `--mocanvas-ui-radius` / `-sm` | `12px` / `8px` | Panel radius / control radius |
58
+ | `--mocanvas-ui-dock` | `300px` | Width the bottom-left and bottom-right docks reserve beside the centred toolbar |
59
+ | `--mocanvas-ui-bottom-dock` | `62px` (`124px` under 1290px) | Height the bottom edge occupies; the style panel stops above it |
60
+ | `--mocanvas-ui-font` / `-mono` | system stacks | Panel type / stats chip |
61
+
62
+ `--mocanvas-ui-dock` is the mechanism that keeps the three bottom panels apart:
63
+ the toolbar is centred on the viewport but may not grow into the reservation, so
64
+ it wraps rather than collide. Shrink it if your zoom bar is narrower than the
65
+ default one.
66
+
67
+ ### Canvas chrome
68
+
69
+ These are read by the editor's indicator layer (`Canvas.tsx`), not by the panels,
70
+ so they apply even with `hideUi`. They are deliberately **not** redefined for the
71
+ dark UI: the canvas keeps its own background, which does not follow
72
+ `prefers-color-scheme`.
73
+
74
+ | Token | Value | Used for |
75
+ | --- | --- | --- |
76
+ | `--mocanvas-selection` | `#2f6fe4` | Selection and hover outlines, handle strokes, brush border |
77
+ | `--mocanvas-selection-fg` | `#ffffff` | Fill behind a solid handle |
78
+ | `--mocanvas-brush-fill` | 12% selection | Brush rectangle interior |
79
+ | `--mocanvas-snap` | `#cf3fe0` | Snap lines and their end markers |
80
+
81
+ The two stroke colours clear 3:1 against a light canvas (`#f9fafb`: 4.45 and
82
+ 3.69) and against a dark one (`#1b1d22`: 3.63 and 4.37). If you render a dark
83
+ canvas, swap `--mocanvas-selection-fg` for a dark value so handles stay filled
84
+ with the canvas colour rather than white.
85
+
86
+ Handles are drawn at 9px (corners), 6px (shape handles) and 5.5px (rotate) in
87
+ screen space, with a 1.5px stroke. Their hit radius is a separate editor
88
+ constant, `HANDLE_HIT_RADIUS` in `packages/editor/src/editor/selectionHandles.ts`.
89
+
90
+ ## Icon grid rules
91
+
92
+ Icons are original artwork on a 24×24 viewBox, painted with `currentColor`.
93
+
94
+ - **Grid.** 24×24. Ink, stroke included, stays inside it.
95
+ - **Weight.** Stroke 1.75, round caps and joins. Only texture marks deviate and
96
+ say so at the call site: the fill hatching (1.25), the dotted rule (2.6 with a
97
+ zero-length dash, so the caps draw the dots), the mono rails (1.4).
98
+ - **Centring.** Ink is optically centred on (12, 12), within half a grid unit.
99
+ The only exceptions are semantic: `valign-top` and `valign-bottom` sit high and
100
+ low on purpose.
101
+ - **Extent.** The longest ink dimension lands between 16 and 18.25 units — about
102
+ 17.75 for a full-bleed form — so no icon reads heavier than its neighbour. The
103
+ `size-*` ramp is exempt: its whole job is to differ in size.
104
+ - **Distinctness.** No two icons may draw the same artwork; `icons.test.tsx`
105
+ enforces this. It is why the handwriting font is a script `a` rather than a
106
+ fourth capital A, and why `oval` gets a wider box than `ellipse`.
107
+ - **Geo icons** are generated from `getGeoGeometry`, the same code the canvas
108
+ draws with, so a toolbar button always matches the shape it creates. Each kind
109
+ is fitted into the box `getGeoIconBox(kind)` returns: square at `GEO_BOX` (16)
110
+ by default, and flatter or narrower for kinds whose name implies a proportion
111
+ (`rectangle`, `oval`, the four arrows). The longest side is always `GEO_BOX`.
112
+
113
+ Render one with `<Icon name="select" size={20} />`. `size` sets the SVG's
114
+ attributes; buttons additionally take their icon size from `--mocanvas-ui-icon`,
115
+ so inline marks like the "mixed" badge keep the size they ask for.
116
+
117
+ ## Overriding the UI
118
+
119
+ ### Turn it off
120
+
121
+ ```tsx
122
+ <Mocanvas hideUi />
123
+ ```
124
+
125
+ This drops the toolbar, zoom bar, style panel and stats chip. `ui.css` still
126
+ loads, so the canvas chrome tokens above keep working. Default keyboard
127
+ shortcuts are wired by `<Mocanvas />` itself and are unaffected; call
128
+ `useKeyboardShortcuts(editor)` yourself if you build on `<Canvas />` directly.
129
+
130
+ ### Replace the canvas chrome
131
+
132
+ ```tsx
133
+ <Mocanvas components={{ Indicators: MyIndicators, Brush: MyBrush, Background: MyGrid }} />
134
+ ```
135
+
136
+ `components` is forwarded to `<Canvas />`. `Indicators` draws selection and hover
137
+ outlines and handles, `Brush` the marquee, `Background` a layer behind the canvas.
138
+ Each receives `{ editor }` and renders into the SVG overlay (`Background` into a
139
+ plain DOM layer). Omit one to keep the default.
140
+
141
+ ### Rebuild the panels
142
+
143
+ Compose your own from the exported parts:
144
+
145
+ ```tsx
146
+ import { Mocanvas, Toolbar, ZoomBar, StylePanel, UiTooltip, Icon } from "mocanvas"
147
+
148
+ <Mocanvas hideUi>
149
+ <Toolbar />
150
+ <MyOwnInspector />
151
+ <UiTooltip />
152
+ </Mocanvas>
153
+ ```
154
+
155
+ `Toolbar`, `ZoomBar`, `StylePanel`, `DebugStats`, `Popover`, `UiTooltip`, `Icon`
156
+ and `TOOLBAR_GROUPS` are all exported. Children of `<Mocanvas>` render above the
157
+ canvas inside the editor container, so the tokens apply to them too.
158
+
159
+ ### Restyle it
160
+
161
+ ```css
162
+ .mocanvas {
163
+ --mocanvas-ui-accent: #12b886;
164
+ --mocanvas-ui-radius: 6px;
165
+ --mocanvas-ui-btn: 44px;
166
+ --mocanvas-selection: #12b886;
167
+ }
168
+ ```
169
+
170
+ ## The panels
171
+
172
+ ### Toolbar — bottom centre
173
+
174
+ Tools in four groups separated by dividers: select and hand; draw and eraser; the
175
+ five common geo kinds plus a disclosure for the other fifteen; text, note, and
176
+ whichever of arrow, line and frame the app registered. Entries whose tool is not
177
+ registered disappear, so a cut-down `tools` prop yields a cut-down bar. The
178
+ active tool is `aria-pressed`; a geo button is pressed only when its own kind is
179
+ active. The disclosure opens a 5-column popover of the remaining kinds and is
180
+ itself marked pressed when one of them is active. The bar is centred on the
181
+ viewport, wraps to more rows rather than growing into the docks, and moves to a
182
+ row of its own below 1290px.
183
+
184
+ ### Zoom bar — bottom left
185
+
186
+ Zoom out, the current percentage (a button that resets to 100%), zoom in, zoom to
187
+ fit, then a divider and undo/redo. Undo and redo are `disabled` when there is
188
+ nothing to undo or redo, at 35% opacity and without a tooltip.
189
+
190
+ ### Style panel — top right
191
+
192
+ Appears when the selection carries at least one style, or when a drawing tool is
193
+ active with nothing selected. Rows are grouped, and separated by a hairline:
194
+
195
+ 1. **Shape** — the geo kind, as one button showing the current shape that opens a
196
+ 20-cell popover.
197
+ 2. **Colour** — the stroke colour, and the label colour when the selection can
198
+ carry one; twelve swatches each, six to a row.
199
+ 3. **Stroke and fill** — Fill, Dash and Size, four choices each.
200
+ 4. **Text** — Font, then horizontal and vertical alignment sharing one row.
201
+ 5. **Opacity** — a slider; only with a selection, since it edits shapes rather
202
+ than a style.
203
+
204
+ Which rows appear is decided by `getStylePanelSections(editor)`, which reads
205
+ `editor.getSharedStyles()` for a selection and
206
+ `editor.getStylePropsForType(toolId)` otherwise. A selection of two lines
207
+ therefore shows Colour, Dash and Size and nothing else. A row whose selected
208
+ shapes disagree shows a dashed "mixed" badge beside its label. The panel scrolls
209
+ when it is taller than the space above the bottom dock, with a fade and a shadow
210
+ at whichever edge has more content behind it. Below 560px it spans the width and
211
+ is capped at 42% of the height.
212
+
213
+ ### Statistics chip — bottom right
214
+
215
+ Shape counts, drawn versus culled, and milliseconds per frame. Toggled with
216
+ `⌥D`, or with the `showStats` prop. It moves above the zoom bar below 560px.
217
+
218
+ ### Tooltip and popover
219
+
220
+ Both are single, `position: fixed`, viewport-clamped layers (`overlays.tsx`).
221
+ A tooltip labels any element with `data-tooltip`, adding `data-shortcut` in a
222
+ muted weight; it appears after a 500ms rest, immediately on keyboard focus, sits
223
+ above its control so it never covers it, and flips below when there is no room.
224
+ `placeNear` does the arithmetic and is unit-tested. Popovers dismiss on outside
225
+ pointer-down and on Escape, which also returns focus to the button.
226
+
227
+ ## States
228
+
229
+ | State | Bare button | Segmented button | Swatch |
230
+ | --- | --- | --- | --- |
231
+ | Rest | transparent | `--mocanvas-ui-control` | transparent |
232
+ | Hover | `--mocanvas-ui-hover` | `--mocanvas-ui-accent-soft` | `--mocanvas-ui-hover` |
233
+ | Pointer down | `--mocanvas-ui-active`, scaled 0.94 | as hover, scaled 0.94 | scaled 0.92 |
234
+ | Selected | accent fill, accent-fg icon | accent fill, accent-fg icon | double ring in accent |
235
+ | Selected + hover | accent fill plus an inset ring | same | same |
236
+ | Focus (keyboard) | 2px accent outline, 2px offset | same | same |
237
+ | Disabled | 35% opacity, no tooltip, default cursor | — | — |
238
+
239
+ The focus ring is offset by 2px so a ring of panel colour separates it from an
240
+ accent-filled button; without that gap, focus would be invisible on the active
241
+ tool. `prefers-reduced-motion` removes every transition.
242
+
243
+ ## Pointer targets
244
+
245
+ Selection handles use a 24x24 px pointer target (`HANDLE_HIT_RADIUS = 12` in
246
+ `packages/editor/src/editor/selectionHandles.ts`), which is the WCAG 2.2
247
+ minimum. Two rules keep that from swallowing small shapes:
248
+
249
+ - Edge handles (top, right, bottom, left) only appear once that edge is at
250
+ least `4 * HANDLE_HIT_RADIUS` long on screen; below that the two corners
251
+ already cover the whole edge.
252
+ - On a selection smaller than six handles across, `getHandleHitRadius` scales
253
+ the target down (never below 4 px) so the shape's interior stays draggable.
254
+
255
+ Both measure the *screen-space* edge lengths of the transformed corners, so a
256
+ rotated selection behaves the same as an upright one.