@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
|
@@ -19,45 +19,33 @@
|
|
|
19
19
|
* Pure functions on plain numbers: they run in Node as they do in the browser, and the editor, the eraser's trail and the
|
|
20
20
|
* rasterizer all draw from the same outline.
|
|
21
21
|
*/
|
|
22
|
-
var __assign = (this && this.__assign) || function () {
|
|
23
|
-
__assign = Object.assign || function(t) {
|
|
24
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
25
|
-
s = arguments[i];
|
|
26
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
27
|
-
t[p] = s[p];
|
|
28
|
-
}
|
|
29
|
-
return t;
|
|
30
|
-
};
|
|
31
|
-
return __assign.apply(this, arguments);
|
|
32
|
-
};
|
|
33
22
|
// How fast a simulated pressure follows the speed of the hand.
|
|
34
|
-
|
|
23
|
+
const RATE_OF_PRESSURE_CHANGE = 0.275;
|
|
35
24
|
// A half turn that is a hair more than π: a cap built from exactly π leaves a visible seam in browsers.
|
|
36
|
-
|
|
25
|
+
const HALF_TURN = Math.PI + 0.0001;
|
|
37
26
|
// The outline of a corner is built from this many steps of a half turn, and a round end cap from this many of one and a half.
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
const CORNER_STEPS = 13;
|
|
28
|
+
const END_CAP_STEPS = 29;
|
|
40
29
|
// A turn sharper than this (the dot product of two directions) is a corner that gets a round join.
|
|
41
|
-
|
|
30
|
+
const SHARP_CORNER = 0;
|
|
42
31
|
// The end of a line within this distance of its last point is dropped from the outline: it is only noise.
|
|
43
|
-
|
|
32
|
+
const END_NOISE_DISTANCE = 3;
|
|
44
33
|
// ───────────────────────────────────────────────────────────────────────── easings
|
|
45
|
-
export
|
|
46
|
-
export
|
|
34
|
+
export const EASE_LINEAR = (progress) => progress;
|
|
35
|
+
export const EASE_OUT_SINE = (progress) => Math.sin((progress * Math.PI) / 2);
|
|
47
36
|
/** The easing of a pen's pressure: mostly linear, with a little of the sine's softness. */
|
|
48
|
-
export
|
|
49
|
-
|
|
50
|
-
|
|
37
|
+
export const EASE_PEN = (progress) => progress * 0.65 + Math.sin((progress * Math.PI) / 2) * 0.35;
|
|
38
|
+
const EASE_OUT_QUADRATIC = (progress) => progress * (2 - progress);
|
|
39
|
+
const EASE_OUT_CUBIC = (progress) => (progress - 1) * (progress - 1) * (progress - 1) + 1;
|
|
51
40
|
// ───────────────────────────────────────────────────────────────────────── the line of points
|
|
52
41
|
/** The samples that can be used: finite coordinates, and a pressure that is either 0 to 1 or negative for unknown. */
|
|
53
42
|
function usableSamples(samples) {
|
|
54
|
-
|
|
55
|
-
for (
|
|
56
|
-
var sample = samples_1[_i];
|
|
43
|
+
const usable = [];
|
|
44
|
+
for (const sample of samples) {
|
|
57
45
|
if (!Number.isFinite(sample.x) || !Number.isFinite(sample.y))
|
|
58
46
|
continue;
|
|
59
|
-
|
|
60
|
-
usable.push({ x: sample.x, y: sample.y, pressure
|
|
47
|
+
const pressure = sample.pressure !== undefined && Number.isFinite(sample.pressure) && sample.pressure >= 0 ? Math.min(1, sample.pressure) : -1;
|
|
48
|
+
usable.push({ x: sample.x, y: sample.y, pressure });
|
|
61
49
|
}
|
|
62
50
|
return usable;
|
|
63
51
|
}
|
|
@@ -68,21 +56,20 @@ function usableSamples(samples) {
|
|
|
68
56
|
* A finished line (`last`) ends exactly on the last sample.
|
|
69
57
|
*/
|
|
70
58
|
export function strokePointsOf(samples, options) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var line = usableSamples(samples);
|
|
59
|
+
const size = options.size;
|
|
60
|
+
const streamline = options.streamline ?? 0.5;
|
|
61
|
+
const finished = options.last === true;
|
|
62
|
+
let line = usableSamples(samples);
|
|
76
63
|
if (line.length === 0)
|
|
77
64
|
return [];
|
|
78
65
|
// How far a point moves towards the next sample: 1 would follow the hand exactly.
|
|
79
|
-
|
|
66
|
+
const follow = 0.15 + (1 - streamline) * 0.85;
|
|
80
67
|
if (line.length === 2) {
|
|
81
68
|
// Two samples are a straight line: points are put between them so that it is drawn as a longer line is.
|
|
82
|
-
|
|
69
|
+
const [first, second] = line;
|
|
83
70
|
line = [first];
|
|
84
|
-
for (
|
|
85
|
-
|
|
71
|
+
for (let step = 1; step < 5; step += 1) {
|
|
72
|
+
const progress = step / 4;
|
|
86
73
|
line.push({
|
|
87
74
|
x: first.x + (second.x - first.x) * progress,
|
|
88
75
|
y: first.y + (second.y - first.y) * progress,
|
|
@@ -92,23 +79,23 @@ export function strokePointsOf(samples, options) {
|
|
|
92
79
|
}
|
|
93
80
|
if (line.length === 1) {
|
|
94
81
|
// A click is a dot: a second point a pixel away gives it a direction.
|
|
95
|
-
line = [line[0],
|
|
82
|
+
line = [line[0], { ...line[0], x: line[0].x + 1, y: line[0].y + 1 }];
|
|
96
83
|
}
|
|
97
|
-
|
|
84
|
+
const points = [
|
|
98
85
|
{ x: line[0].x, y: line[0].y, pressure: line[0].pressure >= 0 ? line[0].pressure : 0.25, vectorX: 1, vectorY: 1, distance: 0, runningLength: 0 }
|
|
99
86
|
];
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
for (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
87
|
+
let reachedMinimumLength = false;
|
|
88
|
+
let runningLength = 0;
|
|
89
|
+
let previous = points[0];
|
|
90
|
+
const lastIndex = line.length - 1;
|
|
91
|
+
for (let index = 1; index < line.length; index += 1) {
|
|
92
|
+
const sample = line[index];
|
|
93
|
+
const atEnd = finished && index === lastIndex;
|
|
94
|
+
const x = atEnd ? sample.x : previous.x + (sample.x - previous.x) * follow;
|
|
95
|
+
const y = atEnd ? sample.y : previous.y + (sample.y - previous.y) * follow;
|
|
109
96
|
if (x === previous.x && y === previous.y)
|
|
110
97
|
continue;
|
|
111
|
-
|
|
98
|
+
const distance = Math.hypot(x - previous.x, y - previous.y);
|
|
112
99
|
runningLength += distance;
|
|
113
100
|
// The start of the line, shorter than the brush is wide, only makes it wobble.
|
|
114
101
|
if (index < lastIndex && !reachedMinimumLength) {
|
|
@@ -116,14 +103,14 @@ export function strokePointsOf(samples, options) {
|
|
|
116
103
|
continue;
|
|
117
104
|
reachedMinimumLength = true;
|
|
118
105
|
}
|
|
119
|
-
|
|
120
|
-
x
|
|
121
|
-
y
|
|
106
|
+
const next = {
|
|
107
|
+
x,
|
|
108
|
+
y,
|
|
122
109
|
pressure: sample.pressure >= 0 ? sample.pressure : 0.5,
|
|
123
110
|
vectorX: (previous.x - x) / distance,
|
|
124
111
|
vectorY: (previous.y - y) / distance,
|
|
125
|
-
distance
|
|
126
|
-
runningLength
|
|
112
|
+
distance,
|
|
113
|
+
runningLength
|
|
127
114
|
};
|
|
128
115
|
points.push(next);
|
|
129
116
|
previous = next;
|
|
@@ -141,15 +128,15 @@ function radiusFor(size, thinning, pressure, easing) {
|
|
|
141
128
|
/** A pressure that follows the speed of the hand: a point close to the one before presses, a far one lifts. */
|
|
142
129
|
function simulatedPressure(previousPressure, distance, size) {
|
|
143
130
|
// How fast the hand moves, in brush widths per point, and what is left of the stillness.
|
|
144
|
-
|
|
145
|
-
|
|
131
|
+
const speed = Math.min(1, distance / size);
|
|
132
|
+
const stillness = Math.min(1, 1 - speed);
|
|
146
133
|
return Math.min(1, previousPressure + (stillness - previousPressure) * (speed * RATE_OF_PRESSURE_CHANGE));
|
|
147
134
|
}
|
|
148
135
|
function rotateAbout(point, center, angle) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
136
|
+
const cosine = Math.cos(angle);
|
|
137
|
+
const sine = Math.sin(angle);
|
|
138
|
+
const offsetX = point.x - center.x;
|
|
139
|
+
const offsetY = point.y - center.y;
|
|
153
140
|
return { x: center.x + offsetX * cosine - offsetY * sine, y: center.y + offsetX * sine + offsetY * cosine };
|
|
154
141
|
}
|
|
155
142
|
/** The length a tapered end narrows over. */
|
|
@@ -172,45 +159,43 @@ export function outlineOf(points, options) {
|
|
|
172
159
|
* from these (with `simulatePressure` off) and come out the same, whatever size its points are later scaled to.
|
|
173
160
|
*/
|
|
174
161
|
export function outlineWithPressures(points, options) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
var endEasing = (_m = (_l = options.end) === null || _l === void 0 ? void 0 : _l.easing) !== null && _m !== void 0 ? _m : EASE_OUT_CUBIC;
|
|
162
|
+
const size = options.size;
|
|
163
|
+
const thinning = options.thinning ?? 0.5;
|
|
164
|
+
const smoothing = options.smoothing ?? 0.5;
|
|
165
|
+
const simulate = options.simulatePressure ?? true;
|
|
166
|
+
const easing = options.easing ?? EASE_LINEAR;
|
|
167
|
+
const finished = options.last === true;
|
|
168
|
+
const startCap = options.start?.cap ?? true;
|
|
169
|
+
const endCap = options.end?.cap ?? true;
|
|
170
|
+
const startEasing = options.start?.easing ?? EASE_OUT_QUADRATIC;
|
|
171
|
+
const endEasing = options.end?.easing ?? EASE_OUT_CUBIC;
|
|
186
172
|
if (points.length === 0 || !(size > 0))
|
|
187
173
|
return { outline: [], pressures: [] };
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
174
|
+
const pressures = points.map((point) => point.pressure);
|
|
175
|
+
const totalLength = points[points.length - 1].runningLength;
|
|
176
|
+
const taperStart = taperLength(options.start?.taper, size, totalLength);
|
|
177
|
+
const taperEnd = taperLength(options.end?.taper, size, totalLength);
|
|
192
178
|
// The least distance between two points of one side of the outline (squared): closer points add a wobble and nothing else.
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
179
|
+
const minimumGapSquared = (size * smoothing) ** 2;
|
|
180
|
+
const left = [];
|
|
181
|
+
const right = [];
|
|
196
182
|
// The pressure the line starts from: the mean of its first ten points.
|
|
197
|
-
|
|
198
|
-
for (
|
|
199
|
-
|
|
200
|
-
var pressure = simulate ? simulatedPressure(previousPressure, point.distance, size) : point.pressure;
|
|
183
|
+
let previousPressure = points[0].pressure;
|
|
184
|
+
for (const point of points.slice(0, 10)) {
|
|
185
|
+
const pressure = simulate ? simulatedPressure(previousPressure, point.distance, size) : point.pressure;
|
|
201
186
|
previousPressure = (previousPressure + pressure) / 2;
|
|
202
187
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
for (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
188
|
+
let radius = radiusFor(size, thinning, points[points.length - 1].pressure, easing);
|
|
189
|
+
let firstRadius;
|
|
190
|
+
let previousVectorX = points[0].vectorX;
|
|
191
|
+
let previousVectorY = points[0].vectorY;
|
|
192
|
+
let previousLeft = { x: points[0].x, y: points[0].y };
|
|
193
|
+
let previousRight = previousLeft;
|
|
194
|
+
let previousWasSharpCorner = false;
|
|
195
|
+
for (let index = 0; index < points.length; index += 1) {
|
|
196
|
+
const point = points[index];
|
|
197
|
+
let pressure = point.pressure;
|
|
198
|
+
const isLast = index === points.length - 1;
|
|
214
199
|
// The very end of a line is noise: leave it out and let the last point finish the line.
|
|
215
200
|
if (!isLast && totalLength - point.runningLength < END_NOISE_DISTANCE) {
|
|
216
201
|
pressures[index] = previousPressure;
|
|
@@ -229,25 +214,25 @@ export function outlineWithPressures(points, options) {
|
|
|
229
214
|
if (firstRadius === undefined)
|
|
230
215
|
firstRadius = radius;
|
|
231
216
|
// A tapered end narrows over its length, and the narrower of the two ends wins where they meet.
|
|
232
|
-
|
|
233
|
-
|
|
217
|
+
const startTaper = point.runningLength < taperStart ? startEasing(point.runningLength / taperStart) : 1;
|
|
218
|
+
const endTaper = totalLength - point.runningLength < taperEnd ? endEasing((totalLength - point.runningLength) / taperEnd) : 1;
|
|
234
219
|
radius = Math.max(0.01, radius * Math.min(startTaper, endTaper));
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
220
|
+
const next = isLast ? point : points[index + 1];
|
|
221
|
+
const nextDot = isLast ? 1 : point.vectorX * next.vectorX + point.vectorY * next.vectorY;
|
|
222
|
+
const previousDot = point.vectorX * previousVectorX + point.vectorY * previousVectorY;
|
|
223
|
+
const isSharpCorner = previousDot < SHARP_CORNER && !previousWasSharpCorner;
|
|
224
|
+
const nextIsSharpCorner = nextDot < SHARP_CORNER;
|
|
240
225
|
if (isSharpCorner || nextIsSharpCorner) {
|
|
241
226
|
// A sharp corner is a half turn round the point, on both sides, and the line goes on from there.
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
for (
|
|
247
|
-
|
|
248
|
-
lastLeft = rotateAbout({ x: point.x -
|
|
227
|
+
const offsetX = previousVectorY * radius;
|
|
228
|
+
const offsetY = -previousVectorX * radius;
|
|
229
|
+
let lastLeft = previousLeft;
|
|
230
|
+
let lastRight = previousRight;
|
|
231
|
+
for (let step = 0; step <= CORNER_STEPS; step += 1) {
|
|
232
|
+
const turn = HALF_TURN * (step / CORNER_STEPS);
|
|
233
|
+
lastLeft = rotateAbout({ x: point.x - offsetX, y: point.y - offsetY }, point, turn);
|
|
249
234
|
left.push(lastLeft);
|
|
250
|
-
lastRight = rotateAbout({ x: point.x +
|
|
235
|
+
lastRight = rotateAbout({ x: point.x + offsetX, y: point.y + offsetY }, point, -turn);
|
|
251
236
|
right.push(lastRight);
|
|
252
237
|
}
|
|
253
238
|
previousLeft = lastLeft;
|
|
@@ -259,24 +244,24 @@ export function outlineWithPressures(points, options) {
|
|
|
259
244
|
previousWasSharpCorner = false;
|
|
260
245
|
if (isLast) {
|
|
261
246
|
// The last point: straight out to both sides of the way the line was going.
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
left.push({ x: point.x -
|
|
265
|
-
right.push({ x: point.x +
|
|
247
|
+
const offsetX = point.vectorY * radius;
|
|
248
|
+
const offsetY = -point.vectorX * radius;
|
|
249
|
+
left.push({ x: point.x - offsetX, y: point.y - offsetY });
|
|
250
|
+
right.push({ x: point.x + offsetX, y: point.y + offsetY });
|
|
266
251
|
continue;
|
|
267
252
|
}
|
|
268
253
|
// An ordinary point: out to both sides along the middle of the way in and the way out.
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
if (index <= 1 ||
|
|
254
|
+
const middleX = next.vectorX + (point.vectorX - next.vectorX) * nextDot;
|
|
255
|
+
const middleY = next.vectorY + (point.vectorY - next.vectorY) * nextDot;
|
|
256
|
+
const offsetX = middleY * radius;
|
|
257
|
+
const offsetY = -middleX * radius;
|
|
258
|
+
const leftPoint = { x: point.x - offsetX, y: point.y - offsetY };
|
|
259
|
+
if (index <= 1 || (leftPoint.x - previousLeft.x) ** 2 + (leftPoint.y - previousLeft.y) ** 2 > minimumGapSquared) {
|
|
275
260
|
left.push(leftPoint);
|
|
276
261
|
previousLeft = leftPoint;
|
|
277
262
|
}
|
|
278
|
-
|
|
279
|
-
if (index <= 1 ||
|
|
263
|
+
const rightPoint = { x: point.x + offsetX, y: point.y + offsetY };
|
|
264
|
+
if (index <= 1 || (rightPoint.x - previousRight.x) ** 2 + (rightPoint.y - previousRight.y) ** 2 > minimumGapSquared) {
|
|
280
265
|
right.push(rightPoint);
|
|
281
266
|
previousRight = rightPoint;
|
|
282
267
|
}
|
|
@@ -284,22 +269,22 @@ export function outlineWithPressures(points, options) {
|
|
|
284
269
|
previousVectorX = point.vectorX;
|
|
285
270
|
previousVectorY = point.vectorY;
|
|
286
271
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
272
|
+
const first = { x: points[0].x, y: points[0].y };
|
|
273
|
+
const last = points.length > 1 ? { x: points[points.length - 1].x, y: points[points.length - 1].y } : { x: points[0].x + 1, y: points[0].y + 1 };
|
|
274
|
+
const startCapPoints = [];
|
|
275
|
+
const endCapPoints = [];
|
|
291
276
|
if (points.length === 1) {
|
|
292
277
|
// A lone point is a dot, unless its taper would make it nothing and the stroke is not finished.
|
|
293
278
|
if (!(taperStart || taperEnd) || finished) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
279
|
+
const around = { x: first.x - last.x, y: first.y - last.y };
|
|
280
|
+
const length = Math.hypot(around.x, around.y) || 1;
|
|
281
|
+
const dotRadius = firstRadius ?? radius;
|
|
297
282
|
// A point a radius away, at right angles to the (arbitrary) direction of the dot, turned a full circle.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
for (
|
|
283
|
+
const start = { x: first.x - (around.y / length) * dotRadius, y: first.y + (around.x / length) * dotRadius };
|
|
284
|
+
const dot = [];
|
|
285
|
+
for (let step = 1; step <= CORNER_STEPS; step += 1)
|
|
301
286
|
dot.push(rotateAbout(start, first, HALF_TURN * 2 * (step / CORNER_STEPS)));
|
|
302
|
-
return { outline: dot, pressures
|
|
287
|
+
return { outline: dot, pressures };
|
|
303
288
|
}
|
|
304
289
|
}
|
|
305
290
|
else {
|
|
@@ -308,25 +293,25 @@ export function outlineWithPressures(points, options) {
|
|
|
308
293
|
}
|
|
309
294
|
else if (startCap) {
|
|
310
295
|
// A round cap: the right side's first point turned half a circle about the start, to the left side's.
|
|
311
|
-
for (
|
|
296
|
+
for (let step = 1; step <= CORNER_STEPS; step += 1)
|
|
312
297
|
startCapPoints.push(rotateAbout(right[0], first, HALF_TURN * (step / CORNER_STEPS)));
|
|
313
298
|
}
|
|
314
299
|
else {
|
|
315
|
-
|
|
316
|
-
|
|
300
|
+
const cornersX = left[0].x - right[0].x;
|
|
301
|
+
const cornersY = left[0].y - right[0].y;
|
|
317
302
|
startCapPoints.push({ x: first.x - cornersX * 0.5, y: first.y - cornersY * 0.5 }, { x: first.x - cornersX * 0.51, y: first.y - cornersY * 0.51 }, { x: first.x + cornersX * 0.51, y: first.y + cornersY * 0.51 }, { x: first.x + cornersX * 0.5, y: first.y + cornersY * 0.5 });
|
|
318
303
|
}
|
|
319
|
-
|
|
304
|
+
const lastPoint = points[points.length - 1];
|
|
320
305
|
// At right angles to the way the line was going when it ended (its vector points back along it).
|
|
321
|
-
|
|
322
|
-
|
|
306
|
+
const directionX = -lastPoint.vectorY;
|
|
307
|
+
const directionY = lastPoint.vectorX;
|
|
323
308
|
if (taperEnd || (taperStart && points.length === 1)) {
|
|
324
309
|
endCapPoints.push(last);
|
|
325
310
|
}
|
|
326
311
|
else if (endCap) {
|
|
327
312
|
// A round end cap goes a turn and a half round the last point, so that a sharp turn at the end is not cut short.
|
|
328
|
-
|
|
329
|
-
for (
|
|
313
|
+
const start = { x: last.x + directionX * radius, y: last.y + directionY * radius };
|
|
314
|
+
for (let step = 1; step < END_CAP_STEPS; step += 1)
|
|
330
315
|
endCapPoints.push(rotateAbout(start, last, HALF_TURN * 3 * (step / END_CAP_STEPS)));
|
|
331
316
|
}
|
|
332
317
|
else {
|
|
@@ -334,7 +319,7 @@ export function outlineWithPressures(points, options) {
|
|
|
334
319
|
}
|
|
335
320
|
}
|
|
336
321
|
// Up the left, round the end, down the right, round the start.
|
|
337
|
-
return { outline: left.concat(endCapPoints, right.reverse(), startCapPoints), pressures
|
|
322
|
+
return { outline: left.concat(endCapPoints, right.reverse(), startCapPoints), pressures };
|
|
338
323
|
}
|
|
339
324
|
/**
|
|
340
325
|
* Points that are already smooth (the line a stroke was stored as) with what the outline is made from: the way back along the
|
|
@@ -342,28 +327,27 @@ export function outlineWithPressures(points, options) {
|
|
|
342
327
|
* given; a point on the one before it adds nothing and is left out.
|
|
343
328
|
*/
|
|
344
329
|
export function strokePointsOfLine(line) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
for (
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
var previous = points[points.length - 1];
|
|
330
|
+
const points = [];
|
|
331
|
+
let runningLength = 0;
|
|
332
|
+
for (const sample of usableSamples(line)) {
|
|
333
|
+
const pressure = sample.pressure >= 0 ? sample.pressure : 0.5;
|
|
334
|
+
const previous = points[points.length - 1];
|
|
351
335
|
if (!previous) {
|
|
352
|
-
points.push({ x: sample.x, y: sample.y, pressure
|
|
336
|
+
points.push({ x: sample.x, y: sample.y, pressure, vectorX: 1, vectorY: 1, distance: 0, runningLength: 0 });
|
|
353
337
|
continue;
|
|
354
338
|
}
|
|
355
|
-
|
|
339
|
+
const distance = Math.hypot(sample.x - previous.x, sample.y - previous.y);
|
|
356
340
|
if (distance === 0)
|
|
357
341
|
continue;
|
|
358
342
|
runningLength += distance;
|
|
359
343
|
points.push({
|
|
360
344
|
x: sample.x,
|
|
361
345
|
y: sample.y,
|
|
362
|
-
pressure
|
|
346
|
+
pressure,
|
|
363
347
|
vectorX: (previous.x - sample.x) / distance,
|
|
364
348
|
vectorY: (previous.y - sample.y) / distance,
|
|
365
|
-
distance
|
|
366
|
-
runningLength
|
|
349
|
+
distance,
|
|
350
|
+
runningLength
|
|
367
351
|
});
|
|
368
352
|
}
|
|
369
353
|
// The first point looks the way the second one does.
|
|
@@ -382,12 +366,12 @@ export function freehandOutline(samples, options) {
|
|
|
382
366
|
return outlineOf(strokePointsOf(samples, options), options);
|
|
383
367
|
}
|
|
384
368
|
// ───────────────────────────────────────────────────────────────────────── as path data
|
|
385
|
-
|
|
386
|
-
|
|
369
|
+
const FIXED_DECIMALS = 2;
|
|
370
|
+
const fixed = (value) => (Math.round(value * 10 ** FIXED_DECIMALS) / 10 ** FIXED_DECIMALS).toString();
|
|
387
371
|
/** The path data of a circle: two arcs, so that it fills as a dot. */
|
|
388
372
|
export function circlePathData(center, radius) {
|
|
389
|
-
|
|
390
|
-
return
|
|
373
|
+
const r = Math.max(radius, 0.01);
|
|
374
|
+
return `M${fixed(center.x - r)},${fixed(center.y)}a${fixed(r)},${fixed(r)} 0 1,0 ${fixed(r * 2)},0a${fixed(r)},${fixed(r)} 0 1,0 ${fixed(-r * 2)},0Z`;
|
|
391
375
|
}
|
|
392
376
|
/**
|
|
393
377
|
* The path data of a closed outline, smoothed: a quadratic curve through the midpoints of the polygon's sides, with the
|
|
@@ -396,19 +380,19 @@ export function circlePathData(center, radius) {
|
|
|
396
380
|
export function outlinePathData(outline) {
|
|
397
381
|
if (outline.length < 4)
|
|
398
382
|
return '';
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
for (
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
data +=
|
|
383
|
+
const [first, second, third] = outline;
|
|
384
|
+
let data = `M${fixed(first.x)},${fixed(first.y)}Q${fixed(second.x)},${fixed(second.y)} ${fixed((second.x + third.x) / 2)},${fixed((second.y + third.y) / 2)}T`;
|
|
385
|
+
for (let index = 2; index < outline.length - 1; index += 1) {
|
|
386
|
+
const current = outline[index];
|
|
387
|
+
const next = outline[index + 1];
|
|
388
|
+
data += `${fixed((current.x + next.x) / 2)},${fixed((current.y + next.y) / 2)} `;
|
|
405
389
|
}
|
|
406
|
-
return
|
|
390
|
+
return `${data.trimEnd()}Z`;
|
|
407
391
|
}
|
|
408
392
|
// ───────────────────────────────────────────────────────────────────────── the brush
|
|
409
393
|
/** Maps a value from one range to another, held inside it. */
|
|
410
394
|
function modulate(value, from, to) {
|
|
411
|
-
|
|
395
|
+
const progress = Math.min(1, Math.max(0, (value - from[0]) / (from[1] - from[0])));
|
|
412
396
|
return to[0] + (to[1] - to[0]) * progress;
|
|
413
397
|
}
|
|
414
398
|
/**
|
|
@@ -420,7 +404,7 @@ export function brushOptions(size, pressure, finished) {
|
|
|
420
404
|
return { size: 1 + size * 1.2, thinning: 0.62, streamline: 0.62, smoothing: 0.62, simulatePressure: false, easing: EASE_PEN, last: finished };
|
|
421
405
|
}
|
|
422
406
|
return {
|
|
423
|
-
size
|
|
407
|
+
size,
|
|
424
408
|
thinning: 0.5,
|
|
425
409
|
streamline: modulate(size, [9, 16], [0.64, 0.74]),
|
|
426
410
|
smoothing: 0.62,
|