@open-artifact/canvas-engine 0.0.0-stage → 0.1.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/src/types.d.ts ADDED
@@ -0,0 +1,355 @@
1
+ /**
2
+ * Public types of the canvas engine. Coordinates are world units unless named
3
+ * `screen` (CSS pixels relative to the canvas' top-left corner). Rotation is in
4
+ * radians, clockwise, about the item's centre.
5
+ */
6
+ /**
7
+ * A colour: a CSS colour (named, `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()`/`rgba()`,
8
+ * `hsl()`/`hsla()`, `transparent`; not `currentColor`, `var()` or newer spaces), `none`,
9
+ * or a 0xRRGGBBAA number.
10
+ */
11
+ export type Color = string | number;
12
+ export interface Point {
13
+ x: number;
14
+ y: number;
15
+ }
16
+ export interface Rect {
17
+ x: number;
18
+ y: number;
19
+ w: number;
20
+ h: number;
21
+ }
22
+ /**
23
+ * Behaviour flags. Defaults: visible, hittable, selectable, movable, resizable, snap
24
+ * target, hover and selection outline are on, all handles show, normal blending;
25
+ * lockAspect, hitStrokeOnly and screenStroke are off.
26
+ */
27
+ export interface ItemFlags {
28
+ visible?: boolean;
29
+ /** Found by hit tests and hover. */
30
+ hittable?: boolean;
31
+ /** Clicking or marquee selects it. */
32
+ selectable?: boolean;
33
+ /** Dragging it moves it (and emits a `move` commit). */
34
+ movable?: boolean;
35
+ /** Shows resize handles when it is the only selected item. */
36
+ resizable?: boolean;
37
+ /** Other items snap to its edges and centre. */
38
+ snap?: boolean;
39
+ /** Resizing keeps the aspect ratio (as Shift does). */
40
+ lockAspect?: boolean;
41
+ /** Only the stroke is hit, not the fill (outlines, connectors). */
42
+ hitStrokeOnly?: boolean;
43
+ /** Stroke width and dashes are in screen pixels, not world units. */
44
+ screenStroke?: boolean;
45
+ /** Draws the hover outline when the pointer is over it. */
46
+ hover?: boolean;
47
+ /** Draws the selection outline and handles when selected (it still counts in the group box). */
48
+ selectionOutline?: boolean;
49
+ /** Which resize handles show: all eight, the four corners, or the left and right edges only. */
50
+ handles?: 'all' | 'corners' | 'width';
51
+ /** 'multiply' darkens what is below (ink on paper). Items blending differently draw in separate batches. */
52
+ blend?: 'normal' | 'multiply';
53
+ }
54
+ interface ItemBase extends ItemFlags {
55
+ /** Stable id, unique across the engine. */
56
+ id: string;
57
+ x: number;
58
+ y: number;
59
+ w: number;
60
+ h: number;
61
+ rotation?: number;
62
+ /** Draw order: by layer, then z, then insertion order. */
63
+ layer?: number;
64
+ z?: number;
65
+ /** 0..1, default 1. */
66
+ opacity?: number;
67
+ }
68
+ export interface Shadow {
69
+ color: Color;
70
+ blur: number;
71
+ dx?: number;
72
+ dy?: number;
73
+ }
74
+ /** A rounded rectangle, optionally textured (an image, a rendered artboard, text). */
75
+ export interface RectItem extends ItemBase {
76
+ kind: 'rect' | 'image';
77
+ /** Fill; for a textured rect, the placeholder shown until the texture arrives. */
78
+ fill?: Color;
79
+ stroke?: Color;
80
+ strokeWidth?: number;
81
+ radius?: number;
82
+ /** Stroke dash [on, off]. */
83
+ dash?: [number, number];
84
+ shadow?: Shadow;
85
+ /** Texture key (see `CanvasEngine.setTexture`); missing keys are asked for with `textureNeeded`. */
86
+ texture?: string;
87
+ }
88
+ export type LineCap = 'butt' | 'round' | 'square';
89
+ export type LineJoin = 'miter' | 'round' | 'bevel';
90
+ /**
91
+ * A vector path in the item's box. Geometry is either SVG path data (`d`) or a
92
+ * polyline (`points`, flat [x0, y0, x1, y1, …]). Coordinates are relative to the
93
+ * box's top-left corner, or to `viewBox` (scaled to fill the box) when given.
94
+ */
95
+ export interface PathItem extends ItemBase {
96
+ kind: 'path';
97
+ d?: string;
98
+ points?: ArrayLike<number>;
99
+ /** Close the polyline. */
100
+ closed?: boolean;
101
+ viewBox?: [number, number];
102
+ fill?: Color;
103
+ fillRule?: 'nonzero' | 'evenodd';
104
+ stroke?: Color;
105
+ strokeWidth?: number;
106
+ cap?: LineCap;
107
+ join?: LineJoin;
108
+ miterLimit?: number;
109
+ dash?: [number, number];
110
+ /**
111
+ * Up to two rects (in path coordinates) where the path is not drawn, e.g. the gap
112
+ * a connector leaves for its label.
113
+ */
114
+ cuts?: Rect[];
115
+ }
116
+ /**
117
+ * A transparent cut-out: whatever the engine draws below it is not drawn, so DOM
118
+ * content placed under the canvas shows through. Items above it still draw on top.
119
+ * Visible holes are reported with the `holes` event, in screen space.
120
+ */
121
+ export interface HoleItem extends ItemBase {
122
+ kind: 'hole';
123
+ radius?: number;
124
+ }
125
+ export type CanvasItem = RectItem | PathItem | HoleItem;
126
+ /** A change to the engine's mirror. Upserts replace the whole item. */
127
+ export interface Patch {
128
+ /** Remove everything first. */
129
+ clear?: boolean;
130
+ remove?: string[];
131
+ upsert?: CanvasItem[];
132
+ }
133
+ /** An image the engine can upload. */
134
+ export type TextureSource = ImageBitmap | HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageData;
135
+ export type Tool = 'select' | 'pan' | 'none';
136
+ export interface Theme {
137
+ background: Color;
138
+ grid: Color;
139
+ selection: Color;
140
+ handle: Color;
141
+ guide: Color;
142
+ marquee: Color;
143
+ hover: Color;
144
+ }
145
+ export interface EngineOptions {
146
+ minZoom: number;
147
+ maxZoom: number;
148
+ /** Snap moves and resizes to other items' edges and centres (Ctrl/Cmd disables while held). */
149
+ snap: boolean;
150
+ /** Snap distance in screen pixels. */
151
+ snapDistance: number;
152
+ /** Pointer travel (px) before a press becomes a drag. */
153
+ dragThreshold: number;
154
+ /** Hit tolerance (px). */
155
+ hitTolerance: number;
156
+ /** Resize handle size (px). */
157
+ handleSize: number;
158
+ /** Quiet time (ms) after which the camera counts as settled. */
159
+ settleMs: number;
160
+ /** Minimum interval (ms) between `camera` events while it moves. */
161
+ cameraEventMs: number;
162
+ /** Plain wheel zooms (instead of panning). */
163
+ wheelZoom: boolean;
164
+ /** Wheel zoom sensitivity per delta unit (px): one event zooms by `exp(-deltaY × zoomSpeed)`. */
165
+ zoomSpeed: number;
166
+ /** The largest zoom factor one wheel event applies (zooming out, its inverse); default 1.35, `Infinity` for none. */
167
+ wheelZoomMax: number;
168
+ /**
169
+ * 'engine': the engine applies selection changes and reports `selection`.
170
+ * 'controlled': it only proposes them (`commit` with op 'select'); call `setSelection`.
171
+ */
172
+ selection: 'engine' | 'controlled';
173
+ /** Marquee selects items it touches ('intersect') or fully contains ('contain'). */
174
+ marquee: 'intersect' | 'contain';
175
+ /** Texture fade-in (ms); 0 disables. */
176
+ fadeMs: number;
177
+ /** Emit `dragging` on every pointer move of a drag (e.g. to reroute connectors). */
178
+ dragEvents: boolean;
179
+ grid: 'none' | 'dots' | 'lines';
180
+ /** Grid spacing in world units at the finest level. */
181
+ gridSpacing: number;
182
+ /** Each coarser grid level is this many times the previous one (lines grid: every n-th line is major). Default 5. */
183
+ gridMajor: number;
184
+ /** A level fades in between these on-screen spacings (px). Default [8, 16]. */
185
+ gridFade: [number, number];
186
+ /** GPU memory the texture atlas may use, in bytes (least recently used textures are evicted beyond it). */
187
+ textureBudget: number;
188
+ theme: Partial<Theme>;
189
+ }
190
+ export interface CameraState {
191
+ /** World point at the canvas' top-left corner. */
192
+ x: number;
193
+ y: number;
194
+ zoom: number;
195
+ }
196
+ export interface GlideOptions {
197
+ /** ms, default 300; 0 jumps. */
198
+ duration?: number;
199
+ /** Screen padding around the rect (px), default 40. */
200
+ padding?: number;
201
+ /** Zoom cap for the fit, default the engine's max zoom. */
202
+ maxZoom?: number;
203
+ }
204
+ /** A visible hole in screen space, with a CSS transform that places a world-sized element over it. */
205
+ export interface HoleRect {
206
+ id: string;
207
+ /** Screen position of the unrotated box's top-left corner. */
208
+ x: number;
209
+ y: number;
210
+ /** Screen size. */
211
+ w: number;
212
+ h: number;
213
+ rotation: number;
214
+ zoom: number;
215
+ /**
216
+ * `matrix(...)` for an element of the item's world size with `transform-origin: 0 0`,
217
+ * positioned at the canvas' top-left: it covers the hole exactly (rotation included).
218
+ */
219
+ transform: string;
220
+ }
221
+ /** Modifier keys held when an event happened. */
222
+ export interface Modifiers {
223
+ shift: boolean;
224
+ alt: boolean;
225
+ ctrl: boolean;
226
+ meta: boolean;
227
+ }
228
+ /** Which handle: a corner or an edge midpoint, by compass direction. */
229
+ export type Handle = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w';
230
+ export type CommitIntent = ({
231
+ op: 'move';
232
+ ids: string[];
233
+ dx: number;
234
+ dy: number;
235
+ } | {
236
+ op: 'resize';
237
+ id: string;
238
+ rect: Rect;
239
+ } | {
240
+ op: 'select';
241
+ ids: string[];
242
+ }) & {
243
+ modifiers: Modifiers;
244
+ };
245
+ /** A double click or context menu on the canvas: the item under it (if any) and where. */
246
+ export interface PointerItemEvent {
247
+ id: string | null;
248
+ world: Point;
249
+ screen: Point;
250
+ modifiers: Modifiers;
251
+ /** The DOM event, when it came from one (to `preventDefault` it). */
252
+ event?: MouseEvent;
253
+ }
254
+ export type GestureKind = 'move' | 'resize' | 'marquee' | 'pan' | 'pinch';
255
+ export interface CanvasEvents {
256
+ /** The selection changed ('engine' selection mode). */
257
+ selection: {
258
+ ids: string[];
259
+ modifiers: Modifiers;
260
+ };
261
+ /** The camera moved (throttled), and once more with `settled` when it stops. */
262
+ camera: CameraState & {
263
+ settled: boolean;
264
+ };
265
+ /** The camera started or stopped moving. */
266
+ moving: {
267
+ moving: boolean;
268
+ };
269
+ /** Visible holes changed (sent at most once per frame). */
270
+ holes: {
271
+ holes: HoleRect[];
272
+ };
273
+ /** A finished gesture the document should apply. The mirror already shows the result. */
274
+ commit: CommitIntent;
275
+ /** Textures the engine wants: visible items whose key it does not hold. Asked once until evicted. */
276
+ textureNeeded: {
277
+ keys: string[];
278
+ };
279
+ hover: {
280
+ id: string | null;
281
+ };
282
+ gesture: {
283
+ kind: GestureKind;
284
+ phase: 'start' | 'end' | 'cancel';
285
+ modifiers: Modifiers;
286
+ };
287
+ /** During a drag, when `dragEvents` is on. */
288
+ dragging: {
289
+ ids: string[];
290
+ dx: number;
291
+ dy: number;
292
+ modifiers: Modifiers;
293
+ };
294
+ /** During a resize, when `dragEvents` is on: the rect the item has now. */
295
+ resizing: {
296
+ id: string;
297
+ rect: Rect;
298
+ modifiers: Modifiers;
299
+ };
300
+ dblclick: PointerItemEvent;
301
+ contextmenu: PointerItemEvent;
302
+ /** After each drawn frame: time spent in the engine for it (ms). */
303
+ frame: {
304
+ engineMs: number;
305
+ };
306
+ contextlost: Record<string, never>;
307
+ contextrestored: Record<string, never>;
308
+ }
309
+ export type CanvasEventName = keyof CanvasEvents;
310
+ export interface Peer {
311
+ color: Color;
312
+ /** World position of their pointer. */
313
+ cursor?: Point | null;
314
+ selection?: string[];
315
+ }
316
+ export interface EngineStats {
317
+ items: number;
318
+ /** Items in the viewport at the last visibility pass. */
319
+ visible: number;
320
+ /** Draw calls in the last frame. */
321
+ drawCalls: number;
322
+ textures: number;
323
+ textureBytes: number;
324
+ atlasLayers: number;
325
+ maxAtlasLayers: number;
326
+ quads: number;
327
+ pathVertices: number;
328
+ pathIndices: number;
329
+ batches: number;
330
+ holes: number;
331
+ /** Size of the wasm linear memory (bytes). */
332
+ wasmMemory: number;
333
+ }
334
+ export interface CreateOptions extends Partial<EngineOptions> {
335
+ /** Device pixel ratio (default window.devicePixelRatio). */
336
+ dpr?: number;
337
+ /** Attach pointer, wheel, keyboard and resize listeners (default true). */
338
+ listen?: boolean;
339
+ /** Initial tool (default 'select'). */
340
+ tool?: Tool;
341
+ }
342
+ /** Pointer input for `CanvasEngine.input` (tests and custom event routing). Coordinates are screen. */
343
+ export interface PointerInput {
344
+ id?: number;
345
+ x: number;
346
+ y: number;
347
+ button?: number;
348
+ buttons?: number;
349
+ shift?: boolean;
350
+ alt?: boolean;
351
+ ctrl?: boolean;
352
+ meta?: boolean;
353
+ type?: 'mouse' | 'pen' | 'touch';
354
+ }
355
+ export {};
package/src/types.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Public types of the canvas engine. Coordinates are world units unless named
3
+ * `screen` (CSS pixels relative to the canvas' top-left corner). Rotation is in
4
+ * radians, clockwise, about the item's centre.
5
+ */
6
+ export {};