@open-artifact/canvas-engine 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +44 -4
- package/src/camera.d.ts +71 -0
- package/src/camera.js +89 -0
- package/src/engine.d.ts +132 -0
- package/src/engine.js +738 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +6 -0
- package/src/protocol.d.ts +146 -0
- package/src/protocol.js +736 -0
- package/src/types.d.ts +355 -0
- package/src/types.js +6 -0
package/src/protocol.js
ADDED
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The binary protocol between TypeScript and the wasm core: id handles, colours,
|
|
3
|
+
* path commands and patch records. Everything here is GPU-free and runs in Node.
|
|
4
|
+
*
|
|
5
|
+
* A patch is a Float64Array of REC-sized records plus a Float32Array of path
|
|
6
|
+
* commands. Record layout (index: field):
|
|
7
|
+
* 0 op (1 upsert, 2 remove, 3 clear) 1 handle 2 kind (0 box, 1 hole, 2 path)
|
|
8
|
+
* 3 layer 4 z 5 x 6 y 7 w 8 h 9 rotation 10 flags 11 opacity
|
|
9
|
+
* 12 fill 13 stroke (0xRRGGBBAA) 14 stroke width 15 radius 16 dash on 17 dash off
|
|
10
|
+
* 18 shadow colour 19 shadow blur 20 shadow dx 21 shadow dy 22 texture handle (-1 none)
|
|
11
|
+
* 23 path offset 24 path length 25 viewBox w 26 viewBox h 27 cap 28 join 29 miter limit
|
|
12
|
+
* 30 fill rule 31 reserved
|
|
13
|
+
* Path commands (f32): 0 M x y, 1 L x y, 2 Q cx cy x y, 3 C c1x c1y c2x c2y x y, 4 Z.
|
|
14
|
+
*/
|
|
15
|
+
export const REC = 32;
|
|
16
|
+
export const OP_UPSERT = 1;
|
|
17
|
+
export const OP_REMOVE = 2;
|
|
18
|
+
export const OP_CLEAR = 3;
|
|
19
|
+
export const KIND_BOX = 0;
|
|
20
|
+
export const KIND_HOLE = 1;
|
|
21
|
+
export const KIND_PATH = 2;
|
|
22
|
+
export const F_VISIBLE = 1;
|
|
23
|
+
export const F_HITTABLE = 2;
|
|
24
|
+
export const F_SELECTABLE = 4;
|
|
25
|
+
export const F_MOVABLE = 8;
|
|
26
|
+
export const F_RESIZABLE = 16;
|
|
27
|
+
export const F_SNAP = 32;
|
|
28
|
+
export const F_LOCK_ASPECT = 64;
|
|
29
|
+
export const F_HIT_STROKE_ONLY = 128;
|
|
30
|
+
export const F_STROKE_SCREEN = 256;
|
|
31
|
+
export const F_NO_HOVER = 512;
|
|
32
|
+
export const F_NO_OUTLINE = 1024;
|
|
33
|
+
export const F_HANDLES_CORNERS = 2048;
|
|
34
|
+
export const F_HANDLES_WIDTH = 4096;
|
|
35
|
+
export const F_MULTIPLY = 8192;
|
|
36
|
+
/** Most cut-out rects a path takes (`PathItem.cuts`). */
|
|
37
|
+
export const MAX_CUTS = 2;
|
|
38
|
+
export const CMD_M = 0;
|
|
39
|
+
export const CMD_L = 1;
|
|
40
|
+
export const CMD_Q = 2;
|
|
41
|
+
export const CMD_C = 3;
|
|
42
|
+
export const CMD_Z = 4;
|
|
43
|
+
/** Option keys for `CanvasCore.set_option`. */
|
|
44
|
+
export const OPTION = {
|
|
45
|
+
minZoom: 1,
|
|
46
|
+
maxZoom: 2,
|
|
47
|
+
snap: 3,
|
|
48
|
+
snapDistance: 4,
|
|
49
|
+
dragThreshold: 5,
|
|
50
|
+
hitTolerance: 6,
|
|
51
|
+
handleSize: 7,
|
|
52
|
+
settleMs: 8,
|
|
53
|
+
cameraEventMs: 9,
|
|
54
|
+
wheelZoom: 10,
|
|
55
|
+
selection: 11,
|
|
56
|
+
marquee: 12,
|
|
57
|
+
fadeMs: 13,
|
|
58
|
+
dragEvents: 14,
|
|
59
|
+
grid: 15,
|
|
60
|
+
gridSpacing: 16,
|
|
61
|
+
textureBudget: 17,
|
|
62
|
+
zoomSpeed: 18,
|
|
63
|
+
wheelZoomMax: 19,
|
|
64
|
+
gridMajor: 20,
|
|
65
|
+
gridFadeStart: 21,
|
|
66
|
+
gridFadeEnd: 22,
|
|
67
|
+
};
|
|
68
|
+
export const THEME_OPTION = {
|
|
69
|
+
background: 30,
|
|
70
|
+
grid: 31,
|
|
71
|
+
selection: 32,
|
|
72
|
+
handle: 33,
|
|
73
|
+
guide: 34,
|
|
74
|
+
marquee: 35,
|
|
75
|
+
hover: 36,
|
|
76
|
+
};
|
|
77
|
+
export const MOD_SHIFT = 1;
|
|
78
|
+
export const MOD_ALT = 2;
|
|
79
|
+
export const MOD_CTRL = 4;
|
|
80
|
+
export const MOD_META = 8;
|
|
81
|
+
export const KEY_SPACE = 1;
|
|
82
|
+
export const KEY_ESCAPE = 2;
|
|
83
|
+
export const KEY_MODIFIER = 3;
|
|
84
|
+
export const TOOLS = { select: 0, pan: 1, none: 2 };
|
|
85
|
+
export const POINTER_TYPES = { mouse: 0, pen: 1, touch: 2 };
|
|
86
|
+
/** Engine cursor codes → CSS cursors. */
|
|
87
|
+
export const CURSORS = ['default', 'grab', 'grabbing', 'move', 'ns-resize', 'ew-resize', 'nwse-resize', 'nesw-resize', 'crosshair'];
|
|
88
|
+
export const FRAME_RENDER = 1;
|
|
89
|
+
export const FRAME_MORE = 2;
|
|
90
|
+
export const FRAME_HOLES = 4;
|
|
91
|
+
/** CSS named colours (CSS Color 4), 0xRRGGBB. */
|
|
92
|
+
const NAMED = {
|
|
93
|
+
aliceblue: 0xf0f8ff, antiquewhite: 0xfaebd7, aqua: 0x00ffff, aquamarine: 0x7fffd4, azure: 0xf0ffff,
|
|
94
|
+
beige: 0xf5f5dc, bisque: 0xffe4c4, black: 0x000000, blanchedalmond: 0xffebcd, blue: 0x0000ff,
|
|
95
|
+
blueviolet: 0x8a2be2, brown: 0xa52a2a, burlywood: 0xdeb887, cadetblue: 0x5f9ea0, chartreuse: 0x7fff00,
|
|
96
|
+
chocolate: 0xd2691e, coral: 0xff7f50, cornflowerblue: 0x6495ed, cornsilk: 0xfff8dc, crimson: 0xdc143c,
|
|
97
|
+
cyan: 0x00ffff, darkblue: 0x00008b, darkcyan: 0x008b8b, darkgoldenrod: 0xb8860b, darkgray: 0xa9a9a9,
|
|
98
|
+
darkgreen: 0x006400, darkgrey: 0xa9a9a9, darkkhaki: 0xbdb76b, darkmagenta: 0x8b008b, darkolivegreen: 0x556b2f,
|
|
99
|
+
darkorange: 0xff8c00, darkorchid: 0x9932cc, darkred: 0x8b0000, darksalmon: 0xe9967a, darkseagreen: 0x8fbc8f,
|
|
100
|
+
darkslateblue: 0x483d8b, darkslategray: 0x2f4f4f, darkslategrey: 0x2f4f4f, darkturquoise: 0x00ced1,
|
|
101
|
+
darkviolet: 0x9400d3, deeppink: 0xff1493, deepskyblue: 0x00bfff, dimgray: 0x696969, dimgrey: 0x696969,
|
|
102
|
+
dodgerblue: 0x1e90ff, firebrick: 0xb22222, floralwhite: 0xfffaf0, forestgreen: 0x228b22, fuchsia: 0xff00ff,
|
|
103
|
+
gainsboro: 0xdcdcdc, ghostwhite: 0xf8f8ff, gold: 0xffd700, goldenrod: 0xdaa520, gray: 0x808080,
|
|
104
|
+
green: 0x008000, greenyellow: 0xadff2f, grey: 0x808080, honeydew: 0xf0fff0, hotpink: 0xff69b4,
|
|
105
|
+
indianred: 0xcd5c5c, indigo: 0x4b0082, ivory: 0xfffff0, khaki: 0xf0e68c, lavender: 0xe6e6fa,
|
|
106
|
+
lavenderblush: 0xfff0f5, lawngreen: 0x7cfc00, lemonchiffon: 0xfffacd, lightblue: 0xadd8e6, lightcoral: 0xf08080,
|
|
107
|
+
lightcyan: 0xe0ffff, lightgoldenrodyellow: 0xfafad2, lightgray: 0xd3d3d3, lightgreen: 0x90ee90,
|
|
108
|
+
lightgrey: 0xd3d3d3, lightpink: 0xffb6c1, lightsalmon: 0xffa07a, lightseagreen: 0x20b2aa,
|
|
109
|
+
lightskyblue: 0x87cefa, lightslategray: 0x778899, lightslategrey: 0x778899, lightsteelblue: 0xb0c4de,
|
|
110
|
+
lightyellow: 0xffffe0, lime: 0x00ff00, limegreen: 0x32cd32, linen: 0xfaf0e6, magenta: 0xff00ff,
|
|
111
|
+
maroon: 0x800000, mediumaquamarine: 0x66cdaa, mediumblue: 0x0000cd, mediumorchid: 0xba55d3,
|
|
112
|
+
mediumpurple: 0x9370db, mediumseagreen: 0x3cb371, mediumslateblue: 0x7b68ee, mediumspringgreen: 0x00fa9a,
|
|
113
|
+
mediumturquoise: 0x48d1cc, mediumvioletred: 0xc71585, midnightblue: 0x191970, mintcream: 0xf5fffa,
|
|
114
|
+
mistyrose: 0xffe4e1, moccasin: 0xffe4b5, navajowhite: 0xffdead, navy: 0x000080, oldlace: 0xfdf5e6,
|
|
115
|
+
olive: 0x808000, olivedrab: 0x6b8e23, orange: 0xffa500, orangered: 0xff4500, orchid: 0xda70d6,
|
|
116
|
+
palegoldenrod: 0xeee8aa, palegreen: 0x98fb98, paleturquoise: 0xafeeee, palevioletred: 0xdb7093,
|
|
117
|
+
papayawhip: 0xffefd5, peachpuff: 0xffdab9, peru: 0xcd853f, pink: 0xffc0cb, plum: 0xdda0dd,
|
|
118
|
+
powderblue: 0xb0e0e6, purple: 0x800080, rebeccapurple: 0x663399, red: 0xff0000, rosybrown: 0xbc8f8f,
|
|
119
|
+
royalblue: 0x4169e1, saddlebrown: 0x8b4513, salmon: 0xfa8072, sandybrown: 0xf4a460, seagreen: 0x2e8b57,
|
|
120
|
+
seashell: 0xfff5ee, sienna: 0xa0522d, silver: 0xc0c0c0, skyblue: 0x87ceeb, slateblue: 0x6a5acd,
|
|
121
|
+
slategray: 0x708090, slategrey: 0x708090, snow: 0xfffafa, springgreen: 0x00ff7f, steelblue: 0x4682b4,
|
|
122
|
+
tan: 0xd2b48c, teal: 0x008080, thistle: 0xd8bfd8, tomato: 0xff6347, turquoise: 0x40e0d0, violet: 0xee82ee,
|
|
123
|
+
wheat: 0xf5deb3, white: 0xffffff, whitesmoke: 0xf5f5f5, yellow: 0xffff00, yellowgreen: 0x9acd32,
|
|
124
|
+
};
|
|
125
|
+
const NUMBER = /^[-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/;
|
|
126
|
+
/** One numeric CSS token: its value and unit ('%', 'deg', '' …), or null when it is not a number. */
|
|
127
|
+
function numeric(token) {
|
|
128
|
+
const m = NUMBER.exec(token);
|
|
129
|
+
if (!m)
|
|
130
|
+
return null;
|
|
131
|
+
const unit = token.slice(m[0].length);
|
|
132
|
+
return /^(%|deg|rad|grad|turn)?$/.test(unit) ? { v: parseFloat(m[0]), unit } : null;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The arguments of `fn(…)` in legacy (comma) or modern (space, `/ alpha`)
|
|
136
|
+
* syntax: three channel tokens and an optional alpha token. `none` reads as 0.
|
|
137
|
+
*/
|
|
138
|
+
function colorArgs(body) {
|
|
139
|
+
const comma = body.includes(',');
|
|
140
|
+
const [main, alpha, extra] = comma ? [body, undefined, undefined] : body.split('/');
|
|
141
|
+
if (extra !== undefined)
|
|
142
|
+
return null;
|
|
143
|
+
const parts = main.split(comma ? ',' : /\s+/).map(p => p.trim()).filter(Boolean);
|
|
144
|
+
if (comma ? parts.length !== 3 && parts.length !== 4 : parts.length !== 3)
|
|
145
|
+
return null;
|
|
146
|
+
const a = comma ? parts[3] : alpha?.trim();
|
|
147
|
+
if (a === '')
|
|
148
|
+
return null;
|
|
149
|
+
const none = (p) => (p === 'none' ? '0' : p);
|
|
150
|
+
return [none(parts[0]), none(parts[1]), none(parts[2]), a === undefined ? undefined : none(a)];
|
|
151
|
+
}
|
|
152
|
+
/** An alpha token (number or percentage) as 0–1. */
|
|
153
|
+
function alphaOf(token) {
|
|
154
|
+
if (token === undefined)
|
|
155
|
+
return 1;
|
|
156
|
+
const n = numeric(token);
|
|
157
|
+
if (!n || (n.unit && n.unit !== '%'))
|
|
158
|
+
return NaN;
|
|
159
|
+
return n.unit === '%' ? n.v / 100 : n.v;
|
|
160
|
+
}
|
|
161
|
+
/** A hue token in degrees. */
|
|
162
|
+
function hueOf(token) {
|
|
163
|
+
const n = numeric(token);
|
|
164
|
+
if (!n)
|
|
165
|
+
return NaN;
|
|
166
|
+
switch (n.unit) {
|
|
167
|
+
case '':
|
|
168
|
+
case 'deg':
|
|
169
|
+
return n.v;
|
|
170
|
+
case 'rad':
|
|
171
|
+
return (n.v * 180) / Math.PI;
|
|
172
|
+
case 'grad':
|
|
173
|
+
return n.v * 0.9;
|
|
174
|
+
case 'turn':
|
|
175
|
+
return n.v * 360;
|
|
176
|
+
default:
|
|
177
|
+
return NaN;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/** A percentage (or plain number in modern syntax) as 0–1. */
|
|
181
|
+
function fractionOf(token) {
|
|
182
|
+
const n = numeric(token);
|
|
183
|
+
if (!n)
|
|
184
|
+
return NaN;
|
|
185
|
+
if (n.unit === '%')
|
|
186
|
+
return n.v / 100;
|
|
187
|
+
return n.unit === '' ? n.v / 100 : NaN;
|
|
188
|
+
}
|
|
189
|
+
/** Packs 0–255 channels and a 0–1 alpha into 0xRRGGBBAA, clamping each. */
|
|
190
|
+
function pack(r, g, b, a) {
|
|
191
|
+
const q = (v, max) => Math.round(Math.min(Math.max(v, 0), max));
|
|
192
|
+
return ((q(r, 255) << 24) | (q(g, 255) << 16) | (q(b, 255) << 8) | q(a * 255, 255)) >>> 0;
|
|
193
|
+
}
|
|
194
|
+
/** HSL (hue in degrees, saturation and lightness 0–1) → 0–255 RGB (CSS Color 4 algorithm). */
|
|
195
|
+
function hslToRgb(h, s, l) {
|
|
196
|
+
const hue = ((h % 360) + 360) % 360;
|
|
197
|
+
const k = (n) => (n + hue / 30) % 12;
|
|
198
|
+
const a = s * Math.min(l, 1 - l);
|
|
199
|
+
const f = (n) => l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1));
|
|
200
|
+
return [f(0) * 255, f(8) * 255, f(4) * 255];
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Parses a colour to 0xRRGGBBAA (unsigned). Reads numbers (already packed),
|
|
204
|
+
* every CSS named colour, `transparent`, `#rgb`/`#rgba`/`#rrggbb`/`#rrggbbaa`,
|
|
205
|
+
* and `rgb()`/`rgba()`/`hsl()`/`hsla()` in comma or space syntax with
|
|
206
|
+
* percentages, hue units and `/ alpha`. In a browser, other colour spaces
|
|
207
|
+
* and `color-mix()` are resolved by painting them ({@link paintedColor}).
|
|
208
|
+
* Throws on anything else (`currentColor`, `var()`, and any of those outside
|
|
209
|
+
* a browser).
|
|
210
|
+
*/
|
|
211
|
+
export function parseColor(c, fallback = 0) {
|
|
212
|
+
if (c === undefined || c === null)
|
|
213
|
+
return fallback;
|
|
214
|
+
if (typeof c === 'number')
|
|
215
|
+
return c >>> 0;
|
|
216
|
+
const s = c.trim().toLowerCase();
|
|
217
|
+
if (s === 'transparent' || s === 'none')
|
|
218
|
+
return 0;
|
|
219
|
+
const named = NAMED[s];
|
|
220
|
+
if (named !== undefined)
|
|
221
|
+
return ((named << 8) | 0xff) >>> 0;
|
|
222
|
+
if (s.startsWith('#')) {
|
|
223
|
+
const h = s.slice(1);
|
|
224
|
+
if (/^[0-9a-f]+$/.test(h)) {
|
|
225
|
+
if (h.length === 3 || h.length === 4) {
|
|
226
|
+
const full = [...h].map(ch => ch + ch).join('');
|
|
227
|
+
return parseInt(h.length === 3 ? full + 'ff' : full, 16) >>> 0;
|
|
228
|
+
}
|
|
229
|
+
if (h.length === 6)
|
|
230
|
+
return parseInt(h + 'ff', 16) >>> 0;
|
|
231
|
+
if (h.length === 8)
|
|
232
|
+
return parseInt(h, 16) >>> 0;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const m = /^(rgba?|hsla?)\(\s*([^()]*)\)$/.exec(s);
|
|
236
|
+
const args = m ? colorArgs(m[2]) : null;
|
|
237
|
+
if (m && args) {
|
|
238
|
+
const [x, y, z, alpha] = args;
|
|
239
|
+
const a = alphaOf(alpha);
|
|
240
|
+
if (m[1].startsWith('rgb')) {
|
|
241
|
+
const channel = (p) => {
|
|
242
|
+
const n = numeric(p);
|
|
243
|
+
return !n ? NaN : n.unit === '%' ? (n.v / 100) * 255 : n.unit === '' ? n.v : NaN;
|
|
244
|
+
};
|
|
245
|
+
const rgb = [channel(x), channel(y), channel(z)];
|
|
246
|
+
if ([...rgb, a].every(Number.isFinite))
|
|
247
|
+
return pack(rgb[0], rgb[1], rgb[2], a);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
const h = hueOf(x);
|
|
251
|
+
const sat = Math.min(Math.max(fractionOf(y), 0), 1);
|
|
252
|
+
const light = Math.min(Math.max(fractionOf(z), 0), 1);
|
|
253
|
+
if ([h, sat, light, a].every(Number.isFinite)) {
|
|
254
|
+
const [r, g, b] = hslToRgb(h, sat, light);
|
|
255
|
+
return pack(r, g, b, a);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const painted = paintedColor(s);
|
|
260
|
+
if (painted !== null)
|
|
261
|
+
return painted;
|
|
262
|
+
throw new TypeError(`canvas-engine: unsupported colour ${JSON.stringify(c)}`);
|
|
263
|
+
}
|
|
264
|
+
let probe;
|
|
265
|
+
const paintedCache = new Map();
|
|
266
|
+
/**
|
|
267
|
+
* Colours the parser does not read itself (`oklch()`, `lab()`, `color()`,
|
|
268
|
+
* `color-mix()`, …), resolved by the browser: painted into a 1×1 canvas and
|
|
269
|
+
* read back as sRGB. Null outside a browser, or when the browser rejects it.
|
|
270
|
+
*/
|
|
271
|
+
function paintedColor(css) {
|
|
272
|
+
const cached = paintedCache.get(css);
|
|
273
|
+
if (cached !== undefined)
|
|
274
|
+
return cached;
|
|
275
|
+
if (probe === undefined) {
|
|
276
|
+
probe =
|
|
277
|
+
typeof OffscreenCanvas !== 'undefined'
|
|
278
|
+
? new OffscreenCanvas(1, 1).getContext('2d', { willReadFrequently: true })
|
|
279
|
+
: typeof document !== 'undefined'
|
|
280
|
+
? document.createElement('canvas').getContext('2d', { willReadFrequently: true })
|
|
281
|
+
: null;
|
|
282
|
+
}
|
|
283
|
+
let value = null;
|
|
284
|
+
if (probe) {
|
|
285
|
+
// A colour the browser rejects leaves fillStyle unchanged: test it against two sentinels.
|
|
286
|
+
probe.fillStyle = '#000';
|
|
287
|
+
probe.fillStyle = css;
|
|
288
|
+
const first = probe.fillStyle;
|
|
289
|
+
probe.fillStyle = '#fff';
|
|
290
|
+
probe.fillStyle = css;
|
|
291
|
+
if (first === probe.fillStyle) {
|
|
292
|
+
probe.clearRect(0, 0, 1, 1);
|
|
293
|
+
probe.fillRect(0, 0, 1, 1);
|
|
294
|
+
const [r, g, b, a] = probe.getImageData(0, 0, 1, 1).data;
|
|
295
|
+
// getImageData is unpremultiplied; alpha 0 reads as transparent black.
|
|
296
|
+
value = a === 0 ? 0 : (((r << 24) | (g << 16) | (b << 8) | a) >>> 0);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (paintedCache.size > 512)
|
|
300
|
+
paintedCache.clear();
|
|
301
|
+
paintedCache.set(css, value);
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
/** Behaviour flags with their defaults. */
|
|
305
|
+
export function encodeFlags(f) {
|
|
306
|
+
let v = 0;
|
|
307
|
+
if (f.visible !== false)
|
|
308
|
+
v |= F_VISIBLE;
|
|
309
|
+
if (f.hittable !== false)
|
|
310
|
+
v |= F_HITTABLE;
|
|
311
|
+
if (f.selectable !== false)
|
|
312
|
+
v |= F_SELECTABLE;
|
|
313
|
+
if (f.movable !== false)
|
|
314
|
+
v |= F_MOVABLE;
|
|
315
|
+
if (f.resizable !== false)
|
|
316
|
+
v |= F_RESIZABLE;
|
|
317
|
+
if (f.snap !== false)
|
|
318
|
+
v |= F_SNAP;
|
|
319
|
+
if (f.lockAspect)
|
|
320
|
+
v |= F_LOCK_ASPECT;
|
|
321
|
+
if (f.hitStrokeOnly)
|
|
322
|
+
v |= F_HIT_STROKE_ONLY;
|
|
323
|
+
if (f.screenStroke)
|
|
324
|
+
v |= F_STROKE_SCREEN;
|
|
325
|
+
if (f.hover === false)
|
|
326
|
+
v |= F_NO_HOVER;
|
|
327
|
+
if (f.selectionOutline === false)
|
|
328
|
+
v |= F_NO_OUTLINE;
|
|
329
|
+
if (f.handles === 'corners')
|
|
330
|
+
v |= F_HANDLES_CORNERS;
|
|
331
|
+
else if (f.handles === 'width')
|
|
332
|
+
v |= F_HANDLES_WIDTH;
|
|
333
|
+
if (f.blend === 'multiply')
|
|
334
|
+
v |= F_MULTIPLY;
|
|
335
|
+
return v;
|
|
336
|
+
}
|
|
337
|
+
// ------------------------------------------------------------------ SVG path data
|
|
338
|
+
/** Splits path data into command letters and numbers (handles `1e-3`, `.5.5`, `-1-2`, arc flags `11`). */
|
|
339
|
+
function tokens(d) {
|
|
340
|
+
const re = /([MmLlHhVvCcSsQqTtAaZz])|([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)/g;
|
|
341
|
+
const out = [];
|
|
342
|
+
let m;
|
|
343
|
+
while ((m = re.exec(d)))
|
|
344
|
+
out.push(m[1] ? { cmd: true, raw: m[1] } : { cmd: false, raw: m[2] });
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
/** Arc parameters to cubic Béziers (SVG implementation notes, F.6). Pushes C commands. */
|
|
348
|
+
function arcToCubics(out, x1, y1, rx, ry, phiDeg, large, sweep, x2, y2) {
|
|
349
|
+
if (rx === 0 || ry === 0) {
|
|
350
|
+
out.push(CMD_L, x2, y2);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (x1 === x2 && y1 === y2)
|
|
354
|
+
return;
|
|
355
|
+
rx = Math.abs(rx);
|
|
356
|
+
ry = Math.abs(ry);
|
|
357
|
+
const phi = (phiDeg * Math.PI) / 180;
|
|
358
|
+
const cos = Math.cos(phi);
|
|
359
|
+
const sin = Math.sin(phi);
|
|
360
|
+
const dx = (x1 - x2) / 2;
|
|
361
|
+
const dy = (y1 - y2) / 2;
|
|
362
|
+
const x1p = cos * dx + sin * dy;
|
|
363
|
+
const y1p = -sin * dx + cos * dy;
|
|
364
|
+
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
365
|
+
if (lambda > 1) {
|
|
366
|
+
const s = Math.sqrt(lambda);
|
|
367
|
+
rx *= s;
|
|
368
|
+
ry *= s;
|
|
369
|
+
}
|
|
370
|
+
const num = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
|
|
371
|
+
const den = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
|
|
372
|
+
let coef = Math.sqrt(Math.max(0, num / den));
|
|
373
|
+
if (large === sweep)
|
|
374
|
+
coef = -coef;
|
|
375
|
+
const cxp = (coef * rx * y1p) / ry;
|
|
376
|
+
const cyp = (-coef * ry * x1p) / rx;
|
|
377
|
+
const cx = cos * cxp - sin * cyp + (x1 + x2) / 2;
|
|
378
|
+
const cy = sin * cxp + cos * cyp + (y1 + y2) / 2;
|
|
379
|
+
const angle = (ux, uy, vx, vy) => {
|
|
380
|
+
const a = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
381
|
+
return a;
|
|
382
|
+
};
|
|
383
|
+
const theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
|
|
384
|
+
let delta = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
|
|
385
|
+
if (!sweep && delta > 0)
|
|
386
|
+
delta -= 2 * Math.PI;
|
|
387
|
+
if (sweep && delta < 0)
|
|
388
|
+
delta += 2 * Math.PI;
|
|
389
|
+
const segments = Math.max(1, Math.ceil(Math.abs(delta) / (Math.PI / 2)));
|
|
390
|
+
const step = delta / segments;
|
|
391
|
+
const k = (4 / 3) * Math.tan(step / 4);
|
|
392
|
+
let t = theta1;
|
|
393
|
+
const point = (a) => {
|
|
394
|
+
const ex = rx * Math.cos(a);
|
|
395
|
+
const ey = ry * Math.sin(a);
|
|
396
|
+
return [cx + cos * ex - sin * ey, cy + sin * ex + cos * ey];
|
|
397
|
+
};
|
|
398
|
+
const deriv = (a) => {
|
|
399
|
+
const ex = -rx * Math.sin(a);
|
|
400
|
+
const ey = ry * Math.cos(a);
|
|
401
|
+
return [cos * ex - sin * ey, sin * ex + cos * ey];
|
|
402
|
+
};
|
|
403
|
+
for (let i = 0; i < segments; i++) {
|
|
404
|
+
const t2 = t + step;
|
|
405
|
+
const [px, py] = point(t);
|
|
406
|
+
const [qx, qy] = point(t2);
|
|
407
|
+
const [d1x, d1y] = deriv(t);
|
|
408
|
+
const [d2x, d2y] = deriv(t2);
|
|
409
|
+
out.push(CMD_C, px + k * d1x, py + k * d1y, qx - k * d2x, qy - k * d2y, i === segments - 1 ? x2 : qx, i === segments - 1 ? y2 : qy);
|
|
410
|
+
t = t2;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* SVG path data → engine path commands (absolute M/L/Q/C/Z). Supports every SVG
|
|
415
|
+
* command, relative forms and implicit repeats; arcs become cubics.
|
|
416
|
+
*/
|
|
417
|
+
export function parseSvgPath(d) {
|
|
418
|
+
const out = [];
|
|
419
|
+
const toks = tokens(d);
|
|
420
|
+
let i = 0;
|
|
421
|
+
let cmd = '';
|
|
422
|
+
let x = 0;
|
|
423
|
+
let y = 0;
|
|
424
|
+
let sx = 0;
|
|
425
|
+
let sy = 0;
|
|
426
|
+
// Last control point for S/T reflection, and which kind it came from.
|
|
427
|
+
let cx = 0;
|
|
428
|
+
let cy = 0;
|
|
429
|
+
let last = '';
|
|
430
|
+
const num = () => {
|
|
431
|
+
const v = toks[i];
|
|
432
|
+
if (!v || v.cmd)
|
|
433
|
+
throw new SyntaxError(`canvas-engine: bad path data near token ${i} in ${JSON.stringify(d.slice(0, 80))}`);
|
|
434
|
+
i++;
|
|
435
|
+
return parseFloat(v.raw);
|
|
436
|
+
};
|
|
437
|
+
// Arc flags are single characters and may run into what follows ("a1 1 0 012 0").
|
|
438
|
+
const flag = () => {
|
|
439
|
+
const v = toks[i];
|
|
440
|
+
if (!v || v.cmd || (v.raw[0] !== '0' && v.raw[0] !== '1'))
|
|
441
|
+
throw new SyntaxError(`canvas-engine: bad arc flag near token ${i}`);
|
|
442
|
+
if (v.raw.length > 1)
|
|
443
|
+
toks[i] = { cmd: false, raw: v.raw.slice(1) };
|
|
444
|
+
else
|
|
445
|
+
i++;
|
|
446
|
+
return v.raw[0] === '1';
|
|
447
|
+
};
|
|
448
|
+
while (i < toks.length) {
|
|
449
|
+
const t = toks[i];
|
|
450
|
+
if (t.cmd) {
|
|
451
|
+
cmd = t.raw;
|
|
452
|
+
i++;
|
|
453
|
+
}
|
|
454
|
+
else if (!cmd) {
|
|
455
|
+
throw new SyntaxError('canvas-engine: path data must start with a command');
|
|
456
|
+
}
|
|
457
|
+
const rel = cmd === cmd.toLowerCase();
|
|
458
|
+
const ox = rel ? x : 0;
|
|
459
|
+
const oy = rel ? y : 0;
|
|
460
|
+
switch (cmd.toUpperCase()) {
|
|
461
|
+
case 'M': {
|
|
462
|
+
x = ox + num();
|
|
463
|
+
y = oy + num();
|
|
464
|
+
sx = x;
|
|
465
|
+
sy = y;
|
|
466
|
+
out.push(CMD_M, x, y);
|
|
467
|
+
cmd = rel ? 'l' : 'L';
|
|
468
|
+
last = 'M';
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
case 'L':
|
|
472
|
+
x = ox + num();
|
|
473
|
+
y = oy + num();
|
|
474
|
+
out.push(CMD_L, x, y);
|
|
475
|
+
last = 'L';
|
|
476
|
+
break;
|
|
477
|
+
case 'H':
|
|
478
|
+
x = ox + num();
|
|
479
|
+
out.push(CMD_L, x, y);
|
|
480
|
+
last = 'L';
|
|
481
|
+
break;
|
|
482
|
+
case 'V':
|
|
483
|
+
y = oy + num();
|
|
484
|
+
out.push(CMD_L, x, y);
|
|
485
|
+
last = 'L';
|
|
486
|
+
break;
|
|
487
|
+
case 'C': {
|
|
488
|
+
const c1x = ox + num();
|
|
489
|
+
const c1y = oy + num();
|
|
490
|
+
cx = ox + num();
|
|
491
|
+
cy = oy + num();
|
|
492
|
+
x = ox + num();
|
|
493
|
+
y = oy + num();
|
|
494
|
+
out.push(CMD_C, c1x, c1y, cx, cy, x, y);
|
|
495
|
+
last = 'C';
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
case 'S': {
|
|
499
|
+
const c1x = last === 'C' ? 2 * x - cx : x;
|
|
500
|
+
const c1y = last === 'C' ? 2 * y - cy : y;
|
|
501
|
+
cx = ox + num();
|
|
502
|
+
cy = oy + num();
|
|
503
|
+
x = ox + num();
|
|
504
|
+
y = oy + num();
|
|
505
|
+
out.push(CMD_C, c1x, c1y, cx, cy, x, y);
|
|
506
|
+
last = 'C';
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
case 'Q':
|
|
510
|
+
cx = ox + num();
|
|
511
|
+
cy = oy + num();
|
|
512
|
+
x = ox + num();
|
|
513
|
+
y = oy + num();
|
|
514
|
+
out.push(CMD_Q, cx, cy, x, y);
|
|
515
|
+
last = 'Q';
|
|
516
|
+
break;
|
|
517
|
+
case 'T':
|
|
518
|
+
cx = last === 'Q' ? 2 * x - cx : x;
|
|
519
|
+
cy = last === 'Q' ? 2 * y - cy : y;
|
|
520
|
+
x = ox + num();
|
|
521
|
+
y = oy + num();
|
|
522
|
+
out.push(CMD_Q, cx, cy, x, y);
|
|
523
|
+
last = 'Q';
|
|
524
|
+
break;
|
|
525
|
+
case 'A': {
|
|
526
|
+
const rx = num();
|
|
527
|
+
const ry = num();
|
|
528
|
+
const rot = num();
|
|
529
|
+
const large = flag();
|
|
530
|
+
const sweep = flag();
|
|
531
|
+
const nx = ox + num();
|
|
532
|
+
const ny = oy + num();
|
|
533
|
+
arcToCubics(out, x, y, rx, ry, rot, large, sweep, nx, ny);
|
|
534
|
+
x = nx;
|
|
535
|
+
y = ny;
|
|
536
|
+
last = 'A';
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
case 'Z':
|
|
540
|
+
out.push(CMD_Z);
|
|
541
|
+
x = sx;
|
|
542
|
+
y = sy;
|
|
543
|
+
last = 'Z';
|
|
544
|
+
// A number after Z continues with an implicit command it does not allow.
|
|
545
|
+
if (toks[i] && !toks[i].cmd)
|
|
546
|
+
throw new SyntaxError('canvas-engine: numbers after Z in path data');
|
|
547
|
+
break;
|
|
548
|
+
default:
|
|
549
|
+
throw new SyntaxError(`canvas-engine: unknown path command ${cmd}`);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return out;
|
|
553
|
+
}
|
|
554
|
+
/** A polyline ([x0, y0, x1, y1, …]) → engine path commands. */
|
|
555
|
+
export function polylineCommands(points, closed = false) {
|
|
556
|
+
const out = [];
|
|
557
|
+
for (let i = 0; i + 1 < points.length; i += 2)
|
|
558
|
+
out.push(i === 0 ? CMD_M : CMD_L, points[i], points[i + 1]);
|
|
559
|
+
if (closed && out.length)
|
|
560
|
+
out.push(CMD_Z);
|
|
561
|
+
return out;
|
|
562
|
+
}
|
|
563
|
+
// ------------------------------------------------------------------ ids
|
|
564
|
+
/** String ids ↔ u32 handles. Freed handles are reused only after `flush` (the end of a patch). */
|
|
565
|
+
export class IdRegistry {
|
|
566
|
+
#handles = new Map();
|
|
567
|
+
#ids = [];
|
|
568
|
+
#free = [];
|
|
569
|
+
#pending = [];
|
|
570
|
+
get size() {
|
|
571
|
+
return this.#handles.size;
|
|
572
|
+
}
|
|
573
|
+
get(id) {
|
|
574
|
+
return this.#handles.get(id);
|
|
575
|
+
}
|
|
576
|
+
/** The handle for `id`, assigning one if it has none. */
|
|
577
|
+
ensure(id) {
|
|
578
|
+
let h = this.#handles.get(id);
|
|
579
|
+
if (h === undefined) {
|
|
580
|
+
h = this.#free.pop() ?? this.#ids.length;
|
|
581
|
+
this.#handles.set(id, h);
|
|
582
|
+
this.#ids[h] = id;
|
|
583
|
+
}
|
|
584
|
+
return h;
|
|
585
|
+
}
|
|
586
|
+
id(handle) {
|
|
587
|
+
return this.#ids[handle];
|
|
588
|
+
}
|
|
589
|
+
ids(handles) {
|
|
590
|
+
const out = [];
|
|
591
|
+
for (let i = 0; i < handles.length; i++) {
|
|
592
|
+
const id = this.#ids[handles[i]];
|
|
593
|
+
if (id !== undefined)
|
|
594
|
+
out.push(id);
|
|
595
|
+
}
|
|
596
|
+
return out;
|
|
597
|
+
}
|
|
598
|
+
release(id) {
|
|
599
|
+
const h = this.#handles.get(id);
|
|
600
|
+
if (h === undefined)
|
|
601
|
+
return undefined;
|
|
602
|
+
this.#handles.delete(id);
|
|
603
|
+
this.#ids[h] = undefined;
|
|
604
|
+
this.#pending.push(h);
|
|
605
|
+
return h;
|
|
606
|
+
}
|
|
607
|
+
clear() {
|
|
608
|
+
for (const h of this.#handles.values())
|
|
609
|
+
this.#pending.push(h);
|
|
610
|
+
this.#handles.clear();
|
|
611
|
+
this.#ids.fill(undefined);
|
|
612
|
+
}
|
|
613
|
+
/** Makes released handles reusable. */
|
|
614
|
+
flush() {
|
|
615
|
+
this.#free.push(...this.#pending);
|
|
616
|
+
this.#pending.length = 0;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
// ------------------------------------------------------------------ patches
|
|
620
|
+
const CAPS = { butt: 0, round: 1, square: 2 };
|
|
621
|
+
const JOINS = { miter: 0, round: 1, bevel: 2 };
|
|
622
|
+
/** Encodes patches; keeps the id registries and a per-item path parse cache. */
|
|
623
|
+
export class PatchEncoder {
|
|
624
|
+
items = new IdRegistry();
|
|
625
|
+
textures = new IdRegistry();
|
|
626
|
+
#pathCache = new Map();
|
|
627
|
+
#commands(item) {
|
|
628
|
+
const prev = this.#pathCache.get(item.id);
|
|
629
|
+
const closed = item.closed ?? false;
|
|
630
|
+
if (prev && prev.d === item.d && prev.points === item.points && prev.closed === closed)
|
|
631
|
+
return prev.cmds;
|
|
632
|
+
const cmds = item.d !== undefined ? parseSvgPath(item.d) : item.points ? polylineCommands(item.points, closed) : [];
|
|
633
|
+
this.#pathCache.set(item.id, { d: item.d, points: item.points, closed, cmds });
|
|
634
|
+
return cmds;
|
|
635
|
+
}
|
|
636
|
+
encode(patch) {
|
|
637
|
+
const upserts = patch.upsert ?? [];
|
|
638
|
+
const removes = patch.remove ?? [];
|
|
639
|
+
const n = (patch.clear ? 1 : 0) + removes.length + upserts.length;
|
|
640
|
+
const ops = new Float64Array(n * REC);
|
|
641
|
+
const pathChunks = [];
|
|
642
|
+
let pathLen = 0;
|
|
643
|
+
let r = 0;
|
|
644
|
+
if (patch.clear) {
|
|
645
|
+
ops[0] = OP_CLEAR;
|
|
646
|
+
this.items.clear();
|
|
647
|
+
this.#pathCache.clear();
|
|
648
|
+
r = 1;
|
|
649
|
+
}
|
|
650
|
+
for (const id of removes) {
|
|
651
|
+
const h = this.items.release(id);
|
|
652
|
+
this.#pathCache.delete(id);
|
|
653
|
+
if (h === undefined)
|
|
654
|
+
continue;
|
|
655
|
+
const o = r * REC;
|
|
656
|
+
ops[o] = OP_REMOVE;
|
|
657
|
+
ops[o + 1] = h;
|
|
658
|
+
r++;
|
|
659
|
+
}
|
|
660
|
+
for (const item of upserts) {
|
|
661
|
+
const o = r * REC;
|
|
662
|
+
r++;
|
|
663
|
+
ops[o] = OP_UPSERT;
|
|
664
|
+
ops[o + 1] = this.items.ensure(item.id);
|
|
665
|
+
ops[o + 3] = item.layer ?? 0;
|
|
666
|
+
ops[o + 4] = item.z ?? 0;
|
|
667
|
+
ops[o + 5] = item.x;
|
|
668
|
+
ops[o + 6] = item.y;
|
|
669
|
+
ops[o + 7] = item.w;
|
|
670
|
+
ops[o + 8] = item.h;
|
|
671
|
+
ops[o + 9] = item.rotation ?? 0;
|
|
672
|
+
ops[o + 10] = encodeFlags(item);
|
|
673
|
+
ops[o + 11] = item.opacity ?? 1;
|
|
674
|
+
ops[o + 22] = -1;
|
|
675
|
+
switch (item.kind) {
|
|
676
|
+
case 'hole':
|
|
677
|
+
ops[o + 2] = KIND_HOLE;
|
|
678
|
+
ops[o + 15] = item.radius ?? 0;
|
|
679
|
+
break;
|
|
680
|
+
case 'path': {
|
|
681
|
+
ops[o + 2] = KIND_PATH;
|
|
682
|
+
ops[o + 12] = parseColor(item.fill);
|
|
683
|
+
ops[o + 13] = parseColor(item.stroke);
|
|
684
|
+
ops[o + 14] = item.strokeWidth ?? 1;
|
|
685
|
+
ops[o + 16] = item.dash?.[0] ?? 0;
|
|
686
|
+
ops[o + 17] = item.dash?.[1] ?? 0;
|
|
687
|
+
const cmds = this.#commands(item);
|
|
688
|
+
ops[o + 23] = pathLen;
|
|
689
|
+
ops[o + 24] = cmds.length;
|
|
690
|
+
pathChunks.push(cmds);
|
|
691
|
+
pathLen += cmds.length;
|
|
692
|
+
// Cut-out rects follow the commands in the path stream; their count is the record's last slot.
|
|
693
|
+
const cuts = item.cuts?.slice(0, MAX_CUTS) ?? [];
|
|
694
|
+
if (cuts.length) {
|
|
695
|
+
const flat = cuts.flatMap(c => [c.x, c.y, c.w, c.h]);
|
|
696
|
+
pathChunks.push(flat);
|
|
697
|
+
pathLen += flat.length;
|
|
698
|
+
ops[o + 31] = cuts.length;
|
|
699
|
+
}
|
|
700
|
+
ops[o + 25] = item.viewBox?.[0] ?? 0;
|
|
701
|
+
ops[o + 26] = item.viewBox?.[1] ?? 0;
|
|
702
|
+
ops[o + 27] = CAPS[item.cap ?? 'butt'];
|
|
703
|
+
ops[o + 28] = JOINS[item.join ?? 'miter'];
|
|
704
|
+
ops[o + 29] = item.miterLimit ?? 4;
|
|
705
|
+
ops[o + 30] = item.fillRule === 'evenodd' ? 1 : 0;
|
|
706
|
+
break;
|
|
707
|
+
}
|
|
708
|
+
default: {
|
|
709
|
+
ops[o + 2] = KIND_BOX;
|
|
710
|
+
ops[o + 12] = parseColor(item.fill);
|
|
711
|
+
ops[o + 13] = parseColor(item.stroke);
|
|
712
|
+
ops[o + 14] = item.strokeWidth ?? (item.stroke !== undefined ? 1 : 0);
|
|
713
|
+
ops[o + 15] = item.radius ?? 0;
|
|
714
|
+
ops[o + 16] = item.dash?.[0] ?? 0;
|
|
715
|
+
ops[o + 17] = item.dash?.[1] ?? 0;
|
|
716
|
+
if (item.shadow) {
|
|
717
|
+
ops[o + 18] = parseColor(item.shadow.color);
|
|
718
|
+
ops[o + 19] = item.shadow.blur;
|
|
719
|
+
ops[o + 20] = item.shadow.dx ?? 0;
|
|
720
|
+
ops[o + 21] = item.shadow.dy ?? 0;
|
|
721
|
+
}
|
|
722
|
+
if (item.texture !== undefined)
|
|
723
|
+
ops[o + 22] = this.textures.ensure(item.texture);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
const paths = new Float32Array(pathLen);
|
|
728
|
+
let off = 0;
|
|
729
|
+
for (const c of pathChunks) {
|
|
730
|
+
paths.set(c, off);
|
|
731
|
+
off += c.length;
|
|
732
|
+
}
|
|
733
|
+
this.items.flush();
|
|
734
|
+
return { ops: r * REC === ops.length ? ops : ops.subarray(0, r * REC), paths, records: r };
|
|
735
|
+
}
|
|
736
|
+
}
|