@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
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the board remembers of how the person last styled each kind of element, so that the next element of that kind starts the way
|
|
3
|
+
* the last one was left ("after changing the fill and the colour of the stroke and the stroke width, those should be maintained across
|
|
4
|
+
* all the shapes until the user edits them again. The same should be true for the other elements as well", 19 Nov 2026).
|
|
5
|
+
*
|
|
6
|
+
* An element is of one of five kinds, and each kind remembers only what the person did to elements of that kind:
|
|
7
|
+
*
|
|
8
|
+
* - `shape`: a rectangle, an ellipse, a triangle and every shape of the library that has an inside (fill, stroke, width, opacity, roundness).
|
|
9
|
+
* - `lineIcon`: the four shapes of the library that are only a line (a check, a cross, a plus, a minus): the colour of the line, its width,
|
|
10
|
+
* its opacity and how round its ends are. They have no inside, so a fill chosen for a rectangle is no business of theirs, and a colour
|
|
11
|
+
* chosen for a check does not turn the next rectangle's outline.
|
|
12
|
+
* - `arrow`: the colour, the width and the opacity of the line (an arrow has no inside and no corners).
|
|
13
|
+
* - `text`: the font, the size, bold, italic, underline, the alignment, the colour of the letters and the opacity.
|
|
14
|
+
* - `pen`: a line drawn by hand. Its three settings are the pen of the Draw tool (`EngineState.penStyle`), so an edit of a drawn line is
|
|
15
|
+
* an edit of the pen, and the engine keeps the pen itself; this file only says that the edit is one.
|
|
16
|
+
*
|
|
17
|
+
* The memory is a set of patches in the panel's own keys (`StylePatch`). An edit of the selection is turned into one by `styleEditOf`,
|
|
18
|
+
* through `acceptedStylePatch`, so a value is remembered exactly as the shape took it (a width of 0 is 1 for an arrow, a width of 900 is
|
|
19
|
+
* 50), and a new element is given the memory by `withRememberedStyle`, through `applyStylePatch`, the one door every style goes through:
|
|
20
|
+
* the head of an arrow follows its width and the corners of a triangle are drawn again for their roundness, as when the panel is used.
|
|
21
|
+
*
|
|
22
|
+
* Everything here is a pure function of values. The engine holds the memory, records the edits of `setStyle` into it and tells the host,
|
|
23
|
+
* which keeps it for the workspace (`CanvasHost`, `workspaceToolSettings.ts`).
|
|
24
|
+
*/
|
|
25
|
+
import { isArrow } from './arrow.js';
|
|
26
|
+
import { isBrushStroke } from './brush.js';
|
|
27
|
+
import { acceptedStylePatch, applyStylePatch, clampedStyleNumber, paintsFillOnStroke } from './style.js';
|
|
28
|
+
export const PATCHED_STYLE_KINDS = ['shape', 'lineIcon', 'arrow', 'text'];
|
|
29
|
+
// The keys of the panel that each kind remembers (a list for each kind, so that `REMEMBERED_STYLE_KEYS` below reads as a table).
|
|
30
|
+
const SHAPE_STYLE_KEYS = ['fill', 'stroke', 'strokeWidth', 'strokeStyle', 'opacity', 'roundness'];
|
|
31
|
+
const LINE_ICON_STYLE_KEYS = ['stroke', 'strokeWidth', 'strokeStyle', 'opacity', 'roundness'];
|
|
32
|
+
const ARROW_STYLE_KEYS = ['stroke', 'strokeWidth', 'strokeStyle', 'opacity'];
|
|
33
|
+
const TEXT_STYLE_KEYS = ['color', 'fontFamily', 'fontSize', 'fontWeight', 'fontStyle', 'underline', 'align', 'opacity'];
|
|
34
|
+
const PEN_STYLE_KEYS = ['stroke', 'strokeWidth', 'opacity'];
|
|
35
|
+
/**
|
|
36
|
+
* The keys of an edit that each kind remembers. What is not here is the element's own business and is never carried over: its place and
|
|
37
|
+
* size, its text, how it is rotated, what it is tied to. (A key that a kind does not take at all, such as a fill for an arrow, is not
|
|
38
|
+
* here either: `acceptedStylePatch` has already dropped it.)
|
|
39
|
+
*/
|
|
40
|
+
export const REMEMBERED_STYLE_KEYS = Object.freeze({
|
|
41
|
+
shape: SHAPE_STYLE_KEYS,
|
|
42
|
+
lineIcon: LINE_ICON_STYLE_KEYS,
|
|
43
|
+
arrow: ARROW_STYLE_KEYS,
|
|
44
|
+
text: TEXT_STYLE_KEYS,
|
|
45
|
+
pen: PEN_STYLE_KEYS
|
|
46
|
+
});
|
|
47
|
+
/** The kind of element a shape is, or null for what has no style to remember (a picture, a widget card). */
|
|
48
|
+
export function styleMemoryKindOf(shape) {
|
|
49
|
+
switch (shape.type) {
|
|
50
|
+
case 'rect':
|
|
51
|
+
case 'ellipse':
|
|
52
|
+
return 'shape';
|
|
53
|
+
case 'text':
|
|
54
|
+
return 'text';
|
|
55
|
+
case 'path':
|
|
56
|
+
if (isBrushStroke(shape))
|
|
57
|
+
return 'pen';
|
|
58
|
+
if (isArrow(shape))
|
|
59
|
+
return 'arrow';
|
|
60
|
+
return paintsFillOnStroke(shape) ? 'lineIcon' : 'shape';
|
|
61
|
+
default:
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* What an edit says about the next element of the shape's kind: the keys of the patch that the shape takes and that its kind
|
|
67
|
+
* remembers, at the values the shape takes them at. Null when it says nothing (a picture, or a patch of keys the kind has no use for).
|
|
68
|
+
*
|
|
69
|
+
* The keys the shape already holds are in it too: a person who picks the colour a shape has already has said what the next one should
|
|
70
|
+
* be, and it may be another colour than the memory holds.
|
|
71
|
+
*/
|
|
72
|
+
export function styleEditOf(shape, patch) {
|
|
73
|
+
const kind = styleMemoryKindOf(shape);
|
|
74
|
+
if (!kind)
|
|
75
|
+
return null;
|
|
76
|
+
const accepted = acceptedStylePatch(shape, patch);
|
|
77
|
+
const style = {};
|
|
78
|
+
for (const key of REMEMBERED_STYLE_KEYS[kind]) {
|
|
79
|
+
const value = accepted[key];
|
|
80
|
+
if (value === undefined)
|
|
81
|
+
continue;
|
|
82
|
+
// A number that is no number is not a width or a size: the memory keeps what it had.
|
|
83
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
84
|
+
continue;
|
|
85
|
+
style[key] = value;
|
|
86
|
+
}
|
|
87
|
+
return Object.keys(style).length > 0 ? { kind, style: style } : null;
|
|
88
|
+
}
|
|
89
|
+
export const NO_REMEMBERED_STYLES = Object.freeze({
|
|
90
|
+
shape: Object.freeze({}),
|
|
91
|
+
lineIcon: Object.freeze({}),
|
|
92
|
+
arrow: Object.freeze({}),
|
|
93
|
+
text: Object.freeze({})
|
|
94
|
+
});
|
|
95
|
+
/**
|
|
96
|
+
* The memory with some edits laid over it, and the kinds that came out different. Where the edits say nothing new the very same object
|
|
97
|
+
* comes back with no kind named, so a drag that goes on saying the same colour tells nobody anything. The pen is not part of the memory
|
|
98
|
+
* (the engine keeps it), so an edit of a drawn line is passed over here.
|
|
99
|
+
*/
|
|
100
|
+
export function rememberStyleEdits(styles, edits) {
|
|
101
|
+
let next = styles;
|
|
102
|
+
const changed = [];
|
|
103
|
+
for (const { kind, style } of edits) {
|
|
104
|
+
if (kind === 'pen')
|
|
105
|
+
continue;
|
|
106
|
+
const current = next[kind];
|
|
107
|
+
let merged = null;
|
|
108
|
+
for (const [key, value] of Object.entries(style)) {
|
|
109
|
+
if (Object.is((merged ?? current)[key], value))
|
|
110
|
+
continue;
|
|
111
|
+
merged = { ...(merged ?? current), [key]: value };
|
|
112
|
+
}
|
|
113
|
+
if (!merged)
|
|
114
|
+
continue;
|
|
115
|
+
next = Object.freeze({ ...next, [kind]: Object.freeze(merged) });
|
|
116
|
+
if (!changed.includes(kind))
|
|
117
|
+
changed.push(kind);
|
|
118
|
+
}
|
|
119
|
+
return { styles: next, changed };
|
|
120
|
+
}
|
|
121
|
+
/** The values that are words of a fixed list: a key and what it may be. */
|
|
122
|
+
const WORDS_OF_KEY = {
|
|
123
|
+
strokeStyle: ['solid', 'dashed', 'dotted'],
|
|
124
|
+
fontWeight: ['normal', 'bold'],
|
|
125
|
+
fontStyle: ['normal', 'italic'],
|
|
126
|
+
align: ['left', 'center', 'right']
|
|
127
|
+
};
|
|
128
|
+
/** The keys whose value is a colour or a name: a text that is not empty. */
|
|
129
|
+
const TEXT_VALUED_KEYS = new Set(['fill', 'stroke', 'color', 'fontFamily']);
|
|
130
|
+
/** The keys whose value is a number, which has a range (see `clampedStyleNumber`). */
|
|
131
|
+
const NUMBER_VALUED_KEYS = new Set(['strokeWidth', 'opacity', 'roundness', 'fontSize']);
|
|
132
|
+
/** A value of a key as the storage may hold it, brought to what the key can be, or undefined when it is no value of the key at all. */
|
|
133
|
+
function checkedStyleValue(key, value) {
|
|
134
|
+
if (TEXT_VALUED_KEYS.has(key))
|
|
135
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;
|
|
136
|
+
if (NUMBER_VALUED_KEYS.has(key))
|
|
137
|
+
return typeof value === 'number' && Number.isFinite(value) ? clampedStyleNumber(key, value) : undefined;
|
|
138
|
+
if (key === 'underline')
|
|
139
|
+
return typeof value === 'boolean' ? value : undefined;
|
|
140
|
+
const words = WORDS_OF_KEY[key];
|
|
141
|
+
return words && typeof value === 'string' && words.includes(value) ? value : undefined;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* What the storage holds for a kind, as a memory can hold it: key by key, a value that is not one of the key is dropped (the rest stays),
|
|
145
|
+
* a number is brought into its range (a width that was kept when the widest was 200 comes down to 50), and a key the kind does not
|
|
146
|
+
* remember is left out. Anything that is not an object is no memory at all.
|
|
147
|
+
*/
|
|
148
|
+
export function sanitizeRememberedStyle(kind, stored) {
|
|
149
|
+
if (!stored || typeof stored !== 'object')
|
|
150
|
+
return Object.freeze({});
|
|
151
|
+
const source = stored;
|
|
152
|
+
const style = {};
|
|
153
|
+
for (const key of REMEMBERED_STYLE_KEYS[kind]) {
|
|
154
|
+
const checked = checkedStyleValue(key, source[key]);
|
|
155
|
+
if (checked !== undefined)
|
|
156
|
+
style[key] = checked;
|
|
157
|
+
}
|
|
158
|
+
return Object.freeze(style);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* What the storage holds for every kind, as a memory can hold it (`sanitizeRememberedStyle` for each). Nothing stored: nothing remembered.
|
|
162
|
+
* A kind the storage has no part for is nothing remembered for that kind.
|
|
163
|
+
*/
|
|
164
|
+
export function sanitizeRememberedStyles(stored) {
|
|
165
|
+
if (!stored)
|
|
166
|
+
return NO_REMEMBERED_STYLES;
|
|
167
|
+
const sanitized = {};
|
|
168
|
+
for (const kind of PATCHED_STYLE_KINDS)
|
|
169
|
+
sanitized[kind] = sanitizeRememberedStyle(kind, stored[kind]);
|
|
170
|
+
return Object.freeze(sanitized);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A new element with what the person last chose for its kind laid over what it starts with. `leave` names keys of the memory that the
|
|
174
|
+
* element keeps as it starts (the rounded square keeps the soft corners it is chosen for). The element comes out as the panel would
|
|
175
|
+
* make it: the head of an arrow follows its width, the corners of a triangle are drawn again for their roundness. The very same object
|
|
176
|
+
* back when there is nothing to lay over.
|
|
177
|
+
*/
|
|
178
|
+
export function withRememberedStyle(element, remembered, options = {}) {
|
|
179
|
+
if (!remembered)
|
|
180
|
+
return element;
|
|
181
|
+
let patch = remembered;
|
|
182
|
+
if (options.leave && options.leave.length > 0) {
|
|
183
|
+
const withoutLeft = { ...remembered };
|
|
184
|
+
for (const key of options.leave)
|
|
185
|
+
delete withoutLeft[key];
|
|
186
|
+
patch = withoutLeft;
|
|
187
|
+
}
|
|
188
|
+
return applyStylePatch(element, patch);
|
|
189
|
+
}
|
|
@@ -8,17 +8,6 @@
|
|
|
8
8
|
* A handle is dragged in the frame's own (unrotated) space, so a turned shape resizes along its own edges.
|
|
9
9
|
* The edge opposite the handle (or the centre, with Alt) stays where it is in the world.
|
|
10
10
|
*/
|
|
11
|
-
var __assign = (this && this.__assign) || function () {
|
|
12
|
-
__assign = Object.assign || function(t) {
|
|
13
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
14
|
-
s = arguments[i];
|
|
15
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
16
|
-
t[p] = s[p];
|
|
17
|
-
}
|
|
18
|
-
return t;
|
|
19
|
-
};
|
|
20
|
-
return __assign.apply(this, arguments);
|
|
21
|
-
};
|
|
22
11
|
import { isArrow, transformArrow } from './arrow.js';
|
|
23
12
|
import { isBrushStroke, reshapeBrushStroke } from './brush.js';
|
|
24
13
|
import { rotatedBoxBounds, unionRects, worldToBoxLocal, clampNumber } from './geometry.js';
|
|
@@ -26,7 +15,7 @@ import { applyMatrixToPoint, boxToWorldMatrix, degreesToRadians, normalizeDegree
|
|
|
26
15
|
import { parsePathData, scalePathCommands, mirrorPathCommands, serializePathData } from './pathData.js';
|
|
27
16
|
import { regeneratedPathCommands } from './shapePaths.js';
|
|
28
17
|
import { MIN_OBJECT_SIZE } from './types.js';
|
|
29
|
-
|
|
18
|
+
const HANDLE_DIRECTIONS = {
|
|
30
19
|
nw: { x: -1, y: -1 },
|
|
31
20
|
n: { x: 0, y: -1 },
|
|
32
21
|
ne: { x: 1, y: -1 },
|
|
@@ -44,11 +33,11 @@ export function selectionFrameOf(shapes) {
|
|
|
44
33
|
if (shapes.length === 0)
|
|
45
34
|
return null;
|
|
46
35
|
if (shapes.length === 1) {
|
|
47
|
-
|
|
36
|
+
const [shape] = shapes;
|
|
48
37
|
return { x: shape.x, y: shape.y, width: shape.width, height: shape.height, rotation: shape.rotation };
|
|
49
38
|
}
|
|
50
|
-
|
|
51
|
-
return union ?
|
|
39
|
+
const union = unionRects(shapes.map((shape) => rotatedBoxBounds(shape)));
|
|
40
|
+
return union ? { ...union, rotation: 0 } : null;
|
|
52
41
|
}
|
|
53
42
|
/** The centre of a frame in the world. */
|
|
54
43
|
export function frameCenter(frame) {
|
|
@@ -60,38 +49,36 @@ export function frameCenter(frame) {
|
|
|
60
49
|
* only the points on its line.
|
|
61
50
|
*/
|
|
62
51
|
export function frameContains(frame, point) {
|
|
63
|
-
|
|
52
|
+
const local = worldToBoxLocal(frame, point);
|
|
64
53
|
return !!local && local.x >= 0 && local.y >= 0 && local.x <= frame.width && local.y <= frame.height;
|
|
65
54
|
}
|
|
66
55
|
/**
|
|
67
56
|
* The frame that results from dragging `handle` to a world point.
|
|
68
57
|
* `frame` is the frame when the drag began (not the one the last pointer move produced).
|
|
69
58
|
*/
|
|
70
|
-
export function computeResizedFrame(frame, handle, pointerWorld, options) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
var direction = HANDLE_DIRECTIONS[handle];
|
|
75
|
-
var local = worldToBoxLocal(frame, pointerWorld);
|
|
59
|
+
export function computeResizedFrame(frame, handle, pointerWorld, options = {}) {
|
|
60
|
+
const minimumSize = options.minimumSize ?? MIN_OBJECT_SIZE;
|
|
61
|
+
const direction = HANDLE_DIRECTIONS[handle];
|
|
62
|
+
const local = worldToBoxLocal(frame, pointerWorld);
|
|
76
63
|
if (!local)
|
|
77
|
-
return
|
|
78
|
-
|
|
64
|
+
return { ...frame, flipX: false, flipY: false };
|
|
65
|
+
const { width, height } = frame;
|
|
79
66
|
// How far the handle's edge is from the anchor (the opposite edge, or the centre), signed: negative means
|
|
80
67
|
// the pointer has crossed the anchor.
|
|
81
|
-
|
|
68
|
+
const extentX = direction.x === 0
|
|
82
69
|
? width
|
|
83
70
|
: options.fromCenter
|
|
84
71
|
? (local.x - width / 2) * direction.x * 2
|
|
85
72
|
: (local.x - (direction.x > 0 ? 0 : width)) * direction.x;
|
|
86
|
-
|
|
73
|
+
const extentY = direction.y === 0
|
|
87
74
|
? height
|
|
88
75
|
: options.fromCenter
|
|
89
76
|
? (local.y - height / 2) * direction.y * 2
|
|
90
77
|
: (local.y - (direction.y > 0 ? 0 : height)) * direction.y;
|
|
91
|
-
|
|
92
|
-
|
|
78
|
+
let signedWidth = extentX;
|
|
79
|
+
let signedHeight = extentY;
|
|
93
80
|
if (options.keepAspect && width > 0 && height > 0) {
|
|
94
|
-
|
|
81
|
+
let scale;
|
|
95
82
|
if (direction.x !== 0 && direction.y !== 0) {
|
|
96
83
|
scale = Math.max(Math.abs(extentX) / width, Math.abs(extentY) / height);
|
|
97
84
|
}
|
|
@@ -101,14 +88,14 @@ export function computeResizedFrame(frame, handle, pointerWorld, options) {
|
|
|
101
88
|
else {
|
|
102
89
|
scale = Math.abs(extentY) / height;
|
|
103
90
|
}
|
|
104
|
-
|
|
91
|
+
const smallestScale = minimumSize / Math.min(width, height);
|
|
105
92
|
if (!options.allowFlip || scale < smallestScale)
|
|
106
93
|
scale = Math.max(scale, smallestScale);
|
|
107
94
|
signedWidth = (direction.x === 0 || extentX >= 0 ? 1 : -1) * width * scale;
|
|
108
95
|
signedHeight = (direction.y === 0 || extentY >= 0 ? 1 : -1) * height * scale;
|
|
109
96
|
}
|
|
110
|
-
|
|
111
|
-
|
|
97
|
+
let flipX = false;
|
|
98
|
+
let flipY = false;
|
|
112
99
|
if (signedWidth < 0) {
|
|
113
100
|
if (options.allowFlip)
|
|
114
101
|
flipX = true;
|
|
@@ -121,22 +108,22 @@ export function computeResizedFrame(frame, handle, pointerWorld, options) {
|
|
|
121
108
|
else
|
|
122
109
|
signedHeight = minimumSize;
|
|
123
110
|
}
|
|
124
|
-
|
|
125
|
-
|
|
111
|
+
const newWidth = Math.max(minimumSize, Math.abs(signedWidth));
|
|
112
|
+
const newHeight = Math.max(minimumSize, Math.abs(signedHeight));
|
|
126
113
|
// The centre of the new box in the frame's own space: half an extent from the anchor, towards the handle.
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
114
|
+
const anchorX = direction.x > 0 ? 0 : width;
|
|
115
|
+
const anchorY = direction.y > 0 ? 0 : height;
|
|
116
|
+
const centerLocalX = direction.x === 0 || options.fromCenter ? width / 2 : anchorX + (direction.x * (flipX ? -newWidth : newWidth)) / 2;
|
|
117
|
+
const centerLocalY = direction.y === 0 || options.fromCenter ? height / 2 : anchorY + (direction.y * (flipY ? -newHeight : newHeight)) / 2;
|
|
118
|
+
const centerWorld = applyMatrixToPoint(boxToWorldMatrix(frame), { x: centerLocalX, y: centerLocalY });
|
|
132
119
|
return {
|
|
133
120
|
x: centerWorld.x - newWidth / 2,
|
|
134
121
|
y: centerWorld.y - newHeight / 2,
|
|
135
122
|
width: newWidth,
|
|
136
123
|
height: newHeight,
|
|
137
124
|
rotation: frame.rotation,
|
|
138
|
-
flipX
|
|
139
|
-
flipY
|
|
125
|
+
flipX,
|
|
126
|
+
flipY
|
|
140
127
|
};
|
|
141
128
|
}
|
|
142
129
|
/**
|
|
@@ -146,21 +133,18 @@ export function computeResizedFrame(frame, handle, pointerWorld, options) {
|
|
|
146
133
|
* is drawn again (its head keeps its size), text keeps its font size when it is resized from a side
|
|
147
134
|
* (wrapping) and scales it from a corner.
|
|
148
135
|
*/
|
|
149
|
-
export function resizeShapeToBox(shape, box, flip, textScale) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
var height = Math.max(MIN_OBJECT_SIZE, box.height);
|
|
155
|
-
var rotation = (_a = box.rotation) !== null && _a !== void 0 ? _a : shape.rotation;
|
|
156
|
-
var base = __assign(__assign({}, shape), { x: box.x, y: box.y, width: width, height: height, rotation: rotation });
|
|
136
|
+
export function resizeShapeToBox(shape, box, flip = { flipX: false, flipY: false }, textScale = null) {
|
|
137
|
+
const width = Math.max(MIN_OBJECT_SIZE, box.width);
|
|
138
|
+
const height = Math.max(MIN_OBJECT_SIZE, box.height);
|
|
139
|
+
const rotation = box.rotation ?? shape.rotation;
|
|
140
|
+
const base = { ...shape, x: box.x, y: box.y, width, height, rotation };
|
|
157
141
|
switch (shape.type) {
|
|
158
142
|
case 'path': {
|
|
159
143
|
if (isArrow(shape)) {
|
|
160
144
|
// The points of the arrow go where the box goes (mirrored by a flip), and the arrow is drawn again from them.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
145
|
+
const mapPoint = (point) => {
|
|
146
|
+
let unitX = shape.width > 0 ? (point.x - shape.x) / shape.width : 0.5;
|
|
147
|
+
let unitY = shape.height > 0 ? (point.y - shape.y) / shape.height : 0.5;
|
|
164
148
|
if (flip.flipX)
|
|
165
149
|
unitX = 1 - unitX;
|
|
166
150
|
if (flip.flipY)
|
|
@@ -170,35 +154,35 @@ export function resizeShapeToBox(shape, box, flip, textScale) {
|
|
|
170
154
|
return transformArrow(shape, mapPoint);
|
|
171
155
|
}
|
|
172
156
|
// A flip toggles the mirror the shape already has.
|
|
173
|
-
|
|
174
|
-
|
|
157
|
+
const flipX = shape.flipX !== flip.flipX;
|
|
158
|
+
const flipY = shape.flipY !== flip.flipY;
|
|
175
159
|
if (isBrushStroke(shape)) {
|
|
176
160
|
// The line goes where the box goes (and is mirrored by this flip); the brush stays as wide as it was.
|
|
177
|
-
|
|
178
|
-
return
|
|
161
|
+
const reshaped = reshapeBrushStroke(shape, { width, height, flipX: flip.flipX, flipY: flip.flipY });
|
|
162
|
+
return { ...base, d: reshaped.d, ink: reshaped.ink, flipX, flipY };
|
|
179
163
|
}
|
|
180
|
-
|
|
181
|
-
|
|
164
|
+
const regenerated = regeneratedPathCommands(shape, width, height);
|
|
165
|
+
let commands;
|
|
182
166
|
if (regenerated) {
|
|
183
167
|
// Rebuilt from its key, so the mirror the shape had is applied again.
|
|
184
168
|
commands = mirrorPathCommands(regenerated, width, height, flipX, flipY);
|
|
185
169
|
}
|
|
186
170
|
else {
|
|
187
171
|
// Scaled from the path it has, which already holds the earlier mirrors: only this flip is applied.
|
|
188
|
-
|
|
189
|
-
|
|
172
|
+
const scaleX = shape.width > 0 ? width / shape.width : 1;
|
|
173
|
+
const scaleY = shape.height > 0 ? height / shape.height : 1;
|
|
190
174
|
commands = mirrorPathCommands(scalePathCommands(parsePathData(shape.d), scaleX, scaleY), width, height, flip.flipX, flip.flipY);
|
|
191
175
|
}
|
|
192
|
-
return
|
|
176
|
+
return { ...base, d: serializePathData(commands), flipX, flipY };
|
|
193
177
|
}
|
|
194
178
|
case 'text': {
|
|
195
|
-
|
|
179
|
+
const text = shape;
|
|
196
180
|
if (textScale !== null && textScale > 0) {
|
|
197
181
|
// A corner drag scales the letters with the box.
|
|
198
|
-
return
|
|
182
|
+
return { ...base, fontSize: clampNumber(text.fontSize * textScale, 4, 600) };
|
|
199
183
|
}
|
|
200
184
|
// A side drag only sets the width the text wraps at.
|
|
201
|
-
return
|
|
185
|
+
return { ...base, autoWidth: false };
|
|
202
186
|
}
|
|
203
187
|
default:
|
|
204
188
|
return base;
|
|
@@ -211,38 +195,38 @@ export function resizeShapeToBox(shape, box, flip, textScale) {
|
|
|
211
195
|
*/
|
|
212
196
|
export function applyFrameResize(shapes, startFrame, resized, isCornerHandle) {
|
|
213
197
|
if (shapes.length === 1) {
|
|
214
|
-
|
|
215
|
-
|
|
198
|
+
const [shape] = shapes;
|
|
199
|
+
const cornerTextScale = shape.type === 'text' && isCornerHandle && startFrame.width > 0 ? resized.width / startFrame.width : null;
|
|
216
200
|
return [
|
|
217
201
|
resizeShapeToBox(shape, resized, { flipX: resized.flipX, flipY: resized.flipY }, cornerTextScale)
|
|
218
202
|
];
|
|
219
203
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
204
|
+
const scaleX = startFrame.width > 0 ? resized.width / startFrame.width : 1;
|
|
205
|
+
const scaleY = startFrame.height > 0 ? resized.height / startFrame.height : 1;
|
|
206
|
+
const mapPoint = (point) => {
|
|
207
|
+
let unitX = startFrame.width > 0 ? (point.x - startFrame.x) / startFrame.width : 0.5;
|
|
208
|
+
let unitY = startFrame.height > 0 ? (point.y - startFrame.y) / startFrame.height : 0.5;
|
|
225
209
|
if (resized.flipX)
|
|
226
210
|
unitX = 1 - unitX;
|
|
227
211
|
if (resized.flipY)
|
|
228
212
|
unitY = 1 - unitY;
|
|
229
213
|
return { x: resized.x + unitX * resized.width, y: resized.y + unitY * resized.height };
|
|
230
214
|
};
|
|
231
|
-
return shapes.map(
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
215
|
+
return shapes.map((shape) => {
|
|
216
|
+
const radians = degreesToRadians(shape.rotation);
|
|
217
|
+
const cosine = Math.cos(radians);
|
|
218
|
+
const sine = Math.sin(radians);
|
|
235
219
|
// The shape's own axes are stretched by the frame; the lengths along them give its new size.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
220
|
+
const widthFactor = Math.hypot(scaleX * cosine, scaleY * sine);
|
|
221
|
+
const heightFactor = Math.hypot(scaleX * sine, scaleY * cosine);
|
|
222
|
+
const newWidth = Math.max(MIN_OBJECT_SIZE, shape.width * widthFactor);
|
|
223
|
+
const newHeight = Math.max(MIN_OBJECT_SIZE, shape.height * heightFactor);
|
|
224
|
+
const center = mapPoint({ x: shape.x + shape.width / 2, y: shape.y + shape.height / 2 });
|
|
241
225
|
// A mirrored shape turns the other way.
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
226
|
+
const mirrors = (resized.flipX ? 1 : 0) + (resized.flipY ? 1 : 0);
|
|
227
|
+
const rotation = mirrors % 2 === 1 ? normalizeDegrees(-shape.rotation) : shape.rotation;
|
|
228
|
+
const box = { x: center.x - newWidth / 2, y: center.y - newHeight / 2, width: newWidth, height: newHeight, rotation };
|
|
229
|
+
const textScale = shape.type === 'text' ? (widthFactor + heightFactor) / 2 : null;
|
|
246
230
|
return resizeShapeToBox(shape, box, { flipX: resized.flipX, flipY: resized.flipY }, textScale);
|
|
247
231
|
});
|
|
248
232
|
}
|
|
@@ -251,21 +235,26 @@ export function applyFrameResize(shapes, startFrame, resized, isCornerHandle) {
|
|
|
251
235
|
* turn (it is always upright): its points go round the point and it is drawn again between them.
|
|
252
236
|
*/
|
|
253
237
|
export function rotateShapesAbout(shapes, pivot, deltaDegrees) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
return shapes.map(
|
|
238
|
+
const radians = degreesToRadians(deltaDegrees);
|
|
239
|
+
const cosine = Math.cos(radians);
|
|
240
|
+
const sine = Math.sin(radians);
|
|
241
|
+
return shapes.map((shape) => {
|
|
258
242
|
if (isArrow(shape)) {
|
|
259
|
-
return transformArrow(shape,
|
|
243
|
+
return transformArrow(shape, (point) => ({
|
|
260
244
|
x: pivot.x + (point.x - pivot.x) * cosine - (point.y - pivot.y) * sine,
|
|
261
245
|
y: pivot.y + (point.x - pivot.x) * sine + (point.y - pivot.y) * cosine
|
|
262
|
-
})
|
|
246
|
+
}));
|
|
263
247
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return
|
|
248
|
+
const centerX = shape.x + shape.width / 2 - pivot.x;
|
|
249
|
+
const centerY = shape.y + shape.height / 2 - pivot.y;
|
|
250
|
+
const rotatedX = pivot.x + centerX * cosine - centerY * sine;
|
|
251
|
+
const rotatedY = pivot.y + centerX * sine + centerY * cosine;
|
|
252
|
+
return {
|
|
253
|
+
...shape,
|
|
254
|
+
x: rotatedX - shape.width / 2,
|
|
255
|
+
y: rotatedY - shape.height / 2,
|
|
256
|
+
rotation: normalizeDegrees(shape.rotation + deltaDegrees)
|
|
257
|
+
};
|
|
269
258
|
});
|
|
270
259
|
}
|
|
271
260
|
/** The angle, in degrees, of the line from `center` to `point` (0 is to the right, growing clockwise on screen). */
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
* Nothing here knows about the DOM, Svelte or the host, so the same records run in Node tests.
|
|
18
18
|
*/
|
|
19
19
|
/** The version of the saved format. A file of any other version (the previous canvas wrote version 1, a string, `1.0`) is not read: there is no converter. */
|
|
20
|
-
export
|
|
21
|
-
export
|
|
22
|
-
export
|
|
23
|
-
export
|
|
20
|
+
export const CANVAS_DOCUMENT_VERSION = 2;
|
|
21
|
+
export const MIN_OBJECT_SIZE = 1;
|
|
22
|
+
export const MIN_ZOOM = 0.05;
|
|
23
|
+
export const MAX_ZOOM = 8;
|
|
24
24
|
/** True for the objects that carry a box (so not for a group). */
|
|
25
25
|
export function isBoxedObject(object) {
|
|
26
26
|
return object.type !== 'group';
|
|
@@ -5,22 +5,11 @@
|
|
|
5
5
|
* `viewport.x` and `viewport.y` are where the world origin lands on the board element, in CSS pixels, and
|
|
6
6
|
* `viewport.zoom` is the scale (1 is 100%). The world layer is drawn with `translate(x, y) scale(zoom)`.
|
|
7
7
|
*/
|
|
8
|
-
var __assign = (this && this.__assign) || function () {
|
|
9
|
-
__assign = Object.assign || function(t) {
|
|
10
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
11
|
-
s = arguments[i];
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
}
|
|
15
|
-
return t;
|
|
16
|
-
};
|
|
17
|
-
return __assign.apply(this, arguments);
|
|
18
|
-
};
|
|
19
8
|
import { clampNumber } from './geometry.js';
|
|
20
9
|
import { MAX_ZOOM, MIN_ZOOM } from './types.js';
|
|
21
|
-
export
|
|
10
|
+
export const INITIAL_VIEWPORT = { x: 0, y: 0, zoom: 1 };
|
|
22
11
|
/** The zoom levels the zoom buttons and the menu step through. */
|
|
23
|
-
export
|
|
12
|
+
export const ZOOM_STEPS = [0.1, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 3, 4, 6, 8];
|
|
24
13
|
export function clampZoom(zoom) {
|
|
25
14
|
if (!Number.isFinite(zoom))
|
|
26
15
|
return 1;
|
|
@@ -45,21 +34,21 @@ export function visibleWorldRect(viewport, viewSize) {
|
|
|
45
34
|
}
|
|
46
35
|
/** Changes the zoom and keeps the world point under `screenPoint` where it is (what the wheel and pinch do). */
|
|
47
36
|
export function zoomAtPoint(viewport, screenPoint, nextZoom) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return { x: screenPoint.x - worldPoint.x * zoom, y: screenPoint.y - worldPoint.y * zoom, zoom
|
|
37
|
+
const zoom = clampZoom(nextZoom);
|
|
38
|
+
const worldPoint = screenToWorld(viewport, screenPoint);
|
|
39
|
+
return { x: screenPoint.x - worldPoint.x * zoom, y: screenPoint.y - worldPoint.y * zoom, zoom };
|
|
51
40
|
}
|
|
52
41
|
export function panViewport(viewport, deltaX, deltaY) {
|
|
53
|
-
return
|
|
42
|
+
return { ...viewport, x: viewport.x + deltaX, y: viewport.y + deltaY };
|
|
54
43
|
}
|
|
55
44
|
/** The next step above (direction 1) or below (-1) the current zoom, or the same zoom at the end of the ladder. */
|
|
56
45
|
export function nextZoomStep(zoom, direction) {
|
|
57
|
-
|
|
46
|
+
const tolerance = 1e-6;
|
|
58
47
|
if (direction === 1) {
|
|
59
|
-
|
|
60
|
-
return step
|
|
48
|
+
const step = ZOOM_STEPS.find((candidate) => candidate > zoom + tolerance);
|
|
49
|
+
return step ?? ZOOM_STEPS[ZOOM_STEPS.length - 1];
|
|
61
50
|
}
|
|
62
|
-
for (
|
|
51
|
+
for (let index = ZOOM_STEPS.length - 1; index >= 0; index -= 1) {
|
|
63
52
|
if (ZOOM_STEPS[index] < zoom - tolerance)
|
|
64
53
|
return ZOOM_STEPS[index];
|
|
65
54
|
}
|
|
@@ -69,29 +58,27 @@ export function nextZoomStep(zoom, direction) {
|
|
|
69
58
|
* The view that shows a rectangle of the world, centred, with room around it.
|
|
70
59
|
* Never zooms in past `maxZoom` (a single small shape is not blown up to fill the board).
|
|
71
60
|
*/
|
|
72
|
-
export function fitViewportToRect(rect, viewSize, padding, maxZoom) {
|
|
73
|
-
if (padding === void 0) { padding = 48; }
|
|
74
|
-
if (maxZoom === void 0) { maxZoom = 1; }
|
|
61
|
+
export function fitViewportToRect(rect, viewSize, padding = 48, maxZoom = 1) {
|
|
75
62
|
if (viewSize.width <= 0 || viewSize.height <= 0)
|
|
76
63
|
return INITIAL_VIEWPORT;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return { x: viewSize.width / 2 - centerX * zoom, y: viewSize.height / 2 - centerY * zoom, zoom
|
|
64
|
+
const availableWidth = Math.max(1, viewSize.width - padding * 2);
|
|
65
|
+
const availableHeight = Math.max(1, viewSize.height - padding * 2);
|
|
66
|
+
const zoom = clampZoom(Math.min(maxZoom, rect.width > 0 ? availableWidth / rect.width : maxZoom, rect.height > 0 ? availableHeight / rect.height : maxZoom));
|
|
67
|
+
const centerX = rect.x + rect.width / 2;
|
|
68
|
+
const centerY = rect.y + rect.height / 2;
|
|
69
|
+
return { x: viewSize.width / 2 - centerX * zoom, y: viewSize.height / 2 - centerY * zoom, zoom };
|
|
83
70
|
}
|
|
84
71
|
/** The CSS transform of the world layer. */
|
|
85
72
|
export function viewportToCssTransform(viewport) {
|
|
86
|
-
return
|
|
73
|
+
return `translate(${Number(viewport.x.toFixed(3))}px, ${Number(viewport.y.toFixed(3))}px) scale(${Number(viewport.zoom.toFixed(5))})`;
|
|
87
74
|
}
|
|
88
75
|
/** A viewport read from untrusted data: anything that is not a finite number falls back to the default. */
|
|
89
76
|
export function sanitizeViewport(value) {
|
|
90
77
|
if (!value || typeof value !== 'object')
|
|
91
78
|
return INITIAL_VIEWPORT;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return { x
|
|
79
|
+
const candidate = value;
|
|
80
|
+
const x = typeof candidate.x === 'number' && Number.isFinite(candidate.x) ? candidate.x : 0;
|
|
81
|
+
const y = typeof candidate.y === 'number' && Number.isFinite(candidate.y) ? candidate.y : 0;
|
|
82
|
+
const zoom = typeof candidate.zoom === 'number' ? clampZoom(candidate.zoom) : 1;
|
|
83
|
+
return { x, y, zoom };
|
|
97
84
|
}
|