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