@widgetic/canvas 0.5.45 → 0.5.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- 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 -2
- package/dist/canvas/host/CanvasHost.js +1615 -1847
- 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/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/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +21 -21
- 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.js +35 -41
- package/dist/canvas/view/CanvasStage.svelte +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- 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/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +9 -2
- package/dist/engine/EngineCore.js +798 -907
- 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.js +84 -96
- 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.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +32 -65
- 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.js +267 -310
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +35 -39
- 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.js +28 -30
- package/dist/poster/boardPoster.js +58 -83
- package/dist/poster/paintBoardPoster.js +16 -39
- package/dist/poster/posterPictures.js +27 -43
- package/dist/settings/rememberedTool.d.ts +50 -0
- package/dist/settings/rememberedTool.js +68 -0
- package/dist/settings/workspaceToolSettings.d.ts +8 -0
- package/dist/settings/workspaceToolSettings.js +35 -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 +11 -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 +2 -2
|
@@ -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
|
}
|