@widgetic/canvas 0.5.39 → 0.5.41
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 +7 -3
- package/dist/canvas/DomCanvas.svelte +7 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +8 -0
- 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.d.ts +2 -0
- package/dist/canvas/chromeLayout.js +31 -17
- package/dist/canvas/eventScope.js +14 -10
- package/dist/canvas/host/CanvasHost.d.ts +6 -0
- package/dist/canvas/host/CanvasHost.js +1556 -1255
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +25 -4
- package/dist/canvas/host/browserServices.js +116 -65
- package/dist/canvas/host/conversion.js +170 -82
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +31 -28
- package/dist/canvas/host/strokeSnap.js +143 -88
- 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 +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/CanvasContextMenu.svelte +69 -39
- package/dist/canvas/ui/CanvasHint.svelte +17 -4
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
- package/dist/canvas/ui/CanvasToolbar.svelte +27 -10
- package/dist/canvas/ui/ColorField.svelte +1 -1
- package/dist/canvas/ui/SelectionPanel.svelte +24 -20
- package/dist/canvas/ui/ShapesMenu.svelte +0 -2
- package/dist/canvas/ui/ToolButton.svelte +11 -3
- package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
- package/dist/canvas/ui/WidgetActions.svelte +1 -1
- package/dist/canvas/ui/ZoomControl.svelte +39 -4
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +2 -2
- package/dist/canvas/ui/colorModel.js +32 -21
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +7 -5
- package/dist/canvas/ui/floatingCard.js +23 -21
- package/dist/canvas/ui/floatingLayer.js +6 -5
- package/dist/canvas/ui/keyHints.d.ts +5 -0
- package/dist/canvas/ui/keyHints.js +23 -7
- package/dist/canvas/ui/popoverSide.js +3 -2
- package/dist/canvas/ui/selectionTraits.js +40 -34
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +5 -2
- package/dist/canvas/ui/sizeLabelVisibility.js +7 -6
- package/dist/canvas/ui/widgetSelectionPanel.d.ts +6 -0
- package/dist/canvas/ui/widgetSelectionPanel.js +9 -0
- package/dist/canvas/view/ObjectView.svelte +2 -1
- package/dist/canvas/view/SelectionOverlay.svelte +34 -14
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.d.ts +14 -4
- package/dist/canvas/view/geometry.js +103 -77
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +4 -1
- package/dist/engine/EngineCore.js +801 -695
- package/dist/engine/codec/canvasDocument.js +173 -173
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +38 -26
- package/dist/engine/core/arrow.d.ts +10 -3
- package/dist/engine/core/arrow.js +96 -75
- package/dist/engine/core/arrowBinding.d.ts +69 -0
- package/dist/engine/core/arrowBinding.js +344 -0
- package/dist/engine/core/arrowBindingRecords.d.ts +43 -0
- package/dist/engine/core/arrowBindingRecords.js +141 -0
- package/dist/engine/core/brush.js +94 -67
- 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 +50 -33
- package/dist/engine/core/hitTest.d.ts +3 -0
- package/dist/engine/core/hitTest.js +83 -69
- 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 +3 -0
- package/dist/engine/core/scene.js +192 -129
- 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.js +64 -31
- package/dist/engine/core/transform.js +94 -83
- package/dist/engine/core/types.d.ts +29 -0
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +36 -23
- package/dist/engine/input/PointerController.js +476 -447
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.d.ts +12 -1
- package/dist/engine/input/gestures.js +342 -251
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.d.ts +9 -1
- package/dist/engine/input/keyboard.js +46 -27
- package/dist/engine/input/selectionHandles.js +50 -45
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +158 -86
- package/dist/engine/state.d.ts +6 -1
- package/dist/engine/state.js +17 -5
- 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 +68 -17
- package/dist/fonts/workspaceFonts.js +90 -32
- package/dist/i18n/labels.js +6 -5
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +6 -6
- package/dist/i18n/messages.js +7 -7
- package/dist/i18n/shortcuts.d.ts +12 -0
- package/dist/i18n/shortcuts.js +32 -18
- package/dist/settings/workspaceToolSettings.js +48 -32
- package/dist/tools/components/catalog.d.ts +11 -0
- package/dist/tools/components/catalog.js +270 -0
- package/dist/tools/components/grouping.d.ts +9 -0
- package/dist/tools/components/grouping.js +33 -0
- package/dist/tools/definitions.d.ts +19 -0
- package/dist/tools/definitions.js +147 -0
- package/dist/tools/executors.d.ts +7 -0
- package/dist/tools/executors.js +324 -0
- package/dist/tools/hostAdapter.d.ts +14 -0
- package/dist/tools/hostAdapter.js +40 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +6 -0
- package/dist/tools/sceneSummary.d.ts +24 -0
- package/dist/tools/sceneSummary.js +170 -0
- package/dist/tools/sketchIntent.d.ts +17 -0
- package/dist/tools/sketchIntent.js +166 -0
- package/dist/tools/types.d.ts +54 -0
- package/dist/tools/types.js +11 -0
- package/package.json +7 -2
|
@@ -4,13 +4,23 @@
|
|
|
4
4
|
* Both work on the units of a selection (a group is one unit), measured by the rectangle around the shapes
|
|
5
5
|
* they draw, and move whole units: nothing is resized.
|
|
6
6
|
*/
|
|
7
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
8
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
9
|
+
if (ar || !(i in from)) {
|
|
10
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
11
|
+
ar[i] = from[i];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
15
|
+
};
|
|
7
16
|
import { boundsOfObjects, normalizeSelection, translateObjects } from './scene.js';
|
|
8
17
|
function unitsOf(scene, ids) {
|
|
9
|
-
|
|
10
|
-
for (
|
|
11
|
-
|
|
18
|
+
var units = [];
|
|
19
|
+
for (var _i = 0, _a = normalizeSelection(scene, ids); _i < _a.length; _i++) {
|
|
20
|
+
var id = _a[_i];
|
|
21
|
+
var bounds = boundsOfObjects(scene, [id]);
|
|
12
22
|
if (bounds)
|
|
13
|
-
units.push({ id, bounds });
|
|
23
|
+
units.push({ id: id, bounds: bounds });
|
|
14
24
|
}
|
|
15
25
|
return units;
|
|
16
26
|
}
|
|
@@ -19,17 +29,18 @@ function unitsOf(scene, ids) {
|
|
|
19
29
|
* Needs two units or more; returns the same scene otherwise, or when everything is already in line.
|
|
20
30
|
*/
|
|
21
31
|
export function alignObjects(scene, ids, mode) {
|
|
22
|
-
|
|
32
|
+
var units = unitsOf(scene, ids);
|
|
23
33
|
if (units.length < 2)
|
|
24
34
|
return scene;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
for (
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
var left = Math.min.apply(Math, units.map(function (unit) { return unit.bounds.x; }));
|
|
36
|
+
var right = Math.max.apply(Math, units.map(function (unit) { return unit.bounds.x + unit.bounds.width; }));
|
|
37
|
+
var top = Math.min.apply(Math, units.map(function (unit) { return unit.bounds.y; }));
|
|
38
|
+
var bottom = Math.max.apply(Math, units.map(function (unit) { return unit.bounds.y + unit.bounds.height; }));
|
|
39
|
+
var next = scene;
|
|
40
|
+
for (var _i = 0, units_1 = units; _i < units_1.length; _i++) {
|
|
41
|
+
var unit = units_1[_i];
|
|
42
|
+
var deltaX = 0;
|
|
43
|
+
var deltaY = 0;
|
|
33
44
|
switch (mode) {
|
|
34
45
|
case 'left':
|
|
35
46
|
deltaX = left - unit.bounds.x;
|
|
@@ -59,21 +70,22 @@ export function alignObjects(scene, ids, mode) {
|
|
|
59
70
|
* Needs three units or more.
|
|
60
71
|
*/
|
|
61
72
|
export function distributeObjects(scene, ids, axis) {
|
|
62
|
-
|
|
73
|
+
var units = unitsOf(scene, ids);
|
|
63
74
|
if (units.length < 3)
|
|
64
75
|
return scene;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
for (
|
|
76
|
-
|
|
76
|
+
var start = function (unit) { return (axis === 'horizontal' ? unit.bounds.x : unit.bounds.y); };
|
|
77
|
+
var size = function (unit) { return (axis === 'horizontal' ? unit.bounds.width : unit.bounds.height); };
|
|
78
|
+
var ordered = __spreadArray([], units, true).sort(function (first, second) { return start(first) - start(second); });
|
|
79
|
+
var first = ordered[0];
|
|
80
|
+
var last = ordered[ordered.length - 1];
|
|
81
|
+
var span = start(last) + size(last) - start(first);
|
|
82
|
+
var occupied = ordered.reduce(function (total, unit) { return total + size(unit); }, 0);
|
|
83
|
+
var gap = (span - occupied) / (ordered.length - 1);
|
|
84
|
+
var next = scene;
|
|
85
|
+
var cursor = start(first) + size(first) + gap;
|
|
86
|
+
for (var _i = 0, _a = ordered.slice(1, -1); _i < _a.length; _i++) {
|
|
87
|
+
var unit = _a[_i];
|
|
88
|
+
var delta = cursor - start(unit);
|
|
77
89
|
next = axis === 'horizontal' ? translateObjects(next, [unit.id], delta, 0) : translateObjects(next, [unit.id], 0, delta);
|
|
78
90
|
cursor += size(unit) + gap;
|
|
79
91
|
}
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* in the coordinates of the box that holds it, ready to be written into a path object, together with that box and the arrow's
|
|
16
16
|
* `ArrowSpec` in the same coordinates.
|
|
17
17
|
*/
|
|
18
|
+
import type { ArrowBindingChange } from './arrowBindingRecords.js';
|
|
18
19
|
import type { PathCommand } from './pathData.js';
|
|
19
20
|
import type { ArrowSpec, PathObject, Point, Rect, ShapeObject } from './types.js';
|
|
20
21
|
/** The wings of the head open this far either side of the shaft (30 degrees). */
|
|
@@ -67,14 +68,20 @@ export declare function arrowPointsInWorld(shape: ArrowObject): {
|
|
|
67
68
|
end: Point;
|
|
68
69
|
middle: Point;
|
|
69
70
|
};
|
|
70
|
-
/**
|
|
71
|
-
|
|
71
|
+
/**
|
|
72
|
+
* What may change in an arrow: the points in the world, the bend, the width of the line the head is sized by, and what its ends are
|
|
73
|
+
* tied to (`startBinding` / `endBinding`: a tie sets an end, `null` frees it, a key that is left out keeps it as it is).
|
|
74
|
+
*/
|
|
75
|
+
export interface ArrowChange extends ArrowBindingChange {
|
|
72
76
|
start?: Point;
|
|
73
77
|
end?: Point;
|
|
74
78
|
bend?: number;
|
|
75
79
|
strokeWidth?: number;
|
|
76
80
|
}
|
|
77
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* The arrow drawn again after a change: a new shape with the same id, the new box, path and points. What its ends are tied to stays
|
|
83
|
+
* as it was unless the change says otherwise (the points do not move because a tie says so here: `arrowBinding.ts` places them).
|
|
84
|
+
*/
|
|
78
85
|
export declare function reshapeArrow(shape: ArrowObject, change?: ArrowChange): ArrowObject;
|
|
79
86
|
/**
|
|
80
87
|
* The arrow after the board around it was changed by a mapping of the world (stretched, mirrored, turned): its ends and the middle of
|
|
@@ -15,22 +15,43 @@
|
|
|
15
15
|
* in the coordinates of the box that holds it, ready to be written into a path object, together with that box and the arrow's
|
|
16
16
|
* `ArrowSpec` in the same coordinates.
|
|
17
17
|
*/
|
|
18
|
+
var __assign = (this && this.__assign) || function () {
|
|
19
|
+
__assign = Object.assign || function(t) {
|
|
20
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
21
|
+
s = arguments[i];
|
|
22
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
23
|
+
t[p] = s[p];
|
|
24
|
+
}
|
|
25
|
+
return t;
|
|
26
|
+
};
|
|
27
|
+
return __assign.apply(this, arguments);
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
38
|
+
import { withBindings } from './arrowBindingRecords.js';
|
|
18
39
|
import { clampNumber } from './geometry.js';
|
|
19
40
|
import { parsePathData, pathBounds, serializePathData, translatePathCommands } from './pathData.js';
|
|
20
41
|
/** The wings of the head open this far either side of the shaft (30 degrees). */
|
|
21
|
-
export
|
|
42
|
+
export var ARROW_HEAD_ANGLE = Math.PI / 6;
|
|
22
43
|
/** The head is this fraction of the length of the arrow ... */
|
|
23
|
-
export
|
|
44
|
+
export var ARROW_HEAD_LENGTH_FRACTION = 1 / 5;
|
|
24
45
|
/** ... but at least this many widths of the line ... */
|
|
25
|
-
export
|
|
46
|
+
export var ARROW_HEAD_MIN_WIDTHS = 1;
|
|
26
47
|
/** ... and at most this many. */
|
|
27
|
-
export
|
|
48
|
+
export var ARROW_HEAD_MAX_WIDTHS = 3;
|
|
28
49
|
/** A bend smaller than this (world units) is not a bend: the arrow is straight. */
|
|
29
|
-
export
|
|
50
|
+
export var MIN_ARROW_BEND = 8;
|
|
30
51
|
/** An arrow bends by at most this many times its own length (a little under a full circle). */
|
|
31
|
-
export
|
|
52
|
+
export var MAX_ARROW_BEND_PER_LENGTH = 2;
|
|
32
53
|
/** The line is at least this wide for the size of the head, so that an arrow drawn with no line still has one. */
|
|
33
|
-
|
|
54
|
+
var MIN_HEAD_LINE_WIDTH = 1;
|
|
34
55
|
/** Whether a shape is an arrow made by the Arrow tool, with the points it is drawn from. */
|
|
35
56
|
export function isArrow(shape) {
|
|
36
57
|
return shape.type === 'path' && shape.arrow !== null;
|
|
@@ -43,28 +64,28 @@ export function drawnBend(bend, length) {
|
|
|
43
64
|
}
|
|
44
65
|
/** The length of the head for an arrow whose shaft is `bodyLength` long, drawn with a line `strokeWidth` wide. */
|
|
45
66
|
export function arrowHeadLength(bodyLength, strokeWidth) {
|
|
46
|
-
|
|
67
|
+
var width = Math.max(strokeWidth, MIN_HEAD_LINE_WIDTH);
|
|
47
68
|
return clampNumber(bodyLength * ARROW_HEAD_LENGTH_FRACTION, width * ARROW_HEAD_MIN_WIDTHS, width * ARROW_HEAD_MAX_WIDTHS);
|
|
48
69
|
}
|
|
49
70
|
/** A point turned about another by an angle (radians, clockwise on screen). */
|
|
50
71
|
function rotateAbout(point, center, radians) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
72
|
+
var cosine = Math.cos(radians);
|
|
73
|
+
var sine = Math.sin(radians);
|
|
74
|
+
var offsetX = point.x - center.x;
|
|
75
|
+
var offsetY = point.y - center.y;
|
|
55
76
|
return { x: center.x + offsetX * cosine - offsetY * sine, y: center.y + offsetX * sine + offsetY * cosine };
|
|
56
77
|
}
|
|
57
78
|
/** The direction from one point to another, and the normal to the right of it. A point that is the other's own has a direction to the right. */
|
|
58
79
|
function directionBetween(start, end) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return { length, unit, normal: { x: -unit.y, y: unit.x } };
|
|
80
|
+
var deltaX = end.x - start.x;
|
|
81
|
+
var deltaY = end.y - start.y;
|
|
82
|
+
var length = Math.hypot(deltaX, deltaY);
|
|
83
|
+
var unit = length > 1e-9 ? { x: deltaX / length, y: deltaY / length } : { x: 1, y: 0 };
|
|
84
|
+
return { length: length, unit: unit, normal: { x: -unit.y, y: unit.x } };
|
|
64
85
|
}
|
|
65
86
|
/** The middle of the arrow: the middle of the line between its ends, pushed `bend` to the right of the way it points. */
|
|
66
87
|
export function arrowMiddle(start, end, bend) {
|
|
67
|
-
|
|
88
|
+
var normal = directionBetween(start, end).normal;
|
|
68
89
|
return { x: (start.x + end.x) / 2 + normal.x * bend, y: (start.y + end.y) / 2 + normal.y * bend };
|
|
69
90
|
}
|
|
70
91
|
/**
|
|
@@ -72,7 +93,7 @@ export function arrowMiddle(start, end, bend) {
|
|
|
72
93
|
* measured along the normal to it (so the middle follows the pointer without sliding along the arrow).
|
|
73
94
|
*/
|
|
74
95
|
export function bendTowards(start, end, point) {
|
|
75
|
-
|
|
96
|
+
var normal = directionBetween(start, end).normal;
|
|
76
97
|
return (point.x - (start.x + end.x) / 2) * normal.x + (point.y - (start.y + end.y) / 2) * normal.y;
|
|
77
98
|
}
|
|
78
99
|
/**
|
|
@@ -80,52 +101,53 @@ export function bendTowards(start, end, point) {
|
|
|
80
101
|
* come back in the frame the points were given in, the path moved to the corner of the box that holds it.
|
|
81
102
|
*/
|
|
82
103
|
export function buildArrow(start, end, bend, strokeWidth) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
104
|
+
var _a, _b;
|
|
105
|
+
var _c = directionBetween(start, end), length = _c.length, unit = _c.unit, normal = _c.normal;
|
|
106
|
+
var bent = drawnBend(bend, length);
|
|
107
|
+
var middle = { x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 };
|
|
108
|
+
var shaft;
|
|
109
|
+
var bodyLength = length;
|
|
88
110
|
// The point of the shaft one head-length back from the tip: the head is the shaft's own direction turned either way.
|
|
89
|
-
|
|
111
|
+
var pointBack;
|
|
90
112
|
if (bent === 0) {
|
|
91
113
|
shaft = [
|
|
92
114
|
['M', start.x, start.y],
|
|
93
115
|
['L', end.x, end.y]
|
|
94
116
|
];
|
|
95
|
-
pointBack = (headLength)
|
|
117
|
+
pointBack = function (headLength) { return ({ x: end.x - unit.x * headLength, y: end.y - unit.y * headLength }); };
|
|
96
118
|
}
|
|
97
119
|
else {
|
|
98
120
|
// The circle through the start, the end and the middle: the arc between the ends that goes by the middle.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
121
|
+
var half = length / 2;
|
|
122
|
+
var sagitta = Math.abs(bent);
|
|
123
|
+
var radius_1 = (half * half + sagitta * sagitta) / (2 * sagitta);
|
|
124
|
+
var centerAlongNormal = bent - Math.sign(bent) * radius_1;
|
|
125
|
+
var center_1 = { x: middle.x + normal.x * centerAlongNormal, y: middle.y + normal.y * centerAlongNormal };
|
|
126
|
+
var largeArc = sagitta > half ? 1 : 0;
|
|
105
127
|
// To the right of the way it points (a positive bend) the arc runs against the clock on the screen.
|
|
106
|
-
|
|
107
|
-
shaft = parsePathData(
|
|
108
|
-
bodyLength = 2 *
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
pointBack = (headLength)
|
|
128
|
+
var sweep = bent > 0 ? 0 : 1;
|
|
129
|
+
shaft = parsePathData("M ".concat(start.x, " ").concat(start.y, " A ").concat(radius_1, " ").concat(radius_1, " 0 ").concat(largeArc, " ").concat(sweep, " ").concat(end.x, " ").concat(end.y));
|
|
130
|
+
bodyLength = 2 * radius_1 * Math.atan2(half, radius_1 - sagitta);
|
|
131
|
+
var travel_1 = bent > 0 ? -1 : 1;
|
|
132
|
+
var tipAngle_1 = Math.atan2(end.y - center_1.y, end.x - center_1.x);
|
|
133
|
+
pointBack = function (headLength) {
|
|
112
134
|
// The point of the arc that is `headLength` from the tip as the crow flies, a little way back along the arc.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return { x:
|
|
135
|
+
var turn = 2 * Math.asin(Math.min(1, headLength / (2 * radius_1)));
|
|
136
|
+
var angle = tipAngle_1 - travel_1 * turn;
|
|
137
|
+
return { x: center_1.x + radius_1 * Math.cos(angle), y: center_1.y + radius_1 * Math.sin(angle) };
|
|
116
138
|
};
|
|
117
139
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
140
|
+
var headLength = arrowHeadLength(bodyLength, strokeWidth);
|
|
141
|
+
var back = pointBack(headLength);
|
|
142
|
+
var leftWing = rotateAbout(back, end, ARROW_HEAD_ANGLE);
|
|
143
|
+
var rightWing = rotateAbout(back, end, -ARROW_HEAD_ANGLE);
|
|
144
|
+
var world = __spreadArray(__spreadArray([], shaft, true), [['M', leftWing.x, leftWing.y], ['L', end.x, end.y], ['L', rightWing.x, rightWing.y]], false);
|
|
145
|
+
var bounds = pathBounds(world);
|
|
146
|
+
var origin = bounds ? { x: bounds.x, y: bounds.y } : { x: start.x, y: start.y };
|
|
147
|
+
var box = { x: origin.x, y: origin.y, width: Math.max((_a = bounds === null || bounds === void 0 ? void 0 : bounds.width) !== null && _a !== void 0 ? _a : 0, 1), height: Math.max((_b = bounds === null || bounds === void 0 ? void 0 : bounds.height) !== null && _b !== void 0 ? _b : 0, 1) };
|
|
126
148
|
return {
|
|
127
149
|
commands: translatePathCommands(world, -origin.x, -origin.y),
|
|
128
|
-
box,
|
|
150
|
+
box: box,
|
|
129
151
|
spec: {
|
|
130
152
|
start: { x: start.x - origin.x, y: start.y - origin.y },
|
|
131
153
|
end: { x: end.x - origin.x, y: end.y - origin.y },
|
|
@@ -135,38 +157,37 @@ export function buildArrow(start, end, bend, strokeWidth) {
|
|
|
135
157
|
}
|
|
136
158
|
/** Where the points of an arrow are in the world: its two ends, and the handle in the middle (on the line while the arrow is straight). */
|
|
137
159
|
export function arrowPointsInWorld(shape) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return { start, end, middle: arrowMiddle(start, end, shape.arrow.bend) };
|
|
160
|
+
var start = { x: shape.x + shape.arrow.start.x, y: shape.y + shape.arrow.start.y };
|
|
161
|
+
var end = { x: shape.x + shape.arrow.end.x, y: shape.y + shape.arrow.end.y };
|
|
162
|
+
return { start: start, end: end, middle: arrowMiddle(start, end, shape.arrow.bend) };
|
|
141
163
|
}
|
|
142
|
-
/**
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
d: serializePathData(geometry.commands),
|
|
156
|
-
arrow: geometry.spec
|
|
164
|
+
/**
|
|
165
|
+
* The arrow drawn again after a change: a new shape with the same id, the new box, path and points. What its ends are tied to stays
|
|
166
|
+
* as it was unless the change says otherwise (the points do not move because a tie says so here: `arrowBinding.ts` places them).
|
|
167
|
+
*/
|
|
168
|
+
export function reshapeArrow(shape, change) {
|
|
169
|
+
var _a, _b, _c, _d;
|
|
170
|
+
if (change === void 0) { change = {}; }
|
|
171
|
+
var current = arrowPointsInWorld(shape);
|
|
172
|
+
var strokeWidth = (_a = change.strokeWidth) !== null && _a !== void 0 ? _a : shape.strokeWidth;
|
|
173
|
+
var geometry = buildArrow((_b = change.start) !== null && _b !== void 0 ? _b : current.start, (_c = change.end) !== null && _c !== void 0 ? _c : current.end, (_d = change.bend) !== null && _d !== void 0 ? _d : shape.arrow.bend, strokeWidth);
|
|
174
|
+
var ties = {
|
|
175
|
+
startBinding: 'startBinding' in change ? change.startBinding : shape.arrow.startBinding,
|
|
176
|
+
endBinding: 'endBinding' in change ? change.endBinding : shape.arrow.endBinding
|
|
157
177
|
};
|
|
178
|
+
return __assign(__assign({}, shape), { x: geometry.box.x, y: geometry.box.y, width: geometry.box.width, height: geometry.box.height, rotation: 0, strokeWidth: strokeWidth, d: serializePathData(geometry.commands), arrow: withBindings(geometry.spec, ties) });
|
|
158
179
|
}
|
|
159
180
|
/**
|
|
160
181
|
* The arrow after the board around it was changed by a mapping of the world (stretched, mirrored, turned): its ends and the middle of
|
|
161
182
|
* its arc go where the mapping sends them, and the bend is whatever puts the arc through those three points.
|
|
162
183
|
*/
|
|
163
184
|
export function transformArrow(shape, mapPoint) {
|
|
164
|
-
|
|
185
|
+
var _a = arrowPointsInWorld(shape), start = _a.start, end = _a.end;
|
|
165
186
|
// The middle the arrow was drawn with, even when it was too small to show: a straight arrow stays straight.
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
187
|
+
var middle = arrowMiddle(start, end, shape.arrow.bend);
|
|
188
|
+
var mappedStart = mapPoint(start);
|
|
189
|
+
var mappedEnd = mapPoint(end);
|
|
190
|
+
var mappedMiddle = mapPoint(middle);
|
|
170
191
|
return reshapeArrow(shape, { start: mappedStart, end: mappedEnd, bend: bendTowards(mappedStart, mappedEnd, mappedMiddle) });
|
|
171
192
|
}
|
|
172
193
|
/**
|
|
@@ -174,9 +195,9 @@ export function transformArrow(shape, mapPoint) {
|
|
|
174
195
|
* tip (the shaft, then two wings, each its own sub-path). Null for a path that is not that.
|
|
175
196
|
*/
|
|
176
197
|
export function arrowPointsOfLegacyPath(commands) {
|
|
177
|
-
|
|
198
|
+
var moves = commands.filter(function (command) { return command[0] === 'M'; });
|
|
178
199
|
if (moves.length < 2)
|
|
179
200
|
return null;
|
|
180
|
-
|
|
201
|
+
var first = moves[0], second = moves[1];
|
|
181
202
|
return { start: { x: first[1], y: first[2] }, end: { x: second[1], y: second[2] } };
|
|
182
203
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arrows that stay tied to shapes, the way tldraw's do.
|
|
3
|
+
*
|
|
4
|
+
* An end of an arrow can be tied to a shape (`ArrowBinding`, see `types.ts`). A tied end is not a point of the board any more but
|
|
5
|
+
* a place on the shape, so it goes where the shape goes: when the shape is moved, stretched, turned, or (for a text) grows, the
|
|
6
|
+
* arrow is drawn again from its new place. Two kinds of tie:
|
|
7
|
+
*
|
|
8
|
+
* - `edge`: the end rests on one point of the shape's outline, kept as a fraction of the shape's own box, so it stays at the same
|
|
9
|
+
* place of the shape whatever happens to the shape. It slides along the outline as the pointer slides along it.
|
|
10
|
+
* - `center`: the end aims at the middle of the shape and rests where the line between the middle and the other end meets the
|
|
11
|
+
* outline, so the arrow always points at the shape and runs round it when the other end moves.
|
|
12
|
+
*
|
|
13
|
+
* Everything here is a pure function of the records. Where the ends rest is decided in `positionBoundArrow`, and the engine runs
|
|
14
|
+
* `reconcileArrowBindings` over every scene it is given, so that every change of the board (a drag, a stretch, a turn, a nudge, a
|
|
15
|
+
* word typed, a font that loaded, an undo) moves the arrows in the same step as the shapes they are tied to. A scene in which every
|
|
16
|
+
* arrow is where its ties say comes back as the very same array, so nothing is recorded for a change that moved nothing.
|
|
17
|
+
*
|
|
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
|
+
* 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).
|
|
21
|
+
*/
|
|
22
|
+
import type { ArrowObject } from './arrow.js';
|
|
23
|
+
import type { Scene } from './scene.js';
|
|
24
|
+
import type { ArrowBinding, ArrowBindHint, ObjectId, Point, SceneObject, ShapeObject } from './types.js';
|
|
25
|
+
/** 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. */
|
|
26
|
+
export declare const ARROW_BIND_REACH_PIXELS = 6;
|
|
27
|
+
/** 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. */
|
|
28
|
+
export declare const ARROW_EDGE_BAND_PIXELS = 14;
|
|
29
|
+
/** 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. */
|
|
30
|
+
export declare const ARROW_EDGE_BAND_FRACTION = 0.25;
|
|
31
|
+
/** Anything that finds a shape by its id: the index of a scene, usually. */
|
|
32
|
+
export type ShapeLookup = (id: ObjectId) => SceneObject | undefined;
|
|
33
|
+
/** The middle of a shape, in the world. */
|
|
34
|
+
export declare function centerOfShape(shape: ShapeObject): Point;
|
|
35
|
+
/** The point of the outline of a shape that an `edge` tie rests on, in the world. */
|
|
36
|
+
export declare function edgePointOf(shape: ShapeObject, anchor: Point): Point;
|
|
37
|
+
/**
|
|
38
|
+
* Where an arrow aimed from `aim` at the middle of a shape meets its outline, in the world. The middle itself when the arrow comes
|
|
39
|
+
* from inside the shape (or from the middle), so that it still points at it.
|
|
40
|
+
*/
|
|
41
|
+
export declare function centerCrossingOf(shape: ShapeObject, aim: Point): Point;
|
|
42
|
+
/**
|
|
43
|
+
* An arrow with its tied ends where their ties say. A tie whose shape is gone is cut and its end stays where it was. The very same
|
|
44
|
+
* object when nothing has to move or be cut, so the function can be run again and again.
|
|
45
|
+
*/
|
|
46
|
+
export declare function positionBoundArrow(arrow: ArrowObject, lookup: ShapeLookup): ArrowObject;
|
|
47
|
+
/**
|
|
48
|
+
* 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
|
|
49
|
+
* scene it is given, so that it must cost nothing when no arrow is tied, and must not make a change out of nothing).
|
|
50
|
+
*/
|
|
51
|
+
export declare function reconcileArrowBindings(scene: Scene): Scene;
|
|
52
|
+
/** What a pointer over a shape would tie an end to: the tie to store, and what the board shows meanwhile. */
|
|
53
|
+
export interface ArrowBindingPick {
|
|
54
|
+
readonly binding: ArrowBinding;
|
|
55
|
+
readonly hint: ArrowBindHint;
|
|
56
|
+
}
|
|
57
|
+
export interface PickArrowBindingOptions {
|
|
58
|
+
/** The zoom of the view: the reach of the pointer and the band of the edge are given in screen pixels. */
|
|
59
|
+
zoom: number;
|
|
60
|
+
/** Shapes to look through (the shape the other end is tied to: both ends of an arrow are never tied to the same one). */
|
|
61
|
+
ignore?: ReadonlySet<ObjectId>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The tie a pointer at a point of the world would make: the topmost shape under it that an arrow can be tied to, tied on its outline
|
|
65
|
+
* when the pointer is within the band of it and at its middle when it is further in. A hollow shape is under the pointer only near its
|
|
66
|
+
* line, as for a click, so that a frame never hides what lies inside it; only when nothing else is under the pointer is the empty
|
|
67
|
+
* middle of a hollow shape taken as the shape. Null when the pointer is over nothing of the kind.
|
|
68
|
+
*/
|
|
69
|
+
export declare function pickArrowBinding(scene: Scene, point: Point, options: PickArrowBindingOptions): ArrowBindingPick | null;
|