@mocanvas/editor 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/ARCHITECTURE.md +421 -0
- package/BENCHMARK.md +519 -0
- package/CLEAN_ROOM.md +50 -0
- package/COMPAT.md +282 -0
- package/CUSTOM_SHAPES.md +880 -0
- package/LICENSE +110 -16
- package/MIGRATION.md +807 -0
- package/README.md +24 -5
- package/UI.md +256 -0
- package/dist/index.d.ts +13091 -1419
- package/dist/index.js +12748 -2126
- package/dist/index.js.map +1 -1
- package/package.json +14 -16
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# @mocanvas/editor
|
|
2
2
|
|
|
3
|
-
The editor core of
|
|
3
|
+
The editor core of mocanvas, without the
|
|
4
4
|
default shapes, tools and UI: the `Editor` facade, `ShapeUtil` and
|
|
5
5
|
`BindingUtil`, the `StateNode` tool state machine, geometry, snapping, history,
|
|
6
6
|
textures, and the `<Canvas />` React renderer with its WebGL2 backend.
|
|
7
7
|
|
|
8
8
|
Use this package when you want to build your own shape set and UI. If you want
|
|
9
|
-
a canvas that works out of the box, use
|
|
9
|
+
a canvas that works out of the box, use `@mocanvas/mocanvas`.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -53,12 +53,31 @@ export function MyCanvas() {
|
|
|
53
53
|
}
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
Writing a shape util and its tool is covered in
|
|
57
|
-
|
|
56
|
+
Writing a shape util and its tool is covered in `CUSTOM_SHAPES.md`, shipped
|
|
57
|
+
inside this package.
|
|
58
58
|
|
|
59
59
|
ESM only. The WebAssembly engine is loaded from `@mocanvas/wasm`; see that
|
|
60
60
|
package's README for how the `.wasm` file is resolved by your bundler.
|
|
61
61
|
|
|
62
62
|
## License
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
**Source-available, not open source.** Free to use for:
|
|
65
|
+
|
|
66
|
+
- personal, non-commercial projects;
|
|
67
|
+
- non-profit organisations;
|
|
68
|
+
- development, evaluation, testing and staging — including inside a for-profit
|
|
69
|
+
company, so you can try it and build against it before committing;
|
|
70
|
+
- teaching and academic research.
|
|
71
|
+
|
|
72
|
+
**Shipping it in a commercial product, service or website needs a written
|
|
73
|
+
agreement with us.** That includes anything sold, anything that earns revenue
|
|
74
|
+
directly or through advertising, and internal tools running a for-profit
|
|
75
|
+
business.
|
|
76
|
+
|
|
77
|
+
To arrange one, or if you are unsure which side of the line you are on, write to
|
|
78
|
+
**mocanvas@symbio.agency** — we would rather answer the question than have you
|
|
79
|
+
guess.
|
|
80
|
+
|
|
81
|
+
The full terms are in `LICENSE`, shipped in this package. Versions released
|
|
82
|
+
earlier under MIT stay available under MIT, on the terms they were released
|
|
83
|
+
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.
|