@widgetic/canvas 0.5.40 → 0.5.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/dist/canvas/DomCanvas.svelte +4 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +8 -0
- package/dist/canvas/autosaveScheduler.js +89 -37
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +19 -7
- package/dist/canvas/chromeLayout.js +18 -17
- package/dist/canvas/eventScope.js +14 -10
- package/dist/canvas/host/CanvasHost.d.ts +6 -0
- package/dist/canvas/host/CanvasHost.js +1556 -1267
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +25 -4
- package/dist/canvas/host/browserServices.js +116 -65
- package/dist/canvas/host/conversion.js +170 -82
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +31 -28
- package/dist/canvas/host/strokeSnap.js +143 -88
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +7 -6
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +25 -13
- package/dist/canvas/shapes/ShapeLibrary.js +148 -138
- package/dist/canvas/shapes/shapeImageStore.js +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/CanvasHint.svelte +16 -3
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
- package/dist/canvas/ui/CanvasToolbar.svelte +7 -7
- package/dist/canvas/ui/ToolButton.svelte +11 -3
- package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
- package/dist/canvas/ui/colorModel.js +32 -21
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +6 -5
- package/dist/canvas/ui/keyHints.js +11 -9
- package/dist/canvas/ui/popoverSide.js +3 -2
- package/dist/canvas/ui/selectionTraits.js +40 -34
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +1 -1
- package/dist/canvas/ui/sizeLabelVisibility.js +1 -1
- package/dist/canvas/view/SelectionOverlay.svelte +33 -14
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.d.ts +14 -4
- package/dist/canvas/view/geometry.js +103 -77
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +4 -1
- package/dist/engine/EngineCore.js +801 -695
- package/dist/engine/codec/canvasDocument.js +173 -173
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +38 -26
- package/dist/engine/core/arrow.d.ts +10 -3
- package/dist/engine/core/arrow.js +96 -75
- package/dist/engine/core/arrowBinding.d.ts +69 -0
- package/dist/engine/core/arrowBinding.js +344 -0
- package/dist/engine/core/arrowBindingRecords.d.ts +43 -0
- package/dist/engine/core/arrowBindingRecords.js +141 -0
- package/dist/engine/core/brush.js +94 -67
- package/dist/engine/core/defaults.js +51 -90
- package/dist/engine/core/freehand.js +163 -147
- package/dist/engine/core/geometry.js +54 -47
- package/dist/engine/core/history.js +50 -33
- package/dist/engine/core/hitTest.d.ts +3 -0
- package/dist/engine/core/hitTest.js +83 -69
- package/dist/engine/core/matrix.js +20 -18
- package/dist/engine/core/pathData.js +210 -183
- package/dist/engine/core/rotationReset.js +6 -5
- package/dist/engine/core/scene.d.ts +3 -0
- package/dist/engine/core/scene.js +192 -129
- package/dist/engine/core/scribble.js +84 -59
- package/dist/engine/core/shapePaths.js +15 -12
- package/dist/engine/core/shapeTools.js +63 -72
- package/dist/engine/core/snapping.js +117 -65
- package/dist/engine/core/style.js +64 -31
- package/dist/engine/core/transform.js +94 -83
- package/dist/engine/core/types.d.ts +29 -0
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +36 -23
- package/dist/engine/input/PointerController.js +476 -447
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.d.ts +12 -1
- package/dist/engine/input/gestures.js +342 -251
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.d.ts +9 -1
- package/dist/engine/input/keyboard.js +46 -27
- package/dist/engine/input/selectionHandles.js +50 -45
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +158 -86
- package/dist/engine/state.d.ts +6 -1
- package/dist/engine/state.js +17 -5
- package/dist/engine/text/layoutText.js +62 -43
- package/dist/engine/text/textMetrics.js +10 -8
- package/dist/engine/widgets.js +137 -101
- package/dist/fonts/interFont.js +68 -17
- package/dist/fonts/workspaceFonts.js +90 -32
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.d.ts +12 -0
- package/dist/i18n/shortcuts.js +32 -18
- package/dist/settings/workspaceToolSettings.js +48 -32
- package/dist/tools/components/catalog.d.ts +11 -0
- package/dist/tools/components/catalog.js +270 -0
- package/dist/tools/components/grouping.d.ts +9 -0
- package/dist/tools/components/grouping.js +33 -0
- package/dist/tools/definitions.d.ts +19 -0
- package/dist/tools/definitions.js +147 -0
- package/dist/tools/executors.d.ts +7 -0
- package/dist/tools/executors.js +324 -0
- package/dist/tools/hostAdapter.d.ts +14 -0
- package/dist/tools/hostAdapter.js +40 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +6 -0
- package/dist/tools/sceneSummary.d.ts +24 -0
- package/dist/tools/sceneSummary.js +170 -0
- package/dist/tools/sketchIntent.d.ts +17 -0
- package/dist/tools/sketchIntent.js +166 -0
- package/dist/tools/types.d.ts +54 -0
- package/dist/tools/types.js +11 -0
- package/package.json +7 -2
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* rasterizer are built from. Pure functions on plain numbers.
|
|
4
4
|
*/
|
|
5
5
|
import { boxToWorldMatrix, applyMatrixToPoint, invertMatrix } from './matrix.js';
|
|
6
|
-
|
|
6
|
+
var EPSILON = 1e-9;
|
|
7
7
|
export function rectRight(rect) {
|
|
8
8
|
return rect.x + rect.width;
|
|
9
9
|
}
|
|
@@ -52,11 +52,12 @@ export function inflateRect(rect, amount) {
|
|
|
52
52
|
export function unionRects(rects) {
|
|
53
53
|
if (rects.length === 0)
|
|
54
54
|
return null;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
for (
|
|
55
|
+
var left = Infinity;
|
|
56
|
+
var top = Infinity;
|
|
57
|
+
var right = -Infinity;
|
|
58
|
+
var bottom = -Infinity;
|
|
59
|
+
for (var _i = 0, rects_1 = rects; _i < rects_1.length; _i++) {
|
|
60
|
+
var rect = rects_1[_i];
|
|
60
61
|
left = Math.min(left, rect.x);
|
|
61
62
|
top = Math.min(top, rect.y);
|
|
62
63
|
right = Math.max(right, rect.x + rect.width);
|
|
@@ -68,11 +69,12 @@ export function unionRects(rects) {
|
|
|
68
69
|
export function boundsOfPoints(points) {
|
|
69
70
|
if (points.length === 0)
|
|
70
71
|
return null;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
for (
|
|
72
|
+
var left = Infinity;
|
|
73
|
+
var top = Infinity;
|
|
74
|
+
var right = -Infinity;
|
|
75
|
+
var bottom = -Infinity;
|
|
76
|
+
for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
|
|
77
|
+
var point = points_1[_i];
|
|
76
78
|
left = Math.min(left, point.x);
|
|
77
79
|
top = Math.min(top, point.y);
|
|
78
80
|
right = Math.max(right, point.x);
|
|
@@ -82,7 +84,7 @@ export function boundsOfPoints(points) {
|
|
|
82
84
|
}
|
|
83
85
|
/** The four corners of a box in world space, clockwise from the top-left (before rotation). */
|
|
84
86
|
export function boxCorners(box) {
|
|
85
|
-
|
|
87
|
+
var matrix = boxToWorldMatrix(box);
|
|
86
88
|
return [
|
|
87
89
|
applyMatrixToPoint(matrix, { x: 0, y: 0 }),
|
|
88
90
|
applyMatrixToPoint(matrix, { x: box.width, y: 0 }),
|
|
@@ -97,11 +99,12 @@ export function rotatedBoxBounds(box) {
|
|
|
97
99
|
return boundsOfPoints(boxCorners(box));
|
|
98
100
|
}
|
|
99
101
|
/** Whether a world point is inside a (possibly rotated) box, with `slop` world units of leeway on every side. */
|
|
100
|
-
export function pointInBox(box, point, slop
|
|
101
|
-
|
|
102
|
+
export function pointInBox(box, point, slop) {
|
|
103
|
+
if (slop === void 0) { slop = 0; }
|
|
104
|
+
var inverse = invertMatrix(boxToWorldMatrix(box));
|
|
102
105
|
if (!inverse)
|
|
103
106
|
return false;
|
|
104
|
-
|
|
107
|
+
var local = applyMatrixToPoint(inverse, point);
|
|
105
108
|
return (local.x >= -slop &&
|
|
106
109
|
local.x <= box.width + slop &&
|
|
107
110
|
local.y >= -slop &&
|
|
@@ -109,7 +112,7 @@ export function pointInBox(box, point, slop = 0) {
|
|
|
109
112
|
}
|
|
110
113
|
/** A world point in the local space of a box. Null when the box is degenerate. */
|
|
111
114
|
export function worldToBoxLocal(box, point) {
|
|
112
|
-
|
|
115
|
+
var inverse = invertMatrix(boxToWorldMatrix(box));
|
|
113
116
|
return inverse ? applyMatrixToPoint(inverse, point) : null;
|
|
114
117
|
}
|
|
115
118
|
export function distanceBetween(first, second) {
|
|
@@ -117,13 +120,13 @@ export function distanceBetween(first, second) {
|
|
|
117
120
|
}
|
|
118
121
|
/** The distance from a point to the segment a-b. */
|
|
119
122
|
export function distanceToSegment(point, a, b) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
+
var deltaX = b.x - a.x;
|
|
124
|
+
var deltaY = b.y - a.y;
|
|
125
|
+
var lengthSquared = deltaX * deltaX + deltaY * deltaY;
|
|
123
126
|
if (lengthSquared < EPSILON)
|
|
124
127
|
return distanceBetween(point, a);
|
|
125
|
-
|
|
126
|
-
|
|
128
|
+
var along = ((point.x - a.x) * deltaX + (point.y - a.y) * deltaY) / lengthSquared;
|
|
129
|
+
var clamped = Math.max(0, Math.min(1, along));
|
|
127
130
|
return Math.hypot(point.x - (a.x + clamped * deltaX), point.y - (a.y + clamped * deltaY));
|
|
128
131
|
}
|
|
129
132
|
/** The distance from a point to a polyline (closed: the last point joins the first). */
|
|
@@ -132,10 +135,10 @@ export function distanceToPolyline(point, vertices, closed) {
|
|
|
132
135
|
return Infinity;
|
|
133
136
|
if (vertices.length === 1)
|
|
134
137
|
return distanceBetween(point, vertices[0]);
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
for (
|
|
138
|
-
|
|
138
|
+
var best = Infinity;
|
|
139
|
+
var last = closed ? vertices.length : vertices.length - 1;
|
|
140
|
+
for (var index = 0; index < last; index += 1) {
|
|
141
|
+
var distance = distanceToSegment(point, vertices[index], vertices[(index + 1) % vertices.length]);
|
|
139
142
|
if (distance < best)
|
|
140
143
|
best = distance;
|
|
141
144
|
}
|
|
@@ -143,11 +146,11 @@ export function distanceToPolyline(point, vertices, closed) {
|
|
|
143
146
|
}
|
|
144
147
|
/** Even-odd test: whether the point lies inside the polygon. */
|
|
145
148
|
export function pointInPolygon(point, vertices) {
|
|
146
|
-
|
|
147
|
-
for (
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
var inside = false;
|
|
150
|
+
for (var index = 0, previous = vertices.length - 1; index < vertices.length; previous = index, index += 1) {
|
|
151
|
+
var current = vertices[index];
|
|
152
|
+
var before = vertices[previous];
|
|
153
|
+
var crosses = current.y > point.y !== before.y > point.y &&
|
|
151
154
|
point.x < ((before.x - current.x) * (point.y - current.y)) / (before.y - current.y) + current.x;
|
|
152
155
|
if (crosses)
|
|
153
156
|
inside = !inside;
|
|
@@ -156,17 +159,19 @@ export function pointInPolygon(point, vertices) {
|
|
|
156
159
|
}
|
|
157
160
|
/** Whether segment p1-p2 crosses segment p3-p4. */
|
|
158
161
|
export function segmentsIntersect(p1, p2, p3, p4) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
162
|
+
var orientation = function (a, b, c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); };
|
|
163
|
+
var d1 = orientation(p3, p4, p1);
|
|
164
|
+
var d2 = orientation(p3, p4, p2);
|
|
165
|
+
var d3 = orientation(p1, p2, p3);
|
|
166
|
+
var d4 = orientation(p1, p2, p4);
|
|
164
167
|
if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
|
|
165
168
|
return true;
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
169
|
+
var onSegment = function (a, b, c) {
|
|
170
|
+
return Math.min(a.x, b.x) - EPSILON <= c.x &&
|
|
171
|
+
c.x <= Math.max(a.x, b.x) + EPSILON &&
|
|
172
|
+
Math.min(a.y, b.y) - EPSILON <= c.y &&
|
|
173
|
+
c.y <= Math.max(a.y, b.y) + EPSILON;
|
|
174
|
+
};
|
|
170
175
|
if (Math.abs(d1) < EPSILON && onSegment(p3, p4, p1))
|
|
171
176
|
return true;
|
|
172
177
|
if (Math.abs(d2) < EPSILON && onSegment(p3, p4, p2))
|
|
@@ -182,7 +187,8 @@ export function polygonIntersectsRect(vertices, rect) {
|
|
|
182
187
|
if (vertices.length === 0)
|
|
183
188
|
return false;
|
|
184
189
|
// Any vertex inside the rectangle.
|
|
185
|
-
for (
|
|
190
|
+
for (var _i = 0, vertices_1 = vertices; _i < vertices_1.length; _i++) {
|
|
191
|
+
var vertex = vertices_1[_i];
|
|
186
192
|
if (rectContainsPoint(rect, vertex))
|
|
187
193
|
return true;
|
|
188
194
|
}
|
|
@@ -190,16 +196,16 @@ export function polygonIntersectsRect(vertices, rect) {
|
|
|
190
196
|
if (pointInPolygon(rectCenter(rect), vertices))
|
|
191
197
|
return true;
|
|
192
198
|
// Any edge crossing any edge of the rectangle.
|
|
193
|
-
|
|
199
|
+
var rectangleCorners = [
|
|
194
200
|
{ x: rect.x, y: rect.y },
|
|
195
201
|
{ x: rect.x + rect.width, y: rect.y },
|
|
196
202
|
{ x: rect.x + rect.width, y: rect.y + rect.height },
|
|
197
203
|
{ x: rect.x, y: rect.y + rect.height }
|
|
198
204
|
];
|
|
199
|
-
for (
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
for (
|
|
205
|
+
for (var index = 0; index < vertices.length; index += 1) {
|
|
206
|
+
var start = vertices[index];
|
|
207
|
+
var end = vertices[(index + 1) % vertices.length];
|
|
208
|
+
for (var side = 0; side < 4; side += 1) {
|
|
203
209
|
if (segmentsIntersect(start, end, rectangleCorners[side], rectangleCorners[(side + 1) % 4]))
|
|
204
210
|
return true;
|
|
205
211
|
}
|
|
@@ -207,8 +213,9 @@ export function polygonIntersectsRect(vertices, rect) {
|
|
|
207
213
|
return false;
|
|
208
214
|
}
|
|
209
215
|
/** Rounds to a number of decimals (default 2): for stored geometry, so JSON does not carry noise digits. */
|
|
210
|
-
export function roundTo(value, decimals
|
|
211
|
-
|
|
216
|
+
export function roundTo(value, decimals) {
|
|
217
|
+
if (decimals === void 0) { decimals = 2; }
|
|
218
|
+
var factor = Math.pow(10, decimals);
|
|
212
219
|
return Math.round(value * factor) / factor;
|
|
213
220
|
}
|
|
214
221
|
export function clampNumber(value, minimum, maximum) {
|
|
@@ -5,35 +5,50 @@
|
|
|
5
5
|
* hundred steps costs a hundred arrays of pointers, not a hundred copies of the board. One step is pushed
|
|
6
6
|
* when a gesture ends (a drag, a resize, a stroke), never while it runs.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
constructor(initial, options = {}) {
|
|
8
|
+
var HistoryStack = /** @class */ (function () {
|
|
9
|
+
function HistoryStack(initial, options) {
|
|
10
|
+
if (options === void 0) { options = {}; }
|
|
11
|
+
var _a, _b;
|
|
12
|
+
this.cursor = 0;
|
|
14
13
|
this.entries = [initial];
|
|
15
|
-
this.limit = Math.max(2, options.limit
|
|
16
|
-
this.isSame = options.isSame
|
|
17
|
-
}
|
|
18
|
-
/** The state the board is in now. */
|
|
19
|
-
get current() {
|
|
20
|
-
return this.entries[this.cursor];
|
|
21
|
-
}
|
|
22
|
-
get canUndo() {
|
|
23
|
-
return this.cursor > 0;
|
|
24
|
-
}
|
|
25
|
-
get canRedo() {
|
|
26
|
-
return this.cursor < this.entries.length - 1;
|
|
27
|
-
}
|
|
28
|
-
/** How many states the stack holds (the start included). */
|
|
29
|
-
get length() {
|
|
30
|
-
return this.entries.length;
|
|
14
|
+
this.limit = Math.max(2, (_a = options.limit) !== null && _a !== void 0 ? _a : 100);
|
|
15
|
+
this.isSame = (_b = options.isSame) !== null && _b !== void 0 ? _b : Object.is;
|
|
31
16
|
}
|
|
17
|
+
Object.defineProperty(HistoryStack.prototype, "current", {
|
|
18
|
+
/** The state the board is in now. */
|
|
19
|
+
get: function () {
|
|
20
|
+
return this.entries[this.cursor];
|
|
21
|
+
},
|
|
22
|
+
enumerable: false,
|
|
23
|
+
configurable: true
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(HistoryStack.prototype, "canUndo", {
|
|
26
|
+
get: function () {
|
|
27
|
+
return this.cursor > 0;
|
|
28
|
+
},
|
|
29
|
+
enumerable: false,
|
|
30
|
+
configurable: true
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(HistoryStack.prototype, "canRedo", {
|
|
33
|
+
get: function () {
|
|
34
|
+
return this.cursor < this.entries.length - 1;
|
|
35
|
+
},
|
|
36
|
+
enumerable: false,
|
|
37
|
+
configurable: true
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(HistoryStack.prototype, "length", {
|
|
40
|
+
/** How many states the stack holds (the start included). */
|
|
41
|
+
get: function () {
|
|
42
|
+
return this.entries.length;
|
|
43
|
+
},
|
|
44
|
+
enumerable: false,
|
|
45
|
+
configurable: true
|
|
46
|
+
});
|
|
32
47
|
/**
|
|
33
48
|
* Adds a step after the current one and forgets the steps that were undone.
|
|
34
49
|
* Returns false (and records nothing) when the state is the same as the current one.
|
|
35
50
|
*/
|
|
36
|
-
push(state) {
|
|
51
|
+
HistoryStack.prototype.push = function (state) {
|
|
37
52
|
if (this.isSame(this.current, state))
|
|
38
53
|
return false;
|
|
39
54
|
this.entries = this.entries.slice(0, this.cursor + 1);
|
|
@@ -42,28 +57,30 @@ export class HistoryStack {
|
|
|
42
57
|
this.entries.splice(0, this.entries.length - this.limit);
|
|
43
58
|
this.cursor = this.entries.length - 1;
|
|
44
59
|
return true;
|
|
45
|
-
}
|
|
60
|
+
};
|
|
46
61
|
/** Goes one step back and returns that state, or null at the start. */
|
|
47
|
-
undo() {
|
|
62
|
+
HistoryStack.prototype.undo = function () {
|
|
48
63
|
if (!this.canUndo)
|
|
49
64
|
return null;
|
|
50
65
|
this.cursor -= 1;
|
|
51
66
|
return this.entries[this.cursor];
|
|
52
|
-
}
|
|
67
|
+
};
|
|
53
68
|
/** Goes one step forward and returns that state, or null at the end. */
|
|
54
|
-
redo() {
|
|
69
|
+
HistoryStack.prototype.redo = function () {
|
|
55
70
|
if (!this.canRedo)
|
|
56
71
|
return null;
|
|
57
72
|
this.cursor += 1;
|
|
58
73
|
return this.entries[this.cursor];
|
|
59
|
-
}
|
|
74
|
+
};
|
|
60
75
|
/** Replaces the current step without adding one (for a change that must not be undone on its own). */
|
|
61
|
-
replaceCurrent(state) {
|
|
76
|
+
HistoryStack.prototype.replaceCurrent = function (state) {
|
|
62
77
|
this.entries[this.cursor] = state;
|
|
63
|
-
}
|
|
78
|
+
};
|
|
64
79
|
/** Forgets everything and starts again from this state (a canvas was loaded). */
|
|
65
|
-
reset(state) {
|
|
80
|
+
HistoryStack.prototype.reset = function (state) {
|
|
66
81
|
this.entries = [state];
|
|
67
82
|
this.cursor = 0;
|
|
68
|
-
}
|
|
69
|
-
|
|
83
|
+
};
|
|
84
|
+
return HistoryStack;
|
|
85
|
+
}());
|
|
86
|
+
export { HistoryStack };
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* - A group answers as a whole: the hit is the group nearest to the board, unless the person has entered a
|
|
11
11
|
* group, whose members are then picked one by one.
|
|
12
12
|
*/
|
|
13
|
+
import type { FlatSubpath } from './pathData.js';
|
|
13
14
|
import type { Scene } from './scene.js';
|
|
14
15
|
import type { ObjectId, Point, Rect, SceneObject, ShapeObject } from './types.js';
|
|
15
16
|
export interface HitTestOptions {
|
|
@@ -28,6 +29,8 @@ export interface MarqueeOptions extends HitTestOptions {
|
|
|
28
29
|
/** `intersect` takes what the marquee touches, `contain` only what it holds entirely. */
|
|
29
30
|
mode: 'intersect' | 'contain';
|
|
30
31
|
}
|
|
32
|
+
/** The outline of a path flattened to straight pieces, in the coordinates of its box (the same pieces for as long as the same data is asked for). */
|
|
33
|
+
export declare function flattenedOutline(data: string): FlatSubpath[];
|
|
31
34
|
/** Whether a world point is on a shape (inside it, or near its outline, depending on the kind of shape). */
|
|
32
35
|
export declare function shapeContainsPoint(shape: ShapeObject, worldPoint: Point, toleranceWorld: number): boolean;
|
|
33
36
|
/** The topmost selectable object under a world point, or null. */
|
|
@@ -16,15 +16,16 @@ import { applyMatrixToPoint, boxToWorldMatrix } from './matrix.js';
|
|
|
16
16
|
import { flattenPath, parsePathData } from './pathData.js';
|
|
17
17
|
import { indexScene, isWithin, topLevelAncestorId } from './scene.js';
|
|
18
18
|
import { isBoxedObject } from './types.js';
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
var DEFAULT_TOLERANCE_PIXELS = 5;
|
|
20
|
+
var FLATTENED_PATH_CACHE_LIMIT = 400;
|
|
21
21
|
/** Flattened outlines by path data, so a hover over a busy board does not parse the same strokes again. */
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
var flattenedPathCache = new Map();
|
|
23
|
+
/** The outline of a path flattened to straight pieces, in the coordinates of its box (the same pieces for as long as the same data is asked for). */
|
|
24
|
+
export function flattenedOutline(data) {
|
|
25
|
+
var cached = flattenedPathCache.get(data);
|
|
25
26
|
if (cached)
|
|
26
27
|
return cached;
|
|
27
|
-
|
|
28
|
+
var flattened = flattenPath(parsePathData(data));
|
|
28
29
|
if (flattenedPathCache.size >= FLATTENED_PATH_CACHE_LIMIT)
|
|
29
30
|
flattenedPathCache.clear();
|
|
30
31
|
flattenedPathCache.set(data, flattened);
|
|
@@ -45,39 +46,41 @@ function halfStroke(shape) {
|
|
|
45
46
|
}
|
|
46
47
|
/** Whether a world point is on a shape (inside it, or near its outline, depending on the kind of shape). */
|
|
47
48
|
export function shapeContainsPoint(shape, worldPoint, toleranceWorld) {
|
|
48
|
-
|
|
49
|
+
var reach = toleranceWorld + halfStroke(shape);
|
|
49
50
|
// Cheap rejection first: outside the box grown by the reach, nothing else can say yes. A brush stroke's outline spills past
|
|
50
51
|
// the box of its line, so the box grows by that as well (the outline itself is the shape: the reach on it stays the tolerance).
|
|
51
52
|
if (!pointInBox(shape, worldPoint, reach + brushOverflow(shape)))
|
|
52
53
|
return false;
|
|
53
|
-
|
|
54
|
+
var local = worldToBoxLocal(shape, worldPoint);
|
|
54
55
|
if (!local)
|
|
55
56
|
return false;
|
|
56
57
|
switch (shape.type) {
|
|
57
58
|
case 'rect': {
|
|
58
59
|
if (!isHollow(shape))
|
|
59
60
|
return true;
|
|
60
|
-
|
|
61
|
+
var edgeDistance = Math.min(Math.abs(local.x), Math.abs(local.x - shape.width), Math.abs(local.y), Math.abs(local.y - shape.height));
|
|
61
62
|
return edgeDistance <= reach;
|
|
62
63
|
}
|
|
63
64
|
case 'ellipse': {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
var radiusX = Math.max(0.5, shape.width / 2);
|
|
66
|
+
var radiusY = Math.max(0.5, shape.height / 2);
|
|
67
|
+
var normalized = Math.hypot((local.x - radiusX) / radiusX, (local.y - radiusY) / radiusY);
|
|
68
|
+
var smallestRadius = Math.min(radiusX, radiusY);
|
|
68
69
|
if (!isHollow(shape))
|
|
69
70
|
return normalized <= 1 + reach / smallestRadius;
|
|
70
71
|
return Math.abs(normalized - 1) * smallestRadius <= reach;
|
|
71
72
|
}
|
|
72
73
|
case 'path': {
|
|
73
|
-
|
|
74
|
-
for (
|
|
74
|
+
var outlines = flattenedOutline(shape.d);
|
|
75
|
+
for (var _i = 0, outlines_1 = outlines; _i < outlines_1.length; _i++) {
|
|
76
|
+
var outline = outlines_1[_i];
|
|
75
77
|
if (distanceToPolyline(local, outline.points, outline.closed) <= reach)
|
|
76
78
|
return true;
|
|
77
79
|
}
|
|
78
80
|
if (!isHollow(shape)) {
|
|
79
81
|
// A fill closes an open outline implicitly, so every outline counts as a polygon here.
|
|
80
|
-
for (
|
|
82
|
+
for (var _a = 0, outlines_2 = outlines; _a < outlines_2.length; _a++) {
|
|
83
|
+
var outline = outlines_2[_a];
|
|
81
84
|
if (outline.points.length >= 3 && pointInPolygon(local, outline.points))
|
|
82
85
|
return true;
|
|
83
86
|
}
|
|
@@ -91,24 +94,26 @@ export function shapeContainsPoint(shape, worldPoint, toleranceWorld) {
|
|
|
91
94
|
}
|
|
92
95
|
/** The unit a person selects when a leaf shape is picked: the group nearest the board, or the entered group's member. */
|
|
93
96
|
function selectableIdFor(index, leafId, enteredGroupId) {
|
|
97
|
+
var _a;
|
|
94
98
|
if (!enteredGroupId || !isWithin(index, leafId, enteredGroupId) || leafId === enteredGroupId) {
|
|
95
99
|
return topLevelAncestorId(index, leafId);
|
|
96
100
|
}
|
|
97
|
-
|
|
101
|
+
var current = index.get(leafId);
|
|
98
102
|
while (current && current.parentId !== null && current.parentId !== enteredGroupId) {
|
|
99
103
|
current = index.get(current.parentId);
|
|
100
104
|
}
|
|
101
|
-
return current
|
|
105
|
+
return (_a = current === null || current === void 0 ? void 0 : current.id) !== null && _a !== void 0 ? _a : leafId;
|
|
102
106
|
}
|
|
103
107
|
/** The topmost selectable object under a world point, or null. */
|
|
104
108
|
export function hitTestPoint(scene, worldPoint, options) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
+
var _a, _b, _c;
|
|
110
|
+
var index = (_a = options.index) !== null && _a !== void 0 ? _a : indexScene(scene);
|
|
111
|
+
var toleranceWorld = ((_b = options.tolerancePixels) !== null && _b !== void 0 ? _b : DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
|
|
112
|
+
for (var position = scene.length - 1; position >= 0; position -= 1) {
|
|
113
|
+
var object = scene[position];
|
|
109
114
|
if (!isBoxedObject(object))
|
|
110
115
|
continue;
|
|
111
|
-
if (options.ignore
|
|
116
|
+
if ((_c = options.ignore) === null || _c === void 0 ? void 0 : _c.has(object.id))
|
|
112
117
|
continue;
|
|
113
118
|
if (shapeContainsPoint(object, worldPoint, toleranceWorld)) {
|
|
114
119
|
return selectableIdFor(index, object.id, options.enteredGroupId);
|
|
@@ -121,18 +126,20 @@ export function hitTestPoint(scene, worldPoint, options) {
|
|
|
121
126
|
* through, one object further down at every click.
|
|
122
127
|
*/
|
|
123
128
|
export function hitTestStack(scene, worldPoint, options) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
+
var _a, _b, _c;
|
|
130
|
+
var index = (_a = options.index) !== null && _a !== void 0 ? _a : indexScene(scene);
|
|
131
|
+
var toleranceWorld = ((_b = options.tolerancePixels) !== null && _b !== void 0 ? _b : DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
|
|
132
|
+
var stack = [];
|
|
133
|
+
var seen = new Set();
|
|
134
|
+
for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
|
|
135
|
+
var object = scene_1[_i];
|
|
129
136
|
if (!isBoxedObject(object))
|
|
130
137
|
continue;
|
|
131
|
-
if (options.ignore
|
|
138
|
+
if ((_c = options.ignore) === null || _c === void 0 ? void 0 : _c.has(object.id))
|
|
132
139
|
continue;
|
|
133
140
|
if (!shapeContainsPoint(object, worldPoint, toleranceWorld))
|
|
134
141
|
continue;
|
|
135
|
-
|
|
142
|
+
var unitId = selectableIdFor(index, object.id, options.enteredGroupId);
|
|
136
143
|
// A unit that is hit again by a later member moves up to that member's place in the paint order.
|
|
137
144
|
if (seen.has(unitId))
|
|
138
145
|
stack.splice(stack.indexOf(unitId), 1);
|
|
@@ -143,12 +150,13 @@ export function hitTestStack(scene, worldPoint, options) {
|
|
|
143
150
|
}
|
|
144
151
|
/** The leaf shape under a world point, ignoring groups (what the eraser removes, what a double-click enters). */
|
|
145
152
|
export function hitTestLeaf(scene, worldPoint, options) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
153
|
+
var _a, _b;
|
|
154
|
+
var toleranceWorld = ((_a = options.tolerancePixels) !== null && _a !== void 0 ? _a : DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
|
|
155
|
+
for (var position = scene.length - 1; position >= 0; position -= 1) {
|
|
156
|
+
var object = scene[position];
|
|
149
157
|
if (!isBoxedObject(object))
|
|
150
158
|
continue;
|
|
151
|
-
if (options.ignore
|
|
159
|
+
if ((_b = options.ignore) === null || _b === void 0 ? void 0 : _b.has(object.id))
|
|
152
160
|
continue;
|
|
153
161
|
if (shapeContainsPoint(object, worldPoint, toleranceWorld))
|
|
154
162
|
return object;
|
|
@@ -157,31 +165,32 @@ export function hitTestLeaf(scene, worldPoint, options) {
|
|
|
157
165
|
}
|
|
158
166
|
/** Whether a polyline (open or closed) touches a rectangle. */
|
|
159
167
|
function polylineTouchesRect(points, closed, rect) {
|
|
160
|
-
for (
|
|
168
|
+
for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
|
|
169
|
+
var point = points_1[_i];
|
|
161
170
|
if (rectContainsPoint(rect, point))
|
|
162
171
|
return true;
|
|
163
172
|
}
|
|
164
|
-
|
|
173
|
+
var corners = [
|
|
165
174
|
{ x: rect.x, y: rect.y },
|
|
166
175
|
{ x: rect.x + rect.width, y: rect.y },
|
|
167
176
|
{ x: rect.x + rect.width, y: rect.y + rect.height },
|
|
168
177
|
{ x: rect.x, y: rect.y + rect.height }
|
|
169
178
|
];
|
|
170
|
-
|
|
171
|
-
for (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
for (
|
|
179
|
+
var last = closed ? points.length : points.length - 1;
|
|
180
|
+
for (var position = 0; position < last; position += 1) {
|
|
181
|
+
var start = points[position];
|
|
182
|
+
var end = points[(position + 1) % points.length];
|
|
183
|
+
for (var side = 0; side < 4; side += 1) {
|
|
175
184
|
if (segmentsIntersect(start, end, corners[side], corners[(side + 1) % 4]))
|
|
176
185
|
return true;
|
|
177
186
|
}
|
|
178
187
|
}
|
|
179
188
|
return false;
|
|
180
189
|
}
|
|
181
|
-
|
|
190
|
+
var shapesInTheWorld = new WeakMap();
|
|
182
191
|
function shapeInTheWorld(shape) {
|
|
183
|
-
|
|
184
|
-
|
|
192
|
+
var data = shape.type === 'path' ? shape.d : null;
|
|
193
|
+
var cached = shapesInTheWorld.get(shape);
|
|
185
194
|
if (cached &&
|
|
186
195
|
cached.x === shape.x &&
|
|
187
196
|
cached.y === shape.y &&
|
|
@@ -191,34 +200,35 @@ function shapeInTheWorld(shape) {
|
|
|
191
200
|
cached.data === data) {
|
|
192
201
|
return cached;
|
|
193
202
|
}
|
|
194
|
-
|
|
195
|
-
|
|
203
|
+
var outlines = [];
|
|
204
|
+
var corners = [];
|
|
196
205
|
if (data !== null) {
|
|
197
|
-
|
|
198
|
-
for (
|
|
199
|
-
|
|
206
|
+
var matrix_1 = boxToWorldMatrix(shape);
|
|
207
|
+
for (var _i = 0, _a = flattenedOutline(data); _i < _a.length; _i++) {
|
|
208
|
+
var outline = _a[_i];
|
|
209
|
+
outlines.push({ points: outline.points.map(function (point) { return applyMatrixToPoint(matrix_1, point); }), closed: outline.closed });
|
|
200
210
|
}
|
|
201
211
|
}
|
|
202
212
|
else {
|
|
203
213
|
corners = boxCorners(shape);
|
|
204
214
|
}
|
|
205
|
-
|
|
215
|
+
var made = {
|
|
206
216
|
x: shape.x,
|
|
207
217
|
y: shape.y,
|
|
208
218
|
width: shape.width,
|
|
209
219
|
height: shape.height,
|
|
210
220
|
rotation: shape.rotation,
|
|
211
|
-
data,
|
|
212
|
-
corners,
|
|
213
|
-
outlines,
|
|
214
|
-
bounds: boundsOfPoints(data !== null ? outlines.flatMap((outline)
|
|
221
|
+
data: data,
|
|
222
|
+
corners: corners,
|
|
223
|
+
outlines: outlines,
|
|
224
|
+
bounds: boundsOfPoints(data !== null ? outlines.flatMap(function (outline) { return outline.points; }) : corners)
|
|
215
225
|
};
|
|
216
226
|
shapesInTheWorld.set(shape, made);
|
|
217
227
|
return made;
|
|
218
228
|
}
|
|
219
229
|
// How far a shape must lie from a rectangle, box to box, before nothing of it can touch the rectangle: far more than the slack
|
|
220
230
|
// the segment tests allow themselves (1e-9), so that the quick answer below never says no where those tests would say yes.
|
|
221
|
-
|
|
231
|
+
var CLEAR_OF_RECT_MARGIN = 1e-6;
|
|
222
232
|
/**
|
|
223
233
|
* Whether a shape touches a rectangle of the world (by its outline or fill, not only by its box).
|
|
224
234
|
*
|
|
@@ -228,10 +238,10 @@ const CLEAR_OF_RECT_MARGIN = 1e-6;
|
|
|
228
238
|
* tested point by point.
|
|
229
239
|
*/
|
|
230
240
|
export function shapeIntersectsRect(shape, rect) {
|
|
231
|
-
|
|
232
|
-
|
|
241
|
+
var world = shapeInTheWorld(shape);
|
|
242
|
+
var bounds = world.bounds;
|
|
233
243
|
if (bounds) {
|
|
234
|
-
|
|
244
|
+
var clear = bounds.x - CLEAR_OF_RECT_MARGIN > rect.x + rect.width ||
|
|
235
245
|
bounds.x + bounds.width + CLEAR_OF_RECT_MARGIN < rect.x ||
|
|
236
246
|
bounds.y - CLEAR_OF_RECT_MARGIN > rect.y + rect.height ||
|
|
237
247
|
bounds.y + bounds.height + CLEAR_OF_RECT_MARGIN < rect.y;
|
|
@@ -241,7 +251,8 @@ export function shapeIntersectsRect(shape, rect) {
|
|
|
241
251
|
return true;
|
|
242
252
|
}
|
|
243
253
|
if (shape.type === 'path') {
|
|
244
|
-
for (
|
|
254
|
+
for (var _i = 0, _a = world.outlines; _i < _a.length; _i++) {
|
|
255
|
+
var outline = _a[_i];
|
|
245
256
|
if (polylineTouchesRect(outline.points, outline.closed, rect))
|
|
246
257
|
return true;
|
|
247
258
|
if (!isHollow(shape) && outline.points.length >= 3 && pointInPolygon(rectCenter(rect), outline.points))
|
|
@@ -257,20 +268,22 @@ export function shapeIntersectsRect(shape, rect) {
|
|
|
257
268
|
}
|
|
258
269
|
/** The selectable objects a marquee takes, each once, in paint order. */
|
|
259
270
|
export function hitTestRect(scene, rect, options) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
271
|
+
var _a, _b;
|
|
272
|
+
var index = (_a = options.index) !== null && _a !== void 0 ? _a : indexScene(scene);
|
|
273
|
+
var found = [];
|
|
274
|
+
var seen = new Set();
|
|
275
|
+
for (var _i = 0, scene_2 = scene; _i < scene_2.length; _i++) {
|
|
276
|
+
var object = scene_2[_i];
|
|
264
277
|
if (!isBoxedObject(object))
|
|
265
278
|
continue;
|
|
266
|
-
if (options.ignore
|
|
279
|
+
if ((_b = options.ignore) === null || _b === void 0 ? void 0 : _b.has(object.id))
|
|
267
280
|
continue;
|
|
268
|
-
|
|
281
|
+
var taken = options.mode === 'contain'
|
|
269
282
|
? rectContainsRect(rect, rotatedBoxBounds(object))
|
|
270
283
|
: shapeIntersectsRect(object, rect);
|
|
271
284
|
if (!taken)
|
|
272
285
|
continue;
|
|
273
|
-
|
|
286
|
+
var unitId = selectableIdFor(index, object.id, options.enteredGroupId);
|
|
274
287
|
if (!seen.has(unitId)) {
|
|
275
288
|
seen.add(unitId);
|
|
276
289
|
found.push(unitId);
|
|
@@ -278,11 +291,12 @@ export function hitTestRect(scene, rect, options) {
|
|
|
278
291
|
}
|
|
279
292
|
// A group is taken whole or not at all in `contain` mode: one member left out leaves the group out.
|
|
280
293
|
if (options.mode === 'contain') {
|
|
281
|
-
return found.filter((id)
|
|
282
|
-
|
|
294
|
+
return found.filter(function (id) {
|
|
295
|
+
var unit = index.get(id);
|
|
283
296
|
if (!unit || unit.type !== 'group')
|
|
284
297
|
return true;
|
|
285
|
-
for (
|
|
298
|
+
for (var _i = 0, scene_3 = scene; _i < scene_3.length; _i++) {
|
|
299
|
+
var object = scene_3[_i];
|
|
286
300
|
if (isBoxedObject(object) && isWithin(index, object.id, id) && !rectContainsRect(rect, rotatedBoxBounds(object))) {
|
|
287
301
|
return false;
|
|
288
302
|
}
|