@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
|
@@ -9,20 +9,8 @@
|
|
|
9
9
|
* so the rules of the trail are held to in Node. The rules and the numbers are the ones of tldraw's eraser trail (tldraw
|
|
10
10
|
* is the reference the owner asked for); the code is our own.
|
|
11
11
|
*/
|
|
12
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
13
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
14
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
15
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
16
|
-
};
|
|
17
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
18
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
19
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
20
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
21
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
22
|
-
};
|
|
23
|
-
var _Scribble_instances, _Scribble_points, _Scribble_size, _Scribble_phase, _Scribble_incoming, _Scribble_sinceStepMs, _Scribble_takeIncoming;
|
|
24
12
|
/** Non-secret runtime constants of the trail. */
|
|
25
|
-
export
|
|
13
|
+
export const SCRIBBLE_CONFIG = {
|
|
26
14
|
/** How wide the head of the trail is, in screen pixels. */
|
|
27
15
|
size: 12,
|
|
28
16
|
/** How opaque the trail is drawn. */
|
|
@@ -41,78 +29,67 @@ export var SCRIBBLE_CONFIG = {
|
|
|
41
29
|
streamline: 0.32
|
|
42
30
|
};
|
|
43
31
|
/** How opaque a shape is drawn while the eraser has marked it: it stays behind, faded, until the pointer is released. */
|
|
44
|
-
export
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// How long it has been since the trail last bit its tail.
|
|
54
|
-
_Scribble_sinceStepMs.set(this, 0);
|
|
55
|
-
}
|
|
32
|
+
export const ERASING_OPACITY = 0.32;
|
|
33
|
+
export class Scribble {
|
|
34
|
+
#points = [];
|
|
35
|
+
#size = SCRIBBLE_CONFIG.size;
|
|
36
|
+
#phase = 'starting';
|
|
37
|
+
// The place the pointer reached that the trail has not taken in yet (the next step takes it).
|
|
38
|
+
#incoming = null;
|
|
39
|
+
// How long it has been since the trail last bit its tail.
|
|
40
|
+
#sinceStepMs = 0;
|
|
56
41
|
/**
|
|
57
42
|
* The pointer reached a place. The next step takes it in; of several places given between two steps the last one counts.
|
|
58
43
|
* A place less than a pixel (on the screen at `zoom`) from the trail's head adds nothing the eye can see.
|
|
59
44
|
*/
|
|
60
|
-
|
|
61
|
-
if (
|
|
45
|
+
add(point, zoom) {
|
|
46
|
+
if (this.#phase === 'stopping')
|
|
62
47
|
return;
|
|
63
48
|
if (!Number.isFinite(point.x) || !Number.isFinite(point.y))
|
|
64
49
|
return;
|
|
65
|
-
|
|
66
|
-
|
|
50
|
+
const head = this.#points[this.#points.length - 1];
|
|
51
|
+
const safeZoom = Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
|
|
67
52
|
if (!head || Math.hypot(point.x - head.x, point.y - head.y) * safeZoom >= SCRIBBLE_CONFIG.minDistance)
|
|
68
|
-
|
|
69
|
-
}
|
|
53
|
+
this.#incoming = point;
|
|
54
|
+
}
|
|
70
55
|
/** The pointer let go: from now on the trail is only eaten, from its tail to its head. */
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
});
|
|
83
|
-
Object.defineProperty(Scribble.prototype, "phase", {
|
|
84
|
-
get: function () {
|
|
85
|
-
return __classPrivateFieldGet(this, _Scribble_phase, "f");
|
|
86
|
-
},
|
|
87
|
-
enumerable: false,
|
|
88
|
-
configurable: true
|
|
89
|
-
});
|
|
56
|
+
stop() {
|
|
57
|
+
this.#phase = 'stopping';
|
|
58
|
+
this.#incoming = null;
|
|
59
|
+
}
|
|
60
|
+
/** True when the trail has been let go and nothing of it is left to show. */
|
|
61
|
+
get finished() {
|
|
62
|
+
return this.#phase === 'stopping' && this.#points.length === 0;
|
|
63
|
+
}
|
|
64
|
+
get phase() {
|
|
65
|
+
return this.#phase;
|
|
66
|
+
}
|
|
90
67
|
/**
|
|
91
68
|
* One frame of the animation clock. `elapsedMs` is the time since the last frame. Returns whether what the trail looks
|
|
92
69
|
* like changed (a new point, a bitten tail, a thinner head), so the caller draws only then.
|
|
93
70
|
*/
|
|
94
|
-
|
|
95
|
-
if (
|
|
71
|
+
step(elapsedMs) {
|
|
72
|
+
if (this.#phase === 'starting') {
|
|
96
73
|
// It only grows at first: nothing is bitten until it holds more than `maxPoints`.
|
|
97
|
-
|
|
98
|
-
if (
|
|
99
|
-
|
|
74
|
+
const grew = this.#takeIncoming();
|
|
75
|
+
if (this.#points.length > SCRIBBLE_CONFIG.maxPoints)
|
|
76
|
+
this.#phase = 'active';
|
|
100
77
|
return grew;
|
|
101
78
|
}
|
|
102
|
-
|
|
103
|
-
|
|
79
|
+
this.#sinceStepMs += Number.isFinite(elapsedMs) && elapsedMs > 0 ? elapsedMs : 0;
|
|
80
|
+
const atStep = this.#sinceStepMs >= SCRIBBLE_CONFIG.stepMs;
|
|
104
81
|
if (atStep)
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
if (
|
|
82
|
+
this.#sinceStepMs = 0;
|
|
83
|
+
if (this.#phase === 'active') {
|
|
84
|
+
if (this.#takeIncoming()) {
|
|
108
85
|
// Following the pointer: a new point at the head, the oldest let go.
|
|
109
|
-
if (
|
|
110
|
-
|
|
86
|
+
if (this.#points.length > SCRIBBLE_CONFIG.maxPoints)
|
|
87
|
+
this.#points.shift();
|
|
111
88
|
return true;
|
|
112
89
|
}
|
|
113
90
|
// The pointer rests: the tail catches up with the head.
|
|
114
|
-
if (atStep &&
|
|
115
|
-
|
|
91
|
+
if (atStep && this.#points.length > 1) {
|
|
92
|
+
this.#points.shift();
|
|
116
93
|
return true;
|
|
117
94
|
}
|
|
118
95
|
return false;
|
|
@@ -120,27 +97,25 @@ var Scribble = /** @class */ (function () {
|
|
|
120
97
|
// Let go: every bite takes a point from the tail and a tenth of the width, until one point is left, and then that goes.
|
|
121
98
|
if (!atStep)
|
|
122
99
|
return false;
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
100
|
+
if (this.#points.length <= 1) {
|
|
101
|
+
const hadPoints = this.#points.length > 0;
|
|
102
|
+
this.#points = [];
|
|
126
103
|
return hadPoints;
|
|
127
104
|
}
|
|
128
|
-
|
|
129
|
-
|
|
105
|
+
this.#size = Math.max(SCRIBBLE_CONFIG.minSize, this.#size * (1 - SCRIBBLE_CONFIG.shrink));
|
|
106
|
+
this.#points.shift();
|
|
130
107
|
return true;
|
|
131
|
-
}
|
|
108
|
+
}
|
|
132
109
|
/** What to draw now. */
|
|
133
|
-
|
|
134
|
-
return { points:
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return true;
|
|
146
|
-
};
|
|
110
|
+
view() {
|
|
111
|
+
return { points: this.#points.slice(), size: this.#size, stopping: this.#phase === 'stopping' };
|
|
112
|
+
}
|
|
113
|
+
#takeIncoming() {
|
|
114
|
+
const incoming = this.#incoming;
|
|
115
|
+
if (!incoming)
|
|
116
|
+
return false;
|
|
117
|
+
this.#incoming = null;
|
|
118
|
+
this.#points.push(incoming);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -11,24 +11,22 @@ import { generatePathForShape, getShapeByKey, roundedPolygonPath } from '../../c
|
|
|
11
11
|
import { clampNumber } from './geometry.js';
|
|
12
12
|
import { fitPathToBox, parsePathData } from './pathData.js';
|
|
13
13
|
/** The `shape` key of a triangle path. */
|
|
14
|
-
export
|
|
14
|
+
export const TRIANGLE_SHAPE_KEY = 'triangle';
|
|
15
15
|
/** The `shape` key of an arrow path (its geometry is in `arrow.ts`). */
|
|
16
|
-
export
|
|
16
|
+
export const ARROW_SHAPE_KEY = 'arrow';
|
|
17
17
|
/** The categories of the library whose shapes keep their proportions when resized (symbols and icons). */
|
|
18
|
-
|
|
18
|
+
const ASPECT_LOCKED_CATEGORIES = new Set(['Symbol Shapes', 'Media Icons', 'UI Icons']);
|
|
19
19
|
/** Whether a library shape keeps its aspect ratio: its own setting, else the rule of its category. */
|
|
20
20
|
export function libraryShapeKeepsAspect(shape) {
|
|
21
|
-
|
|
22
|
-
return (_a = shape.lockAspectRatio) !== null && _a !== void 0 ? _a : ASPECT_LOCKED_CATEGORIES.has(shape.category);
|
|
21
|
+
return shape.lockAspectRatio ?? ASPECT_LOCKED_CATEGORIES.has(shape.category);
|
|
23
22
|
}
|
|
24
23
|
/**
|
|
25
24
|
* A triangle with its apex at the top middle and its base along the bottom, with corners rounded by
|
|
26
25
|
* `roundness` percent (100 rounds by a quarter of the shorter side).
|
|
27
26
|
*/
|
|
28
|
-
export function trianglePathCommands(width, height, roundness) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var data = roundedPolygonPath([
|
|
27
|
+
export function trianglePathCommands(width, height, roundness = 0) {
|
|
28
|
+
const radius = (clampNumber(roundness, 0, 100) / 100) * (Math.min(width, height) / 4);
|
|
29
|
+
const data = roundedPolygonPath([
|
|
32
30
|
[width / 2, 0],
|
|
33
31
|
[width, height],
|
|
34
32
|
[0, height]
|
|
@@ -39,13 +37,12 @@ export function trianglePathCommands(width, height, roundness) {
|
|
|
39
37
|
* A library shape fitted to a box. Shapes that can round their corners are regenerated at the given
|
|
40
38
|
* roundness; the others are the stored outline, scaled. Null for a key the library does not know.
|
|
41
39
|
*/
|
|
42
|
-
export function libraryShapePathCommands(shapeKey, width, height, roundness) {
|
|
43
|
-
|
|
44
|
-
var shape = getShapeByKey(shapeKey);
|
|
40
|
+
export function libraryShapePathCommands(shapeKey, width, height, roundness = 0) {
|
|
41
|
+
const shape = getShapeByKey(shapeKey);
|
|
45
42
|
if (!shape)
|
|
46
43
|
return null;
|
|
47
|
-
|
|
48
|
-
return fitPathToBox(parsePathData(regenerated
|
|
44
|
+
const regenerated = shape.cornerRadiusMode === 'pathRegen' ? generatePathForShape(shape, roundness) : null;
|
|
45
|
+
return fitPathToBox(parsePathData(regenerated ?? shape.pathData), width, height);
|
|
49
46
|
}
|
|
50
47
|
/** Whether a path of this kind is rebuilt from its key when it is resized (rather than scaled). */
|
|
51
48
|
export function isRegeneratedShape(shapeKey) {
|
|
@@ -53,7 +50,7 @@ export function isRegeneratedShape(shapeKey) {
|
|
|
53
50
|
return false;
|
|
54
51
|
if (shapeKey === TRIANGLE_SHAPE_KEY)
|
|
55
52
|
return true;
|
|
56
|
-
|
|
53
|
+
const shape = getShapeByKey(shapeKey);
|
|
57
54
|
return !!shape && shape.cornerRadiusMode === 'pathRegen';
|
|
58
55
|
}
|
|
59
56
|
/** Whether the Roundness control applies to this path. */
|
|
@@ -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 {};
|
|
@@ -3,78 +3,69 @@
|
|
|
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
|
-
var __assign = (this && this.__assign) || function () {
|
|
7
|
-
__assign = Object.assign || function(t) {
|
|
8
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
9
|
-
s = arguments[i];
|
|
10
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
11
|
-
t[p] = s[p];
|
|
12
|
-
}
|
|
13
|
-
return t;
|
|
14
|
-
};
|
|
15
|
-
return __assign.apply(this, arguments);
|
|
16
|
-
};
|
|
17
6
|
import { getShapeByKey } from '../../canvas/shapes/ShapeLibrary.js';
|
|
18
7
|
import { DEFAULT_OBJECT_SIZE, SHAPE_STYLE_DEFAULTS, createEllipseObject, createObjectId, createPathObject, createRectObject, randomShapeFill } from './defaults.js';
|
|
19
8
|
import { buildArrow } from './arrow.js';
|
|
20
9
|
import { distanceBetween, rectFromPoints } from './geometry.js';
|
|
21
10
|
import { ARROW_SHAPE_KEY, TRIANGLE_SHAPE_KEY, libraryShapeKeepsAspect, libraryShapePathCommands, trianglePathCommands } from './shapePaths.js';
|
|
22
11
|
import { serializePathData } from './pathData.js';
|
|
12
|
+
import { MAX_STROKE_WIDTH, MIN_LINE_STROKE_WIDTH } from './style.js';
|
|
13
|
+
import { withRememberedStyle } from './styleMemory.js';
|
|
23
14
|
import { snapAngle } from './transform.js';
|
|
24
15
|
import { MIN_OBJECT_SIZE } from './types.js';
|
|
25
16
|
/** A drag shorter than this (in screen pixels) is a click: the shape is placed at its default size. */
|
|
26
|
-
export
|
|
17
|
+
export const MIN_DRAW_DISTANCE = 4;
|
|
27
18
|
/** The size of a library shape placed by a click. */
|
|
28
|
-
export
|
|
19
|
+
export const LIBRARY_SHAPE_CLICK_SIZE = 50;
|
|
29
20
|
/** The length of an arrow placed by a click. */
|
|
30
|
-
export
|
|
21
|
+
export const ARROW_CLICK_LENGTH = 100;
|
|
31
22
|
/** Shift holds an arrow to a multiple of this many degrees (tldraw divides the circle into 24: 15 degrees). */
|
|
32
|
-
export
|
|
23
|
+
export const ARROW_SNAP_DEGREES = 15;
|
|
33
24
|
/** A triangle's height for a width, when it is equilateral. */
|
|
34
|
-
export
|
|
25
|
+
export const EQUILATERAL_RATIO = Math.sqrt(3) / 2;
|
|
35
26
|
/** A triangle added by the toolbar button rather than drawn. */
|
|
36
|
-
export
|
|
27
|
+
export const BUTTON_TRIANGLE_SIZE = { width: 100, height: 150 };
|
|
37
28
|
/** How soft the corners of a rounded square start (0 to 100: 100 is a pill). The Roundness control moves it afterwards. */
|
|
38
|
-
export
|
|
29
|
+
export const ROUNDED_RECT_ROUNDNESS = 35;
|
|
39
30
|
/** A box of a given aspect (width / height) that has the dragged corner for one of its corners. */
|
|
40
31
|
function boxWithAspect(start, end, aspect) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
const deltaX = end.x - start.x;
|
|
33
|
+
const deltaY = end.y - start.y;
|
|
34
|
+
const signX = deltaX < 0 ? -1 : 1;
|
|
35
|
+
const signY = deltaY < 0 ? -1 : 1;
|
|
45
36
|
// The box takes the larger of what the pointer asked for, so the shape follows the pointer's far corner.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
37
|
+
const width = Math.max(Math.abs(deltaX), Math.abs(deltaY) * aspect);
|
|
38
|
+
const height = width / aspect;
|
|
39
|
+
const right = start.x + signX * width;
|
|
40
|
+
const bottom = start.y + signY * height;
|
|
50
41
|
return rectFromPoints(start, { x: right, y: bottom });
|
|
51
42
|
}
|
|
52
43
|
/** A box of a given size centred on a point. */
|
|
53
44
|
function centeredBox(center, width, height) {
|
|
54
|
-
return { x: center.x - width / 2, y: center.y - height / 2, width
|
|
45
|
+
return { x: center.x - width / 2, y: center.y - height / 2, width, height };
|
|
55
46
|
}
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
|
61
53
|
? {
|
|
62
54
|
fill: SHAPE_STYLE_DEFAULTS.wireframeFill,
|
|
63
55
|
stroke: SHAPE_STYLE_DEFAULTS.wireframeStroke,
|
|
64
56
|
strokeWidth: SHAPE_STYLE_DEFAULTS.wireframeStrokeWidth
|
|
65
57
|
}
|
|
66
58
|
: { fill: randomShapeFill(random), stroke: SHAPE_STYLE_DEFAULTS.stroke, strokeWidth: SHAPE_STYLE_DEFAULTS.strokeWidth };
|
|
67
|
-
return __assign(__assign({}, base), workspacePaint);
|
|
68
59
|
}
|
|
69
60
|
/** The look of a library shape, and of an arrow. */
|
|
70
|
-
export
|
|
71
|
-
export
|
|
61
|
+
export const LIBRARY_SHAPE_PAINT = { fill: '#ffffff', stroke: '#374151', strokeWidth: 3 };
|
|
62
|
+
export const ARROW_PAINT = { fill: 'transparent', stroke: '#111827', strokeWidth: 3 };
|
|
72
63
|
/** The box a shape tool would place for a drag. */
|
|
73
64
|
export function shapeDragBox(options) {
|
|
74
|
-
|
|
65
|
+
const { kind, start, end, constrain, dragged } = options;
|
|
75
66
|
if (kind === 'arrow')
|
|
76
67
|
return arrowDragGeometry(options).box;
|
|
77
|
-
|
|
68
|
+
const aspectLockedLibrary = kind === 'library-shape' && libraryKeepsAspect(options.libraryKey);
|
|
78
69
|
if (!dragged) {
|
|
79
70
|
if (kind === 'triangle')
|
|
80
71
|
return centeredBox(start, DEFAULT_OBJECT_SIZE, DEFAULT_OBJECT_SIZE * EQUILATERAL_RATIO);
|
|
@@ -90,12 +81,12 @@ export function shapeDragBox(options) {
|
|
|
90
81
|
return rectFromPoints(start, end);
|
|
91
82
|
}
|
|
92
83
|
function libraryKeepsAspect(key) {
|
|
93
|
-
|
|
84
|
+
const shape = getShapeByKey(key ?? null);
|
|
94
85
|
return !!shape && libraryShapeKeepsAspect(shape);
|
|
95
86
|
}
|
|
96
87
|
/** The end of an arrow for a drag: where the pointer is, held to a multiple of 15 degrees with Shift, or the default length. */
|
|
97
88
|
export function arrowEnd(options) {
|
|
98
|
-
|
|
89
|
+
const { start, end, constrain, dragged } = options;
|
|
99
90
|
if (!dragged)
|
|
100
91
|
return { x: start.x + ARROW_CLICK_LENGTH, y: start.y };
|
|
101
92
|
if (!constrain)
|
|
@@ -104,51 +95,97 @@ export function arrowEnd(options) {
|
|
|
104
95
|
}
|
|
105
96
|
/** A point turned about a centre, to the nearest multiple of `step` degrees of direction, at the same distance. */
|
|
106
97
|
export function snapPointAround(center, point, step) {
|
|
107
|
-
|
|
108
|
-
|
|
98
|
+
const length = distanceBetween(center, point);
|
|
99
|
+
const angle = (snapAngle((Math.atan2(point.y - center.y, point.x - center.x) * 180) / Math.PI, step) * Math.PI) / 180;
|
|
109
100
|
return { x: center.x + length * Math.cos(angle), y: center.y + length * Math.sin(angle) };
|
|
110
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
|
+
}
|
|
111
114
|
function arrowDragGeometry(options) {
|
|
112
|
-
return buildArrow(options.start, arrowEnd(options), 0,
|
|
115
|
+
return buildArrow(options.start, arrowEnd(options), 0, arrowStrokeWidth(options));
|
|
113
116
|
}
|
|
114
117
|
/** The shape a tool would place for a drag. */
|
|
115
118
|
export function buildDraggedShape(options) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
var kind = options.kind, wireframe = options.wireframe;
|
|
119
|
+
const createId = options.createId ?? createObjectId;
|
|
120
|
+
const id = createId('obj');
|
|
121
|
+
const { kind, wireframe, rememberedStyles } = options;
|
|
120
122
|
if (kind === 'arrow') {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
+
const geometry = arrowDragGeometry(options);
|
|
124
|
+
const arrow = createPathObject({
|
|
125
|
+
id,
|
|
126
|
+
...geometry.box,
|
|
127
|
+
d: serializePathData(geometry.commands),
|
|
128
|
+
arrow: geometry.spec,
|
|
129
|
+
shape: ARROW_SHAPE_KEY,
|
|
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),
|
|
133
|
+
lineCap: 'round',
|
|
134
|
+
lineJoin: 'round'
|
|
135
|
+
});
|
|
136
|
+
return withRememberedStyle(arrow, rememberedStyles?.arrow);
|
|
123
137
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
138
|
+
const box = shapeDragBox(options);
|
|
139
|
+
const width = Math.max(box.width, MIN_OBJECT_SIZE);
|
|
140
|
+
const height = Math.max(box.height, MIN_OBJECT_SIZE);
|
|
141
|
+
const frame = { id, x: box.x, y: box.y, width, height };
|
|
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;
|
|
129
145
|
if (kind === 'rect')
|
|
130
|
-
return createRectObject(
|
|
131
|
-
// 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.
|
|
132
149
|
if (kind === 'rounded-rect') {
|
|
133
|
-
return createRectObject(
|
|
150
|
+
return withRememberedStyle(createRectObject({ ...frame, ...paint, roundness: ROUNDED_RECT_ROUNDNESS }), rememberedShape, { leave: ['roundness'] });
|
|
134
151
|
}
|
|
135
152
|
if (kind === 'ellipse' || kind === 'circle') {
|
|
136
|
-
return createEllipseObject(
|
|
153
|
+
return withRememberedStyle(createEllipseObject({ ...frame, ...paint, aspectLocked: kind === 'circle' }), rememberedShape);
|
|
137
154
|
}
|
|
138
155
|
if (kind === 'triangle') {
|
|
139
|
-
|
|
156
|
+
const triangle = createPathObject({
|
|
157
|
+
...frame,
|
|
158
|
+
...paint,
|
|
159
|
+
d: serializePathData(trianglePathCommands(width, height, 0)),
|
|
160
|
+
shape: TRIANGLE_SHAPE_KEY,
|
|
161
|
+
lineJoin: 'miter'
|
|
162
|
+
});
|
|
163
|
+
return withRememberedStyle(triangle, rememberedShape);
|
|
140
164
|
}
|
|
141
|
-
return buildLibraryShape(
|
|
165
|
+
return buildLibraryShape({ ...frame, key: options.libraryKey ?? null, rememberedStyles });
|
|
142
166
|
}
|
|
143
|
-
/**
|
|
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
|
+
*/
|
|
144
172
|
export function buildLibraryShape(fields) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
173
|
+
const shape = getShapeByKey(fields.key);
|
|
174
|
+
const frame = { id: fields.id, x: fields.x, y: fields.y, width: fields.width, height: fields.height };
|
|
175
|
+
const commands = fields.key && shape ? libraryShapePathCommands(fields.key, fields.width, fields.height, 0) : null;
|
|
148
176
|
if (!shape || !commands)
|
|
149
|
-
return createRectObject(
|
|
150
|
-
|
|
151
|
-
|
|
177
|
+
return withRememberedStyle(createRectObject({ ...frame, ...newShapePaint(true) }), fields.rememberedStyles?.shape);
|
|
178
|
+
const linesOnly = shape.cornerRadiusMode === 'strokeRound';
|
|
179
|
+
const libraryShape = createPathObject({
|
|
180
|
+
...frame,
|
|
181
|
+
...LIBRARY_SHAPE_PAINT,
|
|
152
182
|
// A line-only icon (a check, a cross) has no inside to fill.
|
|
153
|
-
fill: linesOnly ? 'transparent' : LIBRARY_SHAPE_PAINT.fill,
|
|
183
|
+
fill: linesOnly ? 'transparent' : LIBRARY_SHAPE_PAINT.fill,
|
|
184
|
+
d: serializePathData(commands),
|
|
185
|
+
shape: shape.key,
|
|
186
|
+
aspectLocked: libraryShapeKeepsAspect(shape),
|
|
187
|
+
lineCap: linesOnly ? 'butt' : 'round',
|
|
188
|
+
lineJoin: linesOnly ? 'miter' : 'round'
|
|
189
|
+
});
|
|
190
|
+
return withRememberedStyle(libraryShape, linesOnly ? fields.rememberedStyles?.lineIcon : fields.rememberedStyles?.shape);
|
|
154
191
|
}
|