@widgetic/canvas 0.5.41 → 0.5.43
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 +1 -0
- package/dist/canvas/DomCanvas.svelte +49 -20
- package/dist/canvas/DomCanvas.svelte.d.ts +13 -7
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.d.ts +14 -3
- package/dist/canvas/chromeLayout.js +30 -20
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +47 -5
- package/dist/canvas/host/CanvasHost.js +1481 -1566
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.d.ts +33 -0
- package/dist/canvas/host/nearCards.js +78 -0
- package/dist/canvas/host/pageEvents.d.ts +16 -4
- package/dist/canvas/host/pageEvents.js +17 -8
- package/dist/canvas/host/pictureSaves.d.ts +33 -0
- package/dist/canvas/host/pictureSaves.js +42 -0
- package/dist/canvas/host/screenRects.d.ts +5 -0
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.d.ts +88 -0
- package/dist/canvas/host/storedPictures.js +288 -0
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +1 -1
- package/dist/canvas/index.js +1 -1
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.d.ts +45 -2
- package/dist/canvas/shapes/shapeImageStore.js +10 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.d.ts +2 -2
- package/dist/canvas/toolbarPosition.js +4 -4
- package/dist/canvas/ui/SelectionPanel.svelte +28 -5
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
- package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
- package/dist/canvas/ui/ToolButton.svelte +8 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +2 -2
- package/dist/canvas/ui/floatingCard.js +20 -20
- package/dist/canvas/ui/floatingLayer.d.ts +13 -2
- package/dist/canvas/ui/floatingLayer.js +18 -7
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/CanvasStage.svelte +101 -67
- package/dist/canvas/view/LeftToRightScope.svelte +21 -0
- package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
- package/dist/canvas/view/ObjectView.svelte +15 -10
- package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/canvas/view/liftedScene.d.ts +31 -0
- package/dist/canvas/view/liftedScene.js +146 -0
- package/dist/canvas/view/mountedScene.d.ts +71 -0
- package/dist/canvas/view/mountedScene.js +363 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +15 -1
- package/dist/engine/EngineCore.js +724 -797
- package/dist/engine/codec/canvasDocument.d.ts +5 -0
- package/dist/engine/codec/canvasDocument.js +212 -149
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.d.ts +6 -0
- package/dist/engine/core/history.js +49 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.d.ts +10 -0
- package/dist/engine/core/scene.js +153 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.d.ts +8 -2
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +265 -309
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.d.ts +20 -0
- package/dist/engine/state.js +5 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.d.ts +10 -1
- package/dist/fonts/interFont.js +47 -75
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +8 -1
- package/dist/i18n/messages.js +10 -2
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
- package/dist/canvas/ui/SelectorBar.svelte +0 -84
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
|
@@ -7,82 +7,71 @@
|
|
|
7
7
|
* Every other command of the SVG grammar is turned into one of these when it is read, so everything
|
|
8
8
|
* downstream handles five cases.
|
|
9
9
|
*/
|
|
10
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
11
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
12
|
-
if (ar || !(i in from)) {
|
|
13
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
14
|
-
ar[i] = from[i];
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
18
|
-
};
|
|
19
10
|
import { applyMatrixToPoint } from './matrix.js';
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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;
|
|
23
14
|
/**
|
|
24
15
|
* Converts one SVG elliptical arc (endpoint form) into cubic Béziers, one for every quarter turn or less
|
|
25
16
|
* (SVG implementation notes, F.6.5 and F.6.6).
|
|
26
17
|
*/
|
|
27
18
|
function arcToCubicCommands(startX, startY, radiusXInput, radiusYInput, rotationDegrees, largeArc, sweep, endX, endY) {
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
let radiusX = Math.abs(radiusXInput);
|
|
20
|
+
let radiusY = Math.abs(radiusYInput);
|
|
30
21
|
if (radiusX < EPSILON || radiusY < EPSILON)
|
|
31
22
|
return [['L', endX, endY]];
|
|
32
23
|
if (Math.abs(startX - endX) < EPSILON && Math.abs(startY - endY) < EPSILON)
|
|
33
24
|
return [];
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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;
|
|
41
32
|
// Radii too small to span the endpoints grow until they just do.
|
|
42
|
-
|
|
33
|
+
const growth = (rotatedX * rotatedX) / (radiusX * radiusX) + (rotatedY * rotatedY) / (radiusY * radiusY);
|
|
43
34
|
if (growth > 1) {
|
|
44
|
-
|
|
35
|
+
const factor = Math.sqrt(growth);
|
|
45
36
|
radiusX *= factor;
|
|
46
37
|
radiusY *= factor;
|
|
47
38
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
var startAngle = angleBetween(1, 0, (rotatedX - centerRotatedX) / radiusX, (rotatedY - centerRotatedY) / radiusY);
|
|
62
|
-
var sweepAngle = angleBetween((rotatedX - centerRotatedX) / radiusX, (rotatedY - centerRotatedY) / radiusY, (-rotatedX - centerRotatedX) / radiusX, (-rotatedY - centerRotatedY) / radiusY);
|
|
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);
|
|
63
52
|
if (!sweep && sweepAngle > 0)
|
|
64
53
|
sweepAngle -= 2 * Math.PI;
|
|
65
54
|
else if (sweep && sweepAngle < 0)
|
|
66
55
|
sweepAngle += 2 * Math.PI;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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) => [
|
|
71
60
|
centerX + radiusX * unitX * cosine - radiusY * unitY * sine,
|
|
72
61
|
centerY + radiusX * unitX * sine + radiusY * unitY * cosine
|
|
73
|
-
];
|
|
74
|
-
|
|
75
|
-
for (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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);
|
|
86
75
|
commands.push(['C', control1X, control1Y, control2X, control2Y, pointX, pointY]);
|
|
87
76
|
}
|
|
88
77
|
return commands;
|
|
@@ -92,19 +81,19 @@ function arcToCubicCommands(startX, startY, radiusXInput, radiusYInput, rotation
|
|
|
92
81
|
* Anything it cannot read ends the path where it stands (the part before it is kept), like a browser does.
|
|
93
82
|
*/
|
|
94
83
|
export function parsePathData(data) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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;
|
|
102
91
|
// The control point of the last C/S and Q/T, which S and T reflect.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
92
|
+
let lastCubicControl = null;
|
|
93
|
+
let lastQuadraticControl = null;
|
|
94
|
+
const skipSeparators = () => {
|
|
106
95
|
while (position < length) {
|
|
107
|
-
|
|
96
|
+
const character = data.charCodeAt(position);
|
|
108
97
|
// space, tab, newline, carriage return, form feed, comma
|
|
109
98
|
if (character === 32 || character === 9 || character === 10 || character === 13 || character === 12 || character === 44) {
|
|
110
99
|
position += 1;
|
|
@@ -114,31 +103,31 @@ export function parsePathData(data) {
|
|
|
114
103
|
}
|
|
115
104
|
}
|
|
116
105
|
};
|
|
117
|
-
|
|
106
|
+
const readNumber = () => {
|
|
118
107
|
skipSeparators();
|
|
119
108
|
NUMBER_PATTERN.lastIndex = position;
|
|
120
|
-
|
|
109
|
+
const match = NUMBER_PATTERN.exec(data);
|
|
121
110
|
if (!match)
|
|
122
111
|
return null;
|
|
123
112
|
position = NUMBER_PATTERN.lastIndex;
|
|
124
|
-
|
|
113
|
+
const value = Number(match[0]);
|
|
125
114
|
return Number.isFinite(value) ? value : null;
|
|
126
115
|
};
|
|
127
116
|
// Arc flags are one character, 0 or 1, and may touch the number after them ("a1 1 0 00.5.5").
|
|
128
|
-
|
|
117
|
+
const readFlag = () => {
|
|
129
118
|
skipSeparators();
|
|
130
|
-
|
|
119
|
+
const character = data[position];
|
|
131
120
|
if (character !== '0' && character !== '1')
|
|
132
121
|
return null;
|
|
133
122
|
position += 1;
|
|
134
123
|
return character === '1' ? 1 : 0;
|
|
135
124
|
};
|
|
136
|
-
|
|
125
|
+
let commandLetter = '';
|
|
137
126
|
while (true) {
|
|
138
127
|
skipSeparators();
|
|
139
128
|
if (position >= length)
|
|
140
129
|
break;
|
|
141
|
-
|
|
130
|
+
const character = data[position];
|
|
142
131
|
if (/[A-Za-z]/.test(character)) {
|
|
143
132
|
commandLetter = character;
|
|
144
133
|
position += 1;
|
|
@@ -146,9 +135,9 @@ export function parsePathData(data) {
|
|
|
146
135
|
else if (commandLetter === '') {
|
|
147
136
|
break;
|
|
148
137
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
138
|
+
const upper = commandLetter.toUpperCase();
|
|
139
|
+
const relative = commandLetter !== upper;
|
|
140
|
+
const parameterCount = PARAMETER_COUNT[upper];
|
|
152
141
|
if (parameterCount === undefined)
|
|
153
142
|
break;
|
|
154
143
|
if (commands.length === 0 && upper !== 'M')
|
|
@@ -163,11 +152,11 @@ export function parsePathData(data) {
|
|
|
163
152
|
commandLetter = '';
|
|
164
153
|
continue;
|
|
165
154
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
for (
|
|
169
|
-
|
|
170
|
-
|
|
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();
|
|
171
160
|
if (value === null) {
|
|
172
161
|
complete = false;
|
|
173
162
|
break;
|
|
@@ -176,16 +165,16 @@ export function parsePathData(data) {
|
|
|
176
165
|
}
|
|
177
166
|
if (!complete)
|
|
178
167
|
break;
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
168
|
+
const offsetX = relative ? currentX : 0;
|
|
169
|
+
const offsetY = relative ? currentY : 0;
|
|
170
|
+
const previousCubicControl = lastCubicControl;
|
|
171
|
+
const previousQuadraticControl = lastQuadraticControl;
|
|
183
172
|
lastCubicControl = null;
|
|
184
173
|
lastQuadraticControl = null;
|
|
185
174
|
switch (upper) {
|
|
186
175
|
case 'M': {
|
|
187
|
-
|
|
188
|
-
|
|
176
|
+
const x = parameters[0] + offsetX;
|
|
177
|
+
const y = parameters[1] + offsetY;
|
|
189
178
|
commands.push(['M', x, y]);
|
|
190
179
|
currentX = x;
|
|
191
180
|
currentY = y;
|
|
@@ -196,32 +185,32 @@ export function parsePathData(data) {
|
|
|
196
185
|
break;
|
|
197
186
|
}
|
|
198
187
|
case 'L': {
|
|
199
|
-
|
|
200
|
-
|
|
188
|
+
const x = parameters[0] + offsetX;
|
|
189
|
+
const y = parameters[1] + offsetY;
|
|
201
190
|
commands.push(['L', x, y]);
|
|
202
191
|
currentX = x;
|
|
203
192
|
currentY = y;
|
|
204
193
|
break;
|
|
205
194
|
}
|
|
206
195
|
case 'H': {
|
|
207
|
-
|
|
196
|
+
const x = parameters[0] + offsetX;
|
|
208
197
|
commands.push(['L', x, currentY]);
|
|
209
198
|
currentX = x;
|
|
210
199
|
break;
|
|
211
200
|
}
|
|
212
201
|
case 'V': {
|
|
213
|
-
|
|
202
|
+
const y = parameters[0] + offsetY;
|
|
214
203
|
commands.push(['L', currentX, y]);
|
|
215
204
|
currentY = y;
|
|
216
205
|
break;
|
|
217
206
|
}
|
|
218
207
|
case 'C': {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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;
|
|
225
214
|
commands.push(['C', control1X, control1Y, control2X, control2Y, x, y]);
|
|
226
215
|
lastCubicControl = { x: control2X, y: control2Y };
|
|
227
216
|
currentX = x;
|
|
@@ -229,12 +218,12 @@ export function parsePathData(data) {
|
|
|
229
218
|
break;
|
|
230
219
|
}
|
|
231
220
|
case 'S': {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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;
|
|
238
227
|
commands.push(['C', control1X, control1Y, control2X, control2Y, x, y]);
|
|
239
228
|
lastCubicControl = { x: control2X, y: control2Y };
|
|
240
229
|
currentX = x;
|
|
@@ -242,10 +231,10 @@ export function parsePathData(data) {
|
|
|
242
231
|
break;
|
|
243
232
|
}
|
|
244
233
|
case 'Q': {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
234
|
+
const controlX = parameters[0] + offsetX;
|
|
235
|
+
const controlY = parameters[1] + offsetY;
|
|
236
|
+
const x = parameters[2] + offsetX;
|
|
237
|
+
const y = parameters[3] + offsetY;
|
|
249
238
|
commands.push(['Q', controlX, controlY, x, y]);
|
|
250
239
|
lastQuadraticControl = { x: controlX, y: controlY };
|
|
251
240
|
currentX = x;
|
|
@@ -253,10 +242,10 @@ export function parsePathData(data) {
|
|
|
253
242
|
break;
|
|
254
243
|
}
|
|
255
244
|
case 'T': {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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;
|
|
260
249
|
commands.push(['Q', controlX, controlY, x, y]);
|
|
261
250
|
lastQuadraticControl = { x: controlX, y: controlY };
|
|
262
251
|
currentX = x;
|
|
@@ -264,9 +253,9 @@ export function parsePathData(data) {
|
|
|
264
253
|
break;
|
|
265
254
|
}
|
|
266
255
|
case 'A': {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
commands.push
|
|
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));
|
|
270
259
|
currentX = x;
|
|
271
260
|
currentY = y;
|
|
272
261
|
break;
|
|
@@ -276,18 +265,16 @@ export function parsePathData(data) {
|
|
|
276
265
|
return commands;
|
|
277
266
|
}
|
|
278
267
|
function formatNumber(value, decimals) {
|
|
279
|
-
|
|
268
|
+
const rounded = Number(value.toFixed(decimals));
|
|
280
269
|
// Negative zero would print as "-0".
|
|
281
270
|
return String(Object.is(rounded, -0) ? 0 : rounded);
|
|
282
271
|
}
|
|
283
272
|
/** Writes commands as compact path data: `M0 0L10 10Z`. */
|
|
284
|
-
export function serializePathData(commands, decimals) {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
for (var _i = 0, commands_1 = commands; _i < commands_1.length; _i++) {
|
|
288
|
-
var command = commands_1[_i];
|
|
273
|
+
export function serializePathData(commands, decimals = 2) {
|
|
274
|
+
let output = '';
|
|
275
|
+
for (const command of commands) {
|
|
289
276
|
output += command[0];
|
|
290
|
-
for (
|
|
277
|
+
for (let index = 1; index < command.length; index += 1) {
|
|
291
278
|
if (index > 1)
|
|
292
279
|
output += ' ';
|
|
293
280
|
output += formatNumber(command[index], decimals);
|
|
@@ -297,22 +284,22 @@ export function serializePathData(commands, decimals) {
|
|
|
297
284
|
}
|
|
298
285
|
/** Applies a function to every point of every command. */
|
|
299
286
|
function mapPoints(commands, mapPoint) {
|
|
300
|
-
return commands.map(
|
|
287
|
+
return commands.map((command) => {
|
|
301
288
|
switch (command[0]) {
|
|
302
289
|
case 'M':
|
|
303
290
|
case 'L': {
|
|
304
|
-
|
|
291
|
+
const point = mapPoint({ x: command[1], y: command[2] });
|
|
305
292
|
return [command[0], point.x, point.y];
|
|
306
293
|
}
|
|
307
294
|
case 'Q': {
|
|
308
|
-
|
|
309
|
-
|
|
295
|
+
const control = mapPoint({ x: command[1], y: command[2] });
|
|
296
|
+
const end = mapPoint({ x: command[3], y: command[4] });
|
|
310
297
|
return ['Q', control.x, control.y, end.x, end.y];
|
|
311
298
|
}
|
|
312
299
|
case 'C': {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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] });
|
|
316
303
|
return ['C', control1.x, control1.y, control2.x, control2.y, end.x, end.y];
|
|
317
304
|
}
|
|
318
305
|
default:
|
|
@@ -321,67 +308,66 @@ function mapPoints(commands, mapPoint) {
|
|
|
321
308
|
});
|
|
322
309
|
}
|
|
323
310
|
export function transformPathCommands(commands, matrix) {
|
|
324
|
-
return mapPoints(commands,
|
|
311
|
+
return mapPoints(commands, (point) => applyMatrixToPoint(matrix, point));
|
|
325
312
|
}
|
|
326
313
|
export function translatePathCommands(commands, deltaX, deltaY) {
|
|
327
|
-
return mapPoints(commands,
|
|
314
|
+
return mapPoints(commands, (point) => ({ x: point.x + deltaX, y: point.y + deltaY }));
|
|
328
315
|
}
|
|
329
316
|
export function scalePathCommands(commands, scaleX, scaleY) {
|
|
330
|
-
return mapPoints(commands,
|
|
317
|
+
return mapPoints(commands, (point) => ({ x: point.x * scaleX, y: point.y * scaleY }));
|
|
331
318
|
}
|
|
332
319
|
/** Mirrors the path inside a box of the given size, about the box's vertical and/or horizontal middle line. */
|
|
333
320
|
export function mirrorPathCommands(commands, width, height, flipX, flipY) {
|
|
334
321
|
if (!flipX && !flipY)
|
|
335
|
-
return
|
|
336
|
-
return mapPoints(commands,
|
|
322
|
+
return [...commands];
|
|
323
|
+
return mapPoints(commands, (point) => ({
|
|
337
324
|
x: flipX ? width - point.x : point.x,
|
|
338
325
|
y: flipY ? height - point.y : point.y
|
|
339
|
-
})
|
|
326
|
+
}));
|
|
340
327
|
}
|
|
341
328
|
/** The values of a cubic at its extrema along one axis, for the exact bounds of a curve. */
|
|
342
329
|
function cubicExtrema(p0, p1, p2, p3) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
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 = [];
|
|
347
334
|
if (Math.abs(a) < EPSILON) {
|
|
348
335
|
if (Math.abs(b) > EPSILON)
|
|
349
336
|
roots.push(-c / b);
|
|
350
337
|
}
|
|
351
338
|
else {
|
|
352
|
-
|
|
339
|
+
const discriminant = b * b - 4 * a * c;
|
|
353
340
|
if (discriminant >= 0) {
|
|
354
|
-
|
|
341
|
+
const root = Math.sqrt(discriminant);
|
|
355
342
|
roots.push((-b + root) / (2 * a), (-b - root) / (2 * a));
|
|
356
343
|
}
|
|
357
344
|
}
|
|
358
|
-
|
|
359
|
-
for (
|
|
360
|
-
var t = roots_1[_i];
|
|
345
|
+
const values = [];
|
|
346
|
+
for (const t of roots) {
|
|
361
347
|
if (t > 0 && t < 1) {
|
|
362
|
-
|
|
363
|
-
values.push(
|
|
348
|
+
const inverse = 1 - t;
|
|
349
|
+
values.push(inverse ** 3 * p0 + 3 * inverse * inverse * t * p1 + 3 * inverse * t * t * p2 + t ** 3 * p3);
|
|
364
350
|
}
|
|
365
351
|
}
|
|
366
352
|
return values;
|
|
367
353
|
}
|
|
368
354
|
function quadraticExtrema(p0, p1, p2) {
|
|
369
|
-
|
|
355
|
+
const denominator = p0 - 2 * p1 + p2;
|
|
370
356
|
if (Math.abs(denominator) < EPSILON)
|
|
371
357
|
return [];
|
|
372
|
-
|
|
358
|
+
const t = (p0 - p1) / denominator;
|
|
373
359
|
if (t <= 0 || t >= 1)
|
|
374
360
|
return [];
|
|
375
|
-
|
|
361
|
+
const inverse = 1 - t;
|
|
376
362
|
return [inverse * inverse * p0 + 2 * inverse * t * p1 + t * t * p2];
|
|
377
363
|
}
|
|
378
364
|
/** The exact bounding box of a path (curves measured at their extrema, not by their control points). Null when empty. */
|
|
379
365
|
export function pathBounds(commands) {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
366
|
+
let left = Infinity;
|
|
367
|
+
let top = Infinity;
|
|
368
|
+
let right = -Infinity;
|
|
369
|
+
let bottom = -Infinity;
|
|
370
|
+
const include = (x, y) => {
|
|
385
371
|
if (x < left)
|
|
386
372
|
left = x;
|
|
387
373
|
if (x > right)
|
|
@@ -391,12 +377,11 @@ export function pathBounds(commands) {
|
|
|
391
377
|
if (y > bottom)
|
|
392
378
|
bottom = y;
|
|
393
379
|
};
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
for (
|
|
399
|
-
var command = commands_2[_i];
|
|
380
|
+
let currentX = 0;
|
|
381
|
+
let currentY = 0;
|
|
382
|
+
let startX = 0;
|
|
383
|
+
let startY = 0;
|
|
384
|
+
for (const command of commands) {
|
|
400
385
|
switch (command[0]) {
|
|
401
386
|
case 'M':
|
|
402
387
|
currentX = startX = command[1];
|
|
@@ -410,28 +395,20 @@ export function pathBounds(commands) {
|
|
|
410
395
|
break;
|
|
411
396
|
case 'Q': {
|
|
412
397
|
include(command[3], command[4]);
|
|
413
|
-
for (
|
|
414
|
-
var x = _b[_a];
|
|
398
|
+
for (const x of quadraticExtrema(currentX, command[1], command[3]))
|
|
415
399
|
include(x, currentY);
|
|
416
|
-
|
|
417
|
-
for (var _c = 0, _d = quadraticExtrema(currentY, command[2], command[4]); _c < _d.length; _c++) {
|
|
418
|
-
var y = _d[_c];
|
|
400
|
+
for (const y of quadraticExtrema(currentY, command[2], command[4]))
|
|
419
401
|
include(currentX, y);
|
|
420
|
-
}
|
|
421
402
|
currentX = command[3];
|
|
422
403
|
currentY = command[4];
|
|
423
404
|
break;
|
|
424
405
|
}
|
|
425
406
|
case 'C': {
|
|
426
407
|
include(command[5], command[6]);
|
|
427
|
-
for (
|
|
428
|
-
var x = _f[_e];
|
|
408
|
+
for (const x of cubicExtrema(currentX, command[1], command[3], command[5]))
|
|
429
409
|
include(x, currentY);
|
|
430
|
-
|
|
431
|
-
for (var _g = 0, _h = cubicExtrema(currentY, command[2], command[4], command[6]); _g < _h.length; _g++) {
|
|
432
|
-
var y = _h[_g];
|
|
410
|
+
for (const y of cubicExtrema(currentY, command[2], command[4], command[6]))
|
|
433
411
|
include(currentX, y);
|
|
434
|
-
}
|
|
435
412
|
currentX = command[5];
|
|
436
413
|
currentY = command[6];
|
|
437
414
|
break;
|
|
@@ -450,19 +427,15 @@ export function pathBounds(commands) {
|
|
|
450
427
|
* Cuts a path into polylines. Curves are cut every `segmentLength` units of their control polygon
|
|
451
428
|
* (at least 2 and at most 48 pieces), which is plenty for hit tests and the eraser.
|
|
452
429
|
*/
|
|
453
|
-
export function flattenPath(commands, segmentLength) {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return Math.max(2, Math.min(48, Math.ceil(controlLength / Math.max(1, segmentLength))));
|
|
463
|
-
};
|
|
464
|
-
for (var _i = 0, commands_3 = commands; _i < commands_3.length; _i++) {
|
|
465
|
-
var command = commands_3[_i];
|
|
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) {
|
|
466
439
|
switch (command[0]) {
|
|
467
440
|
case 'M':
|
|
468
441
|
current = { points: [{ x: command[1], y: command[2] }], closed: false };
|
|
@@ -484,12 +457,12 @@ export function flattenPath(commands, segmentLength) {
|
|
|
484
457
|
current = { points: [{ x: currentX, y: currentY }], closed: false };
|
|
485
458
|
subpaths.push(current);
|
|
486
459
|
}
|
|
487
|
-
|
|
460
|
+
const controlLength = Math.hypot(command[1] - currentX, command[2] - currentY) +
|
|
488
461
|
Math.hypot(command[3] - command[1], command[4] - command[2]);
|
|
489
|
-
|
|
490
|
-
for (
|
|
491
|
-
|
|
492
|
-
|
|
462
|
+
const pieces = pieceCount(controlLength);
|
|
463
|
+
for (let step = 1; step <= pieces; step += 1) {
|
|
464
|
+
const t = step / pieces;
|
|
465
|
+
const inverse = 1 - t;
|
|
493
466
|
current.points.push({
|
|
494
467
|
x: inverse * inverse * currentX + 2 * inverse * t * command[1] + t * t * command[3],
|
|
495
468
|
y: inverse * inverse * currentY + 2 * inverse * t * command[2] + t * t * command[4]
|
|
@@ -504,17 +477,17 @@ export function flattenPath(commands, segmentLength) {
|
|
|
504
477
|
current = { points: [{ x: currentX, y: currentY }], closed: false };
|
|
505
478
|
subpaths.push(current);
|
|
506
479
|
}
|
|
507
|
-
|
|
480
|
+
const controlLength = Math.hypot(command[1] - currentX, command[2] - currentY) +
|
|
508
481
|
Math.hypot(command[3] - command[1], command[4] - command[2]) +
|
|
509
482
|
Math.hypot(command[5] - command[3], command[6] - command[4]);
|
|
510
|
-
|
|
511
|
-
for (
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
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;
|
|
518
491
|
current.points.push({
|
|
519
492
|
x: weight0 * currentX + weight1 * command[1] + weight2 * command[3] + weight3 * command[5],
|
|
520
493
|
y: weight0 * currentY + weight1 * command[2] + weight2 * command[4] + weight3 * command[6]
|
|
@@ -541,34 +514,34 @@ export function flattenPath(commands, segmentLength) {
|
|
|
541
514
|
* A flat axis (a straight horizontal or vertical line) is centred in its axis instead of scaled.
|
|
542
515
|
*/
|
|
543
516
|
export function fitPathToBox(commands, width, height) {
|
|
544
|
-
|
|
517
|
+
const bounds = pathBounds(commands);
|
|
545
518
|
if (!bounds)
|
|
546
|
-
return
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
return mapPoints(commands,
|
|
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) => ({
|
|
552
525
|
x: bounds.width > EPSILON ? (point.x - bounds.x) * scaleX : point.x + offsetX,
|
|
553
526
|
y: bounds.height > EPSILON ? (point.y - bounds.y) * scaleY : point.y + offsetY
|
|
554
|
-
})
|
|
527
|
+
}));
|
|
555
528
|
}
|
|
556
529
|
/**
|
|
557
530
|
* Moves a path so that its bounding box starts at 0, 0 and reports where the box was.
|
|
558
531
|
* This is how a drawn stroke becomes an object: the path is local, the box says where it stands.
|
|
559
532
|
*/
|
|
560
533
|
export function localizePath(commands) {
|
|
561
|
-
|
|
534
|
+
const bounds = pathBounds(commands);
|
|
562
535
|
if (!bounds)
|
|
563
536
|
return null;
|
|
564
|
-
return { commands: translatePathCommands(commands, -bounds.x, -bounds.y), bounds
|
|
537
|
+
return { commands: translatePathCommands(commands, -bounds.x, -bounds.y), bounds };
|
|
565
538
|
}
|
|
566
539
|
/** A polyline (or polygon) as path commands. */
|
|
567
540
|
export function polylineToPathCommands(points, closed) {
|
|
568
541
|
if (points.length === 0)
|
|
569
542
|
return [];
|
|
570
|
-
|
|
571
|
-
for (
|
|
543
|
+
const commands = [['M', points[0].x, points[0].y]];
|
|
544
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
572
545
|
commands.push(['L', points[index].x, points[index].y]);
|
|
573
546
|
}
|
|
574
547
|
if (closed)
|