@widgetic/canvas 0.5.34 → 0.5.36
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 +105 -36
- package/dist/canvas/DomCanvas.svelte +454 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +222 -0
- package/dist/canvas/autosaveScheduler.d.ts +58 -0
- package/dist/canvas/autosaveScheduler.js +157 -0
- package/dist/canvas/boardFrame.d.ts +13 -0
- package/dist/canvas/boardFrame.js +13 -0
- package/dist/canvas/boardSurface.d.ts +9 -0
- package/dist/canvas/boardSurface.js +9 -0
- package/dist/canvas/chromeLayout.d.ts +76 -0
- package/dist/canvas/chromeLayout.js +121 -0
- package/dist/canvas/emptyBoardHint.d.ts +17 -0
- package/dist/canvas/emptyBoardHint.js +12 -0
- package/dist/canvas/eventScope.d.ts +1 -1
- package/dist/canvas/eventScope.js +1 -1
- package/dist/canvas/host/CanvasHost.d.ts +351 -0
- package/dist/canvas/host/CanvasHost.js +1740 -0
- package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
- package/dist/canvas/host/CanvasHostApi.js +95 -0
- package/dist/canvas/host/browserServices.d.ts +46 -0
- package/dist/canvas/host/browserServices.js +167 -0
- package/dist/canvas/host/conversion.d.ts +51 -0
- package/dist/canvas/host/conversion.js +123 -0
- package/dist/canvas/host/hostObject.d.ts +40 -0
- package/dist/canvas/host/hostObject.js +93 -0
- package/dist/canvas/host/pageEvents.d.ts +24 -0
- package/dist/canvas/host/pageEvents.js +35 -0
- package/dist/canvas/host/screenRects.d.ts +66 -0
- package/dist/canvas/host/screenRects.js +102 -0
- package/dist/canvas/host/strokeSnap.d.ts +67 -0
- package/dist/canvas/host/strokeSnap.js +165 -0
- package/dist/canvas/index.d.ts +9 -7
- package/dist/canvas/index.js +9 -7
- package/dist/canvas/openMarks.d.ts +17 -0
- package/dist/canvas/openMarks.js +26 -0
- package/dist/canvas/panZoomPosition.d.ts +15 -0
- package/dist/canvas/panZoomPosition.js +16 -0
- package/dist/canvas/preferredTextFont.d.ts +12 -0
- package/dist/canvas/preferredTextFont.js +38 -0
- package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
- package/dist/canvas/shapes/ShapeLibrary.js +13 -1
- package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
- package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
- package/dist/canvas/toolbarPosition.d.ts +24 -0
- package/dist/canvas/toolbarPosition.js +24 -0
- package/dist/canvas/types.d.ts +14 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
- package/dist/canvas/ui/CanvasHint.svelte +20 -0
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
- package/dist/canvas/ui/ColorField.svelte +155 -0
- package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
- package/dist/canvas/ui/LoadNotice.svelte +39 -0
- package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
- package/dist/canvas/ui/SelectionPanel.svelte +300 -0
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
- package/dist/canvas/ui/SelectorBar.svelte +82 -0
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
- package/dist/canvas/ui/ShapesMenu.svelte +185 -0
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/ToolButton.svelte +78 -0
- package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
- package/dist/canvas/ui/WidgetActions.svelte +152 -0
- package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
- package/dist/canvas/ui/ZoomControl.svelte +89 -0
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
- package/dist/canvas/ui/colorModel.d.ts +18 -0
- package/dist/canvas/ui/colorModel.js +85 -0
- package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
- package/dist/canvas/ui/colorPickerMessages.js +15 -0
- package/dist/canvas/ui/floatingCard.d.ts +107 -0
- package/dist/canvas/ui/floatingCard.js +107 -0
- package/dist/canvas/ui/floatingLayer.d.ts +21 -0
- package/dist/canvas/ui/floatingLayer.js +52 -0
- package/dist/canvas/ui/icons.d.ts +12 -0
- package/dist/canvas/ui/icons.js +7 -0
- package/dist/canvas/ui/keyHints.d.ts +27 -0
- package/dist/canvas/ui/keyHints.js +43 -0
- package/dist/canvas/ui/popoverSide.d.ts +5 -0
- package/dist/canvas/ui/popoverSide.js +20 -0
- package/dist/canvas/ui/selectionTraits.d.ts +61 -0
- package/dist/canvas/ui/selectionTraits.js +99 -0
- package/dist/canvas/view/CanvasStage.svelte +132 -0
- package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
- package/dist/canvas/view/EraserTrail.svelte +50 -0
- package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
- package/dist/canvas/view/ObjectView.svelte +245 -0
- package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
- package/dist/canvas/view/SelectionOverlay.svelte +487 -0
- package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
- package/dist/canvas/view/TextEditor.svelte +76 -0
- package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.js +34 -0
- package/dist/canvas/view/geometry.d.ts +169 -0
- package/dist/canvas/view/geometry.js +272 -0
- package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
- package/dist/components/index.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +2 -2
- package/dist/engine/EngineCore.d.ts +273 -0
- package/dist/engine/EngineCore.js +1051 -0
- package/dist/engine/codec/canvasDocument.d.ts +57 -0
- package/dist/engine/codec/canvasDocument.js +471 -0
- package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
- package/dist/engine/codec/hostReadableKeys.js +25 -0
- package/dist/engine/core/arrange.d.ts +20 -0
- package/dist/engine/core/arrange.js +81 -0
- package/dist/engine/core/arrow.d.ts +91 -0
- package/dist/engine/core/arrow.js +182 -0
- package/dist/engine/core/brush.d.ts +68 -0
- package/dist/engine/core/brush.js +173 -0
- package/dist/engine/core/defaults.d.ts +70 -0
- package/dist/engine/core/defaults.js +158 -0
- package/dist/engine/core/freehand.d.ts +130 -0
- package/dist/engine/core/freehand.js +430 -0
- package/dist/engine/core/geometry.d.ts +44 -0
- package/dist/engine/core/geometry.js +216 -0
- package/dist/engine/core/history.d.ts +39 -0
- package/dist/engine/core/history.js +69 -0
- package/dist/engine/core/hitTest.d.ts +54 -0
- package/dist/engine/core/hitTest.js +298 -0
- package/dist/engine/core/matrix.d.ts +47 -0
- package/dist/engine/core/matrix.js +101 -0
- package/dist/engine/core/pathData.d.ts +50 -0
- package/dist/engine/core/pathData.js +550 -0
- package/dist/engine/core/rotationReset.d.ts +10 -0
- package/dist/engine/core/rotationReset.js +17 -0
- package/dist/engine/core/scene.d.ts +99 -0
- package/dist/engine/core/scene.js +450 -0
- package/dist/engine/core/scribble.d.ts +64 -0
- package/dist/engine/core/scribble.js +121 -0
- package/dist/engine/core/shapePaths.d.ts +34 -0
- package/dist/engine/core/shapePaths.js +69 -0
- package/dist/engine/core/shapeTools.d.ts +80 -0
- package/dist/engine/core/shapeTools.js +161 -0
- package/dist/engine/core/snapping.d.ts +64 -0
- package/dist/engine/core/snapping.js +164 -0
- package/dist/engine/core/style.d.ts +40 -0
- package/dist/engine/core/style.js +151 -0
- package/dist/engine/core/transform.d.ts +79 -0
- package/dist/engine/core/transform.js +258 -0
- package/dist/engine/core/types.d.ts +238 -0
- package/dist/engine/core/types.js +31 -0
- package/dist/engine/core/viewport.d.ts +32 -0
- package/dist/engine/core/viewport.js +84 -0
- package/dist/engine/input/PointerController.d.ts +38 -0
- package/dist/engine/input/PointerController.js +545 -0
- package/dist/engine/input/arrowHandles.d.ts +38 -0
- package/dist/engine/input/arrowHandles.js +60 -0
- package/dist/engine/input/gestures.d.ts +79 -0
- package/dist/engine/input/gestures.js +769 -0
- package/dist/engine/input/hoverCursor.d.ts +29 -0
- package/dist/engine/input/hoverCursor.js +23 -0
- package/dist/engine/input/keyboard.d.ts +75 -0
- package/dist/engine/input/keyboard.js +264 -0
- package/dist/engine/input/selectionHandles.d.ts +89 -0
- package/dist/engine/input/selectionHandles.js +169 -0
- package/dist/engine/input/types.d.ts +82 -0
- package/dist/engine/input/types.js +5 -0
- package/dist/engine/render/rasterize.d.ts +74 -0
- package/dist/engine/render/rasterize.js +273 -0
- package/dist/engine/state.d.ts +124 -0
- package/dist/engine/state.js +47 -0
- package/dist/engine/text/layoutText.d.ts +53 -0
- package/dist/engine/text/layoutText.js +170 -0
- package/dist/engine/widgets.d.ts +124 -0
- package/dist/engine/widgets.js +345 -0
- package/dist/i18n/labels.d.ts +4 -15
- package/dist/i18n/labels.js +33 -53
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +51 -122
- package/dist/i18n/messages.js +58 -131
- package/dist/i18n/shortcuts.d.ts +0 -1
- package/dist/i18n/shortcuts.js +2 -11
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/package.json +30 -5
- package/dist/canvas/Canvas.svelte +0 -13299
- package/dist/canvas/Canvas.svelte.d.ts +0 -281
- package/dist/canvas/CanvasToolbar.svelte +0 -1498
- package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
- package/dist/canvas/ContextMenu.svelte +0 -305
- package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
- package/dist/canvas/PanZoomPanel.svelte +0 -478
- package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
- package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
- package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
- package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
- package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
- package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
- package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
- package/dist/canvas/shadowRootText.d.ts +0 -5
- package/dist/canvas/shadowRootText.js +0 -43
- package/dist/canvas/shapes/FrameShape.d.ts +0 -97
- package/dist/canvas/shapes/FrameShape.js +0 -953
- package/dist/canvas/shapes/ImageShape.d.ts +0 -47
- package/dist/canvas/shapes/ImageShape.js +0 -339
- package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
- package/dist/canvas/shapes/WidgetShape.js +0 -200
- package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
- package/dist/canvas/widgetPreviewGeometry.js +0 -66
- package/dist/i18n/shapeWords.d.ts +0 -30
- package/dist/i18n/shapeWords.js +0 -36
- package/dist/i18n/slots.d.ts +0 -20
- package/dist/i18n/slots.js +0 -40
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plane geometry for boxes, points and polylines: what hit testing, snapping, selection and the
|
|
3
|
+
* rasterizer are built from. Pure functions on plain numbers.
|
|
4
|
+
*/
|
|
5
|
+
import { boxToWorldMatrix, applyMatrixToPoint, invertMatrix } from './matrix.js';
|
|
6
|
+
const EPSILON = 1e-9;
|
|
7
|
+
export function rectRight(rect) {
|
|
8
|
+
return rect.x + rect.width;
|
|
9
|
+
}
|
|
10
|
+
export function rectBottom(rect) {
|
|
11
|
+
return rect.y + rect.height;
|
|
12
|
+
}
|
|
13
|
+
export function rectCenter(rect) {
|
|
14
|
+
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
15
|
+
}
|
|
16
|
+
/** The rectangle spanned by two corner points, in any order. */
|
|
17
|
+
export function rectFromPoints(first, second) {
|
|
18
|
+
return {
|
|
19
|
+
x: Math.min(first.x, second.x),
|
|
20
|
+
y: Math.min(first.y, second.y),
|
|
21
|
+
width: Math.abs(second.x - first.x),
|
|
22
|
+
height: Math.abs(second.y - first.y)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function rectContainsPoint(rect, point) {
|
|
26
|
+
return (point.x >= rect.x &&
|
|
27
|
+
point.x <= rect.x + rect.width &&
|
|
28
|
+
point.y >= rect.y &&
|
|
29
|
+
point.y <= rect.y + rect.height);
|
|
30
|
+
}
|
|
31
|
+
export function rectsIntersect(first, second) {
|
|
32
|
+
return (first.x <= second.x + second.width &&
|
|
33
|
+
first.x + first.width >= second.x &&
|
|
34
|
+
first.y <= second.y + second.height &&
|
|
35
|
+
first.y + first.height >= second.y);
|
|
36
|
+
}
|
|
37
|
+
export function rectContainsRect(outer, inner) {
|
|
38
|
+
return (inner.x >= outer.x &&
|
|
39
|
+
inner.y >= outer.y &&
|
|
40
|
+
inner.x + inner.width <= outer.x + outer.width &&
|
|
41
|
+
inner.y + inner.height <= outer.y + outer.height);
|
|
42
|
+
}
|
|
43
|
+
export function inflateRect(rect, amount) {
|
|
44
|
+
return {
|
|
45
|
+
x: rect.x - amount,
|
|
46
|
+
y: rect.y - amount,
|
|
47
|
+
width: rect.width + amount * 2,
|
|
48
|
+
height: rect.height + amount * 2
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** The smallest rectangle that holds all of them, or null for none. */
|
|
52
|
+
export function unionRects(rects) {
|
|
53
|
+
if (rects.length === 0)
|
|
54
|
+
return null;
|
|
55
|
+
let left = Infinity;
|
|
56
|
+
let top = Infinity;
|
|
57
|
+
let right = -Infinity;
|
|
58
|
+
let bottom = -Infinity;
|
|
59
|
+
for (const rect of rects) {
|
|
60
|
+
left = Math.min(left, rect.x);
|
|
61
|
+
top = Math.min(top, rect.y);
|
|
62
|
+
right = Math.max(right, rect.x + rect.width);
|
|
63
|
+
bottom = Math.max(bottom, rect.y + rect.height);
|
|
64
|
+
}
|
|
65
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
66
|
+
}
|
|
67
|
+
/** The smallest rectangle that holds all the points, or null for none. */
|
|
68
|
+
export function boundsOfPoints(points) {
|
|
69
|
+
if (points.length === 0)
|
|
70
|
+
return null;
|
|
71
|
+
let left = Infinity;
|
|
72
|
+
let top = Infinity;
|
|
73
|
+
let right = -Infinity;
|
|
74
|
+
let bottom = -Infinity;
|
|
75
|
+
for (const point of points) {
|
|
76
|
+
left = Math.min(left, point.x);
|
|
77
|
+
top = Math.min(top, point.y);
|
|
78
|
+
right = Math.max(right, point.x);
|
|
79
|
+
bottom = Math.max(bottom, point.y);
|
|
80
|
+
}
|
|
81
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
82
|
+
}
|
|
83
|
+
/** The four corners of a box in world space, clockwise from the top-left (before rotation). */
|
|
84
|
+
export function boxCorners(box) {
|
|
85
|
+
const matrix = boxToWorldMatrix(box);
|
|
86
|
+
return [
|
|
87
|
+
applyMatrixToPoint(matrix, { x: 0, y: 0 }),
|
|
88
|
+
applyMatrixToPoint(matrix, { x: box.width, y: 0 }),
|
|
89
|
+
applyMatrixToPoint(matrix, { x: box.width, y: box.height }),
|
|
90
|
+
applyMatrixToPoint(matrix, { x: 0, y: box.height })
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
/** The axis-aligned rectangle around a rotated box. */
|
|
94
|
+
export function rotatedBoxBounds(box) {
|
|
95
|
+
if (!box.rotation)
|
|
96
|
+
return { x: box.x, y: box.y, width: box.width, height: box.height };
|
|
97
|
+
return boundsOfPoints(boxCorners(box));
|
|
98
|
+
}
|
|
99
|
+
/** 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 = 0) {
|
|
101
|
+
const inverse = invertMatrix(boxToWorldMatrix(box));
|
|
102
|
+
if (!inverse)
|
|
103
|
+
return false;
|
|
104
|
+
const local = applyMatrixToPoint(inverse, point);
|
|
105
|
+
return (local.x >= -slop &&
|
|
106
|
+
local.x <= box.width + slop &&
|
|
107
|
+
local.y >= -slop &&
|
|
108
|
+
local.y <= box.height + slop);
|
|
109
|
+
}
|
|
110
|
+
/** A world point in the local space of a box. Null when the box is degenerate. */
|
|
111
|
+
export function worldToBoxLocal(box, point) {
|
|
112
|
+
const inverse = invertMatrix(boxToWorldMatrix(box));
|
|
113
|
+
return inverse ? applyMatrixToPoint(inverse, point) : null;
|
|
114
|
+
}
|
|
115
|
+
export function distanceBetween(first, second) {
|
|
116
|
+
return Math.hypot(second.x - first.x, second.y - first.y);
|
|
117
|
+
}
|
|
118
|
+
/** The distance from a point to the segment a-b. */
|
|
119
|
+
export function distanceToSegment(point, a, b) {
|
|
120
|
+
const deltaX = b.x - a.x;
|
|
121
|
+
const deltaY = b.y - a.y;
|
|
122
|
+
const lengthSquared = deltaX * deltaX + deltaY * deltaY;
|
|
123
|
+
if (lengthSquared < EPSILON)
|
|
124
|
+
return distanceBetween(point, a);
|
|
125
|
+
const along = ((point.x - a.x) * deltaX + (point.y - a.y) * deltaY) / lengthSquared;
|
|
126
|
+
const clamped = Math.max(0, Math.min(1, along));
|
|
127
|
+
return Math.hypot(point.x - (a.x + clamped * deltaX), point.y - (a.y + clamped * deltaY));
|
|
128
|
+
}
|
|
129
|
+
/** The distance from a point to a polyline (closed: the last point joins the first). */
|
|
130
|
+
export function distanceToPolyline(point, vertices, closed) {
|
|
131
|
+
if (vertices.length === 0)
|
|
132
|
+
return Infinity;
|
|
133
|
+
if (vertices.length === 1)
|
|
134
|
+
return distanceBetween(point, vertices[0]);
|
|
135
|
+
let best = Infinity;
|
|
136
|
+
const last = closed ? vertices.length : vertices.length - 1;
|
|
137
|
+
for (let index = 0; index < last; index += 1) {
|
|
138
|
+
const distance = distanceToSegment(point, vertices[index], vertices[(index + 1) % vertices.length]);
|
|
139
|
+
if (distance < best)
|
|
140
|
+
best = distance;
|
|
141
|
+
}
|
|
142
|
+
return best;
|
|
143
|
+
}
|
|
144
|
+
/** Even-odd test: whether the point lies inside the polygon. */
|
|
145
|
+
export function pointInPolygon(point, vertices) {
|
|
146
|
+
let inside = false;
|
|
147
|
+
for (let index = 0, previous = vertices.length - 1; index < vertices.length; previous = index, index += 1) {
|
|
148
|
+
const current = vertices[index];
|
|
149
|
+
const before = vertices[previous];
|
|
150
|
+
const crosses = current.y > point.y !== before.y > point.y &&
|
|
151
|
+
point.x < ((before.x - current.x) * (point.y - current.y)) / (before.y - current.y) + current.x;
|
|
152
|
+
if (crosses)
|
|
153
|
+
inside = !inside;
|
|
154
|
+
}
|
|
155
|
+
return inside;
|
|
156
|
+
}
|
|
157
|
+
/** Whether segment p1-p2 crosses segment p3-p4. */
|
|
158
|
+
export function segmentsIntersect(p1, p2, p3, p4) {
|
|
159
|
+
const orientation = (a, b, c) => (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
|
|
160
|
+
const d1 = orientation(p3, p4, p1);
|
|
161
|
+
const d2 = orientation(p3, p4, p2);
|
|
162
|
+
const d3 = orientation(p1, p2, p3);
|
|
163
|
+
const d4 = orientation(p1, p2, p4);
|
|
164
|
+
if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
|
|
165
|
+
return true;
|
|
166
|
+
const onSegment = (a, b, c) => Math.min(a.x, b.x) - EPSILON <= c.x &&
|
|
167
|
+
c.x <= Math.max(a.x, b.x) + EPSILON &&
|
|
168
|
+
Math.min(a.y, b.y) - EPSILON <= c.y &&
|
|
169
|
+
c.y <= Math.max(a.y, b.y) + EPSILON;
|
|
170
|
+
if (Math.abs(d1) < EPSILON && onSegment(p3, p4, p1))
|
|
171
|
+
return true;
|
|
172
|
+
if (Math.abs(d2) < EPSILON && onSegment(p3, p4, p2))
|
|
173
|
+
return true;
|
|
174
|
+
if (Math.abs(d3) < EPSILON && onSegment(p1, p2, p3))
|
|
175
|
+
return true;
|
|
176
|
+
if (Math.abs(d4) < EPSILON && onSegment(p1, p2, p4))
|
|
177
|
+
return true;
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
/** Whether a polygon (a rotated box, say) and an axis-aligned rectangle touch. */
|
|
181
|
+
export function polygonIntersectsRect(vertices, rect) {
|
|
182
|
+
if (vertices.length === 0)
|
|
183
|
+
return false;
|
|
184
|
+
// Any vertex inside the rectangle.
|
|
185
|
+
for (const vertex of vertices) {
|
|
186
|
+
if (rectContainsPoint(rect, vertex))
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
// The rectangle's centre inside the polygon (the rectangle sits entirely within it).
|
|
190
|
+
if (pointInPolygon(rectCenter(rect), vertices))
|
|
191
|
+
return true;
|
|
192
|
+
// Any edge crossing any edge of the rectangle.
|
|
193
|
+
const rectangleCorners = [
|
|
194
|
+
{ x: rect.x, y: rect.y },
|
|
195
|
+
{ x: rect.x + rect.width, y: rect.y },
|
|
196
|
+
{ x: rect.x + rect.width, y: rect.y + rect.height },
|
|
197
|
+
{ x: rect.x, y: rect.y + rect.height }
|
|
198
|
+
];
|
|
199
|
+
for (let index = 0; index < vertices.length; index += 1) {
|
|
200
|
+
const start = vertices[index];
|
|
201
|
+
const end = vertices[(index + 1) % vertices.length];
|
|
202
|
+
for (let side = 0; side < 4; side += 1) {
|
|
203
|
+
if (segmentsIntersect(start, end, rectangleCorners[side], rectangleCorners[(side + 1) % 4]))
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
/** Rounds to a number of decimals (default 2): for stored geometry, so JSON does not carry noise digits. */
|
|
210
|
+
export function roundTo(value, decimals = 2) {
|
|
211
|
+
const factor = 10 ** decimals;
|
|
212
|
+
return Math.round(value * factor) / factor;
|
|
213
|
+
}
|
|
214
|
+
export function clampNumber(value, minimum, maximum) {
|
|
215
|
+
return Math.min(maximum, Math.max(minimum, value));
|
|
216
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo and redo as a stack of immutable states.
|
|
3
|
+
*
|
|
4
|
+
* Records of the scene are immutable, so a state is only a reference to the array that held them: keeping a
|
|
5
|
+
* hundred steps costs a hundred arrays of pointers, not a hundred copies of the board. One step is pushed
|
|
6
|
+
* when a gesture ends (a drag, a resize, a stroke), never while it runs.
|
|
7
|
+
*/
|
|
8
|
+
export interface HistoryOptions<State> {
|
|
9
|
+
/** How many steps are kept; the oldest is dropped past it. */
|
|
10
|
+
limit?: number;
|
|
11
|
+
/** Whether two states are the same step (the default is identity, which suits immutable scenes). */
|
|
12
|
+
isSame?: (first: State, second: State) => boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare class HistoryStack<State> {
|
|
15
|
+
private entries;
|
|
16
|
+
private cursor;
|
|
17
|
+
private readonly limit;
|
|
18
|
+
private readonly isSame;
|
|
19
|
+
constructor(initial: State, options?: HistoryOptions<State>);
|
|
20
|
+
/** The state the board is in now. */
|
|
21
|
+
get current(): State;
|
|
22
|
+
get canUndo(): boolean;
|
|
23
|
+
get canRedo(): boolean;
|
|
24
|
+
/** How many states the stack holds (the start included). */
|
|
25
|
+
get length(): number;
|
|
26
|
+
/**
|
|
27
|
+
* Adds a step after the current one and forgets the steps that were undone.
|
|
28
|
+
* Returns false (and records nothing) when the state is the same as the current one.
|
|
29
|
+
*/
|
|
30
|
+
push(state: State): boolean;
|
|
31
|
+
/** Goes one step back and returns that state, or null at the start. */
|
|
32
|
+
undo(): State | null;
|
|
33
|
+
/** Goes one step forward and returns that state, or null at the end. */
|
|
34
|
+
redo(): State | null;
|
|
35
|
+
/** Replaces the current step without adding one (for a change that must not be undone on its own). */
|
|
36
|
+
replaceCurrent(state: State): void;
|
|
37
|
+
/** Forgets everything and starts again from this state (a canvas was loaded). */
|
|
38
|
+
reset(state: State): void;
|
|
39
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Undo and redo as a stack of immutable states.
|
|
3
|
+
*
|
|
4
|
+
* Records of the scene are immutable, so a state is only a reference to the array that held them: keeping a
|
|
5
|
+
* hundred steps costs a hundred arrays of pointers, not a hundred copies of the board. One step is pushed
|
|
6
|
+
* when a gesture ends (a drag, a resize, a stroke), never while it runs.
|
|
7
|
+
*/
|
|
8
|
+
export class HistoryStack {
|
|
9
|
+
entries;
|
|
10
|
+
cursor = 0;
|
|
11
|
+
limit;
|
|
12
|
+
isSame;
|
|
13
|
+
constructor(initial, options = {}) {
|
|
14
|
+
this.entries = [initial];
|
|
15
|
+
this.limit = Math.max(2, options.limit ?? 100);
|
|
16
|
+
this.isSame = options.isSame ?? Object.is;
|
|
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;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Adds a step after the current one and forgets the steps that were undone.
|
|
34
|
+
* Returns false (and records nothing) when the state is the same as the current one.
|
|
35
|
+
*/
|
|
36
|
+
push(state) {
|
|
37
|
+
if (this.isSame(this.current, state))
|
|
38
|
+
return false;
|
|
39
|
+
this.entries = this.entries.slice(0, this.cursor + 1);
|
|
40
|
+
this.entries.push(state);
|
|
41
|
+
if (this.entries.length > this.limit)
|
|
42
|
+
this.entries.splice(0, this.entries.length - this.limit);
|
|
43
|
+
this.cursor = this.entries.length - 1;
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
/** Goes one step back and returns that state, or null at the start. */
|
|
47
|
+
undo() {
|
|
48
|
+
if (!this.canUndo)
|
|
49
|
+
return null;
|
|
50
|
+
this.cursor -= 1;
|
|
51
|
+
return this.entries[this.cursor];
|
|
52
|
+
}
|
|
53
|
+
/** Goes one step forward and returns that state, or null at the end. */
|
|
54
|
+
redo() {
|
|
55
|
+
if (!this.canRedo)
|
|
56
|
+
return null;
|
|
57
|
+
this.cursor += 1;
|
|
58
|
+
return this.entries[this.cursor];
|
|
59
|
+
}
|
|
60
|
+
/** Replaces the current step without adding one (for a change that must not be undone on its own). */
|
|
61
|
+
replaceCurrent(state) {
|
|
62
|
+
this.entries[this.cursor] = state;
|
|
63
|
+
}
|
|
64
|
+
/** Forgets everything and starts again from this state (a canvas was loaded). */
|
|
65
|
+
reset(state) {
|
|
66
|
+
this.entries = [state];
|
|
67
|
+
this.cursor = 0;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What is under a point, and what a marquee touches. Decided by geometry on the records, never by reading
|
|
3
|
+
* pixels or asking the DOM, so it is exact, cheap and the same in a test as in a browser.
|
|
4
|
+
*
|
|
5
|
+
* - A shape with a fill is hit anywhere inside it; one without a fill is hit only on its outline (so a
|
|
6
|
+
* hollow frame never hides what lies inside it).
|
|
7
|
+
* - Text, images and widget cards are hit anywhere in their box.
|
|
8
|
+
* - A point counts as "on the outline" when it is within the stroke's half width plus a tolerance that is
|
|
9
|
+
* constant on the screen (so a thin line is as easy to grab at 10% as at 800%).
|
|
10
|
+
* - A group answers as a whole: the hit is the group nearest to the board, unless the person has entered a
|
|
11
|
+
* group, whose members are then picked one by one.
|
|
12
|
+
*/
|
|
13
|
+
import type { Scene } from './scene.js';
|
|
14
|
+
import type { ObjectId, Point, Rect, SceneObject, ShapeObject } from './types.js';
|
|
15
|
+
export interface HitTestOptions {
|
|
16
|
+
/** The zoom of the view: the tolerance is given in screen pixels and converted with it. */
|
|
17
|
+
zoom: number;
|
|
18
|
+
/** How far from an outline a point still counts as on it, in screen pixels. */
|
|
19
|
+
tolerancePixels?: number;
|
|
20
|
+
/** Objects to look through (the ones being dragged, for instance). */
|
|
21
|
+
ignore?: ReadonlySet<ObjectId>;
|
|
22
|
+
/** The group whose members may be picked one by one, or null. */
|
|
23
|
+
enteredGroupId?: ObjectId | null;
|
|
24
|
+
/** A prebuilt index of the scene, when the caller makes many queries on the same scene. */
|
|
25
|
+
index?: ReadonlyMap<ObjectId, SceneObject>;
|
|
26
|
+
}
|
|
27
|
+
export interface MarqueeOptions extends HitTestOptions {
|
|
28
|
+
/** `intersect` takes what the marquee touches, `contain` only what it holds entirely. */
|
|
29
|
+
mode: 'intersect' | 'contain';
|
|
30
|
+
}
|
|
31
|
+
/** Whether a world point is on a shape (inside it, or near its outline, depending on the kind of shape). */
|
|
32
|
+
export declare function shapeContainsPoint(shape: ShapeObject, worldPoint: Point, toleranceWorld: number): boolean;
|
|
33
|
+
/** The topmost selectable object under a world point, or null. */
|
|
34
|
+
export declare function hitTestPoint(scene: Scene, worldPoint: Point, options: HitTestOptions): ObjectId | null;
|
|
35
|
+
/**
|
|
36
|
+
* Every selectable object under a world point, the lowest first and each once. The stack an Alt-click steps
|
|
37
|
+
* through, one object further down at every click.
|
|
38
|
+
*/
|
|
39
|
+
export declare function hitTestStack(scene: Scene, worldPoint: Point, options: HitTestOptions): ObjectId[];
|
|
40
|
+
/** The leaf shape under a world point, ignoring groups (what the eraser removes, what a double-click enters). */
|
|
41
|
+
export declare function hitTestLeaf(scene: Scene, worldPoint: Point, options: HitTestOptions): ShapeObject | null;
|
|
42
|
+
/**
|
|
43
|
+
* Whether a shape touches a rectangle of the world (by its outline or fill, not only by its box).
|
|
44
|
+
*
|
|
45
|
+
* Two answers come before the tests of the outline, both from the rectangle around the shape's own points: a box that lies
|
|
46
|
+
* clear of the rectangle touches nothing of it, and a box that the rectangle holds whole has every point of the shape inside
|
|
47
|
+
* the rectangle. On a board where a marquee holds or misses most of what is on it, only the shapes that straddle its edge are
|
|
48
|
+
* tested point by point.
|
|
49
|
+
*/
|
|
50
|
+
export declare function shapeIntersectsRect(shape: ShapeObject, rect: Rect): boolean;
|
|
51
|
+
/** The selectable objects a marquee takes, each once, in paint order. */
|
|
52
|
+
export declare function hitTestRect(scene: Scene, rect: Rect, options: MarqueeOptions): ObjectId[];
|
|
53
|
+
/** The rectangle of a polyline's points, for callers that need to bound a flattened outline. */
|
|
54
|
+
export declare function outlineBounds(points: readonly Point[]): Rect | null;
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What is under a point, and what a marquee touches. Decided by geometry on the records, never by reading
|
|
3
|
+
* pixels or asking the DOM, so it is exact, cheap and the same in a test as in a browser.
|
|
4
|
+
*
|
|
5
|
+
* - A shape with a fill is hit anywhere inside it; one without a fill is hit only on its outline (so a
|
|
6
|
+
* hollow frame never hides what lies inside it).
|
|
7
|
+
* - Text, images and widget cards are hit anywhere in their box.
|
|
8
|
+
* - A point counts as "on the outline" when it is within the stroke's half width plus a tolerance that is
|
|
9
|
+
* constant on the screen (so a thin line is as easy to grab at 10% as at 800%).
|
|
10
|
+
* - A group answers as a whole: the hit is the group nearest to the board, unless the person has entered a
|
|
11
|
+
* group, whose members are then picked one by one.
|
|
12
|
+
*/
|
|
13
|
+
import { boundsOfPoints, distanceToPolyline, pointInBox, pointInPolygon, polygonIntersectsRect, rectContainsRect, rectCenter, rotatedBoxBounds, segmentsIntersect, boxCorners, worldToBoxLocal, rectContainsPoint } from './geometry.js';
|
|
14
|
+
import { brushOverflow } from './brush.js';
|
|
15
|
+
import { applyMatrixToPoint, boxToWorldMatrix } from './matrix.js';
|
|
16
|
+
import { flattenPath, parsePathData } from './pathData.js';
|
|
17
|
+
import { indexScene, isWithin, topLevelAncestorId } from './scene.js';
|
|
18
|
+
import { isBoxedObject } from './types.js';
|
|
19
|
+
const DEFAULT_TOLERANCE_PIXELS = 5;
|
|
20
|
+
const FLATTENED_PATH_CACHE_LIMIT = 400;
|
|
21
|
+
/** Flattened outlines by path data, so a hover over a busy board does not parse the same strokes again. */
|
|
22
|
+
const flattenedPathCache = new Map();
|
|
23
|
+
function flattenedOutline(data) {
|
|
24
|
+
const cached = flattenedPathCache.get(data);
|
|
25
|
+
if (cached)
|
|
26
|
+
return cached;
|
|
27
|
+
const flattened = flattenPath(parsePathData(data));
|
|
28
|
+
if (flattenedPathCache.size >= FLATTENED_PATH_CACHE_LIMIT)
|
|
29
|
+
flattenedPathCache.clear();
|
|
30
|
+
flattenedPathCache.set(data, flattened);
|
|
31
|
+
return flattened;
|
|
32
|
+
}
|
|
33
|
+
function isHollow(shape) {
|
|
34
|
+
if (shape.type !== 'rect' && shape.type !== 'ellipse' && shape.type !== 'path')
|
|
35
|
+
return false;
|
|
36
|
+
return shape.fill === 'transparent' || shape.fill === 'none' || shape.fill === '' || shape.fillOpacity <= 0;
|
|
37
|
+
}
|
|
38
|
+
/** Half of the stroke that shows (zero when there is none): the part of the outline that lies outside it. */
|
|
39
|
+
function halfStroke(shape) {
|
|
40
|
+
if (shape.type !== 'rect' && shape.type !== 'ellipse' && shape.type !== 'path')
|
|
41
|
+
return 0;
|
|
42
|
+
if (shape.stroke === 'transparent' || shape.stroke === 'none' || shape.strokeWidth <= 0)
|
|
43
|
+
return 0;
|
|
44
|
+
return shape.strokeWidth / 2;
|
|
45
|
+
}
|
|
46
|
+
/** Whether a world point is on a shape (inside it, or near its outline, depending on the kind of shape). */
|
|
47
|
+
export function shapeContainsPoint(shape, worldPoint, toleranceWorld) {
|
|
48
|
+
const reach = toleranceWorld + halfStroke(shape);
|
|
49
|
+
// Cheap rejection first: outside the box grown by the reach, nothing else can say yes. A brush stroke's outline spills past
|
|
50
|
+
// 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
|
+
if (!pointInBox(shape, worldPoint, reach + brushOverflow(shape)))
|
|
52
|
+
return false;
|
|
53
|
+
const local = worldToBoxLocal(shape, worldPoint);
|
|
54
|
+
if (!local)
|
|
55
|
+
return false;
|
|
56
|
+
switch (shape.type) {
|
|
57
|
+
case 'rect': {
|
|
58
|
+
if (!isHollow(shape))
|
|
59
|
+
return true;
|
|
60
|
+
const edgeDistance = Math.min(Math.abs(local.x), Math.abs(local.x - shape.width), Math.abs(local.y), Math.abs(local.y - shape.height));
|
|
61
|
+
return edgeDistance <= reach;
|
|
62
|
+
}
|
|
63
|
+
case 'ellipse': {
|
|
64
|
+
const radiusX = Math.max(0.5, shape.width / 2);
|
|
65
|
+
const radiusY = Math.max(0.5, shape.height / 2);
|
|
66
|
+
const normalized = Math.hypot((local.x - radiusX) / radiusX, (local.y - radiusY) / radiusY);
|
|
67
|
+
const smallestRadius = Math.min(radiusX, radiusY);
|
|
68
|
+
if (!isHollow(shape))
|
|
69
|
+
return normalized <= 1 + reach / smallestRadius;
|
|
70
|
+
return Math.abs(normalized - 1) * smallestRadius <= reach;
|
|
71
|
+
}
|
|
72
|
+
case 'path': {
|
|
73
|
+
const outlines = flattenedOutline(shape.d);
|
|
74
|
+
for (const outline of outlines) {
|
|
75
|
+
if (distanceToPolyline(local, outline.points, outline.closed) <= reach)
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
if (!isHollow(shape)) {
|
|
79
|
+
// A fill closes an open outline implicitly, so every outline counts as a polygon here.
|
|
80
|
+
for (const outline of outlines) {
|
|
81
|
+
if (outline.points.length >= 3 && pointInPolygon(local, outline.points))
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
default:
|
|
88
|
+
// Text, images and widget cards answer for their whole box, which `pointInBox` already checked.
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** The unit a person selects when a leaf shape is picked: the group nearest the board, or the entered group's member. */
|
|
93
|
+
function selectableIdFor(index, leafId, enteredGroupId) {
|
|
94
|
+
if (!enteredGroupId || !isWithin(index, leafId, enteredGroupId) || leafId === enteredGroupId) {
|
|
95
|
+
return topLevelAncestorId(index, leafId);
|
|
96
|
+
}
|
|
97
|
+
let current = index.get(leafId);
|
|
98
|
+
while (current && current.parentId !== null && current.parentId !== enteredGroupId) {
|
|
99
|
+
current = index.get(current.parentId);
|
|
100
|
+
}
|
|
101
|
+
return current?.id ?? leafId;
|
|
102
|
+
}
|
|
103
|
+
/** The topmost selectable object under a world point, or null. */
|
|
104
|
+
export function hitTestPoint(scene, worldPoint, options) {
|
|
105
|
+
const index = options.index ?? indexScene(scene);
|
|
106
|
+
const toleranceWorld = (options.tolerancePixels ?? DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
|
|
107
|
+
for (let position = scene.length - 1; position >= 0; position -= 1) {
|
|
108
|
+
const object = scene[position];
|
|
109
|
+
if (!isBoxedObject(object))
|
|
110
|
+
continue;
|
|
111
|
+
if (options.ignore?.has(object.id))
|
|
112
|
+
continue;
|
|
113
|
+
if (shapeContainsPoint(object, worldPoint, toleranceWorld)) {
|
|
114
|
+
return selectableIdFor(index, object.id, options.enteredGroupId);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Every selectable object under a world point, the lowest first and each once. The stack an Alt-click steps
|
|
121
|
+
* through, one object further down at every click.
|
|
122
|
+
*/
|
|
123
|
+
export function hitTestStack(scene, worldPoint, options) {
|
|
124
|
+
const index = options.index ?? indexScene(scene);
|
|
125
|
+
const toleranceWorld = (options.tolerancePixels ?? DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
|
|
126
|
+
const stack = [];
|
|
127
|
+
const seen = new Set();
|
|
128
|
+
for (const object of scene) {
|
|
129
|
+
if (!isBoxedObject(object))
|
|
130
|
+
continue;
|
|
131
|
+
if (options.ignore?.has(object.id))
|
|
132
|
+
continue;
|
|
133
|
+
if (!shapeContainsPoint(object, worldPoint, toleranceWorld))
|
|
134
|
+
continue;
|
|
135
|
+
const unitId = selectableIdFor(index, object.id, options.enteredGroupId);
|
|
136
|
+
// A unit that is hit again by a later member moves up to that member's place in the paint order.
|
|
137
|
+
if (seen.has(unitId))
|
|
138
|
+
stack.splice(stack.indexOf(unitId), 1);
|
|
139
|
+
seen.add(unitId);
|
|
140
|
+
stack.push(unitId);
|
|
141
|
+
}
|
|
142
|
+
return stack;
|
|
143
|
+
}
|
|
144
|
+
/** The leaf shape under a world point, ignoring groups (what the eraser removes, what a double-click enters). */
|
|
145
|
+
export function hitTestLeaf(scene, worldPoint, options) {
|
|
146
|
+
const toleranceWorld = (options.tolerancePixels ?? DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
|
|
147
|
+
for (let position = scene.length - 1; position >= 0; position -= 1) {
|
|
148
|
+
const object = scene[position];
|
|
149
|
+
if (!isBoxedObject(object))
|
|
150
|
+
continue;
|
|
151
|
+
if (options.ignore?.has(object.id))
|
|
152
|
+
continue;
|
|
153
|
+
if (shapeContainsPoint(object, worldPoint, toleranceWorld))
|
|
154
|
+
return object;
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
/** Whether a polyline (open or closed) touches a rectangle. */
|
|
159
|
+
function polylineTouchesRect(points, closed, rect) {
|
|
160
|
+
for (const point of points) {
|
|
161
|
+
if (rectContainsPoint(rect, point))
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
const corners = [
|
|
165
|
+
{ x: rect.x, y: rect.y },
|
|
166
|
+
{ x: rect.x + rect.width, y: rect.y },
|
|
167
|
+
{ x: rect.x + rect.width, y: rect.y + rect.height },
|
|
168
|
+
{ x: rect.x, y: rect.y + rect.height }
|
|
169
|
+
];
|
|
170
|
+
const last = closed ? points.length : points.length - 1;
|
|
171
|
+
for (let position = 0; position < last; position += 1) {
|
|
172
|
+
const start = points[position];
|
|
173
|
+
const end = points[(position + 1) % points.length];
|
|
174
|
+
for (let side = 0; side < 4; side += 1) {
|
|
175
|
+
if (segmentsIntersect(start, end, corners[side], corners[(side + 1) % 4]))
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
const shapesInTheWorld = new WeakMap();
|
|
182
|
+
function shapeInTheWorld(shape) {
|
|
183
|
+
const data = shape.type === 'path' ? shape.d : null;
|
|
184
|
+
const cached = shapesInTheWorld.get(shape);
|
|
185
|
+
if (cached &&
|
|
186
|
+
cached.x === shape.x &&
|
|
187
|
+
cached.y === shape.y &&
|
|
188
|
+
cached.width === shape.width &&
|
|
189
|
+
cached.height === shape.height &&
|
|
190
|
+
cached.rotation === shape.rotation &&
|
|
191
|
+
cached.data === data) {
|
|
192
|
+
return cached;
|
|
193
|
+
}
|
|
194
|
+
const outlines = [];
|
|
195
|
+
let corners = [];
|
|
196
|
+
if (data !== null) {
|
|
197
|
+
const matrix = boxToWorldMatrix(shape);
|
|
198
|
+
for (const outline of flattenedOutline(data)) {
|
|
199
|
+
outlines.push({ points: outline.points.map((point) => applyMatrixToPoint(matrix, point)), closed: outline.closed });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
corners = boxCorners(shape);
|
|
204
|
+
}
|
|
205
|
+
const made = {
|
|
206
|
+
x: shape.x,
|
|
207
|
+
y: shape.y,
|
|
208
|
+
width: shape.width,
|
|
209
|
+
height: shape.height,
|
|
210
|
+
rotation: shape.rotation,
|
|
211
|
+
data,
|
|
212
|
+
corners,
|
|
213
|
+
outlines,
|
|
214
|
+
bounds: boundsOfPoints(data !== null ? outlines.flatMap((outline) => outline.points) : corners)
|
|
215
|
+
};
|
|
216
|
+
shapesInTheWorld.set(shape, made);
|
|
217
|
+
return made;
|
|
218
|
+
}
|
|
219
|
+
// 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
|
+
// the segment tests allow themselves (1e-9), so that the quick answer below never says no where those tests would say yes.
|
|
221
|
+
const CLEAR_OF_RECT_MARGIN = 1e-6;
|
|
222
|
+
/**
|
|
223
|
+
* Whether a shape touches a rectangle of the world (by its outline or fill, not only by its box).
|
|
224
|
+
*
|
|
225
|
+
* Two answers come before the tests of the outline, both from the rectangle around the shape's own points: a box that lies
|
|
226
|
+
* clear of the rectangle touches nothing of it, and a box that the rectangle holds whole has every point of the shape inside
|
|
227
|
+
* the rectangle. On a board where a marquee holds or misses most of what is on it, only the shapes that straddle its edge are
|
|
228
|
+
* tested point by point.
|
|
229
|
+
*/
|
|
230
|
+
export function shapeIntersectsRect(shape, rect) {
|
|
231
|
+
const world = shapeInTheWorld(shape);
|
|
232
|
+
const bounds = world.bounds;
|
|
233
|
+
if (bounds) {
|
|
234
|
+
const clear = bounds.x - CLEAR_OF_RECT_MARGIN > rect.x + rect.width ||
|
|
235
|
+
bounds.x + bounds.width + CLEAR_OF_RECT_MARGIN < rect.x ||
|
|
236
|
+
bounds.y - CLEAR_OF_RECT_MARGIN > rect.y + rect.height ||
|
|
237
|
+
bounds.y + bounds.height + CLEAR_OF_RECT_MARGIN < rect.y;
|
|
238
|
+
if (clear)
|
|
239
|
+
return false;
|
|
240
|
+
if (rectContainsRect(rect, bounds))
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
if (shape.type === 'path') {
|
|
244
|
+
for (const outline of world.outlines) {
|
|
245
|
+
if (polylineTouchesRect(outline.points, outline.closed, rect))
|
|
246
|
+
return true;
|
|
247
|
+
if (!isHollow(shape) && outline.points.length >= 3 && pointInPolygon(rectCenter(rect), outline.points))
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
if (shape.type === 'rect' && isHollow(shape)) {
|
|
253
|
+
// A hollow rectangle is touched by its four sides, not by its inside.
|
|
254
|
+
return polylineTouchesRect(world.corners, true, rect);
|
|
255
|
+
}
|
|
256
|
+
return polygonIntersectsRect(world.corners, rect);
|
|
257
|
+
}
|
|
258
|
+
/** The selectable objects a marquee takes, each once, in paint order. */
|
|
259
|
+
export function hitTestRect(scene, rect, options) {
|
|
260
|
+
const index = options.index ?? indexScene(scene);
|
|
261
|
+
const found = [];
|
|
262
|
+
const seen = new Set();
|
|
263
|
+
for (const object of scene) {
|
|
264
|
+
if (!isBoxedObject(object))
|
|
265
|
+
continue;
|
|
266
|
+
if (options.ignore?.has(object.id))
|
|
267
|
+
continue;
|
|
268
|
+
const taken = options.mode === 'contain'
|
|
269
|
+
? rectContainsRect(rect, rotatedBoxBounds(object))
|
|
270
|
+
: shapeIntersectsRect(object, rect);
|
|
271
|
+
if (!taken)
|
|
272
|
+
continue;
|
|
273
|
+
const unitId = selectableIdFor(index, object.id, options.enteredGroupId);
|
|
274
|
+
if (!seen.has(unitId)) {
|
|
275
|
+
seen.add(unitId);
|
|
276
|
+
found.push(unitId);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
// A group is taken whole or not at all in `contain` mode: one member left out leaves the group out.
|
|
280
|
+
if (options.mode === 'contain') {
|
|
281
|
+
return found.filter((id) => {
|
|
282
|
+
const unit = index.get(id);
|
|
283
|
+
if (!unit || unit.type !== 'group')
|
|
284
|
+
return true;
|
|
285
|
+
for (const object of scene) {
|
|
286
|
+
if (isBoxedObject(object) && isWithin(index, object.id, id) && !rectContainsRect(rect, rotatedBoxBounds(object))) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return true;
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
return found;
|
|
294
|
+
}
|
|
295
|
+
/** The rectangle of a polyline's points, for callers that need to bound a flattened outline. */
|
|
296
|
+
export function outlineBounds(points) {
|
|
297
|
+
return boundsOfPoints(points);
|
|
298
|
+
}
|