@widgetic/canvas 0.5.43 → 0.5.45
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/dist/canvas/DomCanvas.svelte +56 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +59 -28
- package/dist/canvas/autosaveScheduler.js +89 -37
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +19 -7
- package/dist/canvas/chromeLayout.js +21 -19
- package/dist/canvas/eventScope.js +14 -10
- package/dist/canvas/host/CanvasHost.d.ts +87 -2
- package/dist/canvas/host/CanvasHost.js +1948 -1394
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +32 -5
- package/dist/canvas/host/boardFiles.d.ts +90 -0
- package/dist/canvas/host/boardFiles.js +155 -0
- package/dist/canvas/host/browserServices.d.ts +17 -0
- package/dist/canvas/host/browserServices.js +211 -65
- package/dist/canvas/host/conversion.js +170 -82
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.js +21 -18
- package/dist/canvas/host/pageEvents.js +5 -5
- package/dist/canvas/host/pictureSaves.js +11 -9
- package/dist/canvas/host/screenRects.js +31 -28
- package/dist/canvas/host/storedPictures.js +243 -149
- package/dist/canvas/host/strokeSnap.js +143 -88
- package/dist/canvas/index.d.ts +2 -0
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +7 -6
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +25 -13
- package/dist/canvas/shapes/ShapeLibrary.js +148 -138
- package/dist/canvas/shapes/shapeImageStore.js +4 -4
- package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/types.d.ts +17 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +7 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +4 -1
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +2 -0
- package/dist/canvas/ui/ColorField.svelte +7 -2
- package/dist/canvas/ui/SelectionPanel.svelte +105 -42
- package/dist/canvas/ui/ShapesMenu.svelte +65 -31
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +2 -0
- package/dist/canvas/ui/ShortcutTooltip.svelte +64 -0
- package/dist/canvas/ui/ShortcutTooltip.svelte.d.ts +24 -0
- package/dist/canvas/ui/ToolButton.svelte +35 -54
- package/dist/canvas/ui/ToolButton.svelte.d.ts +4 -1
- package/dist/canvas/ui/colorModel.js +32 -21
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +26 -4
- package/dist/canvas/ui/floatingCard.js +43 -21
- package/dist/canvas/ui/floatingLayer.js +6 -5
- package/dist/canvas/ui/icons.d.ts +1 -1
- package/dist/canvas/ui/icons.js +1 -1
- package/dist/canvas/ui/keyHints.js +11 -9
- package/dist/canvas/ui/popoverSide.js +3 -2
- package/dist/canvas/ui/selectionTraits.d.ts +12 -0
- package/dist/canvas/ui/selectionTraits.js +66 -35
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +4 -2
- package/dist/canvas/ui/sizeLabelVisibility.js +5 -5
- package/dist/canvas/view/CanvasStage.svelte +7 -2
- package/dist/canvas/view/CanvasStage.svelte.d.ts +2 -0
- package/dist/canvas/view/SelectionOverlay.svelte +17 -10
- package/dist/canvas/view/WidgetChromeLayer.svelte +15 -3
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.d.ts +1 -1
- package/dist/canvas/view/geometry.js +90 -71
- package/dist/canvas/view/liftedScene.js +36 -24
- package/dist/canvas/view/mountedScene.js +79 -76
- package/dist/components/CanvasBoardPoster.svelte +206 -0
- package/dist/components/CanvasBoardPoster.svelte.d.ts +17 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte +115 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +9 -0
- package/dist/components/index.d.ts +7 -0
- package/dist/components/index.js +8 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +111 -2
- package/dist/engine/EngineCore.js +1013 -707
- package/dist/engine/codec/canvasDocument.d.ts +7 -0
- package/dist/engine/codec/canvasDocument.js +172 -186
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +38 -26
- package/dist/engine/core/arrow.d.ts +18 -8
- package/dist/engine/core/arrow.js +109 -84
- package/dist/engine/core/arrowBinding.d.ts +1 -1
- package/dist/engine/core/arrowBinding.js +126 -101
- package/dist/engine/core/arrowBindingRecords.d.ts +9 -3
- package/dist/engine/core/arrowBindingRecords.js +48 -29
- package/dist/engine/core/brush.d.ts +8 -0
- package/dist/engine/core/brush.js +98 -78
- package/dist/engine/core/cameraMove.d.ts +73 -0
- package/dist/engine/core/cameraMove.js +172 -0
- package/dist/engine/core/defaults.js +51 -90
- package/dist/engine/core/freehand.js +163 -147
- package/dist/engine/core/geometry.js +54 -47
- package/dist/engine/core/history.js +55 -38
- package/dist/engine/core/hitTest.js +81 -68
- package/dist/engine/core/matrix.js +20 -18
- package/dist/engine/core/pathData.js +210 -183
- package/dist/engine/core/rotationReset.js +6 -5
- package/dist/engine/core/scene.d.ts +22 -0
- package/dist/engine/core/scene.js +288 -139
- package/dist/engine/core/scribble.js +84 -59
- package/dist/engine/core/shapePaths.js +15 -12
- package/dist/engine/core/shapeTools.js +63 -72
- package/dist/engine/core/snapping.js +117 -65
- package/dist/engine/core/style.d.ts +10 -0
- package/dist/engine/core/style.js +82 -33
- package/dist/engine/core/transform.d.ts +6 -0
- package/dist/engine/core/transform.js +103 -83
- package/dist/engine/core/types.d.ts +7 -0
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +36 -23
- package/dist/engine/input/PointerController.d.ts +15 -1
- package/dist/engine/input/PointerController.js +549 -466
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.d.ts +3 -1
- package/dist/engine/input/gestures.js +340 -266
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.d.ts +22 -3
- package/dist/engine/input/keyboard.js +131 -32
- package/dist/engine/input/selectionHandles.js +50 -45
- package/dist/engine/input/types.d.ts +7 -0
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +158 -86
- package/dist/engine/state.d.ts +18 -0
- package/dist/engine/state.js +25 -4
- package/dist/engine/text/layoutText.js +62 -43
- package/dist/engine/text/textMetrics.js +10 -8
- package/dist/engine/widgets.js +137 -101
- package/dist/fonts/interFont.js +83 -30
- package/dist/fonts/workspaceFonts.d.ts +6 -0
- package/dist/fonts/workspaceFonts.js +108 -34
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +6 -0
- package/dist/i18n/messages.js +7 -1
- package/dist/i18n/shortcuts.d.ts +6 -4
- package/dist/i18n/shortcuts.js +30 -26
- package/dist/index.d.ts +2 -1
- package/dist/poster/boardPlacement.d.ts +80 -0
- package/dist/poster/boardPlacement.js +140 -0
- package/dist/poster/boardPoster.d.ts +80 -0
- package/dist/poster/boardPoster.js +251 -0
- package/dist/poster/paintBoardPoster.d.ts +49 -0
- package/dist/poster/paintBoardPoster.js +112 -0
- package/dist/poster/posterPictures.d.ts +32 -0
- package/dist/poster/posterPictures.js +138 -0
- package/dist/settings/workspaceToolSettings.d.ts +6 -0
- package/dist/settings/workspaceToolSettings.js +58 -34
- package/dist/tools/components/catalog.js +74 -64
- package/dist/tools/components/grouping.js +28 -6
- package/dist/tools/definitions.js +31 -9
- package/dist/tools/executors.js +214 -106
- package/dist/tools/hostAdapter.d.ts +5 -1
- package/dist/tools/hostAdapter.js +15 -11
- package/dist/tools/sceneSummary.js +57 -43
- package/dist/tools/sketchIntent.js +21 -13
- package/dist/tools/styleArguments.d.ts +35 -0
- package/dist/tools/styleArguments.js +93 -0
- package/dist/tools/types.d.ts +26 -1
- package/dist/tools/types.js +2 -1
- package/package.json +1 -1
|
@@ -17,34 +17,46 @@
|
|
|
17
17
|
*
|
|
18
18
|
* `pickArrowBinding` is the other half: which shape a pointer would tie an end to (the topmost one under it that an arrow can be tied
|
|
19
19
|
* to; a hollow shape only near its line, as for any click on it) and how (`edge` when the pointer is near the outline, `center` when
|
|
20
|
-
* it is further in).
|
|
20
|
+
* it is further in; a line drawn by hand is always `edge`: it has no middle to aim at).
|
|
21
21
|
*/
|
|
22
|
+
var __assign = (this && this.__assign) || function () {
|
|
23
|
+
__assign = Object.assign || function(t) {
|
|
24
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
25
|
+
s = arguments[i];
|
|
26
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
27
|
+
t[p] = s[p];
|
|
28
|
+
}
|
|
29
|
+
return t;
|
|
30
|
+
};
|
|
31
|
+
return __assign.apply(this, arguments);
|
|
32
|
+
};
|
|
22
33
|
import { arrowPointsInWorld, isArrow, reshapeArrow } from './arrow.js';
|
|
23
34
|
import { ARROW_ENDS, bindingOfEnd, canBeTiedTo, hasBindings } from './arrowBindingRecords.js';
|
|
35
|
+
import { isBrushStroke } from './brush.js';
|
|
24
36
|
import { clampNumber, distanceBetween, pointInPolygon, worldToBoxLocal } from './geometry.js';
|
|
25
37
|
import { flattenedOutline, shapeContainsPoint } from './hitTest.js';
|
|
26
38
|
import { applyMatrixToPoint, boxToWorldMatrix } from './matrix.js';
|
|
27
39
|
/** How far from a shape (screen pixels) the pointer may be and still tie an end to it: the reach a click has on a line. */
|
|
28
|
-
export
|
|
40
|
+
export var ARROW_BIND_REACH_PIXELS = 6;
|
|
29
41
|
/** Within this many screen pixels of the outline (inside or outside) an end rests on that point of the outline; further in it aims at the middle. */
|
|
30
|
-
export
|
|
42
|
+
export var ARROW_EDGE_BAND_PIXELS = 14;
|
|
31
43
|
/** The band never takes more than this fraction of the shorter side of the shape, so that a small shape still has a middle to aim at. */
|
|
32
|
-
export
|
|
44
|
+
export var ARROW_EDGE_BAND_FRACTION = 0.25;
|
|
33
45
|
/** Two points closer than this (world units) are the same place: an arrow that is there already is not drawn again. */
|
|
34
|
-
|
|
46
|
+
var SAME_PLACE = 1e-6;
|
|
35
47
|
/** The straight pieces a quarter of a rounded corner is cut into when the outline of a rounded rectangle is walked. */
|
|
36
|
-
|
|
48
|
+
var CORNER_STEPS = 8;
|
|
37
49
|
/** The radius of the corners of a rectangle with a roundness of 0 to 100 (100 is a pill): the one the board draws (`cornerRadiusOf`). */
|
|
38
50
|
function cornerRadius(shape) {
|
|
39
51
|
return (clampNumber(shape.roundness, 0, 100) / 100) * (Math.min(shape.width, shape.height) / 2);
|
|
40
52
|
}
|
|
41
53
|
/** The outline of a rectangle with rounded corners, clockwise on the screen from the left end of its top edge. */
|
|
42
54
|
function roundedBoxLines(width, height, radius) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
for (
|
|
47
|
-
|
|
55
|
+
var reach = clampNumber(radius, 0, Math.min(width, height) / 2);
|
|
56
|
+
var points = [];
|
|
57
|
+
var corner = function (centerX, centerY, startAngle) {
|
|
58
|
+
for (var step = 0; step <= CORNER_STEPS; step += 1) {
|
|
59
|
+
var angle = startAngle + (step / CORNER_STEPS) * (Math.PI / 2);
|
|
48
60
|
points.push({ x: centerX + reach * Math.cos(angle), y: centerY + reach * Math.sin(angle) });
|
|
49
61
|
}
|
|
50
62
|
};
|
|
@@ -52,44 +64,44 @@ function roundedBoxLines(width, height, radius) {
|
|
|
52
64
|
corner(width - reach, reach, Math.PI * 1.5);
|
|
53
65
|
corner(width - reach, height - reach, 0);
|
|
54
66
|
corner(reach, height - reach, Math.PI / 2);
|
|
55
|
-
return { points, closed: true };
|
|
67
|
+
return { points: points, closed: true };
|
|
56
68
|
}
|
|
57
69
|
function outlineOf(shape) {
|
|
58
|
-
|
|
70
|
+
var width = shape.width, height = shape.height;
|
|
59
71
|
switch (shape.type) {
|
|
60
72
|
case 'ellipse':
|
|
61
|
-
return { kind: 'ellipse', width, height };
|
|
73
|
+
return { kind: 'ellipse', width: width, height: height };
|
|
62
74
|
case 'rect': {
|
|
63
|
-
|
|
64
|
-
return radius > 0.5 ? { kind: 'lines', width, height, lines: [roundedBoxLines(width, height, radius)] } : { kind: 'box', width, height };
|
|
75
|
+
var radius = cornerRadius(shape);
|
|
76
|
+
return radius > 0.5 ? { kind: 'lines', width: width, height: height, lines: [roundedBoxLines(width, height, radius)] } : { kind: 'box', width: width, height: height };
|
|
65
77
|
}
|
|
66
78
|
case 'path': {
|
|
67
|
-
|
|
68
|
-
return lines.length > 0 ? { kind: 'lines', width, height, lines } : { kind: 'box', width, height };
|
|
79
|
+
var lines = flattenedOutline(shape.d).filter(function (outline) { return outline.points.length >= 2; });
|
|
80
|
+
return lines.length > 0 ? { kind: 'lines', width: width, height: height, lines: lines } : { kind: 'box', width: width, height: height };
|
|
69
81
|
}
|
|
70
82
|
default:
|
|
71
83
|
// Text, pictures and widget cards are their box.
|
|
72
|
-
return { kind: 'box', width, height };
|
|
84
|
+
return { kind: 'box', width: width, height: height };
|
|
73
85
|
}
|
|
74
86
|
}
|
|
75
87
|
function closestOnSegment(point, first, second) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
var deltaX = second.x - first.x;
|
|
89
|
+
var deltaY = second.y - first.y;
|
|
90
|
+
var lengthSquared = deltaX * deltaX + deltaY * deltaY;
|
|
79
91
|
if (lengthSquared < 1e-12)
|
|
80
92
|
return first;
|
|
81
|
-
|
|
93
|
+
var along = clampNumber(((point.x - first.x) * deltaX + (point.y - first.y) * deltaY) / lengthSquared, 0, 1);
|
|
82
94
|
return { x: first.x + along * deltaX, y: first.y + along * deltaY };
|
|
83
95
|
}
|
|
84
96
|
/** The point of the outline nearest to a point (both in the coordinates of the box). */
|
|
85
97
|
function nearestOnOutline(outline, point) {
|
|
86
|
-
|
|
98
|
+
var width = outline.width, height = outline.height;
|
|
87
99
|
if (outline.kind === 'box') {
|
|
88
100
|
if (point.x < 0 || point.x > width || point.y < 0 || point.y > height) {
|
|
89
101
|
return { x: clampNumber(point.x, 0, width), y: clampNumber(point.y, 0, height) };
|
|
90
102
|
}
|
|
91
103
|
// Inside: the nearest of the four sides.
|
|
92
|
-
|
|
104
|
+
var least = Math.min(point.x, width - point.x, point.y, height - point.y);
|
|
93
105
|
if (least === point.x)
|
|
94
106
|
return { x: 0, y: point.y };
|
|
95
107
|
if (least === width - point.x)
|
|
@@ -99,24 +111,25 @@ function nearestOnOutline(outline, point) {
|
|
|
99
111
|
return { x: point.x, y: height };
|
|
100
112
|
}
|
|
101
113
|
if (outline.kind === 'ellipse') {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
114
|
+
var radiusX = Math.max(width / 2, 1e-6);
|
|
115
|
+
var radiusY = Math.max(height / 2, 1e-6);
|
|
116
|
+
var offsetX = point.x - width / 2;
|
|
117
|
+
var offsetY = point.y - height / 2;
|
|
118
|
+
var reach = Math.hypot(offsetX / radiusX, offsetY / radiusY);
|
|
107
119
|
// The middle has no nearest point: the one at the right will do.
|
|
108
120
|
if (reach < 1e-9)
|
|
109
121
|
return { x: width, y: height / 2 };
|
|
110
122
|
// The point of the ellipse on the line from the middle through the point.
|
|
111
123
|
return { x: width / 2 + offsetX / reach, y: height / 2 + offsetY / reach };
|
|
112
124
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
for (
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
125
|
+
var nearest = outline.lines[0].points[0];
|
|
126
|
+
var nearestDistance = Infinity;
|
|
127
|
+
for (var _i = 0, _a = outline.lines; _i < _a.length; _i++) {
|
|
128
|
+
var line = _a[_i];
|
|
129
|
+
var last = line.closed ? line.points.length : line.points.length - 1;
|
|
130
|
+
for (var index = 0; index < last; index += 1) {
|
|
131
|
+
var candidate = closestOnSegment(point, line.points[index], line.points[(index + 1) % line.points.length]);
|
|
132
|
+
var distance = distanceBetween(point, candidate);
|
|
120
133
|
if (distance < nearestDistance) {
|
|
121
134
|
nearest = candidate;
|
|
122
135
|
nearestDistance = distance;
|
|
@@ -127,15 +140,15 @@ function nearestOnOutline(outline, point) {
|
|
|
127
140
|
}
|
|
128
141
|
/** Where the segment from `first` to `second` meets the segment from `a` to `b`, as the fraction of the first it is along; null when they do not meet. */
|
|
129
142
|
function segmentMeeting(first, second, a, b) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
143
|
+
var directionX = second.x - first.x;
|
|
144
|
+
var directionY = second.y - first.y;
|
|
145
|
+
var edgeX = b.x - a.x;
|
|
146
|
+
var edgeY = b.y - a.y;
|
|
147
|
+
var denominator = directionX * edgeY - directionY * edgeX;
|
|
135
148
|
if (Math.abs(denominator) < 1e-12)
|
|
136
149
|
return null;
|
|
137
|
-
|
|
138
|
-
|
|
150
|
+
var along = ((a.x - first.x) * edgeY - (a.y - first.y) * edgeX) / denominator;
|
|
151
|
+
var across = ((a.x - first.x) * directionY - (a.y - first.y) * directionX) / denominator;
|
|
139
152
|
return along >= 0 && along <= 1 && across >= 0 && across <= 1 ? along : null;
|
|
140
153
|
}
|
|
141
154
|
/**
|
|
@@ -143,26 +156,27 @@ function segmentMeeting(first, second, a, b) {
|
|
|
143
156
|
* point is inside the shape, or on it: the line meets nothing then.
|
|
144
157
|
*/
|
|
145
158
|
function crossingTowardMiddle(outline, from) {
|
|
146
|
-
|
|
147
|
-
|
|
159
|
+
var width = outline.width, height = outline.height;
|
|
160
|
+
var middle = { x: width / 2, y: height / 2 };
|
|
148
161
|
if (outline.kind === 'lines') {
|
|
149
|
-
|
|
150
|
-
for (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
162
|
+
var nearestAlong = Infinity;
|
|
163
|
+
for (var _i = 0, _a = outline.lines; _i < _a.length; _i++) {
|
|
164
|
+
var line = _a[_i];
|
|
165
|
+
var last = line.closed ? line.points.length : line.points.length - 1;
|
|
166
|
+
for (var index = 0; index < last; index += 1) {
|
|
167
|
+
var along = segmentMeeting(from, middle, line.points[index], line.points[(index + 1) % line.points.length]);
|
|
154
168
|
if (along !== null && along < nearestAlong)
|
|
155
169
|
nearestAlong = along;
|
|
156
170
|
}
|
|
157
171
|
}
|
|
158
172
|
return nearestAlong === Infinity ? null : { x: from.x + (middle.x - from.x) * nearestAlong, y: from.y + (middle.y - from.y) * nearestAlong };
|
|
159
173
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
174
|
+
var radiusX = Math.max(width / 2, 1e-6);
|
|
175
|
+
var radiusY = Math.max(height / 2, 1e-6);
|
|
176
|
+
var offsetX = (from.x - middle.x) / radiusX;
|
|
177
|
+
var offsetY = (from.y - middle.y) / radiusY;
|
|
164
178
|
// How many times the box (or the ellipse) the point is from the middle: past 1 it is outside.
|
|
165
|
-
|
|
179
|
+
var reach = outline.kind === 'box' ? Math.max(Math.abs(offsetX), Math.abs(offsetY)) : Math.hypot(offsetX, offsetY);
|
|
166
180
|
if (reach <= 1 + 1e-9)
|
|
167
181
|
return null;
|
|
168
182
|
return { x: middle.x + (from.x - middle.x) / reach, y: middle.y + (from.y - middle.y) / reach };
|
|
@@ -176,7 +190,7 @@ export function centerOfShape(shape) {
|
|
|
176
190
|
}
|
|
177
191
|
/** The point of the outline of a shape that an `edge` tie rests on, in the world. */
|
|
178
192
|
export function edgePointOf(shape, anchor) {
|
|
179
|
-
|
|
193
|
+
var local = { x: anchor.x * shape.width, y: anchor.y * shape.height };
|
|
180
194
|
// Put back on the outline: a shape that was rebuilt (a rounded corner, a regenerated path) may have moved it a little.
|
|
181
195
|
return toWorld(shape, nearestOnOutline(outlineOf(shape), local));
|
|
182
196
|
}
|
|
@@ -185,15 +199,15 @@ export function edgePointOf(shape, anchor) {
|
|
|
185
199
|
* from inside the shape (or from the middle), so that it still points at it.
|
|
186
200
|
*/
|
|
187
201
|
export function centerCrossingOf(shape, aim) {
|
|
188
|
-
|
|
189
|
-
|
|
202
|
+
var local = worldToBoxLocal(shape, aim);
|
|
203
|
+
var crossing = local ? crossingTowardMiddle(outlineOf(shape), local) : null;
|
|
190
204
|
return crossing ? toWorld(shape, crossing) : centerOfShape(shape);
|
|
191
205
|
}
|
|
192
206
|
/** The shape an arrow's tie holds to, or null when there is none to hold to (it is gone, or an arrow cannot be tied to it). */
|
|
193
207
|
function targetOf(arrow, binding, lookup) {
|
|
194
208
|
if (!binding || binding.targetId === arrow.id)
|
|
195
209
|
return null;
|
|
196
|
-
|
|
210
|
+
var target = lookup(binding.targetId);
|
|
197
211
|
return target && canBeTiedTo(target) ? target : null;
|
|
198
212
|
}
|
|
199
213
|
/**
|
|
@@ -203,13 +217,14 @@ function targetOf(arrow, binding, lookup) {
|
|
|
203
217
|
export function positionBoundArrow(arrow, lookup) {
|
|
204
218
|
if (!hasBindings(arrow.arrow))
|
|
205
219
|
return arrow;
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
for (
|
|
211
|
-
|
|
212
|
-
|
|
220
|
+
var current = arrowPointsInWorld(arrow);
|
|
221
|
+
var targets = { start: null, end: null };
|
|
222
|
+
var bindings = { start: null, end: null };
|
|
223
|
+
var cut = {};
|
|
224
|
+
for (var _i = 0, ARROW_ENDS_1 = ARROW_ENDS; _i < ARROW_ENDS_1.length; _i++) {
|
|
225
|
+
var end = ARROW_ENDS_1[_i];
|
|
226
|
+
var binding = bindingOfEnd(arrow.arrow, end);
|
|
227
|
+
var target = targetOf(arrow, binding, lookup);
|
|
213
228
|
if (binding && target) {
|
|
214
229
|
bindings[end] = binding;
|
|
215
230
|
targets[end] = target;
|
|
@@ -219,42 +234,45 @@ export function positionBoundArrow(arrow, lookup) {
|
|
|
219
234
|
}
|
|
220
235
|
}
|
|
221
236
|
// What the line to the far end is drawn to: its place on the outline, the middle of its shape, or its own place when it is free.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
237
|
+
var lookedAt = function (end) {
|
|
238
|
+
var target = targets[end];
|
|
239
|
+
var binding = bindings[end];
|
|
225
240
|
if (!target || !binding)
|
|
226
241
|
return current[end];
|
|
227
242
|
return binding.kind === 'edge' ? edgePointOf(target, binding.anchor) : centerOfShape(target);
|
|
228
243
|
};
|
|
229
|
-
|
|
230
|
-
for (
|
|
231
|
-
|
|
232
|
-
|
|
244
|
+
var placed = { start: current.start, end: current.end };
|
|
245
|
+
for (var _a = 0, ARROW_ENDS_2 = ARROW_ENDS; _a < ARROW_ENDS_2.length; _a++) {
|
|
246
|
+
var end = ARROW_ENDS_2[_a];
|
|
247
|
+
var target = targets[end];
|
|
248
|
+
var binding = bindings[end];
|
|
233
249
|
if (!target || !binding)
|
|
234
250
|
continue;
|
|
235
251
|
placed[end] = binding.kind === 'edge' ? edgePointOf(target, binding.anchor) : centerCrossingOf(target, lookedAt(end === 'start' ? 'end' : 'start'));
|
|
236
252
|
}
|
|
237
|
-
|
|
253
|
+
var moved = ARROW_ENDS.some(function (end) { return distanceBetween(placed[end], current[end]) > SAME_PLACE; });
|
|
238
254
|
if (!moved && Object.keys(cut).length === 0)
|
|
239
255
|
return arrow;
|
|
240
|
-
return reshapeArrow(arrow, { start: placed.start, end: placed.end,
|
|
256
|
+
return reshapeArrow(arrow, __assign({ start: placed.start, end: placed.end }, cut));
|
|
241
257
|
}
|
|
242
258
|
/**
|
|
243
259
|
* The scene with every tied arrow where its ties say. The very same array when none has to move (the engine runs this over every
|
|
244
260
|
* scene it is given, so that it must cost nothing when no arrow is tied, and must not make a change out of nothing).
|
|
245
261
|
*/
|
|
246
262
|
export function reconcileArrowBindings(scene) {
|
|
247
|
-
if (!scene.some((object)
|
|
263
|
+
if (!scene.some(function (object) { return object.type === 'path' && !!object.arrow && hasBindings(object.arrow); }))
|
|
248
264
|
return scene;
|
|
249
|
-
|
|
250
|
-
for (
|
|
265
|
+
var index = new Map();
|
|
266
|
+
for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
|
|
267
|
+
var object = scene_1[_i];
|
|
251
268
|
index.set(object.id, object);
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
269
|
+
}
|
|
270
|
+
var lookup = function (id) { return index.get(id); };
|
|
271
|
+
var changed = false;
|
|
272
|
+
var next = scene.map(function (object) {
|
|
255
273
|
if (object.type !== 'path' || !isArrow(object) || !hasBindings(object.arrow))
|
|
256
274
|
return object;
|
|
257
|
-
|
|
275
|
+
var placed = positionBoundArrow(object, lookup);
|
|
258
276
|
if (placed !== object)
|
|
259
277
|
changed = true;
|
|
260
278
|
return placed;
|
|
@@ -267,7 +285,7 @@ function edgeBandOf(shape, zoom) {
|
|
|
267
285
|
}
|
|
268
286
|
/** Whether a point is inside a shape, a hollow one included (the way a click is not: a click on the empty middle of a frame reaches what lies under it). */
|
|
269
287
|
function interiorContains(shape, point) {
|
|
270
|
-
|
|
288
|
+
var local = worldToBoxLocal(shape, point);
|
|
271
289
|
if (!local)
|
|
272
290
|
return false;
|
|
273
291
|
switch (shape.type) {
|
|
@@ -276,20 +294,26 @@ function interiorContains(shape, point) {
|
|
|
276
294
|
case 'ellipse':
|
|
277
295
|
return Math.hypot((local.x - shape.width / 2) / Math.max(shape.width / 2, 1e-6), (local.y - shape.height / 2) / Math.max(shape.height / 2, 1e-6)) <= 1;
|
|
278
296
|
case 'path':
|
|
279
|
-
return flattenedOutline(shape.d).some((outline)
|
|
297
|
+
return flattenedOutline(shape.d).some(function (outline) { return outline.points.length >= 3 && pointInPolygon(local, outline.points); });
|
|
280
298
|
default:
|
|
281
299
|
return false;
|
|
282
300
|
}
|
|
283
301
|
}
|
|
284
|
-
/**
|
|
302
|
+
/**
|
|
303
|
+
* The tie a pointer at a point makes on one shape it is over: on the outline within the band of it, at the middle further in.
|
|
304
|
+
*
|
|
305
|
+
* A line drawn by hand (a brush stroke) is always tied on its outline, wherever the pointer is on it: it has no middle to aim at (the
|
|
306
|
+
* middle of its box is mostly empty board, and the box of a straight line is only a unit thick, which leaves the band no width at
|
|
307
|
+
* all), so the end rests on the point of the stroke that is nearest to the pointer and stays at that place of the line.
|
|
308
|
+
*/
|
|
285
309
|
function tieOnShape(shape, point, zoom) {
|
|
286
|
-
|
|
310
|
+
var local = worldToBoxLocal(shape, point);
|
|
287
311
|
if (!local)
|
|
288
312
|
return null;
|
|
289
|
-
|
|
290
|
-
if (distanceBetween(local, nearest) <= edgeBandOf(shape, zoom)) {
|
|
291
|
-
|
|
292
|
-
return { binding: { targetId: shape.id, kind: 'edge', anchor }, hint: { targetId: shape.id, kind: 'edge', point: toWorld(shape, nearest) } };
|
|
313
|
+
var nearest = nearestOnOutline(outlineOf(shape), local);
|
|
314
|
+
if (isBrushStroke(shape) || distanceBetween(local, nearest) <= edgeBandOf(shape, zoom)) {
|
|
315
|
+
var anchor = { x: clampNumber(nearest.x / Math.max(shape.width, 1e-6), 0, 1), y: clampNumber(nearest.y / Math.max(shape.height, 1e-6), 0, 1) };
|
|
316
|
+
return { binding: { targetId: shape.id, kind: 'edge', anchor: anchor }, hint: { targetId: shape.id, kind: 'edge', point: toWorld(shape, nearest) } };
|
|
293
317
|
}
|
|
294
318
|
return { binding: { targetId: shape.id, kind: 'center' }, hint: { targetId: shape.id, kind: 'center', point: centerOfShape(shape) } };
|
|
295
319
|
}
|
|
@@ -300,25 +324,26 @@ function tieOnShape(shape, point, zoom) {
|
|
|
300
324
|
* middle of a hollow shape taken as the shape. Null when the pointer is over nothing of the kind.
|
|
301
325
|
*/
|
|
302
326
|
export function pickArrowBinding(scene, point, options) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
327
|
+
var _a, _b;
|
|
328
|
+
var zoom = Math.max(options.zoom, 1e-6);
|
|
329
|
+
var reach = ARROW_BIND_REACH_PIXELS / zoom;
|
|
330
|
+
for (var position = scene.length - 1; position >= 0; position -= 1) {
|
|
331
|
+
var object = scene[position];
|
|
332
|
+
if (!canBeTiedTo(object) || ((_a = options.ignore) === null || _a === void 0 ? void 0 : _a.has(object.id)))
|
|
308
333
|
continue;
|
|
309
334
|
if (!shapeContainsPoint(object, point, reach))
|
|
310
335
|
continue;
|
|
311
|
-
|
|
336
|
+
var pick = tieOnShape(object, point, zoom);
|
|
312
337
|
if (pick)
|
|
313
338
|
return pick;
|
|
314
339
|
}
|
|
315
|
-
for (
|
|
316
|
-
|
|
317
|
-
if (!canBeTiedTo(object) || options.ignore
|
|
340
|
+
for (var position = scene.length - 1; position >= 0; position -= 1) {
|
|
341
|
+
var object = scene[position];
|
|
342
|
+
if (!canBeTiedTo(object) || ((_b = options.ignore) === null || _b === void 0 ? void 0 : _b.has(object.id)))
|
|
318
343
|
continue;
|
|
319
344
|
if (!interiorContains(object, point))
|
|
320
345
|
continue;
|
|
321
|
-
|
|
346
|
+
var pick = tieOnShape(object, point, zoom);
|
|
322
347
|
if (pick)
|
|
323
348
|
return pick;
|
|
324
349
|
}
|
|
@@ -23,8 +23,14 @@ export interface ArrowBindingChange {
|
|
|
23
23
|
}
|
|
24
24
|
/** The spec with its ties changed. The same spec when the change says what it says already. */
|
|
25
25
|
export declare function withBindings(spec: ArrowSpec, change: ArrowBindingChange): ArrowSpec;
|
|
26
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Whether an arrow can be tied to this object: a shape that has an outline, which is not an arrow or a group. A line drawn by hand
|
|
28
|
+
* (a brush stroke) can be tied to like any other drawing: an arrow may start from it or end on it. An arrow cannot be tied to another
|
|
29
|
+
* arrow, since two arrows that follow each other would have no end to follow.
|
|
30
|
+
*/
|
|
27
31
|
export declare function canBeTiedTo(object: SceneObject): object is ShapeObject;
|
|
32
|
+
/** An arrow, as far as these records are concerned: a path with the points it is drawn from. Its spec, or null for any other object. */
|
|
33
|
+
export declare function arrowSpecOf(object: SceneObject): ArrowSpec | null;
|
|
28
34
|
/**
|
|
29
35
|
* An arrow that is carried, stretched or turned together with some shapes: the ties to shapes that are not among them are cut (an
|
|
30
36
|
* arrow that is moved by itself comes off the shapes it was tied to, the way it does in tldraw). The same object when nothing is cut.
|
|
@@ -37,7 +43,7 @@ export declare function releaseUnmovedEnds<T extends SceneObject>(object: T, mov
|
|
|
37
43
|
export declare function remapEnds<T extends SceneObject>(object: T, idMap: ReadonlyMap<ObjectId, ObjectId>): T;
|
|
38
44
|
/**
|
|
39
45
|
* A scene in which every tie points at something an arrow can be tied to: a tie whose shape is not on the board (a board that was
|
|
40
|
-
* edited by hand, a shape deleted by another tool), is not a shape with an outline, or is the arrow itself
|
|
41
|
-
* array when nothing is cut.
|
|
46
|
+
* edited by hand, a shape deleted by another tool), is not a shape with an outline (a group, another arrow), or is the arrow itself
|
|
47
|
+
* is cut. The very same array when nothing is cut.
|
|
42
48
|
*/
|
|
43
49
|
export declare function dropDanglingBindings<S extends readonly SceneObject[]>(scene: S): S;
|
|
@@ -7,11 +7,23 @@
|
|
|
7
7
|
* every function that makes a spec writes `startBinding` and `endBinding` only while they are set, so an arrow that is tied to
|
|
8
8
|
* nothing is the very same record it was before ties existed.
|
|
9
9
|
*/
|
|
10
|
+
var __assign = (this && this.__assign) || function () {
|
|
11
|
+
__assign = Object.assign || function(t) {
|
|
12
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
13
|
+
s = arguments[i];
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
}
|
|
17
|
+
return t;
|
|
18
|
+
};
|
|
19
|
+
return __assign.apply(this, arguments);
|
|
20
|
+
};
|
|
10
21
|
/** The two ends of an arrow, the tail first. */
|
|
11
|
-
export
|
|
22
|
+
export var ARROW_ENDS = Object.freeze(['start', 'end']);
|
|
12
23
|
/** The tie of one end of an arrow, or null while that end is free. */
|
|
13
24
|
export function bindingOfEnd(spec, end) {
|
|
14
|
-
|
|
25
|
+
var _a;
|
|
26
|
+
return (_a = (end === 'start' ? spec.startBinding : spec.endBinding)) !== null && _a !== void 0 ? _a : null;
|
|
15
27
|
}
|
|
16
28
|
/** Whether either end of an arrow is tied. */
|
|
17
29
|
export function hasBindings(spec) {
|
|
@@ -30,13 +42,18 @@ export function sameBinding(first, second) {
|
|
|
30
42
|
}
|
|
31
43
|
/** The spec with its ties changed. The same spec when the change says what it says already. */
|
|
32
44
|
export function withBindings(spec, change) {
|
|
33
|
-
|
|
34
|
-
|
|
45
|
+
var _a, _b, _c, _d;
|
|
46
|
+
var start = 'startBinding' in change ? ((_a = change.startBinding) !== null && _a !== void 0 ? _a : null) : ((_b = spec.startBinding) !== null && _b !== void 0 ? _b : null);
|
|
47
|
+
var end = 'endBinding' in change ? ((_c = change.endBinding) !== null && _c !== void 0 ? _c : null) : ((_d = spec.endBinding) !== null && _d !== void 0 ? _d : null);
|
|
35
48
|
if (sameBinding(start, spec.startBinding) && sameBinding(end, spec.endBinding))
|
|
36
49
|
return spec;
|
|
37
|
-
return { start: spec.start, end: spec.end, bend: spec.bend,
|
|
50
|
+
return __assign(__assign({ start: spec.start, end: spec.end, bend: spec.bend }, (start ? { startBinding: start } : {})), (end ? { endBinding: end } : {}));
|
|
38
51
|
}
|
|
39
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* Whether an arrow can be tied to this object: a shape that has an outline, which is not an arrow or a group. A line drawn by hand
|
|
54
|
+
* (a brush stroke) can be tied to like any other drawing: an arrow may start from it or end on it. An arrow cannot be tied to another
|
|
55
|
+
* arrow, since two arrows that follow each other would have no end to follow.
|
|
56
|
+
*/
|
|
40
57
|
export function canBeTiedTo(object) {
|
|
41
58
|
switch (object.type) {
|
|
42
59
|
case 'rect':
|
|
@@ -46,28 +63,28 @@ export function canBeTiedTo(object) {
|
|
|
46
63
|
case 'widget':
|
|
47
64
|
return true;
|
|
48
65
|
case 'path':
|
|
49
|
-
return object.arrow === null
|
|
66
|
+
return object.arrow === null;
|
|
50
67
|
default:
|
|
51
68
|
return false;
|
|
52
69
|
}
|
|
53
70
|
}
|
|
54
|
-
/** An arrow, as far as these records are concerned: a path with the points it is drawn from. */
|
|
55
|
-
function arrowSpecOf(object) {
|
|
71
|
+
/** An arrow, as far as these records are concerned: a path with the points it is drawn from. Its spec, or null for any other object. */
|
|
72
|
+
export function arrowSpecOf(object) {
|
|
56
73
|
return object.type === 'path' && object.arrow ? object.arrow : null;
|
|
57
74
|
}
|
|
58
75
|
/** The same arrow with its spec replaced, or the very same object when the spec did not change. */
|
|
59
76
|
function withSpec(object, spec) {
|
|
60
|
-
return spec === object.arrow ? object : {
|
|
77
|
+
return spec === object.arrow ? object : __assign(__assign({}, object), { arrow: spec });
|
|
61
78
|
}
|
|
62
79
|
/**
|
|
63
80
|
* An arrow that is carried, stretched or turned together with some shapes: the ties to shapes that are not among them are cut (an
|
|
64
81
|
* arrow that is moved by itself comes off the shapes it was tied to, the way it does in tldraw). The same object when nothing is cut.
|
|
65
82
|
*/
|
|
66
83
|
export function releaseUnmovedEnds(object, moving) {
|
|
67
|
-
|
|
84
|
+
var spec = arrowSpecOf(object);
|
|
68
85
|
if (!spec || !hasBindings(spec))
|
|
69
86
|
return object;
|
|
70
|
-
|
|
87
|
+
var change = {};
|
|
71
88
|
if (spec.startBinding && !moving.has(spec.startBinding.targetId))
|
|
72
89
|
change.startBinding = null;
|
|
73
90
|
if (spec.endBinding && !moving.has(spec.endBinding.targetId))
|
|
@@ -79,45 +96,47 @@ export function releaseUnmovedEnds(object, moving) {
|
|
|
79
96
|
* alone is a free arrow). `idMap` says which id was copied to which.
|
|
80
97
|
*/
|
|
81
98
|
export function remapEnds(object, idMap) {
|
|
82
|
-
|
|
99
|
+
var spec = arrowSpecOf(object);
|
|
83
100
|
if (!spec || !hasBindings(spec))
|
|
84
101
|
return object;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return binding && copyId ? {
|
|
102
|
+
var remap = function (binding) {
|
|
103
|
+
var copyId = binding ? idMap.get(binding.targetId) : undefined;
|
|
104
|
+
return binding && copyId ? __assign(__assign({}, binding), { targetId: copyId }) : null;
|
|
88
105
|
};
|
|
89
106
|
return withSpec(object, withBindings(spec, { startBinding: remap(spec.startBinding), endBinding: remap(spec.endBinding) }));
|
|
90
107
|
}
|
|
91
108
|
/**
|
|
92
109
|
* A scene in which every tie points at something an arrow can be tied to: a tie whose shape is not on the board (a board that was
|
|
93
|
-
* edited by hand, a shape deleted by another tool), is not a shape with an outline, or is the arrow itself
|
|
94
|
-
* array when nothing is cut.
|
|
110
|
+
* edited by hand, a shape deleted by another tool), is not a shape with an outline (a group, another arrow), or is the arrow itself
|
|
111
|
+
* is cut. The very same array when nothing is cut.
|
|
95
112
|
*/
|
|
96
113
|
export function dropDanglingBindings(scene) {
|
|
97
|
-
if (!scene.some((object)
|
|
98
|
-
|
|
114
|
+
if (!scene.some(function (object) {
|
|
115
|
+
var spec = arrowSpecOf(object);
|
|
99
116
|
return !!spec && hasBindings(spec);
|
|
100
117
|
})) {
|
|
101
118
|
return scene;
|
|
102
119
|
}
|
|
103
|
-
|
|
104
|
-
for (
|
|
120
|
+
var index = new Map();
|
|
121
|
+
for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
|
|
122
|
+
var object = scene_1[_i];
|
|
105
123
|
index.set(object.id, object);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
124
|
+
}
|
|
125
|
+
var changed = false;
|
|
126
|
+
var next = scene.map(function (object) {
|
|
127
|
+
var spec = arrowSpecOf(object);
|
|
109
128
|
if (!spec || !hasBindings(spec))
|
|
110
129
|
return object;
|
|
111
|
-
|
|
112
|
-
|
|
130
|
+
var holds = function (binding) {
|
|
131
|
+
var target = binding ? index.get(binding.targetId) : undefined;
|
|
113
132
|
return !!binding && !!target && target.id !== object.id && canBeTiedTo(target);
|
|
114
133
|
};
|
|
115
|
-
|
|
134
|
+
var change = {};
|
|
116
135
|
if (spec.startBinding && !holds(spec.startBinding))
|
|
117
136
|
change.startBinding = null;
|
|
118
137
|
if (spec.endBinding && !holds(spec.endBinding))
|
|
119
138
|
change.endBinding = null;
|
|
120
|
-
|
|
139
|
+
var cut = withSpec(object, withBindings(spec, change));
|
|
121
140
|
if (cut !== object)
|
|
122
141
|
changed = true;
|
|
123
142
|
return cut;
|
|
@@ -41,10 +41,18 @@ export interface BrushStrokeOptions {
|
|
|
41
41
|
color?: string;
|
|
42
42
|
/** The width of the line (what the Width control of the panel shows). */
|
|
43
43
|
strokeWidth?: number;
|
|
44
|
+
/** How much of the board shows through the whole line, from 0 to 1 (what the Opacity control of the panel shows). Fully opaque by default. */
|
|
45
|
+
opacity?: number;
|
|
44
46
|
/** `pen` reads the pressure from the samples, `speed` (the default) works it out from how fast the hand moved. */
|
|
45
47
|
pressure?: BrushPressure;
|
|
46
48
|
/** The pointer was released: the line ends exactly on the last sample, instead of trailing a little behind the hand. */
|
|
47
49
|
finished?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* The line is held straight and even (Shift): every point gets this pressure (0 to 1) instead of one worked out from the speed
|
|
52
|
+
* of the hand, so it is as wide at one end as at the other, and the line follows its samples exactly, with no trailing behind
|
|
53
|
+
* the hand even while it is still being drawn (the first and the last sample are the two ends it is drawn between).
|
|
54
|
+
*/
|
|
55
|
+
steadyPressure?: number;
|
|
48
56
|
}
|
|
49
57
|
/**
|
|
50
58
|
* The stroke for the samples of a pointer, in world units: the box of its line, the line itself with its pressure, and the
|