@widgetic/canvas 0.5.45 → 0.5.47
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 +3 -2
- package/dist/canvas/DomCanvas.svelte +1 -2
- 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.js +19 -21
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +6 -5
- package/dist/canvas/host/CanvasHost.js +1664 -1861
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/boardFiles.js +35 -51
- package/dist/canvas/host/browserServices.js +108 -196
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.js +18 -21
- package/dist/canvas/host/pageEvents.js +5 -5
- package/dist/canvas/host/pauseCollector.d.ts +18 -0
- package/dist/canvas/host/pauseCollector.js +45 -0
- package/dist/canvas/host/pictureSaves.js +9 -11
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.js +149 -243
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +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.js +4 -4
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/types.d.ts +7 -1
- package/dist/canvas/ui/CanvasContextMenu.svelte +40 -3
- package/dist/canvas/ui/CanvasHint.svelte +10 -9
- package/dist/canvas/ui/SelectionPanel.svelte +14 -12
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +0 -2
- package/dist/canvas/ui/ShapesMenu.svelte +6 -4
- package/dist/canvas/ui/ToolButton.svelte +7 -6
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +28 -8
- package/dist/canvas/ui/floatingCard.js +48 -28
- package/dist/canvas/ui/floatingLayer.js +5 -6
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.d.ts +8 -4
- package/dist/canvas/ui/selectionTraits.js +44 -58
- package/dist/canvas/view/CanvasStage.svelte +1 -1
- package/dist/canvas/view/SelectionOverlay.svelte +2 -2
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.d.ts +1 -1
- package/dist/canvas/view/geometry.js +72 -91
- package/dist/canvas/view/liftedScene.js +24 -36
- package/dist/canvas/view/mountedScene.js +76 -79
- package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
- package/dist/components/CanvasToolbarPlaceholder.svelte +25 -11
- package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +6 -0
- package/dist/components/boardLoaderDelay.d.ts +39 -0
- package/dist/components/boardLoaderDelay.js +71 -0
- package/dist/components/input-controls/ColorIC.svelte +1 -1
- package/dist/components/input-controls/FontSelectorIC.svelte +1 -1
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +42 -10
- package/dist/engine/EngineCore.js +843 -916
- package/dist/engine/codec/canvasDocument.js +192 -155
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +79 -92
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.d.ts +24 -0
- package/dist/engine/core/brush.js +223 -103
- package/dist/engine/core/cameraMove.js +39 -51
- package/dist/engine/core/defaults.js +92 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.js +38 -55
- 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.js +160 -224
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.d.ts +23 -18
- package/dist/engine/core/shapeTools.js +104 -67
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.d.ts +18 -2
- package/dist/engine/core/style.js +79 -80
- package/dist/engine/core/styleMemory.d.ts +88 -0
- package/dist/engine/core/styleMemory.js +189 -0
- package/dist/engine/core/transform.js +84 -95
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +513 -526
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.d.ts +8 -3
- package/dist/engine/input/gestures.js +378 -346
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +42 -41
- 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.js +6 -17
- 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.js +30 -83
- package/dist/fonts/workspaceFonts.js +33 -92
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/index.d.ts +1 -1
- package/dist/poster/boardPlacement.d.ts +4 -1
- package/dist/poster/boardPlacement.js +34 -33
- package/dist/poster/boardPoster.js +58 -83
- package/dist/poster/paintBoardPoster.js +16 -39
- package/dist/poster/posterPictures.js +27 -43
- package/dist/settings/rememberedStyles.d.ts +22 -0
- package/dist/settings/rememberedStyles.js +47 -0
- package/dist/settings/rememberedTool.d.ts +50 -0
- package/dist/settings/rememberedTool.js +68 -0
- package/dist/settings/workspaceToolSettings.d.ts +25 -0
- package/dist/settings/workspaceToolSettings.js +38 -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 +137 -169
- package/dist/tools/hostAdapter.js +10 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/styleArguments.js +19 -21
- package/dist/tools/types.js +1 -1
- package/package.json +5 -5
|
@@ -11,35 +11,24 @@
|
|
|
11
11
|
* hand, and scaling the line must not change how thick it is where the hand dwelled. Everything here is plain functions over
|
|
12
12
|
* plain records, so the same stroke is drawn in the editor, the screenshot rasterizer and the tests.
|
|
13
13
|
*/
|
|
14
|
-
var __assign = (this && this.__assign) || function () {
|
|
15
|
-
__assign = Object.assign || function(t) {
|
|
16
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
17
|
-
s = arguments[i];
|
|
18
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
19
|
-
t[p] = s[p];
|
|
20
|
-
}
|
|
21
|
-
return t;
|
|
22
|
-
};
|
|
23
|
-
return __assign.apply(this, arguments);
|
|
24
|
-
};
|
|
25
14
|
import { SHAPE_STYLE_DEFAULTS, createObjectId, createPathObject } from './defaults.js';
|
|
26
15
|
import { brushOptions, brushReach, brushSizeFor, circlePathData, outlineOf, outlinePathData, outlineWithPressures, strokePointsOf, strokePointsOfLine } from './freehand.js';
|
|
27
16
|
import { MIN_OBJECT_SIZE } from './types.js';
|
|
28
17
|
/** The most points of a line a stroke keeps; a longer line is thinned further, so a stroke never grows without bound. */
|
|
29
|
-
export
|
|
18
|
+
export const BRUSH_MAX_POINTS = 4000;
|
|
30
19
|
// The points of a stroke closer together than this fraction of the brush (and than a unit of the board) add nothing the outline
|
|
31
20
|
// shows: it leaves out points closer than 0.62 of the brush on both of its sides already.
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
const POINT_GAP_OF_SIZE = 0.3;
|
|
22
|
+
const MINIMUM_POINT_GAP = 0.75;
|
|
34
23
|
// The decimals a stored point keeps: a hundredth of a unit is far finer than a pixel at the zoom that goes up to eight.
|
|
35
|
-
|
|
24
|
+
const COORDINATE_DECIMALS = 2;
|
|
36
25
|
// A line that never leaves this distance (in units of the board) from where it began is a click: a dot at the pointer.
|
|
37
|
-
|
|
26
|
+
const DOT_REACH = 1;
|
|
38
27
|
// The pressure of a dot made with a mouse or a finger: the middle of the range, as a line drawn at an even pressure has.
|
|
39
|
-
|
|
28
|
+
const DOT_PRESSURE = 0.5;
|
|
40
29
|
/** The narrowest width the Width control may give a brush stroke. */
|
|
41
|
-
export
|
|
42
|
-
|
|
30
|
+
export const MIN_BRUSH_WIDTH = 1;
|
|
31
|
+
const roundTo = (value, decimals) => Math.round(value * 10 ** decimals) / 10 ** decimals;
|
|
43
32
|
/** Whether an object is a brush stroke (a path that was drawn with the Draw tool and holds its line). */
|
|
44
33
|
export function isBrushStroke(object) {
|
|
45
34
|
return object.type === 'path' && !!object.ink;
|
|
@@ -53,141 +42,272 @@ export function brushOverflow(shape) {
|
|
|
53
42
|
}
|
|
54
43
|
/** The line a brush stroke follows, in the coordinates of its box (what the shape snap measures). */
|
|
55
44
|
export function brushCentreline(shape) {
|
|
56
|
-
return shape.ink.points.map(
|
|
57
|
-
var x = _a[0], y = _a[1];
|
|
58
|
-
return ({ x: x, y: y });
|
|
59
|
-
});
|
|
45
|
+
return shape.ink.points.map(([x, y]) => ({ x, y }));
|
|
60
46
|
}
|
|
61
47
|
/**
|
|
62
48
|
* The outline of a stored line, as path data: drawn again from the points and the pressures it holds, at the width of the brush.
|
|
63
49
|
* Always something to draw: a line the outline cannot be made of (one point, or none) is a dot.
|
|
64
50
|
*/
|
|
65
51
|
export function brushOutlineData(ink, strokeWidth) {
|
|
66
|
-
|
|
67
|
-
var size = brushSizeFor(strokeWidth);
|
|
52
|
+
const size = brushSizeFor(strokeWidth);
|
|
68
53
|
// The pressure is already in the points, so it is read, whatever it came from.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return ({ x: x, y: y, pressure: pressure });
|
|
73
|
-
}));
|
|
74
|
-
var data = outlinePathData(outlineOf(points, options));
|
|
54
|
+
const options = { ...brushOptions(size, ink.pressure, true), simulatePressure: false };
|
|
55
|
+
const points = strokePointsOfLine(ink.points.map(([x, y, pressure]) => ({ x, y, pressure })));
|
|
56
|
+
const data = outlinePathData(outlineOf(points, options));
|
|
75
57
|
if (data)
|
|
76
58
|
return data;
|
|
77
|
-
|
|
59
|
+
const center = points[0] ?? { x: 0, y: 0 };
|
|
78
60
|
return circlePathData(center, Math.max(0.5, size / 2));
|
|
79
61
|
}
|
|
80
|
-
/**
|
|
81
|
-
|
|
82
|
-
|
|
62
|
+
/**
|
|
63
|
+
* The points of a line that the outline needs: the ones that are a gap away from the one kept before them, the first and the last
|
|
64
|
+
* always. A line with more than `BRUSH_MAX_POINTS` of them is thinned further, with a wider gap.
|
|
65
|
+
*/
|
|
66
|
+
function thinKeptPoints(entries, size) {
|
|
67
|
+
let gap = Math.max(MINIMUM_POINT_GAP, size * POINT_GAP_OF_SIZE);
|
|
83
68
|
for (;;) {
|
|
84
|
-
|
|
85
|
-
for (
|
|
86
|
-
|
|
87
|
-
if (Math.hypot(
|
|
88
|
-
kept.push(
|
|
89
|
-
}
|
|
69
|
+
const kept = [entries[0]];
|
|
70
|
+
for (let index = 1; index < entries.length - 1; index += 1) {
|
|
71
|
+
const previous = kept[kept.length - 1].point;
|
|
72
|
+
if (Math.hypot(entries[index].point.x - previous.x, entries[index].point.y - previous.y) >= gap)
|
|
73
|
+
kept.push(entries[index]);
|
|
90
74
|
}
|
|
91
75
|
// The last point is where the hand ended, whatever came before it.
|
|
92
|
-
if (
|
|
93
|
-
kept.push(
|
|
76
|
+
if (entries.length > 1)
|
|
77
|
+
kept.push(entries[entries.length - 1]);
|
|
94
78
|
if (kept.length <= BRUSH_MAX_POINTS)
|
|
95
79
|
return kept;
|
|
96
80
|
gap *= 1.5;
|
|
97
81
|
}
|
|
98
82
|
}
|
|
83
|
+
/** The points of a smoothed line that the outline needs, with the pressure each was drawn with. */
|
|
84
|
+
function keptPoints(points, pressures, size) {
|
|
85
|
+
return thinKeptPoints(points.map((point, index) => ({ point, pressure: pressures[index] })), size);
|
|
86
|
+
}
|
|
87
|
+
// ──────────────────────────────────────────────────────────── a line that is partly held straight
|
|
88
|
+
/**
|
|
89
|
+
* The pressure a straight line (Shift) is drawn at when the device reports none (a mouse, a finger): the one a stroke drawn by
|
|
90
|
+
* hand settles at as soon as the hand moves at an ordinary pace. The straight line is then as thick as the lines drawn by hand
|
|
91
|
+
* beside it (a width of 3 is about 3 px on the screen, and not the 6.6 px of a pen pressing at its middle), and as thick at one
|
|
92
|
+
* end as at the other.
|
|
93
|
+
*/
|
|
94
|
+
export const STRAIGHT_LINE_PRESSURE = 0;
|
|
95
|
+
// Where a straight stretch meets the line the hand drew, the width eases from the hand's to the stretch's over this many brush
|
|
96
|
+
// widths (or over half of the stretch, when it is shorter): a hand that dwelled before Shift went down was thick there, and the
|
|
97
|
+
// stretch is even, so without the easing the line would step in width at the join.
|
|
98
|
+
const STRAIGHT_BLEND_IN_SIZES = 3;
|
|
99
|
+
// Two points of a joined line this close to each other are one: the end of a piece of the line is where the next one begins.
|
|
100
|
+
const JOIN_DISTANCE = 0.01;
|
|
101
|
+
/** The stretches that can be drawn: in order, one after another, each with a length and a pressure between 0 and 1. */
|
|
102
|
+
function usableStraightSpans(spans, samples) {
|
|
103
|
+
if (!spans || spans.length === 0)
|
|
104
|
+
return [];
|
|
105
|
+
// The indexes of a stretch count the samples as they were given, so a sample that is no point would throw them all off.
|
|
106
|
+
if (samples.some((sample) => !Number.isFinite(sample.x) || !Number.isFinite(sample.y)))
|
|
107
|
+
return [];
|
|
108
|
+
const usable = [];
|
|
109
|
+
let reach = 0;
|
|
110
|
+
for (const span of [...spans].sort((a, b) => a.from - b.from)) {
|
|
111
|
+
if (!Number.isInteger(span.from) || !Number.isInteger(span.to))
|
|
112
|
+
continue;
|
|
113
|
+
// A stretch that begins before the one before it ended, ends before it begins, or ends off the line is left out.
|
|
114
|
+
if (span.from < reach || span.to <= span.from || span.to >= samples.length)
|
|
115
|
+
continue;
|
|
116
|
+
// One with no length draws nothing (Shift pressed and let go on the same spot).
|
|
117
|
+
if (Math.hypot(samples[span.to].x - samples[span.from].x, samples[span.to].y - samples[span.from].y) < DOT_REACH)
|
|
118
|
+
continue;
|
|
119
|
+
const pressure = typeof span.pressure === 'number' && Number.isFinite(span.pressure) ? Math.min(1, Math.max(0, span.pressure)) : STRAIGHT_LINE_PRESSURE;
|
|
120
|
+
usable.push({ from: span.from, to: span.to, pressure });
|
|
121
|
+
reach = span.to;
|
|
122
|
+
}
|
|
123
|
+
return usable;
|
|
124
|
+
}
|
|
125
|
+
/** The samples of a straight stretch: its two ends, and the points where its width has settled to its own pressure. */
|
|
126
|
+
function straightStretchSamples(from, to, startPressure, pressure, endPressure, size) {
|
|
127
|
+
const length = Math.hypot(to.x - from.x, to.y - from.y);
|
|
128
|
+
const blend = Math.min(length / 2, size * STRAIGHT_BLEND_IN_SIZES);
|
|
129
|
+
const alongX = (to.x - from.x) / length;
|
|
130
|
+
const alongY = (to.y - from.y) / length;
|
|
131
|
+
const line = [
|
|
132
|
+
{ x: from.x, y: from.y, pressure: startPressure },
|
|
133
|
+
{ x: from.x + alongX * blend, y: from.y + alongY * blend, pressure }
|
|
134
|
+
];
|
|
135
|
+
// A stretch longer than twice the blend has a second point, where the width starts to ease towards the other end.
|
|
136
|
+
if (length - blend * 2 > JOIN_DISTANCE)
|
|
137
|
+
line.push({ x: to.x - alongX * blend, y: to.y - alongY * blend, pressure });
|
|
138
|
+
line.push({ x: to.x, y: to.y, pressure: endPressure });
|
|
139
|
+
return line;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The line of a stroke that is partly drawn by hand and partly held straight. The hand's pieces are smoothed as ever, each ending
|
|
143
|
+
* exactly where a straight stretch begins and beginning again exactly where one ends; the stretches are exact straight lines in
|
|
144
|
+
* their own even pressure, easing to the width of the hand's piece beside them so that the join has no step. The outline
|
|
145
|
+
* (`brushOutlineData`) rounds the corner where a stretch leaves the hand's line at an angle.
|
|
146
|
+
*/
|
|
147
|
+
function keptOfMixedLine(samples, stretches, size, brushLine, finished) {
|
|
148
|
+
// A piece of the hand's line that never leaves a unit of where it began has no length to draw.
|
|
149
|
+
const hasReach = (piece) => piece.some((sample) => Math.hypot(sample.x - piece[0].x, sample.y - piece[0].y) >= DOT_REACH);
|
|
150
|
+
const pieces = [];
|
|
151
|
+
let cursor = 0;
|
|
152
|
+
for (const stretch of stretches) {
|
|
153
|
+
const handSamples = samples.slice(cursor, stretch.from + 1);
|
|
154
|
+
if (stretch.from > cursor && hasReach(handSamples))
|
|
155
|
+
pieces.push({ kind: 'hand', samples: handSamples });
|
|
156
|
+
pieces.push({ kind: 'straight', from: samples[stretch.from], to: samples[stretch.to], pressure: stretch.pressure });
|
|
157
|
+
cursor = stretch.to;
|
|
158
|
+
}
|
|
159
|
+
const handTail = samples.slice(cursor);
|
|
160
|
+
if (handTail.length > 1 && hasReach(handTail))
|
|
161
|
+
pieces.push({ kind: 'hand', samples: handTail });
|
|
162
|
+
// The hand's pieces first: a straight stretch takes the width at each of its ends from the hand's piece beside it. A piece that
|
|
163
|
+
// a stretch follows ends exactly where the stretch begins; the last piece trails the hand until the line is finished.
|
|
164
|
+
const handKept = new Map();
|
|
165
|
+
pieces.forEach((piece, index) => {
|
|
166
|
+
if (piece.kind !== 'hand')
|
|
167
|
+
return;
|
|
168
|
+
const options = { ...brushLine, last: index < pieces.length - 1 ? true : finished };
|
|
169
|
+
const points = strokePointsOf(piece.samples, options);
|
|
170
|
+
if (points.length === 0)
|
|
171
|
+
return;
|
|
172
|
+
const { pressures } = outlineWithPressures(points, options);
|
|
173
|
+
handKept.set(index, keptPoints(points, pressures, size));
|
|
174
|
+
});
|
|
175
|
+
const stretchOptions = { ...brushLine, streamline: 0, simulatePressure: false, last: true };
|
|
176
|
+
const parts = pieces.map((piece, index) => {
|
|
177
|
+
if (piece.kind === 'hand')
|
|
178
|
+
return handKept.get(index) ?? [];
|
|
179
|
+
const before = handKept.get(index - 1);
|
|
180
|
+
const after = handKept.get(index + 1);
|
|
181
|
+
const startPressure = before && before.length > 0 ? before[before.length - 1].pressure : piece.pressure;
|
|
182
|
+
const endPressure = after && after.length > 0 ? after[0].pressure : piece.pressure;
|
|
183
|
+
const points = strokePointsOf(straightStretchSamples(piece.from, piece.to, startPressure, piece.pressure, endPressure, size), stretchOptions);
|
|
184
|
+
if (points.length === 0)
|
|
185
|
+
return [];
|
|
186
|
+
const { pressures } = outlineWithPressures(points, stretchOptions);
|
|
187
|
+
return keptPoints(points, pressures, size);
|
|
188
|
+
});
|
|
189
|
+
const joined = [];
|
|
190
|
+
for (const part of parts) {
|
|
191
|
+
for (const entry of part) {
|
|
192
|
+
const previous = joined[joined.length - 1];
|
|
193
|
+
// The end of a piece is where the next one begins: one point, and the pressure the earlier piece gave it.
|
|
194
|
+
if (previous && Math.hypot(entry.point.x - previous.point.x, entry.point.y - previous.point.y) < JOIN_DISTANCE)
|
|
195
|
+
continue;
|
|
196
|
+
joined.push(entry);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return joined.length > BRUSH_MAX_POINTS ? thinKeptPoints(joined, size) : joined;
|
|
200
|
+
}
|
|
99
201
|
/**
|
|
100
202
|
* The stroke for the samples of a pointer, in world units: the box of its line, the line itself with its pressure, and the
|
|
101
203
|
* outline to draw. Null when there is nothing to draw.
|
|
102
204
|
*/
|
|
103
|
-
export function buildBrushStroke(samples, options) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
var pressure = (_b = options.pressure) !== null && _b !== void 0 ? _b : 'speed';
|
|
108
|
-
var size = brushSizeFor(strokeWidth);
|
|
205
|
+
export function buildBrushStroke(samples, options = {}) {
|
|
206
|
+
const strokeWidth = options.strokeWidth ?? SHAPE_STYLE_DEFAULTS.inkStrokeWidth;
|
|
207
|
+
const pressure = options.pressure ?? 'speed';
|
|
208
|
+
const size = brushSizeFor(strokeWidth);
|
|
109
209
|
// A line of one pressure reads that pressure at every sample, and is not smoothed (smoothing is what makes a line trail the hand).
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
210
|
+
const steadyPressure = typeof options.steadyPressure === 'number' && Number.isFinite(options.steadyPressure) ? Math.min(1, Math.max(0, options.steadyPressure)) : null;
|
|
211
|
+
const brushLine = brushOptions(size, pressure, options.finished === true);
|
|
212
|
+
const lineOptions = steadyPressure === null ? brushLine : { ...brushLine, streamline: 0, simulatePressure: false };
|
|
213
|
+
// Stretches held straight: a line that is one of them from end to end is a straight line (its two ends, one pressure, as above);
|
|
214
|
+
// one that is only partly straight is drawn piece by piece. A line that is made straight as a whole leaves them out.
|
|
215
|
+
const stretches = steadyPressure === null ? usableStraightSpans(options.straightSpans, samples) : [];
|
|
216
|
+
const isStraightEndToEnd = stretches.length === 1 && stretches[0].from === 0 && stretches[0].to === samples.length - 1;
|
|
217
|
+
const straightPressure = isStraightEndToEnd ? stretches[0].pressure : steadyPressure;
|
|
218
|
+
const lineSamples = isStraightEndToEnd ? [samples[0], samples[samples.length - 1]] : samples;
|
|
219
|
+
const wholeLineOptions = isStraightEndToEnd ? { ...brushLine, streamline: 0, simulatePressure: false } : lineOptions;
|
|
220
|
+
const usable = lineSamples
|
|
221
|
+
.filter((sample) => Number.isFinite(sample.x) && Number.isFinite(sample.y))
|
|
222
|
+
.map((sample) => (straightPressure === null ? sample : { x: sample.x, y: sample.y, pressure: straightPressure }));
|
|
116
223
|
if (usable.length === 0)
|
|
117
224
|
return null;
|
|
118
|
-
|
|
119
|
-
if (usable.every(
|
|
225
|
+
let kept;
|
|
226
|
+
if (usable.every((sample) => Math.hypot(sample.x - usable[0].x, sample.y - usable[0].y) < DOT_REACH)) {
|
|
120
227
|
// A click is a dot where the pointer went down: the line has one point, so nothing is drawn a pixel away from it.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
kept = [{ point
|
|
228
|
+
const penPressure = usable.reduce((most, sample) => Math.max(most, sample.pressure ?? 0), 0);
|
|
229
|
+
const dotPressure = pressure === 'pen' && penPressure > 0 ? Math.min(1, penPressure) : DOT_PRESSURE;
|
|
230
|
+
const point = { x: usable[0].x, y: usable[0].y, pressure: dotPressure, vectorX: 0, vectorY: 0, distance: 0, runningLength: 0 };
|
|
231
|
+
kept = [{ point, pressure: dotPressure }];
|
|
232
|
+
}
|
|
233
|
+
else if (stretches.length > 0 && !isStraightEndToEnd) {
|
|
234
|
+
kept = keptOfMixedLine(samples, stretches, size, brushLine, options.finished === true);
|
|
235
|
+
if (kept.length === 0)
|
|
236
|
+
return null;
|
|
125
237
|
}
|
|
126
238
|
else {
|
|
127
|
-
|
|
239
|
+
const smoothed = strokePointsOf(usable, wholeLineOptions);
|
|
128
240
|
// The pass that works the pressure out (from the speed, or read from the pen) so that it can be kept.
|
|
129
|
-
|
|
241
|
+
const { pressures } = outlineWithPressures(smoothed, wholeLineOptions);
|
|
130
242
|
kept = keptPoints(smoothed, pressures, size);
|
|
131
243
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
for (
|
|
137
|
-
var point = kept_1[_i].point;
|
|
244
|
+
let minX = Infinity;
|
|
245
|
+
let minY = Infinity;
|
|
246
|
+
let maxX = -Infinity;
|
|
247
|
+
let maxY = -Infinity;
|
|
248
|
+
for (const { point } of kept) {
|
|
138
249
|
minX = Math.min(minX, point.x);
|
|
139
250
|
minY = Math.min(minY, point.y);
|
|
140
251
|
maxX = Math.max(maxX, point.x);
|
|
141
252
|
maxY = Math.max(maxY, point.y);
|
|
142
253
|
}
|
|
143
254
|
// A line that is flat on one axis (a horizontal line, a dot) gets a box one unit thick, with the line in its middle.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
points: kept.map(
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
];
|
|
158
|
-
}),
|
|
159
|
-
pressure: pressure
|
|
255
|
+
const flatX = maxX - minX < MIN_OBJECT_SIZE;
|
|
256
|
+
const flatY = maxY - minY < MIN_OBJECT_SIZE;
|
|
257
|
+
const boxX = flatX ? (minX + maxX) / 2 - MIN_OBJECT_SIZE / 2 : minX;
|
|
258
|
+
const boxY = flatY ? (minY + maxY) / 2 - MIN_OBJECT_SIZE / 2 : minY;
|
|
259
|
+
const width = flatX ? MIN_OBJECT_SIZE : maxX - minX;
|
|
260
|
+
const height = flatY ? MIN_OBJECT_SIZE : maxY - minY;
|
|
261
|
+
const ink = {
|
|
262
|
+
points: kept.map(({ point, pressure: pointPressure }) => [
|
|
263
|
+
roundTo(point.x - boxX, COORDINATE_DECIMALS),
|
|
264
|
+
roundTo(point.y - boxY, COORDINATE_DECIMALS),
|
|
265
|
+
roundTo(pointPressure, COORDINATE_DECIMALS)
|
|
266
|
+
]),
|
|
267
|
+
pressure
|
|
160
268
|
};
|
|
161
|
-
|
|
269
|
+
const createId = options.createId ?? createObjectId;
|
|
162
270
|
// The whole line shows through as much as it was asked to; a line that was not asked anything is fully opaque, as an object is.
|
|
163
|
-
|
|
164
|
-
return createPathObject(
|
|
271
|
+
const opacity = typeof options.opacity === 'number' && Number.isFinite(options.opacity) ? Math.min(1, Math.max(0, options.opacity)) : null;
|
|
272
|
+
return createPathObject({
|
|
273
|
+
id: createId('obj'),
|
|
274
|
+
x: boxX,
|
|
275
|
+
y: boxY,
|
|
276
|
+
width,
|
|
277
|
+
height,
|
|
278
|
+
d: brushOutlineData(ink, strokeWidth),
|
|
279
|
+
shape: null,
|
|
165
280
|
// The brush is filled: its colour is the fill of the outline, and the outline has no stroke of its own.
|
|
166
|
-
fill:
|
|
281
|
+
fill: options.color ?? SHAPE_STYLE_DEFAULTS.inkStroke,
|
|
282
|
+
fillOpacity: 1,
|
|
283
|
+
stroke: 'transparent',
|
|
284
|
+
strokeWidth,
|
|
285
|
+
lineCap: 'round',
|
|
286
|
+
lineJoin: 'round',
|
|
287
|
+
...(opacity === null ? {} : { opacity }),
|
|
288
|
+
ink
|
|
289
|
+
});
|
|
167
290
|
}
|
|
168
291
|
/**
|
|
169
292
|
* The stroke with its line scaled (and mirrored) into a new box and its outline drawn again at the brush width: the line goes
|
|
170
293
|
* where the box goes, the brush stays as wide as it was. Only `d`, `ink`, the size of the box and `strokeWidth` are written;
|
|
171
294
|
* where the box is, and how it is turned, is the caller's.
|
|
172
295
|
*/
|
|
173
|
-
export function reshapeBrushStroke(shape, change) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
var x = _a[0], y = _a[1], pressure = _a[2];
|
|
183
|
-
var scaledX = x * scaleX;
|
|
184
|
-
var scaledY = y * scaleY;
|
|
296
|
+
export function reshapeBrushStroke(shape, change = {}) {
|
|
297
|
+
const width = Math.max(MIN_OBJECT_SIZE, change.width ?? shape.width);
|
|
298
|
+
const height = Math.max(MIN_OBJECT_SIZE, change.height ?? shape.height);
|
|
299
|
+
const strokeWidth = change.strokeWidth ?? shape.strokeWidth;
|
|
300
|
+
const scaleX = shape.width > 0 ? width / shape.width : 1;
|
|
301
|
+
const scaleY = shape.height > 0 ? height / shape.height : 1;
|
|
302
|
+
const points = shape.ink.points.map(([x, y, pressure]) => {
|
|
303
|
+
const scaledX = x * scaleX;
|
|
304
|
+
const scaledY = y * scaleY;
|
|
185
305
|
return [
|
|
186
306
|
roundTo(change.flipX ? width - scaledX : scaledX, COORDINATE_DECIMALS),
|
|
187
307
|
roundTo(change.flipY ? height - scaledY : scaledY, COORDINATE_DECIMALS),
|
|
188
308
|
pressure
|
|
189
309
|
];
|
|
190
310
|
});
|
|
191
|
-
|
|
192
|
-
return
|
|
311
|
+
const ink = { points, pressure: shape.ink.pressure };
|
|
312
|
+
return { ...shape, width, height, strokeWidth, ink, d: brushOutlineData(ink, strokeWidth) };
|
|
193
313
|
}
|
|
@@ -20,44 +20,33 @@
|
|
|
20
20
|
*
|
|
21
21
|
* Nothing here knows the clock or the page: the engine asks for the view at a progress, and a test asks for the same.
|
|
22
22
|
*/
|
|
23
|
-
var __assign = (this && this.__assign) || function () {
|
|
24
|
-
__assign = Object.assign || function(t) {
|
|
25
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
26
|
-
s = arguments[i];
|
|
27
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
28
|
-
t[p] = s[p];
|
|
29
|
-
}
|
|
30
|
-
return t;
|
|
31
|
-
};
|
|
32
|
-
return __assign.apply(this, arguments);
|
|
33
|
-
};
|
|
34
23
|
import { clampNumber } from './geometry.js';
|
|
35
24
|
import { clampZoom, screenToWorld } from './viewport.js';
|
|
36
25
|
/** The shape a move has when nobody chose one: the position and the zoom ease together. */
|
|
37
|
-
export
|
|
26
|
+
export const DEFAULT_CAMERA_PATH_SHAPE = 'ease';
|
|
38
27
|
/** The shortest a move lasts when nobody gave its length, in milliseconds: a shorter one is too quick for the eye to follow. */
|
|
39
|
-
export
|
|
28
|
+
export const CAMERA_MOVE_MIN_DURATION_MS = 380;
|
|
40
29
|
/** The longest a move lasts when nobody gave its length, in milliseconds: a long way across the board still takes under a second. */
|
|
41
|
-
export
|
|
30
|
+
export const CAMERA_MOVE_MAX_DURATION_MS = 1000;
|
|
42
31
|
/** The time for each unit of the length of a path, in milliseconds. */
|
|
43
|
-
export
|
|
32
|
+
export const CAMERA_MOVE_MS_PER_UNIT = 800;
|
|
44
33
|
/**
|
|
45
34
|
* The share of the time a move takes that a quick move takes (`durationScale` of the move options). The double click on the empty
|
|
46
35
|
* board and Ctrl/Cmd+0 use it: they answer something the person has just done and should feel brisk, while a move a page asks for
|
|
47
36
|
* keeps the time its length gives.
|
|
48
37
|
*/
|
|
49
|
-
export
|
|
38
|
+
export const QUICK_CAMERA_MOVE_SCALE = 0.65;
|
|
50
39
|
/**
|
|
51
40
|
* How much the path zooms out on its way when the move is long (ρ in the paper). The square root of 2 is the value its authors
|
|
52
41
|
* found the most pleasing: a larger one zooms out more, and 0 would be a straight line.
|
|
53
42
|
*/
|
|
54
|
-
|
|
43
|
+
const PATH_CURVATURE = Math.SQRT2;
|
|
55
44
|
/** A move whose centres are closer than this fraction of the narrower view is a plain zoom: there is nothing to pan. */
|
|
56
|
-
|
|
45
|
+
const PLAIN_ZOOM_DISTANCE_RATIO = 1e-6;
|
|
57
46
|
/** Two viewports less than this many pixels apart look the same to a person. */
|
|
58
|
-
|
|
47
|
+
const SAME_VIEWPORT_PIXELS = 0.5;
|
|
59
48
|
/** Two zooms that differ by less than this fraction look the same to a person. */
|
|
60
|
-
|
|
49
|
+
const SAME_ZOOM_RATIO = 1e-4;
|
|
61
50
|
/** The view that a viewport shows in a board element of `viewSize`. */
|
|
62
51
|
export function cameraOfViewport(viewport, viewSize) {
|
|
63
52
|
return { center: screenToWorld(viewport, { x: viewSize.width / 2, y: viewSize.height / 2 }), zoom: viewport.zoom };
|
|
@@ -78,14 +67,14 @@ export function viewportsMatch(first, second) {
|
|
|
78
67
|
}
|
|
79
68
|
/** Slow start, quick middle, slow end: the way a person expects a camera to move. `progress` and the answer are between 0 and 1. */
|
|
80
69
|
export function easeInOutCubic(progress) {
|
|
81
|
-
|
|
82
|
-
return clampedProgress < 0.5 ? 4 *
|
|
70
|
+
const clampedProgress = clampNumber(progress, 0, 1);
|
|
71
|
+
return clampedProgress < 0.5 ? 4 * clampedProgress ** 3 : 1 - (-2 * clampedProgress + 2) ** 3 / 2;
|
|
83
72
|
}
|
|
84
73
|
function isUsableView(view) {
|
|
85
74
|
return Number.isFinite(view.center.x) && Number.isFinite(view.center.y) && Number.isFinite(view.zoom) && view.zoom > 0;
|
|
86
75
|
}
|
|
87
76
|
/** A zoom ratio this close to 1 (its logarithm is this close to 0) is no zoom: the centre then goes by the plain share of the progress. */
|
|
88
|
-
|
|
77
|
+
const NO_ZOOM_LOGARITHM = 1e-9;
|
|
89
78
|
/**
|
|
90
79
|
* The view between two views with no detour: the zoom changes by the same factor at every step, and the centre goes along the
|
|
91
80
|
* straight line between the two centres, the way it does when a person zooms about one fixed point of the board and pans nothing
|
|
@@ -98,8 +87,8 @@ var NO_ZOOM_LOGARITHM = 1e-9;
|
|
|
98
87
|
* screen, as the zoom goes on; with no change of zoom it is `progress` itself, and zooming out it is the same formula.
|
|
99
88
|
*/
|
|
100
89
|
function straightView(from, to, progress) {
|
|
101
|
-
|
|
102
|
-
|
|
90
|
+
const zoomLogarithm = Math.log(to.zoom / from.zoom);
|
|
91
|
+
const lineShare = Math.abs(zoomLogarithm) < NO_ZOOM_LOGARITHM ? progress : Math.expm1(-progress * zoomLogarithm) / Math.expm1(-zoomLogarithm);
|
|
103
92
|
return {
|
|
104
93
|
center: { x: from.center.x + (to.center.x - from.center.x) * lineShare, y: from.center.y + (to.center.y - from.center.y) * lineShare },
|
|
105
94
|
zoom: clampZoom(from.zoom * Math.exp(zoomLogarithm * progress))
|
|
@@ -112,54 +101,53 @@ function straightView(from, to, progress) {
|
|
|
112
101
|
* `shape` is how the view goes (see `CameraPathShape`): by default the position and the zoom ease together, and `'fly'` takes the
|
|
113
102
|
* path of van Wijk and Nuij. How long the move lasts comes from the length of that path whichever shape is asked for.
|
|
114
103
|
*/
|
|
115
|
-
export function createCameraPath(from, to, viewSize, shape) {
|
|
116
|
-
if (shape === void 0) { shape = DEFAULT_CAMERA_PATH_SHAPE; }
|
|
104
|
+
export function createCameraPath(from, to, viewSize, shape = DEFAULT_CAMERA_PATH_SHAPE) {
|
|
117
105
|
// The path measures a view by the width of the world it shows along the longer side of the board.
|
|
118
|
-
|
|
106
|
+
const extent = Math.max(viewSize.width, viewSize.height);
|
|
119
107
|
if (!(extent > 0) || !isUsableView(from) || !isUsableView(to))
|
|
120
108
|
return null;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
109
|
+
const startWidth = extent / from.zoom;
|
|
110
|
+
const endWidth = extent / to.zoom;
|
|
111
|
+
const deltaX = to.center.x - from.center.x;
|
|
112
|
+
const deltaY = to.center.y - from.center.y;
|
|
113
|
+
const distance = Math.hypot(deltaX, deltaY);
|
|
114
|
+
let length;
|
|
115
|
+
let sampleAt;
|
|
128
116
|
if (distance < Math.min(startWidth, endWidth) * PLAIN_ZOOM_DISTANCE_RATIO) {
|
|
129
117
|
// Nothing to pan: a plain zoom, by the same factor at every step.
|
|
130
118
|
length = Math.log(endWidth / startWidth) / PATH_CURVATURE;
|
|
131
|
-
sampleAt =
|
|
119
|
+
sampleAt = (progress) => ({ fraction: progress, width: startWidth * Math.exp(PATH_CURVATURE * length * progress) });
|
|
132
120
|
}
|
|
133
121
|
else {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
122
|
+
const curvatureSquared = PATH_CURVATURE ** 2;
|
|
123
|
+
const widthGrowth = endWidth ** 2 - startWidth ** 2;
|
|
124
|
+
const panPart = curvatureSquared ** 2 * distance ** 2;
|
|
137
125
|
// `asinh` rather than the logarithm of the square root: for a view that zooms in a long way with a short pan, the
|
|
138
126
|
// difference of two nearly equal numbers would come out as 0.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
length = (endAngle -
|
|
142
|
-
sampleAt =
|
|
143
|
-
|
|
127
|
+
const startAngle = -Math.asinh((widthGrowth + panPart) / (2 * startWidth * curvatureSquared * distance));
|
|
128
|
+
const endAngle = -Math.asinh((widthGrowth - panPart) / (2 * endWidth * curvatureSquared * distance));
|
|
129
|
+
length = (endAngle - startAngle) / PATH_CURVATURE;
|
|
130
|
+
sampleAt = (progress) => {
|
|
131
|
+
const along = PATH_CURVATURE * length * progress + startAngle;
|
|
144
132
|
return {
|
|
145
|
-
fraction: (startWidth / (
|
|
146
|
-
width: (startWidth * Math.cosh(
|
|
133
|
+
fraction: (startWidth / (curvatureSquared * distance)) * (Math.cosh(startAngle) * Math.tanh(along) - Math.sinh(startAngle)),
|
|
134
|
+
width: (startWidth * Math.cosh(startAngle)) / Math.cosh(along)
|
|
147
135
|
};
|
|
148
136
|
};
|
|
149
137
|
}
|
|
150
|
-
|
|
138
|
+
const pathLength = Number.isFinite(length) ? Math.abs(length) : 1;
|
|
151
139
|
return {
|
|
152
140
|
length: pathLength,
|
|
153
141
|
duration: clampNumber(pathLength * CAMERA_MOVE_MS_PER_UNIT, CAMERA_MOVE_MIN_DURATION_MS, CAMERA_MOVE_MAX_DURATION_MS),
|
|
154
|
-
at
|
|
142
|
+
at(progress) {
|
|
155
143
|
if (!Number.isFinite(progress) || progress <= 0)
|
|
156
|
-
return { center:
|
|
144
|
+
return { center: { ...from.center }, zoom: from.zoom };
|
|
157
145
|
if (progress >= 1)
|
|
158
|
-
return { center:
|
|
146
|
+
return { center: { ...to.center }, zoom: to.zoom };
|
|
159
147
|
// No detour: the centre and the zoom change at the same rate on the one progress, and arrive together.
|
|
160
148
|
if (shape !== 'fly')
|
|
161
149
|
return straightView(from, to, progress);
|
|
162
|
-
|
|
150
|
+
const sample = sampleAt(progress);
|
|
163
151
|
// A path that came out as something that is not a number (a move across a distance no board has) is walked in a straight line.
|
|
164
152
|
if (!Number.isFinite(sample.fraction) || !(sample.width > 0) || !Number.isFinite(sample.width))
|
|
165
153
|
return straightView(from, to, progress);
|