@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,550 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SVG path data as a list of absolute commands, and what the canvas does with one: read any `d` string
|
|
3
|
+
* (relative commands, H, V, S, T and arcs included), write it back compactly, measure it exactly, move,
|
|
4
|
+
* scale and mirror it, and flatten it into polylines for hit testing.
|
|
5
|
+
*
|
|
6
|
+
* A command is `['M', x, y]`, `['L', x, y]`, `['Q', x1, y1, x, y]`, `['C', x1, y1, x2, y2, x, y]` or `['Z']`.
|
|
7
|
+
* Every other command of the SVG grammar is turned into one of these when it is read, so everything
|
|
8
|
+
* downstream handles five cases.
|
|
9
|
+
*/
|
|
10
|
+
import { applyMatrixToPoint } from './matrix.js';
|
|
11
|
+
const PARAMETER_COUNT = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 };
|
|
12
|
+
const NUMBER_PATTERN = /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/y;
|
|
13
|
+
const EPSILON = 1e-9;
|
|
14
|
+
/**
|
|
15
|
+
* Converts one SVG elliptical arc (endpoint form) into cubic Béziers, one for every quarter turn or less
|
|
16
|
+
* (SVG implementation notes, F.6.5 and F.6.6).
|
|
17
|
+
*/
|
|
18
|
+
function arcToCubicCommands(startX, startY, radiusXInput, radiusYInput, rotationDegrees, largeArc, sweep, endX, endY) {
|
|
19
|
+
let radiusX = Math.abs(radiusXInput);
|
|
20
|
+
let radiusY = Math.abs(radiusYInput);
|
|
21
|
+
if (radiusX < EPSILON || radiusY < EPSILON)
|
|
22
|
+
return [['L', endX, endY]];
|
|
23
|
+
if (Math.abs(startX - endX) < EPSILON && Math.abs(startY - endY) < EPSILON)
|
|
24
|
+
return [];
|
|
25
|
+
const rotation = (rotationDegrees * Math.PI) / 180;
|
|
26
|
+
const cosine = Math.cos(rotation);
|
|
27
|
+
const sine = Math.sin(rotation);
|
|
28
|
+
const halfDeltaX = (startX - endX) / 2;
|
|
29
|
+
const halfDeltaY = (startY - endY) / 2;
|
|
30
|
+
const rotatedX = cosine * halfDeltaX + sine * halfDeltaY;
|
|
31
|
+
const rotatedY = -sine * halfDeltaX + cosine * halfDeltaY;
|
|
32
|
+
// Radii too small to span the endpoints grow until they just do.
|
|
33
|
+
const growth = (rotatedX * rotatedX) / (radiusX * radiusX) + (rotatedY * rotatedY) / (radiusY * radiusY);
|
|
34
|
+
if (growth > 1) {
|
|
35
|
+
const factor = Math.sqrt(growth);
|
|
36
|
+
radiusX *= factor;
|
|
37
|
+
radiusY *= factor;
|
|
38
|
+
}
|
|
39
|
+
const radiusXSquared = radiusX * radiusX;
|
|
40
|
+
const radiusYSquared = radiusY * radiusY;
|
|
41
|
+
const numerator = radiusXSquared * radiusYSquared - radiusXSquared * rotatedY * rotatedY - radiusYSquared * rotatedX * rotatedX;
|
|
42
|
+
const denominator = radiusXSquared * rotatedY * rotatedY + radiusYSquared * rotatedX * rotatedX;
|
|
43
|
+
const sign = largeArc === sweep ? -1 : 1;
|
|
44
|
+
const coefficient = denominator < EPSILON ? 0 : sign * Math.sqrt(Math.max(0, numerator / denominator));
|
|
45
|
+
const centerRotatedX = coefficient * ((radiusX * rotatedY) / radiusY);
|
|
46
|
+
const centerRotatedY = coefficient * (-(radiusY * rotatedX) / radiusX);
|
|
47
|
+
const centerX = cosine * centerRotatedX - sine * centerRotatedY + (startX + endX) / 2;
|
|
48
|
+
const centerY = sine * centerRotatedX + cosine * centerRotatedY + (startY + endY) / 2;
|
|
49
|
+
const angleBetween = (ux, uy, vx, vy) => Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
50
|
+
const startAngle = angleBetween(1, 0, (rotatedX - centerRotatedX) / radiusX, (rotatedY - centerRotatedY) / radiusY);
|
|
51
|
+
let sweepAngle = angleBetween((rotatedX - centerRotatedX) / radiusX, (rotatedY - centerRotatedY) / radiusY, (-rotatedX - centerRotatedX) / radiusX, (-rotatedY - centerRotatedY) / radiusY);
|
|
52
|
+
if (!sweep && sweepAngle > 0)
|
|
53
|
+
sweepAngle -= 2 * Math.PI;
|
|
54
|
+
else if (sweep && sweepAngle < 0)
|
|
55
|
+
sweepAngle += 2 * Math.PI;
|
|
56
|
+
const segmentCount = Math.max(1, Math.ceil(Math.abs(sweepAngle) / (Math.PI / 2) - 1e-9));
|
|
57
|
+
const segmentAngle = sweepAngle / segmentCount;
|
|
58
|
+
const handleLength = (4 / 3) * Math.tan(segmentAngle / 4);
|
|
59
|
+
const toWorld = (unitX, unitY) => [
|
|
60
|
+
centerX + radiusX * unitX * cosine - radiusY * unitY * sine,
|
|
61
|
+
centerY + radiusX * unitX * sine + radiusY * unitY * cosine
|
|
62
|
+
];
|
|
63
|
+
const commands = [];
|
|
64
|
+
for (let index = 0; index < segmentCount; index += 1) {
|
|
65
|
+
const angleStart = startAngle + index * segmentAngle;
|
|
66
|
+
const angleEnd = angleStart + segmentAngle;
|
|
67
|
+
const cosStart = Math.cos(angleStart);
|
|
68
|
+
const sinStart = Math.sin(angleStart);
|
|
69
|
+
const cosEnd = Math.cos(angleEnd);
|
|
70
|
+
const sinEnd = Math.sin(angleEnd);
|
|
71
|
+
const [control1X, control1Y] = toWorld(cosStart - handleLength * sinStart, sinStart + handleLength * cosStart);
|
|
72
|
+
const [control2X, control2Y] = toWorld(cosEnd + handleLength * sinEnd, sinEnd - handleLength * cosEnd);
|
|
73
|
+
const isLast = index === segmentCount - 1;
|
|
74
|
+
const [pointX, pointY] = isLast ? [endX, endY] : toWorld(cosEnd, sinEnd);
|
|
75
|
+
commands.push(['C', control1X, control1Y, control2X, control2Y, pointX, pointY]);
|
|
76
|
+
}
|
|
77
|
+
return commands;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Reads SVG path data into absolute `M`, `L`, `Q`, `C` and `Z` commands.
|
|
81
|
+
* Anything it cannot read ends the path where it stands (the part before it is kept), like a browser does.
|
|
82
|
+
*/
|
|
83
|
+
export function parsePathData(data) {
|
|
84
|
+
const commands = [];
|
|
85
|
+
const length = data.length;
|
|
86
|
+
let position = 0;
|
|
87
|
+
let currentX = 0;
|
|
88
|
+
let currentY = 0;
|
|
89
|
+
let subpathStartX = 0;
|
|
90
|
+
let subpathStartY = 0;
|
|
91
|
+
// The control point of the last C/S and Q/T, which S and T reflect.
|
|
92
|
+
let lastCubicControl = null;
|
|
93
|
+
let lastQuadraticControl = null;
|
|
94
|
+
const skipSeparators = () => {
|
|
95
|
+
while (position < length) {
|
|
96
|
+
const character = data.charCodeAt(position);
|
|
97
|
+
// space, tab, newline, carriage return, form feed, comma
|
|
98
|
+
if (character === 32 || character === 9 || character === 10 || character === 13 || character === 12 || character === 44) {
|
|
99
|
+
position += 1;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const readNumber = () => {
|
|
107
|
+
skipSeparators();
|
|
108
|
+
NUMBER_PATTERN.lastIndex = position;
|
|
109
|
+
const match = NUMBER_PATTERN.exec(data);
|
|
110
|
+
if (!match)
|
|
111
|
+
return null;
|
|
112
|
+
position = NUMBER_PATTERN.lastIndex;
|
|
113
|
+
const value = Number(match[0]);
|
|
114
|
+
return Number.isFinite(value) ? value : null;
|
|
115
|
+
};
|
|
116
|
+
// Arc flags are one character, 0 or 1, and may touch the number after them ("a1 1 0 00.5.5").
|
|
117
|
+
const readFlag = () => {
|
|
118
|
+
skipSeparators();
|
|
119
|
+
const character = data[position];
|
|
120
|
+
if (character !== '0' && character !== '1')
|
|
121
|
+
return null;
|
|
122
|
+
position += 1;
|
|
123
|
+
return character === '1' ? 1 : 0;
|
|
124
|
+
};
|
|
125
|
+
let commandLetter = '';
|
|
126
|
+
while (true) {
|
|
127
|
+
skipSeparators();
|
|
128
|
+
if (position >= length)
|
|
129
|
+
break;
|
|
130
|
+
const character = data[position];
|
|
131
|
+
if (/[A-Za-z]/.test(character)) {
|
|
132
|
+
commandLetter = character;
|
|
133
|
+
position += 1;
|
|
134
|
+
}
|
|
135
|
+
else if (commandLetter === '') {
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
const upper = commandLetter.toUpperCase();
|
|
139
|
+
const relative = commandLetter !== upper;
|
|
140
|
+
const parameterCount = PARAMETER_COUNT[upper];
|
|
141
|
+
if (parameterCount === undefined)
|
|
142
|
+
break;
|
|
143
|
+
if (commands.length === 0 && upper !== 'M')
|
|
144
|
+
break;
|
|
145
|
+
if (upper === 'Z') {
|
|
146
|
+
commands.push(['Z']);
|
|
147
|
+
currentX = subpathStartX;
|
|
148
|
+
currentY = subpathStartY;
|
|
149
|
+
lastCubicControl = null;
|
|
150
|
+
lastQuadraticControl = null;
|
|
151
|
+
// A number after Z is not an implicit repeat: only a letter may follow.
|
|
152
|
+
commandLetter = '';
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const parameters = [];
|
|
156
|
+
let complete = true;
|
|
157
|
+
for (let index = 0; index < parameterCount; index += 1) {
|
|
158
|
+
const isFlag = upper === 'A' && (index === 3 || index === 4);
|
|
159
|
+
const value = isFlag ? readFlag() : readNumber();
|
|
160
|
+
if (value === null) {
|
|
161
|
+
complete = false;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
parameters.push(value);
|
|
165
|
+
}
|
|
166
|
+
if (!complete)
|
|
167
|
+
break;
|
|
168
|
+
const offsetX = relative ? currentX : 0;
|
|
169
|
+
const offsetY = relative ? currentY : 0;
|
|
170
|
+
const previousCubicControl = lastCubicControl;
|
|
171
|
+
const previousQuadraticControl = lastQuadraticControl;
|
|
172
|
+
lastCubicControl = null;
|
|
173
|
+
lastQuadraticControl = null;
|
|
174
|
+
switch (upper) {
|
|
175
|
+
case 'M': {
|
|
176
|
+
const x = parameters[0] + offsetX;
|
|
177
|
+
const y = parameters[1] + offsetY;
|
|
178
|
+
commands.push(['M', x, y]);
|
|
179
|
+
currentX = x;
|
|
180
|
+
currentY = y;
|
|
181
|
+
subpathStartX = x;
|
|
182
|
+
subpathStartY = y;
|
|
183
|
+
// Further coordinate pairs after a moveto are linetos.
|
|
184
|
+
commandLetter = relative ? 'l' : 'L';
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
case 'L': {
|
|
188
|
+
const x = parameters[0] + offsetX;
|
|
189
|
+
const y = parameters[1] + offsetY;
|
|
190
|
+
commands.push(['L', x, y]);
|
|
191
|
+
currentX = x;
|
|
192
|
+
currentY = y;
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case 'H': {
|
|
196
|
+
const x = parameters[0] + offsetX;
|
|
197
|
+
commands.push(['L', x, currentY]);
|
|
198
|
+
currentX = x;
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case 'V': {
|
|
202
|
+
const y = parameters[0] + offsetY;
|
|
203
|
+
commands.push(['L', currentX, y]);
|
|
204
|
+
currentY = y;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
case 'C': {
|
|
208
|
+
const control1X = parameters[0] + offsetX;
|
|
209
|
+
const control1Y = parameters[1] + offsetY;
|
|
210
|
+
const control2X = parameters[2] + offsetX;
|
|
211
|
+
const control2Y = parameters[3] + offsetY;
|
|
212
|
+
const x = parameters[4] + offsetX;
|
|
213
|
+
const y = parameters[5] + offsetY;
|
|
214
|
+
commands.push(['C', control1X, control1Y, control2X, control2Y, x, y]);
|
|
215
|
+
lastCubicControl = { x: control2X, y: control2Y };
|
|
216
|
+
currentX = x;
|
|
217
|
+
currentY = y;
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case 'S': {
|
|
221
|
+
const control1X = previousCubicControl ? 2 * currentX - previousCubicControl.x : currentX;
|
|
222
|
+
const control1Y = previousCubicControl ? 2 * currentY - previousCubicControl.y : currentY;
|
|
223
|
+
const control2X = parameters[0] + offsetX;
|
|
224
|
+
const control2Y = parameters[1] + offsetY;
|
|
225
|
+
const x = parameters[2] + offsetX;
|
|
226
|
+
const y = parameters[3] + offsetY;
|
|
227
|
+
commands.push(['C', control1X, control1Y, control2X, control2Y, x, y]);
|
|
228
|
+
lastCubicControl = { x: control2X, y: control2Y };
|
|
229
|
+
currentX = x;
|
|
230
|
+
currentY = y;
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case 'Q': {
|
|
234
|
+
const controlX = parameters[0] + offsetX;
|
|
235
|
+
const controlY = parameters[1] + offsetY;
|
|
236
|
+
const x = parameters[2] + offsetX;
|
|
237
|
+
const y = parameters[3] + offsetY;
|
|
238
|
+
commands.push(['Q', controlX, controlY, x, y]);
|
|
239
|
+
lastQuadraticControl = { x: controlX, y: controlY };
|
|
240
|
+
currentX = x;
|
|
241
|
+
currentY = y;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
case 'T': {
|
|
245
|
+
const controlX = previousQuadraticControl ? 2 * currentX - previousQuadraticControl.x : currentX;
|
|
246
|
+
const controlY = previousQuadraticControl ? 2 * currentY - previousQuadraticControl.y : currentY;
|
|
247
|
+
const x = parameters[0] + offsetX;
|
|
248
|
+
const y = parameters[1] + offsetY;
|
|
249
|
+
commands.push(['Q', controlX, controlY, x, y]);
|
|
250
|
+
lastQuadraticControl = { x: controlX, y: controlY };
|
|
251
|
+
currentX = x;
|
|
252
|
+
currentY = y;
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
case 'A': {
|
|
256
|
+
const x = parameters[5] + offsetX;
|
|
257
|
+
const y = parameters[6] + offsetY;
|
|
258
|
+
commands.push(...arcToCubicCommands(currentX, currentY, parameters[0], parameters[1], parameters[2], parameters[3] === 1, parameters[4] === 1, x, y));
|
|
259
|
+
currentX = x;
|
|
260
|
+
currentY = y;
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return commands;
|
|
266
|
+
}
|
|
267
|
+
function formatNumber(value, decimals) {
|
|
268
|
+
const rounded = Number(value.toFixed(decimals));
|
|
269
|
+
// Negative zero would print as "-0".
|
|
270
|
+
return String(Object.is(rounded, -0) ? 0 : rounded);
|
|
271
|
+
}
|
|
272
|
+
/** Writes commands as compact path data: `M0 0L10 10Z`. */
|
|
273
|
+
export function serializePathData(commands, decimals = 2) {
|
|
274
|
+
let output = '';
|
|
275
|
+
for (const command of commands) {
|
|
276
|
+
output += command[0];
|
|
277
|
+
for (let index = 1; index < command.length; index += 1) {
|
|
278
|
+
if (index > 1)
|
|
279
|
+
output += ' ';
|
|
280
|
+
output += formatNumber(command[index], decimals);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return output;
|
|
284
|
+
}
|
|
285
|
+
/** Applies a function to every point of every command. */
|
|
286
|
+
function mapPoints(commands, mapPoint) {
|
|
287
|
+
return commands.map((command) => {
|
|
288
|
+
switch (command[0]) {
|
|
289
|
+
case 'M':
|
|
290
|
+
case 'L': {
|
|
291
|
+
const point = mapPoint({ x: command[1], y: command[2] });
|
|
292
|
+
return [command[0], point.x, point.y];
|
|
293
|
+
}
|
|
294
|
+
case 'Q': {
|
|
295
|
+
const control = mapPoint({ x: command[1], y: command[2] });
|
|
296
|
+
const end = mapPoint({ x: command[3], y: command[4] });
|
|
297
|
+
return ['Q', control.x, control.y, end.x, end.y];
|
|
298
|
+
}
|
|
299
|
+
case 'C': {
|
|
300
|
+
const control1 = mapPoint({ x: command[1], y: command[2] });
|
|
301
|
+
const control2 = mapPoint({ x: command[3], y: command[4] });
|
|
302
|
+
const end = mapPoint({ x: command[5], y: command[6] });
|
|
303
|
+
return ['C', control1.x, control1.y, control2.x, control2.y, end.x, end.y];
|
|
304
|
+
}
|
|
305
|
+
default:
|
|
306
|
+
return ['Z'];
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
export function transformPathCommands(commands, matrix) {
|
|
311
|
+
return mapPoints(commands, (point) => applyMatrixToPoint(matrix, point));
|
|
312
|
+
}
|
|
313
|
+
export function translatePathCommands(commands, deltaX, deltaY) {
|
|
314
|
+
return mapPoints(commands, (point) => ({ x: point.x + deltaX, y: point.y + deltaY }));
|
|
315
|
+
}
|
|
316
|
+
export function scalePathCommands(commands, scaleX, scaleY) {
|
|
317
|
+
return mapPoints(commands, (point) => ({ x: point.x * scaleX, y: point.y * scaleY }));
|
|
318
|
+
}
|
|
319
|
+
/** Mirrors the path inside a box of the given size, about the box's vertical and/or horizontal middle line. */
|
|
320
|
+
export function mirrorPathCommands(commands, width, height, flipX, flipY) {
|
|
321
|
+
if (!flipX && !flipY)
|
|
322
|
+
return [...commands];
|
|
323
|
+
return mapPoints(commands, (point) => ({
|
|
324
|
+
x: flipX ? width - point.x : point.x,
|
|
325
|
+
y: flipY ? height - point.y : point.y
|
|
326
|
+
}));
|
|
327
|
+
}
|
|
328
|
+
/** The values of a cubic at its extrema along one axis, for the exact bounds of a curve. */
|
|
329
|
+
function cubicExtrema(p0, p1, p2, p3) {
|
|
330
|
+
const a = -p0 + 3 * p1 - 3 * p2 + p3;
|
|
331
|
+
const b = 2 * (p0 - 2 * p1 + p2);
|
|
332
|
+
const c = p1 - p0;
|
|
333
|
+
const roots = [];
|
|
334
|
+
if (Math.abs(a) < EPSILON) {
|
|
335
|
+
if (Math.abs(b) > EPSILON)
|
|
336
|
+
roots.push(-c / b);
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
const discriminant = b * b - 4 * a * c;
|
|
340
|
+
if (discriminant >= 0) {
|
|
341
|
+
const root = Math.sqrt(discriminant);
|
|
342
|
+
roots.push((-b + root) / (2 * a), (-b - root) / (2 * a));
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const values = [];
|
|
346
|
+
for (const t of roots) {
|
|
347
|
+
if (t > 0 && t < 1) {
|
|
348
|
+
const inverse = 1 - t;
|
|
349
|
+
values.push(inverse ** 3 * p0 + 3 * inverse * inverse * t * p1 + 3 * inverse * t * t * p2 + t ** 3 * p3);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return values;
|
|
353
|
+
}
|
|
354
|
+
function quadraticExtrema(p0, p1, p2) {
|
|
355
|
+
const denominator = p0 - 2 * p1 + p2;
|
|
356
|
+
if (Math.abs(denominator) < EPSILON)
|
|
357
|
+
return [];
|
|
358
|
+
const t = (p0 - p1) / denominator;
|
|
359
|
+
if (t <= 0 || t >= 1)
|
|
360
|
+
return [];
|
|
361
|
+
const inverse = 1 - t;
|
|
362
|
+
return [inverse * inverse * p0 + 2 * inverse * t * p1 + t * t * p2];
|
|
363
|
+
}
|
|
364
|
+
/** The exact bounding box of a path (curves measured at their extrema, not by their control points). Null when empty. */
|
|
365
|
+
export function pathBounds(commands) {
|
|
366
|
+
let left = Infinity;
|
|
367
|
+
let top = Infinity;
|
|
368
|
+
let right = -Infinity;
|
|
369
|
+
let bottom = -Infinity;
|
|
370
|
+
const include = (x, y) => {
|
|
371
|
+
if (x < left)
|
|
372
|
+
left = x;
|
|
373
|
+
if (x > right)
|
|
374
|
+
right = x;
|
|
375
|
+
if (y < top)
|
|
376
|
+
top = y;
|
|
377
|
+
if (y > bottom)
|
|
378
|
+
bottom = y;
|
|
379
|
+
};
|
|
380
|
+
let currentX = 0;
|
|
381
|
+
let currentY = 0;
|
|
382
|
+
let startX = 0;
|
|
383
|
+
let startY = 0;
|
|
384
|
+
for (const command of commands) {
|
|
385
|
+
switch (command[0]) {
|
|
386
|
+
case 'M':
|
|
387
|
+
currentX = startX = command[1];
|
|
388
|
+
currentY = startY = command[2];
|
|
389
|
+
include(currentX, currentY);
|
|
390
|
+
break;
|
|
391
|
+
case 'L':
|
|
392
|
+
include(command[1], command[2]);
|
|
393
|
+
currentX = command[1];
|
|
394
|
+
currentY = command[2];
|
|
395
|
+
break;
|
|
396
|
+
case 'Q': {
|
|
397
|
+
include(command[3], command[4]);
|
|
398
|
+
for (const x of quadraticExtrema(currentX, command[1], command[3]))
|
|
399
|
+
include(x, currentY);
|
|
400
|
+
for (const y of quadraticExtrema(currentY, command[2], command[4]))
|
|
401
|
+
include(currentX, y);
|
|
402
|
+
currentX = command[3];
|
|
403
|
+
currentY = command[4];
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
case 'C': {
|
|
407
|
+
include(command[5], command[6]);
|
|
408
|
+
for (const x of cubicExtrema(currentX, command[1], command[3], command[5]))
|
|
409
|
+
include(x, currentY);
|
|
410
|
+
for (const y of cubicExtrema(currentY, command[2], command[4], command[6]))
|
|
411
|
+
include(currentX, y);
|
|
412
|
+
currentX = command[5];
|
|
413
|
+
currentY = command[6];
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
case 'Z':
|
|
417
|
+
currentX = startX;
|
|
418
|
+
currentY = startY;
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (left === Infinity)
|
|
423
|
+
return null;
|
|
424
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Cuts a path into polylines. Curves are cut every `segmentLength` units of their control polygon
|
|
428
|
+
* (at least 2 and at most 48 pieces), which is plenty for hit tests and the eraser.
|
|
429
|
+
*/
|
|
430
|
+
export function flattenPath(commands, segmentLength = 6) {
|
|
431
|
+
const subpaths = [];
|
|
432
|
+
let current = null;
|
|
433
|
+
let currentX = 0;
|
|
434
|
+
let currentY = 0;
|
|
435
|
+
let startX = 0;
|
|
436
|
+
let startY = 0;
|
|
437
|
+
const pieceCount = (controlLength) => Math.max(2, Math.min(48, Math.ceil(controlLength / Math.max(1, segmentLength))));
|
|
438
|
+
for (const command of commands) {
|
|
439
|
+
switch (command[0]) {
|
|
440
|
+
case 'M':
|
|
441
|
+
current = { points: [{ x: command[1], y: command[2] }], closed: false };
|
|
442
|
+
subpaths.push(current);
|
|
443
|
+
currentX = startX = command[1];
|
|
444
|
+
currentY = startY = command[2];
|
|
445
|
+
break;
|
|
446
|
+
case 'L':
|
|
447
|
+
if (!current) {
|
|
448
|
+
current = { points: [{ x: currentX, y: currentY }], closed: false };
|
|
449
|
+
subpaths.push(current);
|
|
450
|
+
}
|
|
451
|
+
current.points.push({ x: command[1], y: command[2] });
|
|
452
|
+
currentX = command[1];
|
|
453
|
+
currentY = command[2];
|
|
454
|
+
break;
|
|
455
|
+
case 'Q': {
|
|
456
|
+
if (!current) {
|
|
457
|
+
current = { points: [{ x: currentX, y: currentY }], closed: false };
|
|
458
|
+
subpaths.push(current);
|
|
459
|
+
}
|
|
460
|
+
const controlLength = Math.hypot(command[1] - currentX, command[2] - currentY) +
|
|
461
|
+
Math.hypot(command[3] - command[1], command[4] - command[2]);
|
|
462
|
+
const pieces = pieceCount(controlLength);
|
|
463
|
+
for (let step = 1; step <= pieces; step += 1) {
|
|
464
|
+
const t = step / pieces;
|
|
465
|
+
const inverse = 1 - t;
|
|
466
|
+
current.points.push({
|
|
467
|
+
x: inverse * inverse * currentX + 2 * inverse * t * command[1] + t * t * command[3],
|
|
468
|
+
y: inverse * inverse * currentY + 2 * inverse * t * command[2] + t * t * command[4]
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
currentX = command[3];
|
|
472
|
+
currentY = command[4];
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
case 'C': {
|
|
476
|
+
if (!current) {
|
|
477
|
+
current = { points: [{ x: currentX, y: currentY }], closed: false };
|
|
478
|
+
subpaths.push(current);
|
|
479
|
+
}
|
|
480
|
+
const controlLength = Math.hypot(command[1] - currentX, command[2] - currentY) +
|
|
481
|
+
Math.hypot(command[3] - command[1], command[4] - command[2]) +
|
|
482
|
+
Math.hypot(command[5] - command[3], command[6] - command[4]);
|
|
483
|
+
const pieces = pieceCount(controlLength);
|
|
484
|
+
for (let step = 1; step <= pieces; step += 1) {
|
|
485
|
+
const t = step / pieces;
|
|
486
|
+
const inverse = 1 - t;
|
|
487
|
+
const weight0 = inverse ** 3;
|
|
488
|
+
const weight1 = 3 * inverse * inverse * t;
|
|
489
|
+
const weight2 = 3 * inverse * t * t;
|
|
490
|
+
const weight3 = t ** 3;
|
|
491
|
+
current.points.push({
|
|
492
|
+
x: weight0 * currentX + weight1 * command[1] + weight2 * command[3] + weight3 * command[5],
|
|
493
|
+
y: weight0 * currentY + weight1 * command[2] + weight2 * command[4] + weight3 * command[6]
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
currentX = command[5];
|
|
497
|
+
currentY = command[6];
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
case 'Z':
|
|
501
|
+
if (current)
|
|
502
|
+
current.closed = true;
|
|
503
|
+
currentX = startX;
|
|
504
|
+
currentY = startY;
|
|
505
|
+
// What follows a close starts a new piece from the same start point.
|
|
506
|
+
current = null;
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return subpaths;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Moves and scales a path so that its tight bounding box becomes exactly 0..width by 0..height.
|
|
514
|
+
* A flat axis (a straight horizontal or vertical line) is centred in its axis instead of scaled.
|
|
515
|
+
*/
|
|
516
|
+
export function fitPathToBox(commands, width, height) {
|
|
517
|
+
const bounds = pathBounds(commands);
|
|
518
|
+
if (!bounds)
|
|
519
|
+
return [...commands];
|
|
520
|
+
const scaleX = bounds.width > EPSILON ? width / bounds.width : 1;
|
|
521
|
+
const scaleY = bounds.height > EPSILON ? height / bounds.height : 1;
|
|
522
|
+
const offsetX = bounds.width > EPSILON ? 0 : width / 2 - bounds.x;
|
|
523
|
+
const offsetY = bounds.height > EPSILON ? 0 : height / 2 - bounds.y;
|
|
524
|
+
return mapPoints(commands, (point) => ({
|
|
525
|
+
x: bounds.width > EPSILON ? (point.x - bounds.x) * scaleX : point.x + offsetX,
|
|
526
|
+
y: bounds.height > EPSILON ? (point.y - bounds.y) * scaleY : point.y + offsetY
|
|
527
|
+
}));
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Moves a path so that its bounding box starts at 0, 0 and reports where the box was.
|
|
531
|
+
* This is how a drawn stroke becomes an object: the path is local, the box says where it stands.
|
|
532
|
+
*/
|
|
533
|
+
export function localizePath(commands) {
|
|
534
|
+
const bounds = pathBounds(commands);
|
|
535
|
+
if (!bounds)
|
|
536
|
+
return null;
|
|
537
|
+
return { commands: translatePathCommands(commands, -bounds.x, -bounds.y), bounds };
|
|
538
|
+
}
|
|
539
|
+
/** A polyline (or polygon) as path commands. */
|
|
540
|
+
export function polylineToPathCommands(points, closed) {
|
|
541
|
+
if (points.length === 0)
|
|
542
|
+
return [];
|
|
543
|
+
const commands = [['M', points[0].x, points[0].y]];
|
|
544
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
545
|
+
commands.push(['L', points[index].x, points[index].y]);
|
|
546
|
+
}
|
|
547
|
+
if (closed)
|
|
548
|
+
commands.push(['Z']);
|
|
549
|
+
return commands;
|
|
550
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Easing for standing a shape upright again (double-click on the rotation dot).
|
|
3
|
+
* Uses the shortest turn, and one history commit when the animation ends.
|
|
4
|
+
*/
|
|
5
|
+
/** How long the upright animation runs, in milliseconds. */
|
|
6
|
+
export declare const ROTATION_RESET_DURATION_MS = 220;
|
|
7
|
+
/** Ease-out progress in [0, 1] for `elapsed` ms since the animation started. */
|
|
8
|
+
export declare function rotationResetProgress(elapsed: number, duration?: number): number;
|
|
9
|
+
/** Rotation in degrees after `progress` of the upright animation from `startRotation`. */
|
|
10
|
+
export declare function rotationAfterResetProgress(startRotation: number, progress: number): number;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Easing for standing a shape upright again (double-click on the rotation dot).
|
|
3
|
+
* Uses the shortest turn, and one history commit when the animation ends.
|
|
4
|
+
*/
|
|
5
|
+
import { normalizeDegrees, normalizeSignedDegrees } from './matrix.js';
|
|
6
|
+
/** How long the upright animation runs, in milliseconds. */
|
|
7
|
+
export const ROTATION_RESET_DURATION_MS = 220;
|
|
8
|
+
/** Ease-out progress in [0, 1] for `elapsed` ms since the animation started. */
|
|
9
|
+
export function rotationResetProgress(elapsed, duration = ROTATION_RESET_DURATION_MS) {
|
|
10
|
+
const t = Math.min(1, Math.max(0, elapsed / duration));
|
|
11
|
+
return 1 - (1 - t) ** 3;
|
|
12
|
+
}
|
|
13
|
+
/** Rotation in degrees after `progress` of the upright animation from `startRotation`. */
|
|
14
|
+
export function rotationAfterResetProgress(startRotation, progress) {
|
|
15
|
+
const delta = -normalizeSignedDegrees(startRotation);
|
|
16
|
+
return normalizeDegrees(startRotation + delta * progress);
|
|
17
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operations on a scene: the z-ordered list of records, with groups as parents of their members.
|
|
3
|
+
*
|
|
4
|
+
* Every function is pure. A function that changes the scene returns a new array and keeps the identity of
|
|
5
|
+
* every record it did not touch (and returns the very same array when nothing changed), so a store can
|
|
6
|
+
* compare by reference and a renderer can skip what did not move.
|
|
7
|
+
*
|
|
8
|
+
* Structure (reorder, group, ungroup, remove) is done on a tree built from the list and flattened back,
|
|
9
|
+
* which keeps one rule true everywhere: a group comes first, directly followed by its members, and the
|
|
10
|
+
* paint order of the board is the order of the list.
|
|
11
|
+
*/
|
|
12
|
+
import type { ObjectId, ObjectPatch, Rect, SceneObject, ShapeObject } from './types.js';
|
|
13
|
+
export type Scene = readonly SceneObject[];
|
|
14
|
+
/** One object with the objects that belong to it (members of a group). */
|
|
15
|
+
export interface SceneNode {
|
|
16
|
+
object: SceneObject;
|
|
17
|
+
children: SceneNode[];
|
|
18
|
+
}
|
|
19
|
+
export type ReorderMode = 'front' | 'forward' | 'backward' | 'back';
|
|
20
|
+
export declare function indexScene(scene: Scene): Map<ObjectId, SceneObject>;
|
|
21
|
+
export declare function findObject(scene: Scene, id: ObjectId): SceneObject | undefined;
|
|
22
|
+
/** The direct members of a group, in paint order. */
|
|
23
|
+
export declare function childrenOf(scene: Scene, groupId: ObjectId): SceneObject[];
|
|
24
|
+
/** Every object below a group, at any depth, in list order. */
|
|
25
|
+
export declare function descendantsOf(scene: Scene, groupId: ObjectId): SceneObject[];
|
|
26
|
+
/** The ids given, plus every id below a group among them. */
|
|
27
|
+
export declare function withDescendantIds(scene: Scene, ids: Iterable<ObjectId>): Set<ObjectId>;
|
|
28
|
+
/** The shapes drawn by the given objects: a shape is itself, a group is its members at any depth. Unique, in paint order. */
|
|
29
|
+
export declare function leafShapesOf(scene: Scene, ids: Iterable<ObjectId>): ShapeObject[];
|
|
30
|
+
/** The group nearest to the board that contains the object, or the object itself when it is not in a group. */
|
|
31
|
+
export declare function topLevelAncestorId(index: ReadonlyMap<ObjectId, SceneObject>, id: ObjectId): ObjectId;
|
|
32
|
+
/** True when `id` is `ancestorId` or lies below it. */
|
|
33
|
+
export declare function isWithin(index: ReadonlyMap<ObjectId, SceneObject>, id: ObjectId, ancestorId: ObjectId): boolean;
|
|
34
|
+
/** Whether two lists hold the same ids in the same order (the very same list is the quick case). */
|
|
35
|
+
export declare function sameIds(first: readonly ObjectId[], second: readonly ObjectId[]): boolean;
|
|
36
|
+
/** Drops ids that are not on the board, repeats, and members whose group is selected too. */
|
|
37
|
+
export declare function normalizeSelection(scene: Scene, ids: Iterable<ObjectId>): ObjectId[];
|
|
38
|
+
/** The rectangle around a shape on the board (turned boxes measured by their corners), without its stroke. */
|
|
39
|
+
export declare function shapeBounds(shape: ShapeObject): Rect;
|
|
40
|
+
/** The same, grown by what the stroke adds outside the outline: what a thumbnail or a screenshot must include. */
|
|
41
|
+
export declare function shapeVisualBounds(shape: ShapeObject): Rect;
|
|
42
|
+
/** The rectangle around the given objects, groups included (a group is as big as its members together). */
|
|
43
|
+
export declare function boundsOfObjects(scene: Scene, ids: Iterable<ObjectId>): Rect | null;
|
|
44
|
+
/** The rectangle around every shape on the board, strokes included. Null for an empty board. */
|
|
45
|
+
export declare function boardVisualBounds(scene: Scene): Rect | null;
|
|
46
|
+
export declare function buildSceneTree(scene: Scene): SceneNode[];
|
|
47
|
+
/** Writes a tree back as a list, setting every parent id to the node it hangs from. */
|
|
48
|
+
export declare function flattenSceneTree(roots: readonly SceneNode[]): SceneObject[];
|
|
49
|
+
/** Repairs a list of records into a valid scene: unique ids, groups before their members, no empty groups. */
|
|
50
|
+
export declare function normalizeScene(scene: Scene): SceneObject[];
|
|
51
|
+
export declare function addObjects(scene: Scene, objects: readonly SceneObject[]): SceneObject[];
|
|
52
|
+
/** Removes the objects and everything below them, then any group left with no members. */
|
|
53
|
+
export declare function removeObjects(scene: Scene, ids: Iterable<ObjectId>): Scene;
|
|
54
|
+
/** Changes one object. Returns the same scene when the object is missing or the patch changes nothing. */
|
|
55
|
+
export declare function patchObject(scene: Scene, id: ObjectId, patch: ObjectPatch): Scene;
|
|
56
|
+
/** Changes several objects at once, each with its own patch. */
|
|
57
|
+
export declare function patchObjects(scene: Scene, patches: ReadonlyMap<ObjectId, ObjectPatch>): Scene;
|
|
58
|
+
/** Replaces one record by another (its id stays the one of the record it replaces). */
|
|
59
|
+
export declare function replaceObject(scene: Scene, id: ObjectId, replacement: SceneObject): Scene;
|
|
60
|
+
/** The ids of the shapes drawn by the given objects (a group is its members at any depth): who moves when they are carried. */
|
|
61
|
+
export declare function movingShapeIds(scene: Scene, ids: Iterable<ObjectId>): Set<ObjectId>;
|
|
62
|
+
/**
|
|
63
|
+
* Moves the shapes with the given ids by a distance. A drag carries the same shapes at every step: it asks `movingShapeIds`
|
|
64
|
+
* once and calls this at every move, so no step walks the groups of the board again to find out who is carried.
|
|
65
|
+
*/
|
|
66
|
+
export declare function translateShapes(scene: Scene, moving: ReadonlySet<ObjectId>, deltaX: number, deltaY: number): Scene;
|
|
67
|
+
/** Moves the shapes drawn by the given objects by a distance. */
|
|
68
|
+
export declare function translateObjects(scene: Scene, ids: Iterable<ObjectId>, deltaX: number, deltaY: number): Scene;
|
|
69
|
+
/**
|
|
70
|
+
* Changes the paint order of the objects, each among its own siblings (a member of a group moves inside the
|
|
71
|
+
* group, never out of it).
|
|
72
|
+
*/
|
|
73
|
+
export declare function reorderObjects(scene: Scene, ids: Iterable<ObjectId>, mode: ReorderMode): Scene;
|
|
74
|
+
/**
|
|
75
|
+
* Puts the objects in a new group, at the paint position of the highest one.
|
|
76
|
+
* Objects inside a group are grouped inside it when they all share it; otherwise the groups they sit in
|
|
77
|
+
* are grouped whole (a selection never tears a group apart).
|
|
78
|
+
* Returns null when there is nothing to group (fewer than two units).
|
|
79
|
+
*/
|
|
80
|
+
export declare function groupObjects(scene: Scene, ids: Iterable<ObjectId>, groupId?: ObjectId): {
|
|
81
|
+
scene: Scene;
|
|
82
|
+
groupId: ObjectId;
|
|
83
|
+
} | null;
|
|
84
|
+
/** Dissolves groups: their members take the group's place in the list and in its parent. */
|
|
85
|
+
export declare function ungroupObjects(scene: Scene, ids: Iterable<ObjectId>): {
|
|
86
|
+
scene: Scene;
|
|
87
|
+
releasedIds: ObjectId[];
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Copies objects (a group is copied with its members) under new ids, shifted by an offset.
|
|
91
|
+
* The copies are returned in paint order, ready to be added on top of the board.
|
|
92
|
+
*/
|
|
93
|
+
export declare function duplicateObjects(scene: Scene, ids: Iterable<ObjectId>, offset?: {
|
|
94
|
+
x: number;
|
|
95
|
+
y: number;
|
|
96
|
+
}, makeId?: (type: SceneObject['type']) => ObjectId): {
|
|
97
|
+
objects: SceneObject[];
|
|
98
|
+
idMap: Map<ObjectId, ObjectId>;
|
|
99
|
+
};
|