@g9tm/lynxcontrols-canvas 0.0.0-stage → 2.5.8

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.
Files changed (60) hide show
  1. package/README.md +39 -2
  2. package/catalog/cards/G9Canvas.md +17 -0
  3. package/catalog/cards/G9CanvasStylePanel.md +13 -0
  4. package/catalog/catalog.json +2334 -0
  5. package/catalog/index.md +6 -0
  6. package/dist/.tsbuildinfo +1 -0
  7. package/dist/board-controller.d.ts +161 -0
  8. package/dist/board-controller.d.ts.map +1 -0
  9. package/dist/board-controller.jsx +812 -0
  10. package/dist/board-model.d.ts +42 -0
  11. package/dist/board-model.d.ts.map +1 -0
  12. package/dist/board-model.jsx +66 -0
  13. package/dist/canvas.css +81 -0
  14. package/dist/canvas.css.map +1 -0
  15. package/dist/canvas.d.ts +144 -0
  16. package/dist/canvas.d.ts.map +1 -0
  17. package/dist/canvas.jsx +481 -0
  18. package/dist/element-core.d.ts +155 -0
  19. package/dist/element-core.d.ts.map +1 -0
  20. package/dist/element-core.jsx +1104 -0
  21. package/dist/export.d.ts +7 -0
  22. package/dist/export.d.ts.map +1 -0
  23. package/dist/export.jsx +75 -0
  24. package/dist/index.d.ts +8 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.jsx +7 -0
  27. package/dist/observable.d.ts +5 -0
  28. package/dist/observable.d.ts.map +1 -0
  29. package/dist/observable.jsx +14 -0
  30. package/dist/protocol.d.ts +258 -0
  31. package/dist/protocol.d.ts.map +1 -0
  32. package/dist/protocol.jsx +2 -0
  33. package/dist/scene/edit.d.ts +59 -0
  34. package/dist/scene/edit.d.ts.map +1 -0
  35. package/dist/scene/edit.jsx +895 -0
  36. package/dist/scene/elements.d.ts +160 -0
  37. package/dist/scene/elements.d.ts.map +1 -0
  38. package/dist/scene/elements.jsx +392 -0
  39. package/dist/scene/geometry.d.ts +36 -0
  40. package/dist/scene/geometry.d.ts.map +1 -0
  41. package/dist/scene/geometry.jsx +367 -0
  42. package/dist/scene/index.d.ts +8 -0
  43. package/dist/scene/index.d.ts.map +1 -0
  44. package/dist/scene/index.jsx +6 -0
  45. package/dist/scene/path-data-parser.d.ts +17 -0
  46. package/dist/scene/path-data-parser.d.ts.map +1 -0
  47. package/dist/scene/path-data-parser.jsx +128 -0
  48. package/dist/scene/scene.d.ts +58 -0
  49. package/dist/scene/scene.d.ts.map +1 -0
  50. package/dist/scene/scene.jsx +169 -0
  51. package/dist/scene/shapes.d.ts +24 -0
  52. package/dist/scene/shapes.d.ts.map +1 -0
  53. package/dist/scene/shapes.jsx +807 -0
  54. package/dist/scene/types.d.ts +118 -0
  55. package/dist/scene/types.d.ts.map +1 -0
  56. package/dist/scene/types.jsx +1 -0
  57. package/dist/words.d.ts +100 -0
  58. package/dist/words.d.ts.map +1 -0
  59. package/dist/words.jsx +186 -0
  60. package/package.json +50 -4
