@widgetic/canvas 0.5.34 → 0.5.36
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 +105 -36
- package/dist/canvas/DomCanvas.svelte +454 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +222 -0
- package/dist/canvas/autosaveScheduler.d.ts +58 -0
- package/dist/canvas/autosaveScheduler.js +157 -0
- package/dist/canvas/boardFrame.d.ts +13 -0
- package/dist/canvas/boardFrame.js +13 -0
- package/dist/canvas/boardSurface.d.ts +9 -0
- package/dist/canvas/boardSurface.js +9 -0
- package/dist/canvas/chromeLayout.d.ts +76 -0
- package/dist/canvas/chromeLayout.js +121 -0
- package/dist/canvas/emptyBoardHint.d.ts +17 -0
- package/dist/canvas/emptyBoardHint.js +12 -0
- package/dist/canvas/eventScope.d.ts +1 -1
- package/dist/canvas/eventScope.js +1 -1
- package/dist/canvas/host/CanvasHost.d.ts +351 -0
- package/dist/canvas/host/CanvasHost.js +1740 -0
- package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
- package/dist/canvas/host/CanvasHostApi.js +95 -0
- package/dist/canvas/host/browserServices.d.ts +46 -0
- package/dist/canvas/host/browserServices.js +167 -0
- package/dist/canvas/host/conversion.d.ts +51 -0
- package/dist/canvas/host/conversion.js +123 -0
- package/dist/canvas/host/hostObject.d.ts +40 -0
- package/dist/canvas/host/hostObject.js +93 -0
- package/dist/canvas/host/pageEvents.d.ts +24 -0
- package/dist/canvas/host/pageEvents.js +35 -0
- package/dist/canvas/host/screenRects.d.ts +66 -0
- package/dist/canvas/host/screenRects.js +102 -0
- package/dist/canvas/host/strokeSnap.d.ts +67 -0
- package/dist/canvas/host/strokeSnap.js +165 -0
- package/dist/canvas/index.d.ts +9 -7
- package/dist/canvas/index.js +9 -7
- package/dist/canvas/openMarks.d.ts +17 -0
- package/dist/canvas/openMarks.js +26 -0
- package/dist/canvas/panZoomPosition.d.ts +15 -0
- package/dist/canvas/panZoomPosition.js +16 -0
- package/dist/canvas/preferredTextFont.d.ts +12 -0
- package/dist/canvas/preferredTextFont.js +38 -0
- package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
- package/dist/canvas/shapes/ShapeLibrary.js +13 -1
- package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
- package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
- package/dist/canvas/toolbarPosition.d.ts +24 -0
- package/dist/canvas/toolbarPosition.js +24 -0
- package/dist/canvas/types.d.ts +14 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
- package/dist/canvas/ui/CanvasHint.svelte +20 -0
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
- package/dist/canvas/ui/ColorField.svelte +155 -0
- package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
- package/dist/canvas/ui/LoadNotice.svelte +39 -0
- package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
- package/dist/canvas/ui/SelectionPanel.svelte +300 -0
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
- package/dist/canvas/ui/SelectorBar.svelte +82 -0
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
- package/dist/canvas/ui/ShapesMenu.svelte +185 -0
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/ToolButton.svelte +78 -0
- package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
- package/dist/canvas/ui/WidgetActions.svelte +152 -0
- package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
- package/dist/canvas/ui/ZoomControl.svelte +89 -0
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
- package/dist/canvas/ui/colorModel.d.ts +18 -0
- package/dist/canvas/ui/colorModel.js +85 -0
- package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
- package/dist/canvas/ui/colorPickerMessages.js +15 -0
- package/dist/canvas/ui/floatingCard.d.ts +107 -0
- package/dist/canvas/ui/floatingCard.js +107 -0
- package/dist/canvas/ui/floatingLayer.d.ts +21 -0
- package/dist/canvas/ui/floatingLayer.js +52 -0
- package/dist/canvas/ui/icons.d.ts +12 -0
- package/dist/canvas/ui/icons.js +7 -0
- package/dist/canvas/ui/keyHints.d.ts +27 -0
- package/dist/canvas/ui/keyHints.js +43 -0
- package/dist/canvas/ui/popoverSide.d.ts +5 -0
- package/dist/canvas/ui/popoverSide.js +20 -0
- package/dist/canvas/ui/selectionTraits.d.ts +61 -0
- package/dist/canvas/ui/selectionTraits.js +99 -0
- package/dist/canvas/view/CanvasStage.svelte +132 -0
- package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
- package/dist/canvas/view/EraserTrail.svelte +50 -0
- package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
- package/dist/canvas/view/ObjectView.svelte +245 -0
- package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
- package/dist/canvas/view/SelectionOverlay.svelte +487 -0
- package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
- package/dist/canvas/view/TextEditor.svelte +76 -0
- package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.js +34 -0
- package/dist/canvas/view/geometry.d.ts +169 -0
- package/dist/canvas/view/geometry.js +272 -0
- package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
- package/dist/components/index.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +2 -2
- package/dist/engine/EngineCore.d.ts +273 -0
- package/dist/engine/EngineCore.js +1051 -0
- package/dist/engine/codec/canvasDocument.d.ts +57 -0
- package/dist/engine/codec/canvasDocument.js +471 -0
- package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
- package/dist/engine/codec/hostReadableKeys.js +25 -0
- package/dist/engine/core/arrange.d.ts +20 -0
- package/dist/engine/core/arrange.js +81 -0
- package/dist/engine/core/arrow.d.ts +91 -0
- package/dist/engine/core/arrow.js +182 -0
- package/dist/engine/core/brush.d.ts +68 -0
- package/dist/engine/core/brush.js +173 -0
- package/dist/engine/core/defaults.d.ts +70 -0
- package/dist/engine/core/defaults.js +158 -0
- package/dist/engine/core/freehand.d.ts +130 -0
- package/dist/engine/core/freehand.js +430 -0
- package/dist/engine/core/geometry.d.ts +44 -0
- package/dist/engine/core/geometry.js +216 -0
- package/dist/engine/core/history.d.ts +39 -0
- package/dist/engine/core/history.js +69 -0
- package/dist/engine/core/hitTest.d.ts +54 -0
- package/dist/engine/core/hitTest.js +298 -0
- package/dist/engine/core/matrix.d.ts +47 -0
- package/dist/engine/core/matrix.js +101 -0
- package/dist/engine/core/pathData.d.ts +50 -0
- package/dist/engine/core/pathData.js +550 -0
- package/dist/engine/core/rotationReset.d.ts +10 -0
- package/dist/engine/core/rotationReset.js +17 -0
- package/dist/engine/core/scene.d.ts +99 -0
- package/dist/engine/core/scene.js +450 -0
- package/dist/engine/core/scribble.d.ts +64 -0
- package/dist/engine/core/scribble.js +121 -0
- package/dist/engine/core/shapePaths.d.ts +34 -0
- package/dist/engine/core/shapePaths.js +69 -0
- package/dist/engine/core/shapeTools.d.ts +80 -0
- package/dist/engine/core/shapeTools.js +161 -0
- package/dist/engine/core/snapping.d.ts +64 -0
- package/dist/engine/core/snapping.js +164 -0
- package/dist/engine/core/style.d.ts +40 -0
- package/dist/engine/core/style.js +151 -0
- package/dist/engine/core/transform.d.ts +79 -0
- package/dist/engine/core/transform.js +258 -0
- package/dist/engine/core/types.d.ts +238 -0
- package/dist/engine/core/types.js +31 -0
- package/dist/engine/core/viewport.d.ts +32 -0
- package/dist/engine/core/viewport.js +84 -0
- package/dist/engine/input/PointerController.d.ts +38 -0
- package/dist/engine/input/PointerController.js +545 -0
- package/dist/engine/input/arrowHandles.d.ts +38 -0
- package/dist/engine/input/arrowHandles.js +60 -0
- package/dist/engine/input/gestures.d.ts +79 -0
- package/dist/engine/input/gestures.js +769 -0
- package/dist/engine/input/hoverCursor.d.ts +29 -0
- package/dist/engine/input/hoverCursor.js +23 -0
- package/dist/engine/input/keyboard.d.ts +75 -0
- package/dist/engine/input/keyboard.js +264 -0
- package/dist/engine/input/selectionHandles.d.ts +89 -0
- package/dist/engine/input/selectionHandles.js +169 -0
- package/dist/engine/input/types.d.ts +82 -0
- package/dist/engine/input/types.js +5 -0
- package/dist/engine/render/rasterize.d.ts +74 -0
- package/dist/engine/render/rasterize.js +273 -0
- package/dist/engine/state.d.ts +124 -0
- package/dist/engine/state.js +47 -0
- package/dist/engine/text/layoutText.d.ts +53 -0
- package/dist/engine/text/layoutText.js +170 -0
- package/dist/engine/widgets.d.ts +124 -0
- package/dist/engine/widgets.js +345 -0
- package/dist/i18n/labels.d.ts +4 -15
- package/dist/i18n/labels.js +33 -53
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +51 -122
- package/dist/i18n/messages.js +58 -131
- package/dist/i18n/shortcuts.d.ts +0 -1
- package/dist/i18n/shortcuts.js +2 -11
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/package.json +30 -5
- package/dist/canvas/Canvas.svelte +0 -13299
- package/dist/canvas/Canvas.svelte.d.ts +0 -281
- package/dist/canvas/CanvasToolbar.svelte +0 -1498
- package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
- package/dist/canvas/ContextMenu.svelte +0 -305
- package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
- package/dist/canvas/PanZoomPanel.svelte +0 -478
- package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
- package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
- package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
- package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
- package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
- package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
- package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
- package/dist/canvas/shadowRootText.d.ts +0 -5
- package/dist/canvas/shadowRootText.js +0 -43
- package/dist/canvas/shapes/FrameShape.d.ts +0 -97
- package/dist/canvas/shapes/FrameShape.js +0 -953
- package/dist/canvas/shapes/ImageShape.d.ts +0 -47
- package/dist/canvas/shapes/ImageShape.js +0 -339
- package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
- package/dist/canvas/shapes/WidgetShape.js +0 -200
- package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
- package/dist/canvas/widgetPreviewGeometry.js +0 -66
- package/dist/i18n/shapeWords.d.ts +0 -30
- package/dist/i18n/shapeWords.js +0 -36
- package/dist/i18n/slots.d.ts +0 -20
- package/dist/i18n/slots.js +0 -40
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lining objects up with each other: align their edges or middles, and space them out evenly.
|
|
3
|
+
*
|
|
4
|
+
* Both work on the units of a selection (a group is one unit), measured by the rectangle around the shapes
|
|
5
|
+
* they draw, and move whole units: nothing is resized.
|
|
6
|
+
*/
|
|
7
|
+
import { boundsOfObjects, normalizeSelection, translateObjects } from './scene.js';
|
|
8
|
+
function unitsOf(scene, ids) {
|
|
9
|
+
const units = [];
|
|
10
|
+
for (const id of normalizeSelection(scene, ids)) {
|
|
11
|
+
const bounds = boundsOfObjects(scene, [id]);
|
|
12
|
+
if (bounds)
|
|
13
|
+
units.push({ id, bounds });
|
|
14
|
+
}
|
|
15
|
+
return units;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Moves every unit so that its chosen edge (or middle) is where the same edge of the whole selection is.
|
|
19
|
+
* Needs two units or more; returns the same scene otherwise, or when everything is already in line.
|
|
20
|
+
*/
|
|
21
|
+
export function alignObjects(scene, ids, mode) {
|
|
22
|
+
const units = unitsOf(scene, ids);
|
|
23
|
+
if (units.length < 2)
|
|
24
|
+
return scene;
|
|
25
|
+
const left = Math.min(...units.map((unit) => unit.bounds.x));
|
|
26
|
+
const right = Math.max(...units.map((unit) => unit.bounds.x + unit.bounds.width));
|
|
27
|
+
const top = Math.min(...units.map((unit) => unit.bounds.y));
|
|
28
|
+
const bottom = Math.max(...units.map((unit) => unit.bounds.y + unit.bounds.height));
|
|
29
|
+
let next = scene;
|
|
30
|
+
for (const unit of units) {
|
|
31
|
+
let deltaX = 0;
|
|
32
|
+
let deltaY = 0;
|
|
33
|
+
switch (mode) {
|
|
34
|
+
case 'left':
|
|
35
|
+
deltaX = left - unit.bounds.x;
|
|
36
|
+
break;
|
|
37
|
+
case 'right':
|
|
38
|
+
deltaX = right - (unit.bounds.x + unit.bounds.width);
|
|
39
|
+
break;
|
|
40
|
+
case 'center':
|
|
41
|
+
deltaX = (left + right) / 2 - (unit.bounds.x + unit.bounds.width / 2);
|
|
42
|
+
break;
|
|
43
|
+
case 'top':
|
|
44
|
+
deltaY = top - unit.bounds.y;
|
|
45
|
+
break;
|
|
46
|
+
case 'bottom':
|
|
47
|
+
deltaY = bottom - (unit.bounds.y + unit.bounds.height);
|
|
48
|
+
break;
|
|
49
|
+
case 'middle':
|
|
50
|
+
deltaY = (top + bottom) / 2 - (unit.bounds.y + unit.bounds.height / 2);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
next = translateObjects(next, [unit.id], deltaX, deltaY);
|
|
54
|
+
}
|
|
55
|
+
return next;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Spaces the units so the gaps between neighbours are equal, keeping the first and the last where they are.
|
|
59
|
+
* Needs three units or more.
|
|
60
|
+
*/
|
|
61
|
+
export function distributeObjects(scene, ids, axis) {
|
|
62
|
+
const units = unitsOf(scene, ids);
|
|
63
|
+
if (units.length < 3)
|
|
64
|
+
return scene;
|
|
65
|
+
const start = (unit) => (axis === 'horizontal' ? unit.bounds.x : unit.bounds.y);
|
|
66
|
+
const size = (unit) => (axis === 'horizontal' ? unit.bounds.width : unit.bounds.height);
|
|
67
|
+
const ordered = [...units].sort((first, second) => start(first) - start(second));
|
|
68
|
+
const first = ordered[0];
|
|
69
|
+
const last = ordered[ordered.length - 1];
|
|
70
|
+
const span = start(last) + size(last) - start(first);
|
|
71
|
+
const occupied = ordered.reduce((total, unit) => total + size(unit), 0);
|
|
72
|
+
const gap = (span - occupied) / (ordered.length - 1);
|
|
73
|
+
let next = scene;
|
|
74
|
+
let cursor = start(first) + size(first) + gap;
|
|
75
|
+
for (const unit of ordered.slice(1, -1)) {
|
|
76
|
+
const delta = cursor - start(unit);
|
|
77
|
+
next = axis === 'horizontal' ? translateObjects(next, [unit.id], delta, 0) : translateObjects(next, [unit.id], 0, delta);
|
|
78
|
+
cursor += size(unit) + gap;
|
|
79
|
+
}
|
|
80
|
+
return next;
|
|
81
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arrows, drawn the way tldraw draws them (the numbers were taken from reading its arrow code; the code is this package's own).
|
|
3
|
+
*
|
|
4
|
+
* An arrow is a start, an end and a bend. From those two things are drawn, again at every change:
|
|
5
|
+
*
|
|
6
|
+
* - The shaft: a straight line from the start to the end, or, when the middle is pushed to the side by `bend`, the arc of the
|
|
7
|
+
* circle that goes through the start, the end and that middle. A bend smaller than `MIN_ARROW_BEND` is no bend at all, so an
|
|
8
|
+
* arrow is straight until it is clearly curved.
|
|
9
|
+
* - The head, an open chevron on the end: two wings that open `ARROW_HEAD_ANGLE` (30 degrees) either side of the shaft. Its
|
|
10
|
+
* length is a fifth of the arrow, but never less than the width of the line and never more than three times it, so a short
|
|
11
|
+
* arrow has a short head and a long one does not get a big one. Because the head is drawn from the arrow and not scaled with it,
|
|
12
|
+
* it keeps its size when the arrow is stretched, and it grows with the width of the line.
|
|
13
|
+
*
|
|
14
|
+
* Everything here is a pure function of numbers, in whatever frame the points are given in (the world, usually). The path comes back
|
|
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
|
+
* `ArrowSpec` in the same coordinates.
|
|
17
|
+
*/
|
|
18
|
+
import type { PathCommand } from './pathData.js';
|
|
19
|
+
import type { ArrowSpec, PathObject, Point, Rect, ShapeObject } from './types.js';
|
|
20
|
+
/** The wings of the head open this far either side of the shaft (30 degrees). */
|
|
21
|
+
export declare const ARROW_HEAD_ANGLE: number;
|
|
22
|
+
/** The head is this fraction of the length of the arrow ... */
|
|
23
|
+
export declare const ARROW_HEAD_LENGTH_FRACTION: number;
|
|
24
|
+
/** ... but at least this many widths of the line ... */
|
|
25
|
+
export declare const ARROW_HEAD_MIN_WIDTHS = 1;
|
|
26
|
+
/** ... and at most this many. */
|
|
27
|
+
export declare const ARROW_HEAD_MAX_WIDTHS = 3;
|
|
28
|
+
/** A bend smaller than this (world units) is not a bend: the arrow is straight. */
|
|
29
|
+
export declare const MIN_ARROW_BEND = 8;
|
|
30
|
+
/** An arrow bends by at most this many times its own length (a little under a full circle). */
|
|
31
|
+
export declare const MAX_ARROW_BEND_PER_LENGTH = 2;
|
|
32
|
+
/** The three things of an arrow a person can take hold of. */
|
|
33
|
+
export type ArrowHandleId = 'start' | 'end' | 'bend';
|
|
34
|
+
export interface ArrowGeometry {
|
|
35
|
+
/** The path, in the coordinates of `box`. */
|
|
36
|
+
commands: PathCommand[];
|
|
37
|
+
/** The box that holds the path, in the frame the points were given in. At least 1 by 1. */
|
|
38
|
+
box: Rect;
|
|
39
|
+
/** The arrow in the coordinates of `box`. The bend is the one that is drawn (zero when it is below `MIN_ARROW_BEND`). */
|
|
40
|
+
spec: ArrowSpec;
|
|
41
|
+
}
|
|
42
|
+
/** An arrow as the shape that holds it. */
|
|
43
|
+
export type ArrowObject = PathObject & {
|
|
44
|
+
arrow: ArrowSpec;
|
|
45
|
+
};
|
|
46
|
+
/** Whether a shape is an arrow made by the Arrow tool, with the points it is drawn from. */
|
|
47
|
+
export declare function isArrow(shape: ShapeObject): shape is ArrowObject;
|
|
48
|
+
/** The bend that is drawn for a bend that was asked for: none below the threshold, and never more than the arrow can hold. */
|
|
49
|
+
export declare function drawnBend(bend: number, length: number): number;
|
|
50
|
+
/** The length of the head for an arrow whose shaft is `bodyLength` long, drawn with a line `strokeWidth` wide. */
|
|
51
|
+
export declare function arrowHeadLength(bodyLength: number, strokeWidth: number): number;
|
|
52
|
+
/** The middle of the arrow: the middle of the line between its ends, pushed `bend` to the right of the way it points. */
|
|
53
|
+
export declare function arrowMiddle(start: Point, end: Point, bend: number): Point;
|
|
54
|
+
/**
|
|
55
|
+
* The bend that puts the middle of the arrow where a point is: how far the point is from the middle of the line between the ends,
|
|
56
|
+
* measured along the normal to it (so the middle follows the pointer without sliding along the arrow).
|
|
57
|
+
*/
|
|
58
|
+
export declare function bendTowards(start: Point, end: Point, point: Point): number;
|
|
59
|
+
/**
|
|
60
|
+
* Draws an arrow from `start` to `end` with its middle pushed `bend` to the side, with a line `strokeWidth` wide. Points and path
|
|
61
|
+
* come back in the frame the points were given in, the path moved to the corner of the box that holds it.
|
|
62
|
+
*/
|
|
63
|
+
export declare function buildArrow(start: Point, end: Point, bend: number, strokeWidth: number): ArrowGeometry;
|
|
64
|
+
/** 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). */
|
|
65
|
+
export declare function arrowPointsInWorld(shape: ArrowObject): {
|
|
66
|
+
start: Point;
|
|
67
|
+
end: Point;
|
|
68
|
+
middle: Point;
|
|
69
|
+
};
|
|
70
|
+
/** What may change in an arrow: the points in the world, the bend, the width of the line the head is sized by. */
|
|
71
|
+
export interface ArrowChange {
|
|
72
|
+
start?: Point;
|
|
73
|
+
end?: Point;
|
|
74
|
+
bend?: number;
|
|
75
|
+
strokeWidth?: number;
|
|
76
|
+
}
|
|
77
|
+
/** The arrow drawn again after a change: a new shape with the same id, the new box, path and points. */
|
|
78
|
+
export declare function reshapeArrow(shape: ArrowObject, change?: ArrowChange): ArrowObject;
|
|
79
|
+
/**
|
|
80
|
+
* The arrow after the board around it was changed by a mapping of the world (stretched, mirrored, turned): its ends and the middle of
|
|
81
|
+
* its arc go where the mapping sends them, and the bend is whatever puts the arc through those three points.
|
|
82
|
+
*/
|
|
83
|
+
export declare function transformArrow(shape: ArrowObject, mapPoint: (point: Point) => Point): ArrowObject;
|
|
84
|
+
/**
|
|
85
|
+
* The arrow of a path an older board saved without its points: its first line starts at the tail and its head's wings meet at the
|
|
86
|
+
* tip (the shaft, then two wings, each its own sub-path). Null for a path that is not that.
|
|
87
|
+
*/
|
|
88
|
+
export declare function arrowPointsOfLegacyPath(commands: readonly PathCommand[]): {
|
|
89
|
+
start: Point;
|
|
90
|
+
end: Point;
|
|
91
|
+
} | null;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arrows, drawn the way tldraw draws them (the numbers were taken from reading its arrow code; the code is this package's own).
|
|
3
|
+
*
|
|
4
|
+
* An arrow is a start, an end and a bend. From those two things are drawn, again at every change:
|
|
5
|
+
*
|
|
6
|
+
* - The shaft: a straight line from the start to the end, or, when the middle is pushed to the side by `bend`, the arc of the
|
|
7
|
+
* circle that goes through the start, the end and that middle. A bend smaller than `MIN_ARROW_BEND` is no bend at all, so an
|
|
8
|
+
* arrow is straight until it is clearly curved.
|
|
9
|
+
* - The head, an open chevron on the end: two wings that open `ARROW_HEAD_ANGLE` (30 degrees) either side of the shaft. Its
|
|
10
|
+
* length is a fifth of the arrow, but never less than the width of the line and never more than three times it, so a short
|
|
11
|
+
* arrow has a short head and a long one does not get a big one. Because the head is drawn from the arrow and not scaled with it,
|
|
12
|
+
* it keeps its size when the arrow is stretched, and it grows with the width of the line.
|
|
13
|
+
*
|
|
14
|
+
* Everything here is a pure function of numbers, in whatever frame the points are given in (the world, usually). The path comes back
|
|
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
|
+
* `ArrowSpec` in the same coordinates.
|
|
17
|
+
*/
|
|
18
|
+
import { clampNumber } from './geometry.js';
|
|
19
|
+
import { parsePathData, pathBounds, serializePathData, translatePathCommands } from './pathData.js';
|
|
20
|
+
/** The wings of the head open this far either side of the shaft (30 degrees). */
|
|
21
|
+
export const ARROW_HEAD_ANGLE = Math.PI / 6;
|
|
22
|
+
/** The head is this fraction of the length of the arrow ... */
|
|
23
|
+
export const ARROW_HEAD_LENGTH_FRACTION = 1 / 5;
|
|
24
|
+
/** ... but at least this many widths of the line ... */
|
|
25
|
+
export const ARROW_HEAD_MIN_WIDTHS = 1;
|
|
26
|
+
/** ... and at most this many. */
|
|
27
|
+
export const ARROW_HEAD_MAX_WIDTHS = 3;
|
|
28
|
+
/** A bend smaller than this (world units) is not a bend: the arrow is straight. */
|
|
29
|
+
export const MIN_ARROW_BEND = 8;
|
|
30
|
+
/** An arrow bends by at most this many times its own length (a little under a full circle). */
|
|
31
|
+
export const MAX_ARROW_BEND_PER_LENGTH = 2;
|
|
32
|
+
/** 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
|
+
const MIN_HEAD_LINE_WIDTH = 1;
|
|
34
|
+
/** Whether a shape is an arrow made by the Arrow tool, with the points it is drawn from. */
|
|
35
|
+
export function isArrow(shape) {
|
|
36
|
+
return shape.type === 'path' && shape.arrow !== null;
|
|
37
|
+
}
|
|
38
|
+
/** The bend that is drawn for a bend that was asked for: none below the threshold, and never more than the arrow can hold. */
|
|
39
|
+
export function drawnBend(bend, length) {
|
|
40
|
+
if (!Number.isFinite(bend) || Math.abs(bend) < MIN_ARROW_BEND)
|
|
41
|
+
return 0;
|
|
42
|
+
return clampNumber(bend, -MAX_ARROW_BEND_PER_LENGTH * length, MAX_ARROW_BEND_PER_LENGTH * length);
|
|
43
|
+
}
|
|
44
|
+
/** The length of the head for an arrow whose shaft is `bodyLength` long, drawn with a line `strokeWidth` wide. */
|
|
45
|
+
export function arrowHeadLength(bodyLength, strokeWidth) {
|
|
46
|
+
const width = Math.max(strokeWidth, MIN_HEAD_LINE_WIDTH);
|
|
47
|
+
return clampNumber(bodyLength * ARROW_HEAD_LENGTH_FRACTION, width * ARROW_HEAD_MIN_WIDTHS, width * ARROW_HEAD_MAX_WIDTHS);
|
|
48
|
+
}
|
|
49
|
+
/** A point turned about another by an angle (radians, clockwise on screen). */
|
|
50
|
+
function rotateAbout(point, center, radians) {
|
|
51
|
+
const cosine = Math.cos(radians);
|
|
52
|
+
const sine = Math.sin(radians);
|
|
53
|
+
const offsetX = point.x - center.x;
|
|
54
|
+
const offsetY = point.y - center.y;
|
|
55
|
+
return { x: center.x + offsetX * cosine - offsetY * sine, y: center.y + offsetX * sine + offsetY * cosine };
|
|
56
|
+
}
|
|
57
|
+
/** 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
|
+
function directionBetween(start, end) {
|
|
59
|
+
const deltaX = end.x - start.x;
|
|
60
|
+
const deltaY = end.y - start.y;
|
|
61
|
+
const length = Math.hypot(deltaX, deltaY);
|
|
62
|
+
const unit = length > 1e-9 ? { x: deltaX / length, y: deltaY / length } : { x: 1, y: 0 };
|
|
63
|
+
return { length, unit, normal: { x: -unit.y, y: unit.x } };
|
|
64
|
+
}
|
|
65
|
+
/** The middle of the arrow: the middle of the line between its ends, pushed `bend` to the right of the way it points. */
|
|
66
|
+
export function arrowMiddle(start, end, bend) {
|
|
67
|
+
const { normal } = directionBetween(start, end);
|
|
68
|
+
return { x: (start.x + end.x) / 2 + normal.x * bend, y: (start.y + end.y) / 2 + normal.y * bend };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The bend that puts the middle of the arrow where a point is: how far the point is from the middle of the line between the ends,
|
|
72
|
+
* measured along the normal to it (so the middle follows the pointer without sliding along the arrow).
|
|
73
|
+
*/
|
|
74
|
+
export function bendTowards(start, end, point) {
|
|
75
|
+
const { normal } = directionBetween(start, end);
|
|
76
|
+
return (point.x - (start.x + end.x) / 2) * normal.x + (point.y - (start.y + end.y) / 2) * normal.y;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Draws an arrow from `start` to `end` with its middle pushed `bend` to the side, with a line `strokeWidth` wide. Points and path
|
|
80
|
+
* come back in the frame the points were given in, the path moved to the corner of the box that holds it.
|
|
81
|
+
*/
|
|
82
|
+
export function buildArrow(start, end, bend, strokeWidth) {
|
|
83
|
+
const { length, unit, normal } = directionBetween(start, end);
|
|
84
|
+
const bent = drawnBend(bend, length);
|
|
85
|
+
const middle = { x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 };
|
|
86
|
+
let shaft;
|
|
87
|
+
let bodyLength = length;
|
|
88
|
+
// The point of the shaft one head-length back from the tip: the head is the shaft's own direction turned either way.
|
|
89
|
+
let pointBack;
|
|
90
|
+
if (bent === 0) {
|
|
91
|
+
shaft = [
|
|
92
|
+
['M', start.x, start.y],
|
|
93
|
+
['L', end.x, end.y]
|
|
94
|
+
];
|
|
95
|
+
pointBack = (headLength) => ({ x: end.x - unit.x * headLength, y: end.y - unit.y * headLength });
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// The circle through the start, the end and the middle: the arc between the ends that goes by the middle.
|
|
99
|
+
const half = length / 2;
|
|
100
|
+
const sagitta = Math.abs(bent);
|
|
101
|
+
const radius = (half * half + sagitta * sagitta) / (2 * sagitta);
|
|
102
|
+
const centerAlongNormal = bent - Math.sign(bent) * radius;
|
|
103
|
+
const center = { x: middle.x + normal.x * centerAlongNormal, y: middle.y + normal.y * centerAlongNormal };
|
|
104
|
+
const largeArc = sagitta > half ? 1 : 0;
|
|
105
|
+
// To the right of the way it points (a positive bend) the arc runs against the clock on the screen.
|
|
106
|
+
const sweep = bent > 0 ? 0 : 1;
|
|
107
|
+
shaft = parsePathData(`M ${start.x} ${start.y} A ${radius} ${radius} 0 ${largeArc} ${sweep} ${end.x} ${end.y}`);
|
|
108
|
+
bodyLength = 2 * radius * Math.atan2(half, radius - sagitta);
|
|
109
|
+
const travel = bent > 0 ? -1 : 1;
|
|
110
|
+
const tipAngle = Math.atan2(end.y - center.y, end.x - center.x);
|
|
111
|
+
pointBack = (headLength) => {
|
|
112
|
+
// The point of the arc that is `headLength` from the tip as the crow flies, a little way back along the arc.
|
|
113
|
+
const turn = 2 * Math.asin(Math.min(1, headLength / (2 * radius)));
|
|
114
|
+
const angle = tipAngle - travel * turn;
|
|
115
|
+
return { x: center.x + radius * Math.cos(angle), y: center.y + radius * Math.sin(angle) };
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const headLength = arrowHeadLength(bodyLength, strokeWidth);
|
|
119
|
+
const back = pointBack(headLength);
|
|
120
|
+
const leftWing = rotateAbout(back, end, ARROW_HEAD_ANGLE);
|
|
121
|
+
const rightWing = rotateAbout(back, end, -ARROW_HEAD_ANGLE);
|
|
122
|
+
const world = [...shaft, ['M', leftWing.x, leftWing.y], ['L', end.x, end.y], ['L', rightWing.x, rightWing.y]];
|
|
123
|
+
const bounds = pathBounds(world);
|
|
124
|
+
const origin = bounds ? { x: bounds.x, y: bounds.y } : { x: start.x, y: start.y };
|
|
125
|
+
const box = { x: origin.x, y: origin.y, width: Math.max(bounds?.width ?? 0, 1), height: Math.max(bounds?.height ?? 0, 1) };
|
|
126
|
+
return {
|
|
127
|
+
commands: translatePathCommands(world, -origin.x, -origin.y),
|
|
128
|
+
box,
|
|
129
|
+
spec: {
|
|
130
|
+
start: { x: start.x - origin.x, y: start.y - origin.y },
|
|
131
|
+
end: { x: end.x - origin.x, y: end.y - origin.y },
|
|
132
|
+
bend: bent
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** 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
|
+
export function arrowPointsInWorld(shape) {
|
|
138
|
+
const start = { x: shape.x + shape.arrow.start.x, y: shape.y + shape.arrow.start.y };
|
|
139
|
+
const end = { x: shape.x + shape.arrow.end.x, y: shape.y + shape.arrow.end.y };
|
|
140
|
+
return { start, end, middle: arrowMiddle(start, end, shape.arrow.bend) };
|
|
141
|
+
}
|
|
142
|
+
/** The arrow drawn again after a change: a new shape with the same id, the new box, path and points. */
|
|
143
|
+
export function reshapeArrow(shape, change = {}) {
|
|
144
|
+
const current = arrowPointsInWorld(shape);
|
|
145
|
+
const strokeWidth = change.strokeWidth ?? shape.strokeWidth;
|
|
146
|
+
const geometry = buildArrow(change.start ?? current.start, change.end ?? current.end, change.bend ?? shape.arrow.bend, strokeWidth);
|
|
147
|
+
return {
|
|
148
|
+
...shape,
|
|
149
|
+
x: geometry.box.x,
|
|
150
|
+
y: geometry.box.y,
|
|
151
|
+
width: geometry.box.width,
|
|
152
|
+
height: geometry.box.height,
|
|
153
|
+
rotation: 0,
|
|
154
|
+
strokeWidth,
|
|
155
|
+
d: serializePathData(geometry.commands),
|
|
156
|
+
arrow: geometry.spec
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* 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
|
+
* its arc go where the mapping sends them, and the bend is whatever puts the arc through those three points.
|
|
162
|
+
*/
|
|
163
|
+
export function transformArrow(shape, mapPoint) {
|
|
164
|
+
const { start, end } = arrowPointsInWorld(shape);
|
|
165
|
+
// The middle the arrow was drawn with, even when it was too small to show: a straight arrow stays straight.
|
|
166
|
+
const middle = arrowMiddle(start, end, shape.arrow.bend);
|
|
167
|
+
const mappedStart = mapPoint(start);
|
|
168
|
+
const mappedEnd = mapPoint(end);
|
|
169
|
+
const mappedMiddle = mapPoint(middle);
|
|
170
|
+
return reshapeArrow(shape, { start: mappedStart, end: mappedEnd, bend: bendTowards(mappedStart, mappedEnd, mappedMiddle) });
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The arrow of a path an older board saved without its points: its first line starts at the tail and its head's wings meet at the
|
|
174
|
+
* tip (the shaft, then two wings, each its own sub-path). Null for a path that is not that.
|
|
175
|
+
*/
|
|
176
|
+
export function arrowPointsOfLegacyPath(commands) {
|
|
177
|
+
const moves = commands.filter((command) => command[0] === 'M');
|
|
178
|
+
if (moves.length < 2)
|
|
179
|
+
return null;
|
|
180
|
+
const [first, second] = moves;
|
|
181
|
+
return { start: { x: first[1], y: first[2] }, end: { x: second[1], y: second[2] } };
|
|
182
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brush strokes: what the Draw tool puts on the board.
|
|
3
|
+
*
|
|
4
|
+
* A stroke is a path with `ink` set. It holds two things that belong together: the line the hand went along, smoothed, with the
|
|
5
|
+
* pressure of the brush at every point of it (`ink`), and the outline of that line drawn with the brush (`d`), filled with the
|
|
6
|
+
* stroke's colour. The outline is made from the line (`brushOutlineData`), so whatever changes the stroke (a resize, a new width,
|
|
7
|
+
* a mirror) changes the line and draws the outline again: the brush keeps its width when the stroke is made bigger, the way the
|
|
8
|
+
* line of any other shape does, instead of being stretched with it.
|
|
9
|
+
*
|
|
10
|
+
* The pressure is stored with the line, not worked out again: a stroke drawn with a mouse got its swelling from the speed of the
|
|
11
|
+
* hand, and scaling the line must not change how thick it is where the hand dwelled. Everything here is plain functions over
|
|
12
|
+
* plain records, so the same stroke is drawn in the editor, the screenshot rasterizer and the tests.
|
|
13
|
+
*/
|
|
14
|
+
import type { FreehandSample } from './freehand.js';
|
|
15
|
+
import type { BrushInk, BrushPressure, ObjectId, PathObject, Point, SceneObject } from './types.js';
|
|
16
|
+
/** A path that is a brush stroke. */
|
|
17
|
+
export type BrushPath = PathObject & {
|
|
18
|
+
ink: BrushInk;
|
|
19
|
+
};
|
|
20
|
+
/** The most points of a line a stroke keeps; a longer line is thinned further, so a stroke never grows without bound. */
|
|
21
|
+
export declare const BRUSH_MAX_POINTS = 4000;
|
|
22
|
+
/** The narrowest width the Width control may give a brush stroke. */
|
|
23
|
+
export declare const MIN_BRUSH_WIDTH = 1;
|
|
24
|
+
/** Whether an object is a brush stroke (a path that was drawn with the Draw tool and holds its line). */
|
|
25
|
+
export declare function isBrushStroke(object: SceneObject): object is BrushPath;
|
|
26
|
+
/**
|
|
27
|
+
* How far the outline of a stroke can reach beyond the box of its line, for what must not miss any of it (a hit test's quick
|
|
28
|
+
* rejection, the bounds of a screenshot). Zero for a path that is not a brush stroke.
|
|
29
|
+
*/
|
|
30
|
+
export declare function brushOverflow(shape: SceneObject): number;
|
|
31
|
+
/** The line a brush stroke follows, in the coordinates of its box (what the shape snap measures). */
|
|
32
|
+
export declare function brushCentreline(shape: BrushPath): Point[];
|
|
33
|
+
/**
|
|
34
|
+
* The outline of a stored line, as path data: drawn again from the points and the pressures it holds, at the width of the brush.
|
|
35
|
+
* Always something to draw: a line the outline cannot be made of (one point, or none) is a dot.
|
|
36
|
+
*/
|
|
37
|
+
export declare function brushOutlineData(ink: BrushInk, strokeWidth: number): string;
|
|
38
|
+
export interface BrushStrokeOptions {
|
|
39
|
+
createId?: (prefix: string) => ObjectId;
|
|
40
|
+
/** The colour of the line. */
|
|
41
|
+
color?: string;
|
|
42
|
+
/** The width of the line (what the Width control of the panel shows). */
|
|
43
|
+
strokeWidth?: number;
|
|
44
|
+
/** `pen` reads the pressure from the samples, `speed` (the default) works it out from how fast the hand moved. */
|
|
45
|
+
pressure?: BrushPressure;
|
|
46
|
+
/** The pointer was released: the line ends exactly on the last sample, instead of trailing a little behind the hand. */
|
|
47
|
+
finished?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The stroke for the samples of a pointer, in world units: the box of its line, the line itself with its pressure, and the
|
|
51
|
+
* outline to draw. Null when there is nothing to draw.
|
|
52
|
+
*/
|
|
53
|
+
export declare function buildBrushStroke(samples: readonly FreehandSample[], options?: BrushStrokeOptions): PathObject | null;
|
|
54
|
+
/** What can change about a stroke's line: the size of its box, a mirror, the width of the brush. */
|
|
55
|
+
export interface BrushReshape {
|
|
56
|
+
width?: number;
|
|
57
|
+
height?: number;
|
|
58
|
+
/** Mirror the line across the vertical (or horizontal) middle of the new box. */
|
|
59
|
+
flipX?: boolean;
|
|
60
|
+
flipY?: boolean;
|
|
61
|
+
strokeWidth?: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The stroke with its line scaled (and mirrored) into a new box and its outline drawn again at the brush width: the line goes
|
|
65
|
+
* where the box goes, the brush stays as wide as it was. Only `d`, `ink`, the size of the box and `strokeWidth` are written;
|
|
66
|
+
* where the box is, and how it is turned, is the caller's.
|
|
67
|
+
*/
|
|
68
|
+
export declare function reshapeBrushStroke(shape: BrushPath, change?: BrushReshape): BrushPath;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brush strokes: what the Draw tool puts on the board.
|
|
3
|
+
*
|
|
4
|
+
* A stroke is a path with `ink` set. It holds two things that belong together: the line the hand went along, smoothed, with the
|
|
5
|
+
* pressure of the brush at every point of it (`ink`), and the outline of that line drawn with the brush (`d`), filled with the
|
|
6
|
+
* stroke's colour. The outline is made from the line (`brushOutlineData`), so whatever changes the stroke (a resize, a new width,
|
|
7
|
+
* a mirror) changes the line and draws the outline again: the brush keeps its width when the stroke is made bigger, the way the
|
|
8
|
+
* line of any other shape does, instead of being stretched with it.
|
|
9
|
+
*
|
|
10
|
+
* The pressure is stored with the line, not worked out again: a stroke drawn with a mouse got its swelling from the speed of the
|
|
11
|
+
* hand, and scaling the line must not change how thick it is where the hand dwelled. Everything here is plain functions over
|
|
12
|
+
* plain records, so the same stroke is drawn in the editor, the screenshot rasterizer and the tests.
|
|
13
|
+
*/
|
|
14
|
+
import { SHAPE_STYLE_DEFAULTS, createObjectId, createPathObject } from './defaults.js';
|
|
15
|
+
import { brushOptions, brushReach, brushSizeFor, circlePathData, outlineOf, outlinePathData, outlineWithPressures, strokePointsOf, strokePointsOfLine } from './freehand.js';
|
|
16
|
+
import { MIN_OBJECT_SIZE } from './types.js';
|
|
17
|
+
/** The most points of a line a stroke keeps; a longer line is thinned further, so a stroke never grows without bound. */
|
|
18
|
+
export const BRUSH_MAX_POINTS = 4000;
|
|
19
|
+
// The points of a stroke closer together than this fraction of the brush (and than a unit of the board) add nothing the outline
|
|
20
|
+
// shows: it leaves out points closer than 0.62 of the brush on both of its sides already.
|
|
21
|
+
const POINT_GAP_OF_SIZE = 0.3;
|
|
22
|
+
const MINIMUM_POINT_GAP = 0.75;
|
|
23
|
+
// The decimals a stored point keeps: a hundredth of a unit is far finer than a pixel at the zoom that goes up to eight.
|
|
24
|
+
const COORDINATE_DECIMALS = 2;
|
|
25
|
+
// A line that never leaves this distance (in units of the board) from where it began is a click: a dot at the pointer.
|
|
26
|
+
const DOT_REACH = 1;
|
|
27
|
+
// The pressure of a dot made with a mouse or a finger: the middle of the range, as a line drawn at an even pressure has.
|
|
28
|
+
const DOT_PRESSURE = 0.5;
|
|
29
|
+
/** The narrowest width the Width control may give a brush stroke. */
|
|
30
|
+
export const MIN_BRUSH_WIDTH = 1;
|
|
31
|
+
const roundTo = (value, decimals) => Math.round(value * 10 ** decimals) / 10 ** decimals;
|
|
32
|
+
/** Whether an object is a brush stroke (a path that was drawn with the Draw tool and holds its line). */
|
|
33
|
+
export function isBrushStroke(object) {
|
|
34
|
+
return object.type === 'path' && !!object.ink;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* How far the outline of a stroke can reach beyond the box of its line, for what must not miss any of it (a hit test's quick
|
|
38
|
+
* rejection, the bounds of a screenshot). Zero for a path that is not a brush stroke.
|
|
39
|
+
*/
|
|
40
|
+
export function brushOverflow(shape) {
|
|
41
|
+
return isBrushStroke(shape) ? brushReach(brushSizeFor(shape.strokeWidth)) : 0;
|
|
42
|
+
}
|
|
43
|
+
/** The line a brush stroke follows, in the coordinates of its box (what the shape snap measures). */
|
|
44
|
+
export function brushCentreline(shape) {
|
|
45
|
+
return shape.ink.points.map(([x, y]) => ({ x, y }));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The outline of a stored line, as path data: drawn again from the points and the pressures it holds, at the width of the brush.
|
|
49
|
+
* Always something to draw: a line the outline cannot be made of (one point, or none) is a dot.
|
|
50
|
+
*/
|
|
51
|
+
export function brushOutlineData(ink, strokeWidth) {
|
|
52
|
+
const size = brushSizeFor(strokeWidth);
|
|
53
|
+
// The pressure is already in the points, so it is read, whatever it came from.
|
|
54
|
+
const options = { ...brushOptions(size, ink.pressure, true), simulatePressure: false };
|
|
55
|
+
const points = strokePointsOfLine(ink.points.map(([x, y, pressure]) => ({ x, y, pressure })));
|
|
56
|
+
const data = outlinePathData(outlineOf(points, options));
|
|
57
|
+
if (data)
|
|
58
|
+
return data;
|
|
59
|
+
const center = points[0] ?? { x: 0, y: 0 };
|
|
60
|
+
return circlePathData(center, Math.max(0.5, size / 2));
|
|
61
|
+
}
|
|
62
|
+
/** The points of a smoothed line that the outline needs, with the pressure each was drawn with. */
|
|
63
|
+
function keptPoints(points, pressures, size) {
|
|
64
|
+
let gap = Math.max(MINIMUM_POINT_GAP, size * POINT_GAP_OF_SIZE);
|
|
65
|
+
for (;;) {
|
|
66
|
+
const kept = [{ point: points[0], pressure: pressures[0] }];
|
|
67
|
+
for (let index = 1; index < points.length - 1; index += 1) {
|
|
68
|
+
const previous = kept[kept.length - 1].point;
|
|
69
|
+
if (Math.hypot(points[index].x - previous.x, points[index].y - previous.y) >= gap) {
|
|
70
|
+
kept.push({ point: points[index], pressure: pressures[index] });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// The last point is where the hand ended, whatever came before it.
|
|
74
|
+
if (points.length > 1)
|
|
75
|
+
kept.push({ point: points[points.length - 1], pressure: pressures[points.length - 1] });
|
|
76
|
+
if (kept.length <= BRUSH_MAX_POINTS)
|
|
77
|
+
return kept;
|
|
78
|
+
gap *= 1.5;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The stroke for the samples of a pointer, in world units: the box of its line, the line itself with its pressure, and the
|
|
83
|
+
* outline to draw. Null when there is nothing to draw.
|
|
84
|
+
*/
|
|
85
|
+
export function buildBrushStroke(samples, options = {}) {
|
|
86
|
+
const strokeWidth = options.strokeWidth ?? SHAPE_STYLE_DEFAULTS.inkStrokeWidth;
|
|
87
|
+
const pressure = options.pressure ?? 'speed';
|
|
88
|
+
const size = brushSizeFor(strokeWidth);
|
|
89
|
+
const lineOptions = brushOptions(size, pressure, options.finished === true);
|
|
90
|
+
const usable = samples.filter((sample) => Number.isFinite(sample.x) && Number.isFinite(sample.y));
|
|
91
|
+
if (usable.length === 0)
|
|
92
|
+
return null;
|
|
93
|
+
let kept;
|
|
94
|
+
if (usable.every((sample) => Math.hypot(sample.x - usable[0].x, sample.y - usable[0].y) < DOT_REACH)) {
|
|
95
|
+
// A click is a dot where the pointer went down: the line has one point, so nothing is drawn a pixel away from it.
|
|
96
|
+
const penPressure = usable.reduce((most, sample) => Math.max(most, sample.pressure ?? 0), 0);
|
|
97
|
+
const dotPressure = pressure === 'pen' && penPressure > 0 ? Math.min(1, penPressure) : DOT_PRESSURE;
|
|
98
|
+
const point = { x: usable[0].x, y: usable[0].y, pressure: dotPressure, vectorX: 0, vectorY: 0, distance: 0, runningLength: 0 };
|
|
99
|
+
kept = [{ point, pressure: dotPressure }];
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const smoothed = strokePointsOf(usable, lineOptions);
|
|
103
|
+
// The pass that works the pressure out (from the speed, or read from the pen) so that it can be kept.
|
|
104
|
+
const { pressures } = outlineWithPressures(smoothed, lineOptions);
|
|
105
|
+
kept = keptPoints(smoothed, pressures, size);
|
|
106
|
+
}
|
|
107
|
+
let minX = Infinity;
|
|
108
|
+
let minY = Infinity;
|
|
109
|
+
let maxX = -Infinity;
|
|
110
|
+
let maxY = -Infinity;
|
|
111
|
+
for (const { point } of kept) {
|
|
112
|
+
minX = Math.min(minX, point.x);
|
|
113
|
+
minY = Math.min(minY, point.y);
|
|
114
|
+
maxX = Math.max(maxX, point.x);
|
|
115
|
+
maxY = Math.max(maxY, point.y);
|
|
116
|
+
}
|
|
117
|
+
// A line that is flat on one axis (a horizontal line, a dot) gets a box one unit thick, with the line in its middle.
|
|
118
|
+
const flatX = maxX - minX < MIN_OBJECT_SIZE;
|
|
119
|
+
const flatY = maxY - minY < MIN_OBJECT_SIZE;
|
|
120
|
+
const boxX = flatX ? (minX + maxX) / 2 - MIN_OBJECT_SIZE / 2 : minX;
|
|
121
|
+
const boxY = flatY ? (minY + maxY) / 2 - MIN_OBJECT_SIZE / 2 : minY;
|
|
122
|
+
const width = flatX ? MIN_OBJECT_SIZE : maxX - minX;
|
|
123
|
+
const height = flatY ? MIN_OBJECT_SIZE : maxY - minY;
|
|
124
|
+
const ink = {
|
|
125
|
+
points: kept.map(({ point, pressure: pointPressure }) => [
|
|
126
|
+
roundTo(point.x - boxX, COORDINATE_DECIMALS),
|
|
127
|
+
roundTo(point.y - boxY, COORDINATE_DECIMALS),
|
|
128
|
+
roundTo(pointPressure, COORDINATE_DECIMALS)
|
|
129
|
+
]),
|
|
130
|
+
pressure
|
|
131
|
+
};
|
|
132
|
+
const createId = options.createId ?? createObjectId;
|
|
133
|
+
return createPathObject({
|
|
134
|
+
id: createId('obj'),
|
|
135
|
+
x: boxX,
|
|
136
|
+
y: boxY,
|
|
137
|
+
width,
|
|
138
|
+
height,
|
|
139
|
+
d: brushOutlineData(ink, strokeWidth),
|
|
140
|
+
shape: null,
|
|
141
|
+
// The brush is filled: its colour is the fill of the outline, and the outline has no stroke of its own.
|
|
142
|
+
fill: options.color ?? SHAPE_STYLE_DEFAULTS.inkStroke,
|
|
143
|
+
fillOpacity: 1,
|
|
144
|
+
stroke: 'transparent',
|
|
145
|
+
strokeWidth,
|
|
146
|
+
lineCap: 'round',
|
|
147
|
+
lineJoin: 'round',
|
|
148
|
+
ink
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The stroke with its line scaled (and mirrored) into a new box and its outline drawn again at the brush width: the line goes
|
|
153
|
+
* where the box goes, the brush stays as wide as it was. Only `d`, `ink`, the size of the box and `strokeWidth` are written;
|
|
154
|
+
* where the box is, and how it is turned, is the caller's.
|
|
155
|
+
*/
|
|
156
|
+
export function reshapeBrushStroke(shape, change = {}) {
|
|
157
|
+
const width = Math.max(MIN_OBJECT_SIZE, change.width ?? shape.width);
|
|
158
|
+
const height = Math.max(MIN_OBJECT_SIZE, change.height ?? shape.height);
|
|
159
|
+
const strokeWidth = change.strokeWidth ?? shape.strokeWidth;
|
|
160
|
+
const scaleX = shape.width > 0 ? width / shape.width : 1;
|
|
161
|
+
const scaleY = shape.height > 0 ? height / shape.height : 1;
|
|
162
|
+
const points = shape.ink.points.map(([x, y, pressure]) => {
|
|
163
|
+
const scaledX = x * scaleX;
|
|
164
|
+
const scaledY = y * scaleY;
|
|
165
|
+
return [
|
|
166
|
+
roundTo(change.flipX ? width - scaledX : scaledX, COORDINATE_DECIMALS),
|
|
167
|
+
roundTo(change.flipY ? height - scaledY : scaledY, COORDINATE_DECIMALS),
|
|
168
|
+
pressure
|
|
169
|
+
];
|
|
170
|
+
});
|
|
171
|
+
const ink = { points, pressure: shape.ink.pressure };
|
|
172
|
+
return { ...shape, width, height, strokeWidth, ink, d: brushOutlineData(ink, strokeWidth) };
|
|
173
|
+
}
|