@widgetic/canvas 0.5.46 → 0.5.48
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 +4 -1
- package/dist/canvas/DomCanvas.svelte +5 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +1 -0
- package/dist/canvas/eventScope.js +7 -1
- package/dist/canvas/host/CanvasHost.d.ts +5 -3
- package/dist/canvas/host/CanvasHost.js +114 -74
- package/dist/canvas/host/pauseCollector.d.ts +18 -0
- package/dist/canvas/host/pauseCollector.js +45 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +40 -3
- package/dist/canvas/ui/CanvasHint.svelte +10 -9
- package/dist/canvas/ui/CanvasToolbar.svelte +17 -14
- 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/floatingCard.d.ts +28 -8
- package/dist/canvas/ui/floatingCard.js +28 -8
- package/dist/canvas/ui/selectionTraits.d.ts +8 -4
- package/dist/canvas/ui/selectionTraits.js +10 -18
- package/dist/canvas/view/SelectionOverlay.svelte +5 -5
- package/dist/canvas/view/WidgetChromeLayer.svelte +10 -2
- package/dist/canvas/view/geometry.d.ts +1 -1
- package/dist/canvas/view/geometry.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +1 -1
- package/dist/components/input-controls/FontSelectorIC.svelte +1 -1
- package/dist/engine/EngineCore.d.ts +33 -8
- package/dist/engine/EngineCore.js +66 -30
- package/dist/engine/core/brush.d.ts +24 -0
- package/dist/engine/core/brush.js +145 -13
- package/dist/engine/core/shapeTools.d.ts +23 -18
- package/dist/engine/core/shapeTools.js +45 -17
- package/dist/engine/core/style.d.ts +18 -2
- package/dist/engine/core/style.js +57 -25
- package/dist/engine/core/styleMemory.d.ts +88 -0
- package/dist/engine/core/styleMemory.js +189 -0
- package/dist/engine/input/gestures.d.ts +8 -3
- package/dist/engine/input/gestures.js +123 -48
- package/dist/engine/input/keyboard.js +14 -5
- package/dist/poster/boardPlacement.d.ts +4 -1
- package/dist/poster/boardPlacement.js +6 -3
- package/dist/settings/rememberedStyles.d.ts +22 -0
- package/dist/settings/rememberedStyles.js +47 -0
- package/dist/settings/rememberedTool.js +2 -1
- package/dist/settings/workspaceToolSettings.d.ts +17 -0
- package/dist/settings/workspaceToolSettings.js +4 -1
- package/dist/tools/hostAdapter.js +0 -1
- package/package.json +4 -4
|
@@ -22,6 +22,7 @@ import { addObjectsBehindWidgetCards, boardVisualBounds, boundsOfObjects, duplic
|
|
|
22
22
|
import { MIN_BRUSH_WIDTH } from './core/brush.js';
|
|
23
23
|
import { clampNumber } from './core/geometry.js';
|
|
24
24
|
import { MAX_STROKE_WIDTH, applyStylePatch } from './core/style.js';
|
|
25
|
+
import { NO_REMEMBERED_STYLES, rememberStyleEdits, sanitizeRememberedStyles, styleEditOf, withRememberedStyle } from './core/styleMemory.js';
|
|
25
26
|
import { Scribble } from './core/scribble.js';
|
|
26
27
|
import { selectionFrameOf } from './core/transform.js';
|
|
27
28
|
import { INITIAL_VIEWPORT, clampZoom, fitViewportToRect, nextZoomStep, panViewport, sanitizeViewport, screenToWorld, visibleWorldRect, zoomAtPoint } from './core/viewport.js';
|
|
@@ -72,8 +73,12 @@ export class EngineCore {
|
|
|
72
73
|
#textSession = null;
|
|
73
74
|
/** The last font family chosen in the panel; new text objects start with this instead of the catalog default. */
|
|
74
75
|
#preferredTextFontFamily = null;
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
/**
|
|
77
|
+
* What the person last chose for each kind of element (a shape, a line icon, an arrow, a text): the next element of the kind starts
|
|
78
|
+
* with it. Set by every restyle of the selection (`setStyle`) and by the host when it opens the workspace (`setRememberedStyles`).
|
|
79
|
+
* The pen of the Draw tool is not here: it is `EngineState.penStyle`.
|
|
80
|
+
*/
|
|
81
|
+
#rememberedStyles = NO_REMEMBERED_STYLES;
|
|
77
82
|
// The eraser's trail while it lives; what the views draw of it is `state.scribble`.
|
|
78
83
|
#scribble = null;
|
|
79
84
|
#indexes = new WeakMap();
|
|
@@ -861,15 +866,48 @@ export class EngineCore {
|
|
|
861
866
|
setPreferredTextFontFamily(fontFamily) {
|
|
862
867
|
this.#preferredTextFontFamily = fontFamily;
|
|
863
868
|
}
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
+
/**
|
|
870
|
+
* What the person last chose for each kind of element: the next shape, line icon, arrow or text starts with it, laid over the look it
|
|
871
|
+
* would start with (see `core/styleMemory.ts`). It is what the panel was used for, in this session and, when the host loaded it
|
|
872
|
+
* (`setRememberedStyles`), in the sessions before. The pen of the Draw tool is `state.penStyle`.
|
|
873
|
+
*/
|
|
874
|
+
get rememberedStyles() {
|
|
875
|
+
return this.#rememberedStyles;
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Loads what was kept for a workspace (the host does, when the board is opened in one), instead of what the engine has: a kind that
|
|
879
|
+
* the storage does not hold goes back to nothing remembered. What the storage holds is checked key by key (a value that is not one of
|
|
880
|
+
* the key is dropped, a number is brought into its range), so a damaged or an older entry cannot give a shape a look it cannot have.
|
|
881
|
+
* Not an edit of the person: nobody is told (`onStyleRemembered`) and nothing is saved because of it.
|
|
882
|
+
*/
|
|
883
|
+
setRememberedStyles(stored) {
|
|
884
|
+
this.#rememberedStyles = sanitizeRememberedStyles(stored);
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Takes in what restyling the selection says about the next element of each kind (`setStyle` calls it for every change, a drag of a
|
|
888
|
+
* slider included, and for a value the selection has already). The pen is the pen of the Draw tool: an edit of a line drawn by hand
|
|
889
|
+
* is an edit of the pen, so that the next line is drawn the way the last one was left. The host is told which kinds came out
|
|
890
|
+
* different, once for each change.
|
|
891
|
+
*/
|
|
892
|
+
#rememberStyleEdits(edits) {
|
|
893
|
+
if (edits.length === 0)
|
|
894
|
+
return;
|
|
895
|
+
const { styles, changed } = rememberStyleEdits(this.#rememberedStyles, edits);
|
|
896
|
+
this.#rememberedStyles = styles;
|
|
897
|
+
const kinds = [...changed];
|
|
898
|
+
const penEdit = {};
|
|
899
|
+
for (const { kind, style } of edits)
|
|
900
|
+
if (kind === 'pen')
|
|
901
|
+
Object.assign(penEdit, style);
|
|
902
|
+
if (Object.keys(penEdit).length > 0 && this.setPenStyle({ color: penEdit.stroke, width: penEdit.strokeWidth, opacity: penEdit.opacity }))
|
|
903
|
+
kinds.push('pen');
|
|
904
|
+
if (kinds.length === 0)
|
|
905
|
+
return;
|
|
906
|
+
this.#callHook(() => this.hooks.onStyleRemembered?.(kinds), 'style memory hook');
|
|
869
907
|
}
|
|
870
908
|
/**
|
|
871
909
|
* Changes how the Draw tool draws: the lines drawn from now on take the colour, the width and the opacity that are asked for. A key
|
|
872
|
-
* that is left out stays as it was; a value that cannot be a line's (a width under 1 or above
|
|
910
|
+
* that is left out stays as it was; a value that cannot be a line's (a width under 1 or above 50, an opacity out of range, a
|
|
873
911
|
* colour that is not a word, a number that is not a number) is brought to what a line can be, or ignored when it is not a value
|
|
874
912
|
* at all. Nothing on the board changes (that is `setStyle`, for the selection), and nothing is added to the history. Returns
|
|
875
913
|
* whether the pen came out different.
|
|
@@ -884,20 +922,6 @@ export class EngineCore {
|
|
|
884
922
|
this.#set({ penStyle: Object.freeze({ color, width, opacity }) });
|
|
885
923
|
return true;
|
|
886
924
|
}
|
|
887
|
-
workspacePaintForNewShape(wireframe) {
|
|
888
|
-
if (!this.#workspacePaintDefaults)
|
|
889
|
-
return {};
|
|
890
|
-
if (wireframe) {
|
|
891
|
-
const { stroke, strokeWidth, strokeStyle, opacity } = this.#workspacePaintDefaults;
|
|
892
|
-
return {
|
|
893
|
-
...(stroke !== undefined ? { stroke } : {}),
|
|
894
|
-
...(strokeWidth !== undefined ? { strokeWidth } : {}),
|
|
895
|
-
...(strokeStyle !== undefined ? { strokeStyle } : {}),
|
|
896
|
-
...(opacity !== undefined ? { opacity } : {})
|
|
897
|
-
};
|
|
898
|
-
}
|
|
899
|
-
return { ...this.#workspacePaintDefaults };
|
|
900
|
-
}
|
|
901
925
|
/**
|
|
902
926
|
* Makes a text object, puts it on the board and starts editing it. A click makes a label whose width follows its
|
|
903
927
|
* text; a width makes a box whose lines wrap. Nothing is recorded until the editing ends with text in it.
|
|
@@ -915,10 +939,11 @@ export class EngineCore {
|
|
|
915
939
|
y: point.y,
|
|
916
940
|
width: fixedWidth ? options.width : TEXT_DEFAULTS.minimumWidth,
|
|
917
941
|
autoWidth: !fixedWidth,
|
|
918
|
-
fontFamily: this.#preferredTextFontFamily ??
|
|
919
|
-
...this.#workspaceTextDefaults
|
|
942
|
+
fontFamily: this.#preferredTextFontFamily ?? TEXT_DEFAULTS.fontFamily
|
|
920
943
|
});
|
|
921
|
-
|
|
944
|
+
// What the person last chose for a text (the font, the size, bold, italic, underline, the alignment, the colour of the letters,
|
|
945
|
+
// the opacity) is laid over what a text starts with; the box is laid out below for the size it comes out at.
|
|
946
|
+
const fitted = this.#fitText(withRememberedStyle(base, this.#rememberedStyles.text));
|
|
922
947
|
// The placeholder is centred on the point it is asked for, whatever its size (a bigger font makes a bigger prompt).
|
|
923
948
|
const text = options.placeholder ? { ...fitted, x: point.x - fitted.width / 2, y: point.y - fitted.height / 2 } : fitted;
|
|
924
949
|
const sceneBefore = this.#state.scene;
|
|
@@ -1002,24 +1027,35 @@ export class EngineCore {
|
|
|
1002
1027
|
return kept;
|
|
1003
1028
|
}
|
|
1004
1029
|
// ──────────────────────────────────────────────────────── commands on the selection
|
|
1005
|
-
/**
|
|
1030
|
+
/**
|
|
1031
|
+
* Changes how the selection looks. `commit: false` is for a slider that is still being dragged: call `commit()` when it ends.
|
|
1032
|
+
*
|
|
1033
|
+
* What the person chose is also what the next element of each kind in the selection starts with, until they choose again (a
|
|
1034
|
+
* rectangle's fill, stroke and width are the next rectangle's, the next ellipse's, the next star's; an arrow's colour is the next
|
|
1035
|
+
* arrow's; a text's size is the next text's; the colour of a line drawn by hand is the pen's). It is kept for every change, a drag
|
|
1036
|
+
* of a slider included and a value the selection has already, and the host is told (`onStyleRemembered`). See `core/styleMemory.ts`.
|
|
1037
|
+
*/
|
|
1006
1038
|
setStyle(patch, options = {}) {
|
|
1007
1039
|
const scene = this.#state.scene;
|
|
1008
1040
|
const targetIds = withDescendantIds(scene, this.#state.selectedIds);
|
|
1009
1041
|
let changed = false;
|
|
1042
|
+
const edits = [];
|
|
1010
1043
|
const next = scene.map((object) => {
|
|
1011
1044
|
if (!targetIds.has(object.id) || object.type === 'group')
|
|
1012
1045
|
return object;
|
|
1046
|
+
const edit = styleEditOf(object, patch);
|
|
1047
|
+
if (edit)
|
|
1048
|
+
edits.push(edit);
|
|
1013
1049
|
const styled = applyStylePatch(object, patch);
|
|
1014
1050
|
if (styled === object)
|
|
1015
1051
|
return object;
|
|
1016
1052
|
changed = true;
|
|
1017
1053
|
return this.#fitText(styled);
|
|
1018
1054
|
});
|
|
1019
|
-
if (
|
|
1020
|
-
|
|
1021
|
-
this
|
|
1022
|
-
return
|
|
1055
|
+
if (changed)
|
|
1056
|
+
this.applyScene(next, options.commit === false ? 'transient' : 'push');
|
|
1057
|
+
this.#rememberStyleEdits(edits);
|
|
1058
|
+
return changed;
|
|
1023
1059
|
}
|
|
1024
1060
|
/**
|
|
1025
1061
|
* Turns the one selected shape back upright, about its own middle (a double click on the turning dot). One undo step.
|
|
@@ -35,6 +35,22 @@ export declare function brushCentreline(shape: BrushPath): Point[];
|
|
|
35
35
|
* Always something to draw: a line the outline cannot be made of (one point, or none) is a dot.
|
|
36
36
|
*/
|
|
37
37
|
export declare function brushOutlineData(ink: BrushInk, strokeWidth: number): string;
|
|
38
|
+
/**
|
|
39
|
+
* The pressure a straight line (Shift) is drawn at when the device reports none (a mouse, a finger): the one a stroke drawn by
|
|
40
|
+
* 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
|
|
41
|
+
* 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
|
|
42
|
+
* end as at the other.
|
|
43
|
+
*/
|
|
44
|
+
export declare const STRAIGHT_LINE_PRESSURE = 0;
|
|
45
|
+
/** A stretch of a stroke's line held straight (Shift held while drawing): a straight line of one even width between two samples. */
|
|
46
|
+
export interface BrushStraightSpan {
|
|
47
|
+
/** The index of the sample the stretch begins at: where the pointer was when Shift went down. */
|
|
48
|
+
from: number;
|
|
49
|
+
/** The index of the sample the stretch ends at: where the pointer was when Shift went up, or where it is now. */
|
|
50
|
+
to: number;
|
|
51
|
+
/** The pressure (0 to 1) it is drawn at all along; `STRAIGHT_LINE_PRESSURE` when it is not given. */
|
|
52
|
+
pressure?: number;
|
|
53
|
+
}
|
|
38
54
|
export interface BrushStrokeOptions {
|
|
39
55
|
createId?: (prefix: string) => ObjectId;
|
|
40
56
|
/** The colour of the line. */
|
|
@@ -53,6 +69,14 @@ export interface BrushStrokeOptions {
|
|
|
53
69
|
* the hand even while it is still being drawn (the first and the last sample are the two ends it is drawn between).
|
|
54
70
|
*/
|
|
55
71
|
steadyPressure?: number;
|
|
72
|
+
/**
|
|
73
|
+
* The stretches of the line held straight (Shift held while drawing), each between two of the samples (by index). A stretch is
|
|
74
|
+
* drawn as an exact straight line of one even width; what lies between the stretches is the hand's, smoothed as ever, and ends
|
|
75
|
+
* on the sample where a stretch begins and begins again where it ends. A line that is a single stretch from its first sample to
|
|
76
|
+
* its last is a straight line like the one `steadyPressure` draws. Stretches that overlap or have no length are left out, and
|
|
77
|
+
* so are all of them when `steadyPressure` is given (it makes the whole line straight).
|
|
78
|
+
*/
|
|
79
|
+
straightSpans?: readonly BrushStraightSpan[];
|
|
56
80
|
}
|
|
57
81
|
/**
|
|
58
82
|
* The stroke for the samples of a pointer, in world units: the box of its line, the line itself with its pressure, and the
|
|
@@ -59,25 +59,145 @@ export function brushOutlineData(ink, strokeWidth) {
|
|
|
59
59
|
const center = points[0] ?? { x: 0, y: 0 };
|
|
60
60
|
return circlePathData(center, Math.max(0.5, size / 2));
|
|
61
61
|
}
|
|
62
|
-
/**
|
|
63
|
-
|
|
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) {
|
|
64
67
|
let gap = Math.max(MINIMUM_POINT_GAP, size * POINT_GAP_OF_SIZE);
|
|
65
68
|
for (;;) {
|
|
66
|
-
const kept = [
|
|
67
|
-
for (let index = 1; index <
|
|
69
|
+
const kept = [entries[0]];
|
|
70
|
+
for (let index = 1; index < entries.length - 1; index += 1) {
|
|
68
71
|
const previous = kept[kept.length - 1].point;
|
|
69
|
-
if (Math.hypot(
|
|
70
|
-
kept.push(
|
|
71
|
-
}
|
|
72
|
+
if (Math.hypot(entries[index].point.x - previous.x, entries[index].point.y - previous.y) >= gap)
|
|
73
|
+
kept.push(entries[index]);
|
|
72
74
|
}
|
|
73
75
|
// The last point is where the hand ended, whatever came before it.
|
|
74
|
-
if (
|
|
75
|
-
kept.push(
|
|
76
|
+
if (entries.length > 1)
|
|
77
|
+
kept.push(entries[entries.length - 1]);
|
|
76
78
|
if (kept.length <= BRUSH_MAX_POINTS)
|
|
77
79
|
return kept;
|
|
78
80
|
gap *= 1.5;
|
|
79
81
|
}
|
|
80
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
|
+
}
|
|
81
201
|
/**
|
|
82
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
|
|
83
203
|
* outline to draw. Null when there is nothing to draw.
|
|
@@ -90,9 +210,16 @@ export function buildBrushStroke(samples, options = {}) {
|
|
|
90
210
|
const steadyPressure = typeof options.steadyPressure === 'number' && Number.isFinite(options.steadyPressure) ? Math.min(1, Math.max(0, options.steadyPressure)) : null;
|
|
91
211
|
const brushLine = brushOptions(size, pressure, options.finished === true);
|
|
92
212
|
const lineOptions = steadyPressure === null ? brushLine : { ...brushLine, streamline: 0, simulatePressure: false };
|
|
93
|
-
|
|
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
|
|
94
221
|
.filter((sample) => Number.isFinite(sample.x) && Number.isFinite(sample.y))
|
|
95
|
-
.map((sample) => (
|
|
222
|
+
.map((sample) => (straightPressure === null ? sample : { x: sample.x, y: sample.y, pressure: straightPressure }));
|
|
96
223
|
if (usable.length === 0)
|
|
97
224
|
return null;
|
|
98
225
|
let kept;
|
|
@@ -103,10 +230,15 @@ export function buildBrushStroke(samples, options = {}) {
|
|
|
103
230
|
const point = { x: usable[0].x, y: usable[0].y, pressure: dotPressure, vectorX: 0, vectorY: 0, distance: 0, runningLength: 0 };
|
|
104
231
|
kept = [{ point, pressure: dotPressure }];
|
|
105
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;
|
|
237
|
+
}
|
|
106
238
|
else {
|
|
107
|
-
const smoothed = strokePointsOf(usable,
|
|
239
|
+
const smoothed = strokePointsOf(usable, wholeLineOptions);
|
|
108
240
|
// The pass that works the pressure out (from the speed, or read from the pen) so that it can be kept.
|
|
109
|
-
const { pressures } = outlineWithPressures(smoothed,
|
|
241
|
+
const { pressures } = outlineWithPressures(smoothed, wholeLineOptions);
|
|
110
242
|
kept = keptPoints(smoothed, pressures, size);
|
|
111
243
|
}
|
|
112
244
|
let minX = Infinity;
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* place. Nothing here touches the board; the pointer controller calls these on every move for the preview and
|
|
4
4
|
* once more on release for the shape that is kept.
|
|
5
5
|
*/
|
|
6
|
+
import type { RememberedStyles } from './styleMemory.js';
|
|
6
7
|
import type { EllipseObject, ObjectId, PathObject, Point, Rect, RectObject } from './types.js';
|
|
7
|
-
type WorkspaceShapePaint = Partial<Pick<RectObject, 'fill' | 'stroke' | 'strokeWidth' | 'strokeStyle' | 'opacity' | 'roundness'>>;
|
|
8
8
|
/** A drag shorter than this (in screen pixels) is a click: the shape is placed at its default size. */
|
|
9
9
|
export declare const MIN_DRAW_DISTANCE = 4;
|
|
10
10
|
/** The size of a library shape placed by a click. */
|
|
@@ -40,24 +40,25 @@ export interface ShapeDraftOptions {
|
|
|
40
40
|
wireframe: boolean;
|
|
41
41
|
createId?: (prefix: string) => ObjectId;
|
|
42
42
|
random?: () => number;
|
|
43
|
-
/**
|
|
44
|
-
|
|
43
|
+
/**
|
|
44
|
+
* What the person last chose for each kind of element (`EngineCore.rememberedStyles`): a new shape, line icon or arrow starts with it,
|
|
45
|
+
* laid over the look it would start with (the wireframe's white fill and grey outline, a random colour, the arrow's own line). Left
|
|
46
|
+
* out, the shape starts with that look alone.
|
|
47
|
+
*/
|
|
48
|
+
rememberedStyles?: RememberedStyles;
|
|
45
49
|
}
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
/**
|
|
51
|
+
* The paint a new shape of the tools starts with, by the look the board is in. What the person chose for the shapes before is laid over
|
|
52
|
+
* it afterwards (`withRememberedStyle`), so it is the same in both looks: the wireframe look decides only how a shape starts.
|
|
53
|
+
*/
|
|
54
|
+
export declare function newShapePaint(wireframe: boolean, random?: () => number): {
|
|
55
|
+
fill: "#ffffff";
|
|
56
|
+
stroke: "#374151";
|
|
57
|
+
strokeWidth: 2;
|
|
54
58
|
} | {
|
|
55
|
-
roundness?: number | undefined;
|
|
56
|
-
opacity?: number | undefined;
|
|
57
59
|
fill: string;
|
|
58
|
-
stroke:
|
|
59
|
-
strokeWidth:
|
|
60
|
-
strokeStyle?: import("./types.js").StrokeStyle | undefined;
|
|
60
|
+
stroke: "#000000";
|
|
61
|
+
strokeWidth: 1;
|
|
61
62
|
};
|
|
62
63
|
/** The look of a library shape, and of an arrow. */
|
|
63
64
|
export declare const LIBRARY_SHAPE_PAINT: {
|
|
@@ -78,7 +79,11 @@ export declare function arrowEnd(options: Pick<ShapeDraftOptions, 'start' | 'end
|
|
|
78
79
|
export declare function snapPointAround(center: Point, point: Point, step: number): Point;
|
|
79
80
|
/** The shape a tool would place for a drag. */
|
|
80
81
|
export declare function buildDraggedShape(options: ShapeDraftOptions): RectObject | EllipseObject | PathObject;
|
|
81
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* A library shape in a box, with the look and the line ends its kind has, and what the person last chose for its kind laid over that
|
|
84
|
+
* (`rememberedStyles`: the shapes' for a shape with an inside, the line icons' for a check, a cross, a plus and a minus). Null key or an
|
|
85
|
+
* unknown key gives a plain rectangle.
|
|
86
|
+
*/
|
|
82
87
|
export declare function buildLibraryShape(fields: {
|
|
83
88
|
id: ObjectId;
|
|
84
89
|
x: number;
|
|
@@ -86,5 +91,5 @@ export declare function buildLibraryShape(fields: {
|
|
|
86
91
|
width: number;
|
|
87
92
|
height: number;
|
|
88
93
|
key: string | null;
|
|
94
|
+
rememberedStyles?: RememberedStyles;
|
|
89
95
|
}): RectObject | PathObject;
|
|
90
|
-
export {};
|
|
@@ -9,6 +9,8 @@ import { buildArrow } from './arrow.js';
|
|
|
9
9
|
import { distanceBetween, rectFromPoints } from './geometry.js';
|
|
10
10
|
import { ARROW_SHAPE_KEY, TRIANGLE_SHAPE_KEY, libraryShapeKeepsAspect, libraryShapePathCommands, trianglePathCommands } from './shapePaths.js';
|
|
11
11
|
import { serializePathData } from './pathData.js';
|
|
12
|
+
import { MAX_STROKE_WIDTH, MIN_LINE_STROKE_WIDTH } from './style.js';
|
|
13
|
+
import { withRememberedStyle } from './styleMemory.js';
|
|
12
14
|
import { snapAngle } from './transform.js';
|
|
13
15
|
import { MIN_OBJECT_SIZE } from './types.js';
|
|
14
16
|
/** A drag shorter than this (in screen pixels) is a click: the shape is placed at its default size. */
|
|
@@ -42,16 +44,18 @@ function boxWithAspect(start, end, aspect) {
|
|
|
42
44
|
function centeredBox(center, width, height) {
|
|
43
45
|
return { x: center.x - width / 2, y: center.y - height / 2, width, height };
|
|
44
46
|
}
|
|
45
|
-
/**
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
/**
|
|
48
|
+
* The paint a new shape of the tools starts with, by the look the board is in. What the person chose for the shapes before is laid over
|
|
49
|
+
* it afterwards (`withRememberedStyle`), so it is the same in both looks: the wireframe look decides only how a shape starts.
|
|
50
|
+
*/
|
|
51
|
+
export function newShapePaint(wireframe, random = Math.random) {
|
|
52
|
+
return wireframe
|
|
48
53
|
? {
|
|
49
54
|
fill: SHAPE_STYLE_DEFAULTS.wireframeFill,
|
|
50
55
|
stroke: SHAPE_STYLE_DEFAULTS.wireframeStroke,
|
|
51
56
|
strokeWidth: SHAPE_STYLE_DEFAULTS.wireframeStrokeWidth
|
|
52
57
|
}
|
|
53
58
|
: { fill: randomShapeFill(random), stroke: SHAPE_STYLE_DEFAULTS.stroke, strokeWidth: SHAPE_STYLE_DEFAULTS.strokeWidth };
|
|
54
|
-
return { ...base, ...workspacePaint };
|
|
55
59
|
}
|
|
56
60
|
/** The look of a library shape, and of an arrow. */
|
|
57
61
|
export const LIBRARY_SHAPE_PAINT = { fill: '#ffffff', stroke: '#374151', strokeWidth: 3 };
|
|
@@ -95,61 +99,84 @@ export function snapPointAround(center, point, step) {
|
|
|
95
99
|
const angle = (snapAngle((Math.atan2(point.y - center.y, point.x - center.x) * 180) / Math.PI, step) * Math.PI) / 180;
|
|
96
100
|
return { x: center.x + length * Math.cos(angle), y: center.y + length * Math.sin(angle) };
|
|
97
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* The width an arrow starts with: the one the person last gave an arrow, or the Arrow tool's own. It is needed before the arrow exists,
|
|
104
|
+
* because the head of an arrow is drawn for the width of its line (a wider line has a bigger head), and the box that holds the arrow
|
|
105
|
+
* (which the drag's preview and the hit test use) is the box of that drawing.
|
|
106
|
+
*/
|
|
107
|
+
function arrowStrokeWidth(options) {
|
|
108
|
+
const remembered = options.rememberedStyles?.arrow.strokeWidth;
|
|
109
|
+
if (typeof remembered !== 'number' || !Number.isFinite(remembered))
|
|
110
|
+
return ARROW_PAINT.strokeWidth;
|
|
111
|
+
// An arrow is its line: never thinner than the thinnest, never wider than the Width control goes.
|
|
112
|
+
return Math.min(Math.max(remembered, MIN_LINE_STROKE_WIDTH), MAX_STROKE_WIDTH);
|
|
113
|
+
}
|
|
98
114
|
function arrowDragGeometry(options) {
|
|
99
|
-
return buildArrow(options.start, arrowEnd(options), 0,
|
|
115
|
+
return buildArrow(options.start, arrowEnd(options), 0, arrowStrokeWidth(options));
|
|
100
116
|
}
|
|
101
117
|
/** The shape a tool would place for a drag. */
|
|
102
118
|
export function buildDraggedShape(options) {
|
|
103
119
|
const createId = options.createId ?? createObjectId;
|
|
104
120
|
const id = createId('obj');
|
|
105
|
-
const { kind, wireframe } = options;
|
|
121
|
+
const { kind, wireframe, rememberedStyles } = options;
|
|
106
122
|
if (kind === 'arrow') {
|
|
107
123
|
const geometry = arrowDragGeometry(options);
|
|
108
|
-
|
|
124
|
+
const arrow = createPathObject({
|
|
109
125
|
id,
|
|
110
126
|
...geometry.box,
|
|
111
127
|
d: serializePathData(geometry.commands),
|
|
112
128
|
arrow: geometry.spec,
|
|
113
129
|
shape: ARROW_SHAPE_KEY,
|
|
114
130
|
...ARROW_PAINT,
|
|
131
|
+
// The width the head was drawn for (the Arrow tool's own, unless the person gave arrows another).
|
|
132
|
+
strokeWidth: arrowStrokeWidth(options),
|
|
115
133
|
lineCap: 'round',
|
|
116
134
|
lineJoin: 'round'
|
|
117
135
|
});
|
|
136
|
+
return withRememberedStyle(arrow, rememberedStyles?.arrow);
|
|
118
137
|
}
|
|
119
138
|
const box = shapeDragBox(options);
|
|
120
139
|
const width = Math.max(box.width, MIN_OBJECT_SIZE);
|
|
121
140
|
const height = Math.max(box.height, MIN_OBJECT_SIZE);
|
|
122
141
|
const frame = { id, x: box.x, y: box.y, width, height };
|
|
123
|
-
const paint = newShapePaint(wireframe, options.random
|
|
142
|
+
const paint = newShapePaint(wireframe, options.random);
|
|
143
|
+
// What the person last chose for the shapes is laid over the look the shape starts with, in the wireframe look and outside it.
|
|
144
|
+
const rememberedShape = rememberedStyles?.shape;
|
|
124
145
|
if (kind === 'rect')
|
|
125
|
-
return createRectObject({ ...frame, ...paint });
|
|
126
|
-
// The rounded square of the shapes menu: a rectangle that starts with soft corners (its Roundness control still moves them).
|
|
146
|
+
return withRememberedStyle(createRectObject({ ...frame, ...paint }), rememberedShape);
|
|
147
|
+
// The rounded square of the shapes menu: a rectangle that starts with soft corners (its Roundness control still moves them). The
|
|
148
|
+
// soft corners are what the entry is for, so the roundness the person last chose for a shape is not laid over them.
|
|
127
149
|
if (kind === 'rounded-rect') {
|
|
128
|
-
return createRectObject({ ...frame, ...paint, roundness: ROUNDED_RECT_ROUNDNESS });
|
|
150
|
+
return withRememberedStyle(createRectObject({ ...frame, ...paint, roundness: ROUNDED_RECT_ROUNDNESS }), rememberedShape, { leave: ['roundness'] });
|
|
129
151
|
}
|
|
130
152
|
if (kind === 'ellipse' || kind === 'circle') {
|
|
131
|
-
return createEllipseObject({ ...frame, ...paint, aspectLocked: kind === 'circle' });
|
|
153
|
+
return withRememberedStyle(createEllipseObject({ ...frame, ...paint, aspectLocked: kind === 'circle' }), rememberedShape);
|
|
132
154
|
}
|
|
133
155
|
if (kind === 'triangle') {
|
|
134
|
-
|
|
156
|
+
const triangle = createPathObject({
|
|
135
157
|
...frame,
|
|
136
158
|
...paint,
|
|
137
159
|
d: serializePathData(trianglePathCommands(width, height, 0)),
|
|
138
160
|
shape: TRIANGLE_SHAPE_KEY,
|
|
139
161
|
lineJoin: 'miter'
|
|
140
162
|
});
|
|
163
|
+
return withRememberedStyle(triangle, rememberedShape);
|
|
141
164
|
}
|
|
142
|
-
return buildLibraryShape({ ...frame, key: options.libraryKey ?? null });
|
|
165
|
+
return buildLibraryShape({ ...frame, key: options.libraryKey ?? null, rememberedStyles });
|
|
143
166
|
}
|
|
144
|
-
/**
|
|
167
|
+
/**
|
|
168
|
+
* A library shape in a box, with the look and the line ends its kind has, and what the person last chose for its kind laid over that
|
|
169
|
+
* (`rememberedStyles`: the shapes' for a shape with an inside, the line icons' for a check, a cross, a plus and a minus). Null key or an
|
|
170
|
+
* unknown key gives a plain rectangle.
|
|
171
|
+
*/
|
|
145
172
|
export function buildLibraryShape(fields) {
|
|
146
173
|
const shape = getShapeByKey(fields.key);
|
|
147
174
|
const frame = { id: fields.id, x: fields.x, y: fields.y, width: fields.width, height: fields.height };
|
|
148
175
|
const commands = fields.key && shape ? libraryShapePathCommands(fields.key, fields.width, fields.height, 0) : null;
|
|
149
176
|
if (!shape || !commands)
|
|
150
|
-
return createRectObject({ ...frame, ...newShapePaint(true) });
|
|
177
|
+
return withRememberedStyle(createRectObject({ ...frame, ...newShapePaint(true) }), fields.rememberedStyles?.shape);
|
|
151
178
|
const linesOnly = shape.cornerRadiusMode === 'strokeRound';
|
|
152
|
-
|
|
179
|
+
const libraryShape = createPathObject({
|
|
153
180
|
...frame,
|
|
154
181
|
...LIBRARY_SHAPE_PAINT,
|
|
155
182
|
// A line-only icon (a check, a cross) has no inside to fill.
|
|
@@ -160,4 +187,5 @@ export function buildLibraryShape(fields) {
|
|
|
160
187
|
lineCap: linesOnly ? 'butt' : 'round',
|
|
161
188
|
lineJoin: linesOnly ? 'miter' : 'round'
|
|
162
189
|
});
|
|
190
|
+
return withRememberedStyle(libraryShape, linesOnly ? fields.rememberedStyles?.lineIcon : fields.rememberedStyles?.shape);
|
|
163
191
|
}
|
|
@@ -29,8 +29,11 @@ export interface StylePatch {
|
|
|
29
29
|
}
|
|
30
30
|
/** Whether the Roundness control means something for the path (rounded corners or round line ends). */
|
|
31
31
|
export declare function pathSupportsRoundness(shapeKey: string | null): boolean;
|
|
32
|
-
/**
|
|
33
|
-
|
|
32
|
+
/**
|
|
33
|
+
* The widest a line may be made with the Width control, for every kind of shape, an arrow and a line drawn by hand (the pen's too: see
|
|
34
|
+
* `EngineCore.setPenStyle`). It was 200; a line thicker than 50 is not needed. A board saved with a thicker one opens as it was.
|
|
35
|
+
*/
|
|
36
|
+
export declare const MAX_STROKE_WIDTH = 50;
|
|
34
37
|
/** The thinnest line a shape that is only a line can be given (the brush's own least is the same): with none, nothing of it is left. */
|
|
35
38
|
export declare const MIN_LINE_STROKE_WIDTH = 1;
|
|
36
39
|
/**
|
|
@@ -46,5 +49,18 @@ export declare function paintsFillOnStroke(shape: ShapeObject): boolean;
|
|
|
46
49
|
* follow the pressure) the fill of that outline. The panel's Stroke colour and the shape snap read and write the line this way.
|
|
47
50
|
*/
|
|
48
51
|
export declare function lineColorOf(shape: ShapeObject): string;
|
|
52
|
+
/**
|
|
53
|
+
* A number of a patch brought into the range its key has (a width from 0 to 50, an opacity from 0 to 1, a size from 4 to 600 ...). A key
|
|
54
|
+
* with no range keeps the number as it is. The one place the ranges are applied, so a value kept for later and a value written to a shape
|
|
55
|
+
* can never disagree on what the key allows.
|
|
56
|
+
*/
|
|
57
|
+
export declare function clampedStyleNumber(key: keyof StylePatch, value: number): number;
|
|
58
|
+
/**
|
|
59
|
+
* The keys of a patch that a shape takes, at the values it takes them at: what `applyStylePatch` writes into the shape, whether or not
|
|
60
|
+
* the shape already has it. A number is brought into its range, a line is never made so thin that it is gone, and the Fill of a shape
|
|
61
|
+
* that is only a line (a check, a cross) is its Stroke. This is also what an edit of the selection says about the next element of its
|
|
62
|
+
* kind (see `styleMemory.ts`), so the memory and the shape always agree on what a value is.
|
|
63
|
+
*/
|
|
64
|
+
export declare function acceptedStylePatch(shape: ShapeObject, requested: StylePatch): StylePatch;
|
|
49
65
|
/** Applies the keys of a patch that mean something for the shape. Same object back when nothing changes. */
|
|
50
66
|
export declare function applyStylePatch(shape: ShapeObject, requested: StylePatch): ShapeObject;
|