@@ -0,0 +1,1104 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) Object.defineProperty(obj, key, {
3
+ value: value,
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true
7
+ });
8
+ else obj[key] = value;
9
+ return obj;
10
+ }
11
+ const MIN_ZOOM = 0.1;
12
+ const MAX_ZOOM = 30;
13
+ const DOUBLE_TAP_MS = 350;
14
+ class CanvasCore {
15
+ apply(message) {
16
+ let scene = false;
17
+ if (message.reset) {
18
+ this.items.clear();
19
+ this.order = [];
20
+ scene = true;
21
+ }
22
+ if (message.items) {
23
+ for (const item of message.items)this.items.set(item.id, item);
24
+ scene = true;
25
+ }
26
+ if (message.remove) {
27
+ for (const id of message.remove)this.items.delete(id);
28
+ scene = true;
29
+ }
30
+ if (message.order) {
31
+ this.order = message.order;
32
+ scene = true;
33
+ }
34
+ if (message.files) {
35
+ this.files = {
36
+ ...this.files,
37
+ ...message.files
38
+ };
39
+ scene = true;
40
+ }
41
+ if (message.hidden) {
42
+ this.hidden = new Set(message.hidden);
43
+ scene = true;
44
+ }
45
+ if (message.faded) {
46
+ this.faded = new Set(message.faded);
47
+ scene = true;
48
+ }
49
+ if (void 0 !== message.selection) {
50
+ var _ref, _ref1;
51
+ var _this_selection, _this_selection1;
52
+ const movingBefore = null != (_ref = null == (_this_selection = this.selection) ? void 0 : _this_selection.ids.join()) ? _ref : '';
53
+ this.selection = message.selection;
54
+ if (this.live && movingBefore !== (null != (_ref1 = null == (_this_selection1 = this.selection) ? void 0 : _this_selection1.ids.join()) ? _ref1 : '')) scene = true;
55
+ }
56
+ if (message.peers) this.peers = message.peers;
57
+ if (message.done) for (const g of message.done){
58
+ var _this_live;
59
+ this.previews.delete(g);
60
+ if ((null == (_this_live = this.live) ? void 0 : _this_live.g) === g) {
61
+ this.live = null;
62
+ scene = true;
63
+ }
64
+ }
65
+ if (message.gesture) this.answer(message.gesture.id, message.gesture.grab);
66
+ if (message.view) this.setView(message.view, true);
67
+ if (scene) this.sceneVersion++;
68
+ this.host.invalidate();
69
+ }
70
+ setTool(tool) {
71
+ if (tool === this.tool) return;
72
+ this.tool = tool;
73
+ this.cancelGesture();
74
+ }
75
+ setSize(width, height) {
76
+ if (width === this.width && height === this.height) return;
77
+ this.width = width;
78
+ this.height = height;
79
+ this.sceneVersion++;
80
+ this.emitView(true);
81
+ this.host.invalidate();
82
+ }
83
+ toScene(x, y) {
84
+ return [
85
+ x / this.view.zoom - this.view.scrollX,
86
+ y / this.view.zoom - this.view.scrollY
87
+ ];
88
+ }
89
+ toScreen(x, y) {
90
+ return [
91
+ (x + this.view.scrollX) * this.view.zoom,
92
+ (y + this.view.scrollY) * this.view.zoom
93
+ ];
94
+ }
95
+ setView(view, announce) {
96
+ const zoom = Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, view.zoom));
97
+ if (!Number.isFinite(view.scrollX) || !Number.isFinite(view.scrollY) || !Number.isFinite(zoom)) return;
98
+ this.view = {
99
+ scrollX: view.scrollX,
100
+ scrollY: view.scrollY,
101
+ zoom
102
+ };
103
+ if (announce) this.emitView(true);
104
+ this.host.invalidate();
105
+ }
106
+ zoomAt(x, y, zoom) {
107
+ const [sx, sy] = this.toScene(x, y);
108
+ const next = Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, zoom));
109
+ this.setView({
110
+ zoom: next,
111
+ scrollX: x / next - sx,
112
+ scrollY: y / next - sy
113
+ }, false);
114
+ this.emitView(false);
115
+ }
116
+ wheel(dx, dy, x, y, ctrl, shift) {
117
+ if (ctrl) {
118
+ const factor = Math.exp(0.01 * -Math.max(-50, Math.min(50, dy)));
119
+ this.zoomAt(x, y, this.view.zoom * factor);
120
+ return;
121
+ }
122
+ const [mx, my] = shift && !dx ? [
123
+ dy,
124
+ 0
125
+ ] : [
126
+ dx,
127
+ dy
128
+ ];
129
+ this.setView({
130
+ zoom: this.view.zoom,
131
+ scrollX: this.view.scrollX - mx / this.view.zoom,
132
+ scrollY: this.view.scrollY - my / this.view.zoom
133
+ }, false);
134
+ this.emitView(false);
135
+ }
136
+ emitView(force) {
137
+ const now = this.host.now();
138
+ const send = ()=>{
139
+ this.lastViewEmit = this.host.now();
140
+ this.viewTimer = null;
141
+ this.host.emit('cvview', {
142
+ ...this.view,
143
+ width: this.width,
144
+ height: this.height
145
+ });
146
+ };
147
+ if (force || now - this.lastViewEmit > 120) {
148
+ if (this.viewTimer) clearTimeout(this.viewTimer);
149
+ send();
150
+ } else if (!this.viewTimer) this.viewTimer = setTimeout(send, 120);
151
+ }
152
+ framePolygon() {
153
+ var _this_selection, _this_selection1;
154
+ const frame = null == (_this_selection = this.selection) ? void 0 : _this_selection.frame;
155
+ if (!frame || (null == (_this_selection1 = this.selection) ? void 0 : _this_selection1.handles) === 'none') return null;
156
+ return {
157
+ box: frame.b,
158
+ angle: frame.a
159
+ };
160
+ }
161
+ handles(touch = false) {
162
+ var _this_selection;
163
+ const frame = this.framePolygon();
164
+ if (!frame || !this.editable) return [];
165
+ const [x1, y1, x2, y2] = frame.box;
166
+ const pad = (touch ? 8 : 4) / this.view.zoom;
167
+ const cx = (x1 + x2) / 2;
168
+ const cy = (y1 + y2) / 2;
169
+ const left = x1 - pad;
170
+ const right = x2 + pad;
171
+ const top = y1 - pad;
172
+ const bottom = y2 + pad;
173
+ const list = [
174
+ {
175
+ handle: 'nw',
176
+ x: left,
177
+ y: top
178
+ },
179
+ {
180
+ handle: 'ne',
181
+ x: right,
182
+ y: top
183
+ },
184
+ {
185
+ handle: 'sw',
186
+ x: left,
187
+ y: bottom
188
+ },
189
+ {
190
+ handle: 'se',
191
+ x: right,
192
+ y: bottom
193
+ }
194
+ ];
195
+ const small = (x2 - x1) * this.view.zoom < 40 || (y2 - y1) * this.view.zoom < 40;
196
+ if ((null == (_this_selection = this.selection) ? void 0 : _this_selection.handles) === 'all' && !small) list.push({
197
+ handle: 'n',
198
+ x: cx,
199
+ y: top
200
+ }, {
201
+ handle: 's',
202
+ x: cx,
203
+ y: bottom
204
+ }, {
205
+ handle: 'w',
206
+ x: left,
207
+ y: cy
208
+ }, {
209
+ handle: 'e',
210
+ x: right,
211
+ y: cy
212
+ });
213
+ list.push({
214
+ handle: 'rotation',
215
+ x: cx,
216
+ y: top - (touch ? 34 : 22) / this.view.zoom
217
+ });
218
+ return list.map((entry)=>{
219
+ const [x, y] = rotatePoint([
220
+ entry.x,
221
+ entry.y
222
+ ], [
223
+ cx,
224
+ cy
225
+ ], frame.angle);
226
+ return {
227
+ handle: entry.handle,
228
+ x,
229
+ y
230
+ };
231
+ });
232
+ }
233
+ insideFrame(point) {
234
+ var _ref;
235
+ var _this_selection, _this_selection1;
236
+ const frame = null != (_ref = null == (_this_selection = this.selection) ? void 0 : _this_selection.frame) ? _ref : (null == (_this_selection1 = this.selection) ? void 0 : _this_selection1.boxes.length) === 1 ? this.selection.boxes[0] : null;
237
+ if (!frame) return false;
238
+ const [x1, y1, x2, y2] = frame.b;
239
+ const [px, py] = rotatePoint(point, [
240
+ (x1 + x2) / 2,
241
+ (y1 + y2) / 2
242
+ ], -frame.a);
243
+ const pad = 4 / this.view.zoom;
244
+ return px >= x1 - pad && px <= x2 + pad && py >= y1 - pad && py <= y2 + pad;
245
+ }
246
+ liveMatrix() {
247
+ var _ref;
248
+ var _this_live;
249
+ const gesture = this.gesture;
250
+ if ((null == gesture ? void 0 : gesture.kind) === 'move' && this.moveStarted) return translate(gesture.end[0] - gesture.start[0], gesture.end[1] - gesture.start[1]);
251
+ if ((null == gesture ? void 0 : gesture.kind) === 'resize') return resizeMatrix(gesture.from, gesture.to, gesture.angle);
252
+ if ((null == gesture ? void 0 : gesture.kind) === 'rotate') return rotateMatrix(gesture.delta, gesture.pivot);
253
+ return null != (_ref = null == (_this_live = this.live) ? void 0 : _this_live.matrix) ? _ref : null;
254
+ }
255
+ movingIds() {
256
+ var _ref;
257
+ var _this_selection;
258
+ return this.liveMatrix() ? new Set(null != (_ref = null == (_this_selection = this.selection) ? void 0 : _this_selection.ids) ? _ref : []) : EMPTY;
259
+ }
260
+ marquee() {
261
+ const gesture = this.gesture;
262
+ if ((null == gesture ? void 0 : gesture.kind) !== 'marquee') return null;
263
+ return [
264
+ Math.min(gesture.start[0], gesture.end[0]),
265
+ Math.min(gesture.start[1], gesture.end[1]),
266
+ Math.max(gesture.start[0], gesture.end[0]),
267
+ Math.max(gesture.start[1], gesture.end[1])
268
+ ];
269
+ }
270
+ activePreviews() {
271
+ const list = [
272
+ ...this.previews.values()
273
+ ];
274
+ const gesture = this.gesture;
275
+ if ((null == gesture ? void 0 : gesture.kind) === 'draw') list.push({
276
+ kind: 'stroke',
277
+ points: gesture.points,
278
+ pressures: gesture.pressures
279
+ });
280
+ else if ((null == gesture ? void 0 : gesture.kind) === 'shape') list.push({
281
+ kind: 'shape',
282
+ tool: gesture.tool,
283
+ b: box(gesture.start, gesture.end)
284
+ });
285
+ else if ((null == gesture ? void 0 : gesture.kind) === 'line') list.push({
286
+ kind: 'line',
287
+ tool: gesture.tool,
288
+ points: [
289
+ gesture.start,
290
+ gesture.end
291
+ ]
292
+ });
293
+ else if ((null == gesture ? void 0 : gesture.kind) === 'point') list.push({
294
+ kind: 'point',
295
+ points: gesture.points
296
+ });
297
+ return list;
298
+ }
299
+ pointerDown(input) {
300
+ this.pointers.set(input.id, {
301
+ x: input.x,
302
+ y: input.y,
303
+ type: input.type
304
+ });
305
+ if (2 === this.pointers.size) return void this.startPinch();
306
+ if (this.pointers.size > 2) return;
307
+ const scene = this.toScene(input.x, input.y);
308
+ const touch = 'touch' === input.type;
309
+ if (1 === input.button || 2 === input.button || 'hand' === this.tool || !this.editable && 'laser' !== this.tool) {
310
+ if (2 === input.button) return;
311
+ this.gesture = {
312
+ kind: 'pan',
313
+ lastX: input.x,
314
+ lastY: input.y
315
+ };
316
+ this.panning = true;
317
+ return;
318
+ }
319
+ const g = ++this.seq;
320
+ switch(this.tool){
321
+ case 'freedraw':
322
+ var _input_pressure;
323
+ this.gesture = {
324
+ kind: 'draw',
325
+ g,
326
+ points: [
327
+ scene[0],
328
+ scene[1]
329
+ ],
330
+ pressures: 'pen' === input.type ? [
331
+ null != (_input_pressure = input.pressure) ? _input_pressure : 0.5
332
+ ] : null
333
+ };
334
+ break;
335
+ case 'rectangle':
336
+ case 'ellipse':
337
+ case 'diamond':
338
+ case 'frame':
339
+ this.gesture = {
340
+ kind: 'shape',
341
+ g,
342
+ tool: this.tool,
343
+ start: scene,
344
+ end: scene
345
+ };
346
+ break;
347
+ case 'arrow':
348
+ case 'line':
349
+ this.gesture = {
350
+ kind: 'line',
351
+ g,
352
+ tool: this.tool,
353
+ start: scene,
354
+ end: scene
355
+ };
356
+ break;
357
+ case 'eraser':
358
+ this.gesture = {
359
+ kind: 'erase',
360
+ g,
361
+ points: [
362
+ scene[0],
363
+ scene[1]
364
+ ]
365
+ };
366
+ this.eraserTrail = [
367
+ {
368
+ x: input.x,
369
+ y: input.y,
370
+ t: this.host.now()
371
+ }
372
+ ];
373
+ break;
374
+ case 'selection':
375
+ {
376
+ var _this_selection, _this_selection1;
377
+ const handle = this.handleAt(scene, touch);
378
+ const frame = this.framePolygon();
379
+ if (handle && frame) {
380
+ if ('rotation' === handle) {
381
+ const pivot = [
382
+ (frame.box[0] + frame.box[2]) / 2,
383
+ (frame.box[1] + frame.box[3]) / 2
384
+ ];
385
+ this.gesture = {
386
+ kind: 'rotate',
387
+ g,
388
+ pivot,
389
+ startAngle: Math.atan2(scene[1] - pivot[1], scene[0] - pivot[0]),
390
+ delta: 0,
391
+ from: frame.box,
392
+ shift: !!input.shift
393
+ };
394
+ } else this.gesture = {
395
+ kind: 'resize',
396
+ g,
397
+ handle,
398
+ from: frame.box,
399
+ angle: frame.angle,
400
+ to: frame.box,
401
+ shift: !!input.shift
402
+ };
403
+ this.sceneVersion++;
404
+ break;
405
+ }
406
+ const point = this.pointAt(scene, touch);
407
+ if (null !== point && (null == (_this_selection = this.selection) ? void 0 : _this_selection.points)) {
408
+ this.gesture = {
409
+ kind: 'point',
410
+ g,
411
+ index: point,
412
+ points: this.selection.points.map((p)=>[
413
+ p[0],
414
+ p[1]
415
+ ])
416
+ };
417
+ break;
418
+ }
419
+ if ((null == (_this_selection1 = this.selection) ? void 0 : _this_selection1.ids.length) && this.insideFrame(scene) && !input.shift) {
420
+ this.gesture = {
421
+ kind: 'move',
422
+ g,
423
+ start: scene,
424
+ end: scene,
425
+ alt: !!input.alt,
426
+ shift: !!input.shift
427
+ };
428
+ this.moveStarted = false;
429
+ break;
430
+ }
431
+ this.gesture = {
432
+ kind: 'pending',
433
+ g,
434
+ start: scene,
435
+ end: scene,
436
+ released: false,
437
+ shift: !!input.shift,
438
+ alt: !!input.alt,
439
+ since: this.host.now()
440
+ };
441
+ this.host.emit('cvdown', {
442
+ g,
443
+ x: scene[0],
444
+ y: scene[1],
445
+ ...input.shift ? {
446
+ shift: true
447
+ } : {}
448
+ });
449
+ break;
450
+ }
451
+ default:
452
+ this.gesture = {
453
+ kind: 'tap',
454
+ g,
455
+ start: scene,
456
+ screen: [
457
+ input.x,
458
+ input.y
459
+ ],
460
+ tool: this.tool,
461
+ shift: !!input.shift
462
+ };
463
+ break;
464
+ }
465
+ this.cursor(scene, true);
466
+ this.host.invalidate();
467
+ }
468
+ pointerMove(input) {
469
+ const known = this.pointers.get(input.id);
470
+ if (!known) {
471
+ if ('touch' !== input.type) this.cursor(this.toScene(input.x, input.y), false);
472
+ return;
473
+ }
474
+ known.x = input.x;
475
+ known.y = input.y;
476
+ const gesture = this.gesture;
477
+ if (!gesture) return;
478
+ const scene = this.toScene(input.x, input.y);
479
+ const slop = ('touch' === input.type ? 8 : 4) / this.view.zoom;
480
+ switch(gesture.kind){
481
+ case 'pan':
482
+ this.setView({
483
+ zoom: this.view.zoom,
484
+ scrollX: this.view.scrollX + (input.x - gesture.lastX) / this.view.zoom,
485
+ scrollY: this.view.scrollY + (input.y - gesture.lastY) / this.view.zoom
486
+ }, false);
487
+ gesture.lastX = input.x;
488
+ gesture.lastY = input.y;
489
+ this.emitView(false);
490
+ return;
491
+ case 'pinch':
492
+ this.updatePinch();
493
+ return;
494
+ case 'draw':
495
+ {
496
+ var _input_pressure;
497
+ var _gesture_pressures;
498
+ const last = gesture.points.length - 2;
499
+ if (Math.hypot(scene[0] - gesture.points[last], scene[1] - gesture.points[last + 1]) * this.view.zoom < 1) return;
500
+ gesture.points.push(scene[0], scene[1]);
501
+ null == (_gesture_pressures = gesture.pressures) || _gesture_pressures.push(null != (_input_pressure = input.pressure) ? _input_pressure : 0.5);
502
+ break;
503
+ }
504
+ case 'shape':
505
+ case 'line':
506
+ gesture.end = input.shift ? constrain(gesture.start, scene, 'line' === gesture.kind) : scene;
507
+ break;
508
+ case 'erase':
509
+ gesture.points.push(scene[0], scene[1]);
510
+ this.eraserTrail.push({
511
+ x: input.x,
512
+ y: input.y,
513
+ t: this.host.now()
514
+ });
515
+ break;
516
+ case 'pending':
517
+ gesture.end = scene;
518
+ break;
519
+ case 'move':
520
+ if (!this.moveStarted && Math.hypot(scene[0] - gesture.start[0], scene[1] - gesture.start[1]) < slop) return;
521
+ if (!this.moveStarted) {
522
+ this.moveStarted = true;
523
+ this.sceneVersion++;
524
+ }
525
+ gesture.end = input.shift ? constrain(gesture.start, scene, true) : scene;
526
+ break;
527
+ case 'marquee':
528
+ gesture.end = scene;
529
+ break;
530
+ case 'resize':
531
+ gesture.to = resizeBox(gesture.from, gesture.angle, gesture.handle, scene, !!input.shift || gesture.shift);
532
+ break;
533
+ case 'rotate':
534
+ {
535
+ let delta = Math.atan2(scene[1] - gesture.pivot[1], scene[0] - gesture.pivot[0]) - gesture.startAngle;
536
+ if (input.shift) delta = Math.PI / 12 * Math.round(delta / (Math.PI / 12));
537
+ gesture.delta = delta;
538
+ break;
539
+ }
540
+ case 'point':
541
+ gesture.points[gesture.index] = scene;
542
+ break;
543
+ case 'tap':
544
+ break;
545
+ }
546
+ this.cursor(scene, true);
547
+ this.host.invalidate();
548
+ }
549
+ pointerUp(input) {
550
+ if (!this.pointers.has(input.id)) return;
551
+ this.pointers.delete(input.id);
552
+ const gesture = this.gesture;
553
+ if ((null == gesture ? void 0 : gesture.kind) === 'pinch') {
554
+ if (0 === this.pointers.size) this.endCamera();
555
+ return;
556
+ }
557
+ if (this.pointers.size > 0) return;
558
+ this.gesture = null;
559
+ if (!gesture) return;
560
+ const scene = this.toScene(input.x, input.y);
561
+ const slop = ('touch' === input.type ? 8 : 4) / this.view.zoom;
562
+ switch(gesture.kind){
563
+ case 'pan':
564
+ this.endCamera();
565
+ break;
566
+ case 'draw':
567
+ this.previews.set(gesture.g, {
568
+ kind: 'stroke',
569
+ points: gesture.points,
570
+ pressures: gesture.pressures
571
+ });
572
+ this.host.emit('cvstroke', {
573
+ g: gesture.g,
574
+ points: gesture.points,
575
+ pressures: gesture.pressures
576
+ });
577
+ break;
578
+ case 'shape':
579
+ {
580
+ const b = box(gesture.start, gesture.end);
581
+ if (b[2] - b[0] < slop && b[3] - b[1] < slop) {
582
+ this.tap(gesture.g, scene, input, this.tool);
583
+ break;
584
+ }
585
+ this.previews.set(gesture.g, {
586
+ kind: 'shape',
587
+ tool: gesture.tool,
588
+ b
589
+ });
590
+ this.host.emit('cvshape', {
591
+ g: gesture.g,
592
+ tool: gesture.tool,
593
+ b
594
+ });
595
+ break;
596
+ }
597
+ case 'line':
598
+ {
599
+ if (Math.hypot(gesture.end[0] - gesture.start[0], gesture.end[1] - gesture.start[1]) < slop) {
600
+ this.tap(gesture.g, scene, input, this.tool);
601
+ break;
602
+ }
603
+ const points = [
604
+ gesture.start,
605
+ gesture.end
606
+ ];
607
+ this.previews.set(gesture.g, {
608
+ kind: 'line',
609
+ tool: gesture.tool,
610
+ points
611
+ });
612
+ this.host.emit('cvline', {
613
+ g: gesture.g,
614
+ tool: gesture.tool,
615
+ points
616
+ });
617
+ break;
618
+ }
619
+ case 'erase':
620
+ this.host.emit('cverase', {
621
+ g: gesture.g,
622
+ points: gesture.points
623
+ });
624
+ break;
625
+ case 'pending':
626
+ gesture.end = scene;
627
+ gesture.released = true;
628
+ if (Math.hypot(scene[0] - gesture.start[0], scene[1] - gesture.start[1]) < slop) this.tap(gesture.g, gesture.start, input, 'selection');
629
+ else this.gesture = gesture;
630
+ break;
631
+ case 'move':
632
+ if (!this.moveStarted) {
633
+ this.tap(gesture.g, gesture.start, input, 'selection');
634
+ break;
635
+ }
636
+ this.live = {
637
+ g: gesture.g,
638
+ matrix: translate(gesture.end[0] - gesture.start[0], gesture.end[1] - gesture.start[1])
639
+ };
640
+ this.host.emit('cvmove', {
641
+ g: gesture.g,
642
+ dx: gesture.end[0] - gesture.start[0],
643
+ dy: gesture.end[1] - gesture.start[1],
644
+ ...gesture.alt ? {
645
+ alt: true
646
+ } : {}
647
+ });
648
+ break;
649
+ case 'marquee':
650
+ {
651
+ const b = box(gesture.start, gesture.end);
652
+ this.host.emit('cvbox', {
653
+ g: gesture.g,
654
+ b,
655
+ ...gesture.shift ? {
656
+ shift: true
657
+ } : {}
658
+ });
659
+ break;
660
+ }
661
+ case 'resize':
662
+ if (sameBox(gesture.from, gesture.to)) {
663
+ this.sceneVersion++;
664
+ break;
665
+ }
666
+ this.live = {
667
+ g: gesture.g,
668
+ matrix: resizeMatrix(gesture.from, gesture.to, gesture.angle)
669
+ };
670
+ this.host.emit('cvtransform', {
671
+ g: gesture.g,
672
+ handle: gesture.handle,
673
+ from: gesture.from,
674
+ to: gesture.to,
675
+ angle: 0,
676
+ ...gesture.shift ? {
677
+ shift: true
678
+ } : {}
679
+ });
680
+ break;
681
+ case 'rotate':
682
+ if (Math.abs(gesture.delta) < 1e-3) {
683
+ this.sceneVersion++;
684
+ break;
685
+ }
686
+ this.live = {
687
+ g: gesture.g,
688
+ matrix: rotateMatrix(gesture.delta, gesture.pivot)
689
+ };
690
+ this.host.emit('cvtransform', {
691
+ g: gesture.g,
692
+ handle: 'rotation',
693
+ from: gesture.from,
694
+ to: gesture.from,
695
+ angle: gesture.delta
696
+ });
697
+ break;
698
+ case 'point':
699
+ {
700
+ const point = gesture.points[gesture.index];
701
+ this.previews.set(gesture.g, {
702
+ kind: 'point',
703
+ points: gesture.points
704
+ });
705
+ this.host.emit('cvpoint', {
706
+ g: gesture.g,
707
+ index: gesture.index,
708
+ x: point[0],
709
+ y: point[1]
710
+ });
711
+ break;
712
+ }
713
+ case 'tap':
714
+ if (Math.hypot(input.x - gesture.screen[0], input.y - gesture.screen[1]) < ('touch' === input.type ? 12 : 6)) this.tap(gesture.g, gesture.start, input, gesture.tool);
715
+ break;
716
+ }
717
+ this.cursor(scene, false);
718
+ this.host.invalidate();
719
+ }
720
+ pointerCancel(id) {
721
+ this.pointers.delete(id);
722
+ if (0 === this.pointers.size) {
723
+ var _this_gesture, _this_gesture1;
724
+ if ((null == (_this_gesture = this.gesture) ? void 0 : _this_gesture.kind) === 'pinch' || (null == (_this_gesture1 = this.gesture) ? void 0 : _this_gesture1.kind) === 'pan') this.endCamera();
725
+ else this.cancelGesture();
726
+ }
727
+ }
728
+ pointerLeave() {
729
+ this.host.emit('cvcursor', {
730
+ x: 0,
731
+ y: 0,
732
+ down: false,
733
+ gone: true
734
+ });
735
+ }
736
+ tap(g, point, input, tool) {
737
+ const now = this.host.now();
738
+ const last = this.lastTap;
739
+ const double = !!last && now - last.time < DOUBLE_TAP_MS && Math.hypot(point[0] - last.x, point[1] - last.y) * this.view.zoom < 24;
740
+ this.lastTap = double ? null : {
741
+ time: now,
742
+ x: point[0],
743
+ y: point[1]
744
+ };
745
+ this.host.emit('cvtap', {
746
+ g,
747
+ x: point[0],
748
+ y: point[1],
749
+ count: double ? 2 : 1,
750
+ tool,
751
+ ...input.shift ? {
752
+ shift: true
753
+ } : {}
754
+ });
755
+ }
756
+ answer(g, grab) {
757
+ const gesture = this.gesture;
758
+ if ((null == gesture ? void 0 : gesture.kind) !== 'pending' || gesture.g !== g) return;
759
+ if (grab) {
760
+ const move = {
761
+ kind: 'move',
762
+ g,
763
+ start: gesture.start,
764
+ end: gesture.end,
765
+ alt: gesture.alt,
766
+ shift: gesture.shift
767
+ };
768
+ this.moveStarted = Math.hypot(gesture.end[0] - gesture.start[0], gesture.end[1] - gesture.start[1]) * this.view.zoom >= 4;
769
+ if (this.moveStarted) this.sceneVersion++;
770
+ if (gesture.released) {
771
+ this.gesture = null;
772
+ if (this.moveStarted) {
773
+ this.live = {
774
+ g,
775
+ matrix: translate(gesture.end[0] - gesture.start[0], gesture.end[1] - gesture.start[1])
776
+ };
777
+ this.host.emit('cvmove', {
778
+ g,
779
+ dx: gesture.end[0] - gesture.start[0],
780
+ dy: gesture.end[1] - gesture.start[1],
781
+ ...gesture.alt ? {
782
+ alt: true
783
+ } : {}
784
+ });
785
+ }
786
+ } else this.gesture = move;
787
+ } else {
788
+ const marquee = {
789
+ kind: 'marquee',
790
+ g,
791
+ start: gesture.start,
792
+ end: gesture.end,
793
+ shift: gesture.shift
794
+ };
795
+ if (gesture.released) {
796
+ this.gesture = null;
797
+ this.host.emit('cvbox', {
798
+ g,
799
+ b: box(gesture.start, gesture.end),
800
+ ...gesture.shift ? {
801
+ shift: true
802
+ } : {}
803
+ });
804
+ } else this.gesture = marquee;
805
+ }
806
+ this.host.invalidate();
807
+ }
808
+ handleAt(scene, touch) {
809
+ const radius = (touch ? 22 : 9) / this.view.zoom;
810
+ let best = null;
811
+ let bestDistance = radius;
812
+ for (const entry of this.handles(touch)){
813
+ const distance = Math.hypot(entry.x - scene[0], entry.y - scene[1]);
814
+ if (distance <= bestDistance) {
815
+ best = entry.handle;
816
+ bestDistance = distance;
817
+ }
818
+ }
819
+ return best;
820
+ }
821
+ pointAt(scene, touch) {
822
+ var _this_selection;
823
+ const points = null == (_this_selection = this.selection) ? void 0 : _this_selection.points;
824
+ if (!points || !this.editable) return null;
825
+ const radius = (touch ? 20 : 8) / this.view.zoom;
826
+ for(let i = 0; i < points.length; i++)if (Math.hypot(points[i][0] - scene[0], points[i][1] - scene[1]) <= radius) return i;
827
+ return null;
828
+ }
829
+ startPinch() {
830
+ const unfinished = this.gesture;
831
+ if (unfinished && 'pan' !== unfinished.kind && 'pinch' !== unfinished.kind) this.cancelGesture();
832
+ const [a, b] = [
833
+ ...this.pointers.keys()
834
+ ];
835
+ const pa = this.pointers.get(a);
836
+ const pb = this.pointers.get(b);
837
+ const mid = [
838
+ (pa.x + pb.x) / 2,
839
+ (pa.y + pb.y) / 2
840
+ ];
841
+ this.gesture = {
842
+ kind: 'pinch',
843
+ a,
844
+ b,
845
+ startDistance: Math.max(1, Math.hypot(pa.x - pb.x, pa.y - pb.y)),
846
+ startZoom: this.view.zoom,
847
+ sceneMid: this.toScene(mid[0], mid[1])
848
+ };
849
+ this.panning = true;
850
+ this.host.invalidate();
851
+ }
852
+ updatePinch() {
853
+ const gesture = this.gesture;
854
+ if ((null == gesture ? void 0 : gesture.kind) !== 'pinch') return;
855
+ const pa = this.pointers.get(gesture.a);
856
+ const pb = this.pointers.get(gesture.b);
857
+ if (!pa || !pb) return;
858
+ const mid = [
859
+ (pa.x + pb.x) / 2,
860
+ (pa.y + pb.y) / 2
861
+ ];
862
+ const zoom = Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, gesture.startZoom * (Math.hypot(pa.x - pb.x, pa.y - pb.y) / gesture.startDistance)));
863
+ this.setView({
864
+ zoom,
865
+ scrollX: mid[0] / zoom - gesture.sceneMid[0],
866
+ scrollY: mid[1] / zoom - gesture.sceneMid[1]
867
+ }, false);
868
+ this.emitView(false);
869
+ }
870
+ endCamera() {
871
+ this.gesture = null;
872
+ this.panning = false;
873
+ this.sceneVersion++;
874
+ this.emitView(true);
875
+ this.host.invalidate();
876
+ }
877
+ cancelGesture() {
878
+ const gesture = this.gesture;
879
+ this.gesture = null;
880
+ this.panning = false;
881
+ if ((null == gesture ? void 0 : gesture.kind) === 'pending') this.host.emit('cvbox', {
882
+ g: gesture.g,
883
+ b: [
884
+ gesture.start[0],
885
+ gesture.start[1],
886
+ gesture.start[0],
887
+ gesture.start[1]
888
+ ]
889
+ });
890
+ if ((null == gesture ? void 0 : gesture.kind) === 'move' || (null == gesture ? void 0 : gesture.kind) === 'resize' || (null == gesture ? void 0 : gesture.kind) === 'rotate') this.sceneVersion++;
891
+ this.eraserTrail = [];
892
+ this.host.invalidate();
893
+ }
894
+ cursor(scene, down) {
895
+ const now = this.host.now();
896
+ if (now - this.lastCursor < 50) return;
897
+ this.lastCursor = now;
898
+ this.host.emit('cvcursor', {
899
+ x: scene[0],
900
+ y: scene[1],
901
+ down
902
+ });
903
+ }
904
+ trail() {
905
+ const now = this.host.now();
906
+ this.eraserTrail = this.eraserTrail.filter((point)=>now - point.t < 300);
907
+ return this.eraserTrail;
908
+ }
909
+ get busy() {
910
+ return null !== this.gesture;
911
+ }
912
+ constructor(host){
913
+ _define_property(this, "host", void 0);
914
+ _define_property(this, "items", void 0);
915
+ _define_property(this, "order", void 0);
916
+ _define_property(this, "files", void 0);
917
+ _define_property(this, "view", void 0);
918
+ _define_property(this, "width", void 0);
919
+ _define_property(this, "height", void 0);
920
+ _define_property(this, "selection", void 0);
921
+ _define_property(this, "peers", void 0);
922
+ _define_property(this, "hidden", void 0);
923
+ _define_property(this, "faded", void 0);
924
+ _define_property(this, "tool", void 0);
925
+ _define_property(this, "editable", void 0);
926
+ _define_property(this, "stroke", void 0);
927
+ _define_property(this, "sceneVersion", void 0);
928
+ _define_property(this, "previews", void 0);
929
+ _define_property(this, "live", void 0);
930
+ _define_property(this, "pointers", void 0);
931
+ _define_property(this, "gesture", void 0);
932
+ _define_property(this, "seq", void 0);
933
+ _define_property(this, "lastTap", void 0);
934
+ _define_property(this, "lastCursor", void 0);
935
+ _define_property(this, "lastViewEmit", void 0);
936
+ _define_property(this, "viewTimer", void 0);
937
+ _define_property(this, "eraserTrail", void 0);
938
+ _define_property(this, "panning", void 0);
939
+ _define_property(this, "moveStarted", void 0);
940
+ this.host = host;
941
+ this.items = new Map();
942
+ this.order = [];
943
+ this.files = {};
944
+ this.view = {
945
+ scrollX: 0,
946
+ scrollY: 0,
947
+ zoom: 1
948
+ };
949
+ this.width = 0;
950
+ this.height = 0;
951
+ this.selection = null;
952
+ this.peers = [];
953
+ this.hidden = new Set();
954
+ this.faded = new Set();
955
+ this.tool = 'selection';
956
+ this.editable = false;
957
+ this.stroke = {
958
+ color: '#1e1e1e',
959
+ width: 2
960
+ };
961
+ this.sceneVersion = 0;
962
+ this.previews = new Map();
963
+ this.live = null;
964
+ this.pointers = new Map();
965
+ this.gesture = null;
966
+ this.seq = 0;
967
+ this.lastTap = null;
968
+ this.lastCursor = 0;
969
+ this.lastViewEmit = 0;
970
+ this.viewTimer = null;
971
+ this.eraserTrail = [];
972
+ this.panning = false;
973
+ this.moveStarted = false;
974
+ }
975
+ }
976
+ const EMPTY = new Set();
977
+ function box(a, b) {
978
+ return [
979
+ Math.min(a[0], b[0]),
980
+ Math.min(a[1], b[1]),
981
+ Math.max(a[0], b[0]),
982
+ Math.max(a[1], b[1])
983
+ ];
984
+ }
985
+ function sameBox(a, b) {
986
+ return Math.abs(a[0] - b[0]) < 1e-6 && Math.abs(a[1] - b[1]) < 1e-6 && Math.abs(a[2] - b[2]) < 1e-6 && Math.abs(a[3] - b[3]) < 1e-6;
987
+ }
988
+ function constrain(start, point, line) {
989
+ const dx = point[0] - start[0];
990
+ const dy = point[1] - start[1];
991
+ if (line) {
992
+ const angle = Math.PI / 12 * Math.round(Math.atan2(dy, dx) / (Math.PI / 12));
993
+ const length = Math.hypot(dx, dy);
994
+ return [
995
+ start[0] + Math.cos(angle) * length,
996
+ start[1] + Math.sin(angle) * length
997
+ ];
998
+ }
999
+ const size = Math.max(Math.abs(dx), Math.abs(dy));
1000
+ return [
1001
+ start[0] + Math.sign(dx || 1) * size,
1002
+ start[1] + Math.sign(dy || 1) * size
1003
+ ];
1004
+ }
1005
+ function rotatePoint(point, centre, angle) {
1006
+ if (!angle) return point;
1007
+ const cos = Math.cos(angle);
1008
+ const sin = Math.sin(angle);
1009
+ const dx = point[0] - centre[0];
1010
+ const dy = point[1] - centre[1];
1011
+ return [
1012
+ dx * cos - dy * sin + centre[0],
1013
+ dx * sin + dy * cos + centre[1]
1014
+ ];
1015
+ }
1016
+ function resizeBox(from, angle, handle, point, keepAspect) {
1017
+ const centre = [
1018
+ (from[0] + from[2]) / 2,
1019
+ (from[1] + from[3]) / 2
1020
+ ];
1021
+ const [px, py] = rotatePoint(point, centre, -angle);
1022
+ let [x1, y1, x2, y2] = from;
1023
+ if (handle.includes('w')) x1 = Math.min(px, x2 - 1);
1024
+ if (handle.includes('e')) x2 = Math.max(px, x1 + 1);
1025
+ if (handle.includes('n')) y1 = Math.min(py, y2 - 1);
1026
+ if (handle.includes('s')) y2 = Math.max(py, y1 + 1);
1027
+ if (keepAspect && 2 === handle.length) {
1028
+ const ratio = (from[2] - from[0]) / Math.max(1e-6, from[3] - from[1]);
1029
+ const w = x2 - x1;
1030
+ const h = y2 - y1;
1031
+ if (w / ratio > h) {
1032
+ const nh = w / ratio;
1033
+ if (handle.includes('n')) y1 = y2 - nh;
1034
+ else y2 = y1 + nh;
1035
+ } else {
1036
+ const nw = h * ratio;
1037
+ if (handle.includes('w')) x1 = x2 - nw;
1038
+ else x2 = x1 + nw;
1039
+ }
1040
+ }
1041
+ return [
1042
+ x1,
1043
+ y1,
1044
+ x2,
1045
+ y2
1046
+ ];
1047
+ }
1048
+ function translate(dx, dy) {
1049
+ return [
1050
+ 1,
1051
+ 0,
1052
+ 0,
1053
+ 1,
1054
+ dx,
1055
+ dy
1056
+ ];
1057
+ }
1058
+ function multiply(m, n) {
1059
+ return [
1060
+ m[0] * n[0] + m[2] * n[1],
1061
+ m[1] * n[0] + m[3] * n[1],
1062
+ m[0] * n[2] + m[2] * n[3],
1063
+ m[1] * n[2] + m[3] * n[3],
1064
+ m[0] * n[4] + m[2] * n[5] + m[4],
1065
+ m[1] * n[4] + m[3] * n[5] + m[5]
1066
+ ];
1067
+ }
1068
+ function rotateMatrix(angle, [cx, cy]) {
1069
+ const cos = Math.cos(angle);
1070
+ const sin = Math.sin(angle);
1071
+ return multiply(translate(cx, cy), multiply([
1072
+ cos,
1073
+ sin,
1074
+ -sin,
1075
+ cos,
1076
+ 0,
1077
+ 0
1078
+ ], translate(-cx, -cy)));
1079
+ }
1080
+ function resizeMatrix(from, to, angle) {
1081
+ const sx = (to[2] - to[0]) / Math.max(1e-6, from[2] - from[0]);
1082
+ const sy = (to[3] - to[1]) / Math.max(1e-6, from[3] - from[1]);
1083
+ const centre = [
1084
+ (from[0] + from[2]) / 2,
1085
+ (from[1] + from[3]) / 2
1086
+ ];
1087
+ const scale = multiply(translate(to[0], to[1]), multiply([
1088
+ sx,
1089
+ 0,
1090
+ 0,
1091
+ sy,
1092
+ 0,
1093
+ 0
1094
+ ], translate(-from[0], -from[1])));
1095
+ if (!angle) return scale;
1096
+ return multiply(rotateMatrix(angle, centre), multiply(scale, rotateMatrix(-angle, centre)));
1097
+ }
1098
+ function applyMatrix(m, [x, y]) {
1099
+ return [
1100
+ m[0] * x + m[2] * y + m[4],
1101
+ m[1] * x + m[3] * y + m[5]
1102
+ ];
1103
+ }
1104
+ export { CanvasCore, applyMatrix, multiply, resizeBox, resizeMatrix, rotateMatrix, rotatePoint, translate };