@widgetic/canvas 0.5.35 → 0.5.37
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 +449 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +221 -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 +348 -0
- package/dist/canvas/host/CanvasHost.js +1715 -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 +53 -0
- package/dist/canvas/host/conversion.js +125 -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 +7 -7
- package/dist/canvas/index.js +8 -8
- 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 +11 -11
- package/dist/canvas/toolbarPosition.js +11 -24
- 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 +21 -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 +156 -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 +145 -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 +52 -124
- package/dist/i18n/messages.js +59 -133
- package/dist/i18n/shortcuts.d.ts +0 -1
- package/dist/i18n/shortcuts.js +2 -11
- package/dist/index.d.ts +4 -2
- package/dist/index.js +5 -3
- package/package.json +30 -5
- package/dist/canvas/Canvas.svelte +0 -13363
- package/dist/canvas/Canvas.svelte.d.ts +0 -286
- 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,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The path of the shapes the canvas can build from a key: triangles and the shapes of the library. (The ink of the
|
|
3
|
+
* Draw tool is in `brush.ts`, arrows are in `arrow.ts`.) Each function returns commands in the local space of the
|
|
4
|
+
* box it was asked for, so the result can be written straight into a path object.
|
|
5
|
+
*
|
|
6
|
+
* Shapes that can be rebuilt (a triangle, a library shape whose corners can be rounded) are rebuilt from
|
|
7
|
+
* their key at every resize, so a corner radius stays the same fraction of the shape instead of being
|
|
8
|
+
* stretched with it.
|
|
9
|
+
*/
|
|
10
|
+
import { generatePathForShape, getShapeByKey, roundedPolygonPath } from '../../canvas/shapes/ShapeLibrary.js';
|
|
11
|
+
import { clampNumber } from './geometry.js';
|
|
12
|
+
import { fitPathToBox, parsePathData } from './pathData.js';
|
|
13
|
+
/** The `shape` key of a triangle path. */
|
|
14
|
+
export const TRIANGLE_SHAPE_KEY = 'triangle';
|
|
15
|
+
/** The `shape` key of an arrow path (its geometry is in `arrow.ts`). */
|
|
16
|
+
export const ARROW_SHAPE_KEY = 'arrow';
|
|
17
|
+
/** The categories of the library whose shapes keep their proportions when resized (symbols and icons). */
|
|
18
|
+
const ASPECT_LOCKED_CATEGORIES = new Set(['Symbol Shapes', 'Media Icons', 'UI Icons']);
|
|
19
|
+
/** Whether a library shape keeps its aspect ratio: its own setting, else the rule of its category. */
|
|
20
|
+
export function libraryShapeKeepsAspect(shape) {
|
|
21
|
+
return shape.lockAspectRatio ?? ASPECT_LOCKED_CATEGORIES.has(shape.category);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A triangle with its apex at the top middle and its base along the bottom, with corners rounded by
|
|
25
|
+
* `roundness` percent (100 rounds by a quarter of the shorter side).
|
|
26
|
+
*/
|
|
27
|
+
export function trianglePathCommands(width, height, roundness = 0) {
|
|
28
|
+
const radius = (clampNumber(roundness, 0, 100) / 100) * (Math.min(width, height) / 4);
|
|
29
|
+
const data = roundedPolygonPath([
|
|
30
|
+
[width / 2, 0],
|
|
31
|
+
[width, height],
|
|
32
|
+
[0, height]
|
|
33
|
+
], radius);
|
|
34
|
+
return parsePathData(data);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A library shape fitted to a box. Shapes that can round their corners are regenerated at the given
|
|
38
|
+
* roundness; the others are the stored outline, scaled. Null for a key the library does not know.
|
|
39
|
+
*/
|
|
40
|
+
export function libraryShapePathCommands(shapeKey, width, height, roundness = 0) {
|
|
41
|
+
const shape = getShapeByKey(shapeKey);
|
|
42
|
+
if (!shape)
|
|
43
|
+
return null;
|
|
44
|
+
const regenerated = shape.cornerRadiusMode === 'pathRegen' ? generatePathForShape(shape, roundness) : null;
|
|
45
|
+
return fitPathToBox(parsePathData(regenerated ?? shape.pathData), width, height);
|
|
46
|
+
}
|
|
47
|
+
/** Whether a path of this kind is rebuilt from its key when it is resized (rather than scaled). */
|
|
48
|
+
export function isRegeneratedShape(shapeKey) {
|
|
49
|
+
if (!shapeKey)
|
|
50
|
+
return false;
|
|
51
|
+
if (shapeKey === TRIANGLE_SHAPE_KEY)
|
|
52
|
+
return true;
|
|
53
|
+
const shape = getShapeByKey(shapeKey);
|
|
54
|
+
return !!shape && shape.cornerRadiusMode === 'pathRegen';
|
|
55
|
+
}
|
|
56
|
+
/** Whether the Roundness control applies to this path. */
|
|
57
|
+
export function shapeSupportsRoundness(shapeKey) {
|
|
58
|
+
return isRegeneratedShape(shapeKey);
|
|
59
|
+
}
|
|
60
|
+
/** The path of a regenerated shape at a size, or null when the shape is drawn rather than generated. */
|
|
61
|
+
export function regeneratedPathCommands(shape, width, height) {
|
|
62
|
+
if (!shape.shape)
|
|
63
|
+
return null;
|
|
64
|
+
if (shape.shape === TRIANGLE_SHAPE_KEY)
|
|
65
|
+
return trianglePathCommands(width, height, shape.roundness);
|
|
66
|
+
if (!isRegeneratedShape(shape.shape))
|
|
67
|
+
return null;
|
|
68
|
+
return libraryShapePathCommands(shape.shape, width, height, shape.roundness);
|
|
69
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the shape tools make: given where the pointer went down and where it is now, the shape a tool would
|
|
3
|
+
* place. Nothing here touches the board; the pointer controller calls these on every move for the preview and
|
|
4
|
+
* once more on release for the shape that is kept.
|
|
5
|
+
*/
|
|
6
|
+
import type { EllipseObject, ObjectId, PathObject, Point, Rect, RectObject } from './types.js';
|
|
7
|
+
/** A drag shorter than this (in screen pixels) is a click: the shape is placed at its default size. */
|
|
8
|
+
export declare const MIN_DRAW_DISTANCE = 4;
|
|
9
|
+
/** The size of a library shape placed by a click. */
|
|
10
|
+
export declare const LIBRARY_SHAPE_CLICK_SIZE = 50;
|
|
11
|
+
/** The length of an arrow placed by a click. */
|
|
12
|
+
export declare const ARROW_CLICK_LENGTH = 100;
|
|
13
|
+
/** Shift holds an arrow to a multiple of this many degrees (tldraw divides the circle into 24: 15 degrees). */
|
|
14
|
+
export declare const ARROW_SNAP_DEGREES = 15;
|
|
15
|
+
/** A triangle's height for a width, when it is equilateral. */
|
|
16
|
+
export declare const EQUILATERAL_RATIO: number;
|
|
17
|
+
/** A triangle added by the toolbar button rather than drawn. */
|
|
18
|
+
export declare const BUTTON_TRIANGLE_SIZE: {
|
|
19
|
+
readonly width: 100;
|
|
20
|
+
readonly height: 150;
|
|
21
|
+
};
|
|
22
|
+
/** How soft the corners of a rounded square start (0 to 100: 100 is a pill). The Roundness control moves it afterwards. */
|
|
23
|
+
export declare const ROUNDED_RECT_ROUNDNESS = 35;
|
|
24
|
+
/** The shape tools: the ones that drag a shape out. */
|
|
25
|
+
export type DragShapeKind = 'rect' | 'rounded-rect' | 'ellipse' | 'circle' | 'triangle' | 'arrow' | 'library-shape';
|
|
26
|
+
export interface ShapeDraftOptions {
|
|
27
|
+
kind: DragShapeKind;
|
|
28
|
+
/** Where the pointer went down, in world units. */
|
|
29
|
+
start: Point;
|
|
30
|
+
/** Where it is now. */
|
|
31
|
+
end: Point;
|
|
32
|
+
/** Shift: constrain (a square, an equilateral triangle, an arrow at a multiple of 15 degrees). */
|
|
33
|
+
constrain: boolean;
|
|
34
|
+
/** Whether the pointer moved far enough to count as a drag (otherwise the default size is used). */
|
|
35
|
+
dragged: boolean;
|
|
36
|
+
/** The key of the library shape, for the `library-shape` kind. */
|
|
37
|
+
libraryKey?: string | null;
|
|
38
|
+
/** Wireframe look (white with a grey outline) or a random colour. */
|
|
39
|
+
wireframe: boolean;
|
|
40
|
+
createId?: (prefix: string) => ObjectId;
|
|
41
|
+
random?: () => number;
|
|
42
|
+
}
|
|
43
|
+
/** The paint of a new shape of the tools, by the look the board is in. */
|
|
44
|
+
export declare function newShapePaint(wireframe: boolean, random?: () => number): {
|
|
45
|
+
fill: "#ffffff";
|
|
46
|
+
stroke: "#374151";
|
|
47
|
+
strokeWidth: 2;
|
|
48
|
+
} | {
|
|
49
|
+
fill: string;
|
|
50
|
+
stroke: "#000000";
|
|
51
|
+
strokeWidth: 1;
|
|
52
|
+
};
|
|
53
|
+
/** The look of a library shape, and of an arrow. */
|
|
54
|
+
export declare const LIBRARY_SHAPE_PAINT: {
|
|
55
|
+
readonly fill: "#ffffff";
|
|
56
|
+
readonly stroke: "#374151";
|
|
57
|
+
readonly strokeWidth: 3;
|
|
58
|
+
};
|
|
59
|
+
export declare const ARROW_PAINT: {
|
|
60
|
+
readonly fill: "transparent";
|
|
61
|
+
readonly stroke: "#111827";
|
|
62
|
+
readonly strokeWidth: 3;
|
|
63
|
+
};
|
|
64
|
+
/** The box a shape tool would place for a drag. */
|
|
65
|
+
export declare function shapeDragBox(options: ShapeDraftOptions): Rect;
|
|
66
|
+
/** The end of an arrow for a drag: where the pointer is, held to a multiple of 15 degrees with Shift, or the default length. */
|
|
67
|
+
export declare function arrowEnd(options: Pick<ShapeDraftOptions, 'start' | 'end' | 'constrain' | 'dragged'>): Point;
|
|
68
|
+
/** A point turned about a centre, to the nearest multiple of `step` degrees of direction, at the same distance. */
|
|
69
|
+
export declare function snapPointAround(center: Point, point: Point, step: number): Point;
|
|
70
|
+
/** The shape a tool would place for a drag. */
|
|
71
|
+
export declare function buildDraggedShape(options: ShapeDraftOptions): RectObject | EllipseObject | PathObject;
|
|
72
|
+
/** A library shape in a box, with the look and the line ends its kind has. Null key or an unknown key gives a plain rectangle. */
|
|
73
|
+
export declare function buildLibraryShape(fields: {
|
|
74
|
+
id: ObjectId;
|
|
75
|
+
x: number;
|
|
76
|
+
y: number;
|
|
77
|
+
width: number;
|
|
78
|
+
height: number;
|
|
79
|
+
key: string | null;
|
|
80
|
+
}): RectObject | PathObject;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the shape tools make: given where the pointer went down and where it is now, the shape a tool would
|
|
3
|
+
* place. Nothing here touches the board; the pointer controller calls these on every move for the preview and
|
|
4
|
+
* once more on release for the shape that is kept.
|
|
5
|
+
*/
|
|
6
|
+
import { getShapeByKey } from '../../canvas/shapes/ShapeLibrary.js';
|
|
7
|
+
import { DEFAULT_OBJECT_SIZE, SHAPE_STYLE_DEFAULTS, createEllipseObject, createObjectId, createPathObject, createRectObject, randomShapeFill } from './defaults.js';
|
|
8
|
+
import { buildArrow } from './arrow.js';
|
|
9
|
+
import { distanceBetween, rectFromPoints } from './geometry.js';
|
|
10
|
+
import { ARROW_SHAPE_KEY, TRIANGLE_SHAPE_KEY, libraryShapeKeepsAspect, libraryShapePathCommands, trianglePathCommands } from './shapePaths.js';
|
|
11
|
+
import { serializePathData } from './pathData.js';
|
|
12
|
+
import { snapAngle } from './transform.js';
|
|
13
|
+
import { MIN_OBJECT_SIZE } from './types.js';
|
|
14
|
+
/** A drag shorter than this (in screen pixels) is a click: the shape is placed at its default size. */
|
|
15
|
+
export const MIN_DRAW_DISTANCE = 4;
|
|
16
|
+
/** The size of a library shape placed by a click. */
|
|
17
|
+
export const LIBRARY_SHAPE_CLICK_SIZE = 50;
|
|
18
|
+
/** The length of an arrow placed by a click. */
|
|
19
|
+
export const ARROW_CLICK_LENGTH = 100;
|
|
20
|
+
/** Shift holds an arrow to a multiple of this many degrees (tldraw divides the circle into 24: 15 degrees). */
|
|
21
|
+
export const ARROW_SNAP_DEGREES = 15;
|
|
22
|
+
/** A triangle's height for a width, when it is equilateral. */
|
|
23
|
+
export const EQUILATERAL_RATIO = Math.sqrt(3) / 2;
|
|
24
|
+
/** A triangle added by the toolbar button rather than drawn. */
|
|
25
|
+
export const BUTTON_TRIANGLE_SIZE = { width: 100, height: 150 };
|
|
26
|
+
/** How soft the corners of a rounded square start (0 to 100: 100 is a pill). The Roundness control moves it afterwards. */
|
|
27
|
+
export const ROUNDED_RECT_ROUNDNESS = 35;
|
|
28
|
+
/** A box of a given aspect (width / height) that has the dragged corner for one of its corners. */
|
|
29
|
+
function boxWithAspect(start, end, aspect) {
|
|
30
|
+
const deltaX = end.x - start.x;
|
|
31
|
+
const deltaY = end.y - start.y;
|
|
32
|
+
const signX = deltaX < 0 ? -1 : 1;
|
|
33
|
+
const signY = deltaY < 0 ? -1 : 1;
|
|
34
|
+
// The box takes the larger of what the pointer asked for, so the shape follows the pointer's far corner.
|
|
35
|
+
const width = Math.max(Math.abs(deltaX), Math.abs(deltaY) * aspect);
|
|
36
|
+
const height = width / aspect;
|
|
37
|
+
const right = start.x + signX * width;
|
|
38
|
+
const bottom = start.y + signY * height;
|
|
39
|
+
return rectFromPoints(start, { x: right, y: bottom });
|
|
40
|
+
}
|
|
41
|
+
/** A box of a given size centred on a point. */
|
|
42
|
+
function centeredBox(center, width, height) {
|
|
43
|
+
return { x: center.x - width / 2, y: center.y - height / 2, width, height };
|
|
44
|
+
}
|
|
45
|
+
/** The paint of a new shape of the tools, by the look the board is in. */
|
|
46
|
+
export function newShapePaint(wireframe, random = Math.random) {
|
|
47
|
+
return wireframe
|
|
48
|
+
? {
|
|
49
|
+
fill: SHAPE_STYLE_DEFAULTS.wireframeFill,
|
|
50
|
+
stroke: SHAPE_STYLE_DEFAULTS.wireframeStroke,
|
|
51
|
+
strokeWidth: SHAPE_STYLE_DEFAULTS.wireframeStrokeWidth
|
|
52
|
+
}
|
|
53
|
+
: { fill: randomShapeFill(random), stroke: SHAPE_STYLE_DEFAULTS.stroke, strokeWidth: SHAPE_STYLE_DEFAULTS.strokeWidth };
|
|
54
|
+
}
|
|
55
|
+
/** The look of a library shape, and of an arrow. */
|
|
56
|
+
export const LIBRARY_SHAPE_PAINT = { fill: '#ffffff', stroke: '#374151', strokeWidth: 3 };
|
|
57
|
+
export const ARROW_PAINT = { fill: 'transparent', stroke: '#111827', strokeWidth: 3 };
|
|
58
|
+
/** The box a shape tool would place for a drag. */
|
|
59
|
+
export function shapeDragBox(options) {
|
|
60
|
+
const { kind, start, end, constrain, dragged } = options;
|
|
61
|
+
if (kind === 'arrow')
|
|
62
|
+
return arrowDragGeometry(options).box;
|
|
63
|
+
const aspectLockedLibrary = kind === 'library-shape' && libraryKeepsAspect(options.libraryKey);
|
|
64
|
+
if (!dragged) {
|
|
65
|
+
if (kind === 'triangle')
|
|
66
|
+
return centeredBox(start, DEFAULT_OBJECT_SIZE, DEFAULT_OBJECT_SIZE * EQUILATERAL_RATIO);
|
|
67
|
+
if (kind === 'library-shape')
|
|
68
|
+
return centeredBox(start, LIBRARY_SHAPE_CLICK_SIZE, LIBRARY_SHAPE_CLICK_SIZE);
|
|
69
|
+
return centeredBox(start, DEFAULT_OBJECT_SIZE, DEFAULT_OBJECT_SIZE);
|
|
70
|
+
}
|
|
71
|
+
if (kind === 'triangle') {
|
|
72
|
+
return constrain ? boxWithAspect(start, end, 1 / EQUILATERAL_RATIO) : rectFromPoints(start, end);
|
|
73
|
+
}
|
|
74
|
+
if (kind === 'circle' || aspectLockedLibrary || constrain)
|
|
75
|
+
return boxWithAspect(start, end, 1);
|
|
76
|
+
return rectFromPoints(start, end);
|
|
77
|
+
}
|
|
78
|
+
function libraryKeepsAspect(key) {
|
|
79
|
+
const shape = getShapeByKey(key ?? null);
|
|
80
|
+
return !!shape && libraryShapeKeepsAspect(shape);
|
|
81
|
+
}
|
|
82
|
+
/** The end of an arrow for a drag: where the pointer is, held to a multiple of 15 degrees with Shift, or the default length. */
|
|
83
|
+
export function arrowEnd(options) {
|
|
84
|
+
const { start, end, constrain, dragged } = options;
|
|
85
|
+
if (!dragged)
|
|
86
|
+
return { x: start.x + ARROW_CLICK_LENGTH, y: start.y };
|
|
87
|
+
if (!constrain)
|
|
88
|
+
return end;
|
|
89
|
+
return snapPointAround(start, end, ARROW_SNAP_DEGREES);
|
|
90
|
+
}
|
|
91
|
+
/** A point turned about a centre, to the nearest multiple of `step` degrees of direction, at the same distance. */
|
|
92
|
+
export function snapPointAround(center, point, step) {
|
|
93
|
+
const length = distanceBetween(center, point);
|
|
94
|
+
const angle = (snapAngle((Math.atan2(point.y - center.y, point.x - center.x) * 180) / Math.PI, step) * Math.PI) / 180;
|
|
95
|
+
return { x: center.x + length * Math.cos(angle), y: center.y + length * Math.sin(angle) };
|
|
96
|
+
}
|
|
97
|
+
function arrowDragGeometry(options) {
|
|
98
|
+
return buildArrow(options.start, arrowEnd(options), 0, ARROW_PAINT.strokeWidth);
|
|
99
|
+
}
|
|
100
|
+
/** The shape a tool would place for a drag. */
|
|
101
|
+
export function buildDraggedShape(options) {
|
|
102
|
+
const createId = options.createId ?? createObjectId;
|
|
103
|
+
const id = createId('obj');
|
|
104
|
+
const { kind, wireframe } = options;
|
|
105
|
+
if (kind === 'arrow') {
|
|
106
|
+
const geometry = arrowDragGeometry(options);
|
|
107
|
+
return createPathObject({
|
|
108
|
+
id,
|
|
109
|
+
...geometry.box,
|
|
110
|
+
d: serializePathData(geometry.commands),
|
|
111
|
+
arrow: geometry.spec,
|
|
112
|
+
shape: ARROW_SHAPE_KEY,
|
|
113
|
+
...ARROW_PAINT,
|
|
114
|
+
lineCap: 'round',
|
|
115
|
+
lineJoin: 'round'
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const box = shapeDragBox(options);
|
|
119
|
+
const width = Math.max(box.width, MIN_OBJECT_SIZE);
|
|
120
|
+
const height = Math.max(box.height, MIN_OBJECT_SIZE);
|
|
121
|
+
const frame = { id, x: box.x, y: box.y, width, height };
|
|
122
|
+
if (kind === 'rect')
|
|
123
|
+
return createRectObject({ ...frame, ...newShapePaint(wireframe, options.random) });
|
|
124
|
+
// The rounded square of the shapes menu: a rectangle that starts with soft corners (its Roundness control still moves them).
|
|
125
|
+
if (kind === 'rounded-rect') {
|
|
126
|
+
return createRectObject({ ...frame, ...newShapePaint(wireframe, options.random), roundness: ROUNDED_RECT_ROUNDNESS });
|
|
127
|
+
}
|
|
128
|
+
if (kind === 'ellipse' || kind === 'circle') {
|
|
129
|
+
return createEllipseObject({ ...frame, ...newShapePaint(wireframe, options.random), aspectLocked: kind === 'circle' });
|
|
130
|
+
}
|
|
131
|
+
if (kind === 'triangle') {
|
|
132
|
+
return createPathObject({
|
|
133
|
+
...frame,
|
|
134
|
+
...newShapePaint(wireframe, options.random),
|
|
135
|
+
d: serializePathData(trianglePathCommands(width, height, 0)),
|
|
136
|
+
shape: TRIANGLE_SHAPE_KEY,
|
|
137
|
+
lineJoin: 'miter'
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return buildLibraryShape({ ...frame, key: options.libraryKey ?? null });
|
|
141
|
+
}
|
|
142
|
+
/** A library shape in a box, with the look and the line ends its kind has. Null key or an unknown key gives a plain rectangle. */
|
|
143
|
+
export function buildLibraryShape(fields) {
|
|
144
|
+
const shape = getShapeByKey(fields.key);
|
|
145
|
+
const frame = { id: fields.id, x: fields.x, y: fields.y, width: fields.width, height: fields.height };
|
|
146
|
+
const commands = fields.key && shape ? libraryShapePathCommands(fields.key, fields.width, fields.height, 0) : null;
|
|
147
|
+
if (!shape || !commands)
|
|
148
|
+
return createRectObject({ ...frame, ...newShapePaint(true) });
|
|
149
|
+
const linesOnly = shape.cornerRadiusMode === 'strokeRound';
|
|
150
|
+
return createPathObject({
|
|
151
|
+
...frame,
|
|
152
|
+
...LIBRARY_SHAPE_PAINT,
|
|
153
|
+
// A line-only icon (a check, a cross) has no inside to fill.
|
|
154
|
+
fill: linesOnly ? 'transparent' : LIBRARY_SHAPE_PAINT.fill,
|
|
155
|
+
d: serializePathData(commands),
|
|
156
|
+
shape: shape.key,
|
|
157
|
+
aspectLocked: libraryShapeKeepsAspect(shape),
|
|
158
|
+
lineCap: linesOnly ? 'butt' : 'round',
|
|
159
|
+
lineJoin: linesOnly ? 'miter' : 'round'
|
|
160
|
+
});
|
|
161
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alignment guides: while a selection is dragged (or an edge is resized) its left, middle and right (top,
|
|
3
|
+
* middle and bottom) line up with the same lines of the other shapes, and a guide is drawn along the line
|
|
4
|
+
* that matched.
|
|
5
|
+
*
|
|
6
|
+
* The reach of a snap is measured on the screen, so it feels the same at any zoom, and it shrinks as the
|
|
7
|
+
* pointer speeds up: sweeping quickly across a board must not catch on every shape on the way, only a slow,
|
|
8
|
+
* deliberate move should (the same idea Memoreco's layout editor uses).
|
|
9
|
+
*/
|
|
10
|
+
import type { Point, Rect } from './types.js';
|
|
11
|
+
/** How far, in screen pixels, a line reaches to catch the dragged edge when the pointer is slow. */
|
|
12
|
+
export declare const SNAP_THRESHOLD_PIXELS = 8;
|
|
13
|
+
/** At or below this speed (pixels per millisecond) the snap has its full reach. */
|
|
14
|
+
export declare const SNAP_SLOW_SPEED = 0.25;
|
|
15
|
+
/** At or above this speed the snap has no reach at all. */
|
|
16
|
+
export declare const SNAP_FAST_SPEED = 0.9;
|
|
17
|
+
/** Weight of the newest sample in the smoothed pointer speed. */
|
|
18
|
+
export declare const SNAP_SPEED_SMOOTHING = 0.35;
|
|
19
|
+
export type GuideOrientation = 'vertical' | 'horizontal';
|
|
20
|
+
/** A guide line in the world: a vertical one is at x = `position`, running from `from` to `to` in y. */
|
|
21
|
+
export interface SnapGuide {
|
|
22
|
+
orientation: GuideOrientation;
|
|
23
|
+
position: number;
|
|
24
|
+
from: number;
|
|
25
|
+
to: number;
|
|
26
|
+
}
|
|
27
|
+
export interface SnapResult {
|
|
28
|
+
/** How far to move the selection (world units) to land on the lines, 0 where nothing caught. */
|
|
29
|
+
deltaX: number;
|
|
30
|
+
deltaY: number;
|
|
31
|
+
guides: SnapGuide[];
|
|
32
|
+
}
|
|
33
|
+
export declare const NO_SNAP: SnapResult;
|
|
34
|
+
/** The reach of a snap in world units for a pointer moving at `speed`, at `zoom`. */
|
|
35
|
+
export declare function snapReachInWorld(zoom: number, speed?: number, basePixels?: number): number;
|
|
36
|
+
/** Follows how fast the pointer moves, smoothed, in screen pixels per millisecond. */
|
|
37
|
+
export declare class PointerSpeedTracker {
|
|
38
|
+
#private;
|
|
39
|
+
/** Records a pointer position (screen space) and returns the smoothed speed. */
|
|
40
|
+
update(x: number, y: number, time: number): number;
|
|
41
|
+
get speed(): number;
|
|
42
|
+
reset(): void;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The guides to draw for a box that sits where it will land: every line of the other shapes that one of
|
|
46
|
+
* its anchors is on, spanning both shapes.
|
|
47
|
+
*/
|
|
48
|
+
export declare function guidesFor(box: Rect, others: readonly Rect[], anchorsOf?: 'x' | 'y' | 'both'): SnapGuide[];
|
|
49
|
+
/**
|
|
50
|
+
* Snaps a box that is being moved. `moving` is where the box would be without snapping; the result says how
|
|
51
|
+
* far to nudge it and which guides to show once it is there.
|
|
52
|
+
*/
|
|
53
|
+
export declare function computeMoveSnap(moving: Rect, others: readonly Rect[], reach: number): SnapResult;
|
|
54
|
+
/**
|
|
55
|
+
* Snaps one point (the corner or edge a handle drags, or a corner of a shape being drawn) to the lines of
|
|
56
|
+
* the other shapes. `axes` says which coordinates may snap (an edge handle moves one only).
|
|
57
|
+
*/
|
|
58
|
+
export declare function snapPoint(point: Point, others: readonly Rect[], reach: number, axes?: {
|
|
59
|
+
x: boolean;
|
|
60
|
+
y: boolean;
|
|
61
|
+
}): {
|
|
62
|
+
point: Point;
|
|
63
|
+
guides: SnapGuide[];
|
|
64
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alignment guides: while a selection is dragged (or an edge is resized) its left, middle and right (top,
|
|
3
|
+
* middle and bottom) line up with the same lines of the other shapes, and a guide is drawn along the line
|
|
4
|
+
* that matched.
|
|
5
|
+
*
|
|
6
|
+
* The reach of a snap is measured on the screen, so it feels the same at any zoom, and it shrinks as the
|
|
7
|
+
* pointer speeds up: sweeping quickly across a board must not catch on every shape on the way, only a slow,
|
|
8
|
+
* deliberate move should (the same idea Memoreco's layout editor uses).
|
|
9
|
+
*/
|
|
10
|
+
import { clampNumber } from './geometry.js';
|
|
11
|
+
/** How far, in screen pixels, a line reaches to catch the dragged edge when the pointer is slow. */
|
|
12
|
+
export const SNAP_THRESHOLD_PIXELS = 8;
|
|
13
|
+
/** At or below this speed (pixels per millisecond) the snap has its full reach. */
|
|
14
|
+
export const SNAP_SLOW_SPEED = 0.25;
|
|
15
|
+
/** At or above this speed the snap has no reach at all. */
|
|
16
|
+
export const SNAP_FAST_SPEED = 0.9;
|
|
17
|
+
/** Weight of the newest sample in the smoothed pointer speed. */
|
|
18
|
+
export const SNAP_SPEED_SMOOTHING = 0.35;
|
|
19
|
+
export const NO_SNAP = Object.freeze({ deltaX: 0, deltaY: 0, guides: [] });
|
|
20
|
+
/** The reach of a snap in world units for a pointer moving at `speed`, at `zoom`. */
|
|
21
|
+
export function snapReachInWorld(zoom, speed = 0, basePixels = SNAP_THRESHOLD_PIXELS) {
|
|
22
|
+
const speedFactor = clampNumber((speed - SNAP_SLOW_SPEED) / (SNAP_FAST_SPEED - SNAP_SLOW_SPEED), 0, 1);
|
|
23
|
+
return (basePixels * (1 - speedFactor)) / Math.max(zoom, 1e-6);
|
|
24
|
+
}
|
|
25
|
+
/** Follows how fast the pointer moves, smoothed, in screen pixels per millisecond. */
|
|
26
|
+
export class PointerSpeedTracker {
|
|
27
|
+
#last = null;
|
|
28
|
+
#speed = 0;
|
|
29
|
+
/** Records a pointer position (screen space) and returns the smoothed speed. */
|
|
30
|
+
update(x, y, time) {
|
|
31
|
+
if (this.#last && time > this.#last.time) {
|
|
32
|
+
const instant = Math.hypot(x - this.#last.x, y - this.#last.y) / (time - this.#last.time);
|
|
33
|
+
this.#speed = this.#speed * (1 - SNAP_SPEED_SMOOTHING) + instant * SNAP_SPEED_SMOOTHING;
|
|
34
|
+
}
|
|
35
|
+
this.#last = { x, y, time };
|
|
36
|
+
return this.#speed;
|
|
37
|
+
}
|
|
38
|
+
get speed() {
|
|
39
|
+
return this.#speed;
|
|
40
|
+
}
|
|
41
|
+
reset() {
|
|
42
|
+
this.#last = null;
|
|
43
|
+
this.#speed = 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function verticalLinesOf(rects) {
|
|
47
|
+
const lines = [];
|
|
48
|
+
for (const rect of rects) {
|
|
49
|
+
lines.push({ position: rect.x, rect }, { position: rect.x + rect.width / 2, rect }, { position: rect.x + rect.width, rect });
|
|
50
|
+
}
|
|
51
|
+
return lines;
|
|
52
|
+
}
|
|
53
|
+
function horizontalLinesOf(rects) {
|
|
54
|
+
const lines = [];
|
|
55
|
+
for (const rect of rects) {
|
|
56
|
+
lines.push({ position: rect.y, rect }, { position: rect.y + rect.height / 2, rect }, { position: rect.y + rect.height, rect });
|
|
57
|
+
}
|
|
58
|
+
return lines;
|
|
59
|
+
}
|
|
60
|
+
/** The smallest signed distance from any anchor to any line, if it is within `reach`. */
|
|
61
|
+
function nearestOffset(anchors, lines, reach) {
|
|
62
|
+
let best = null;
|
|
63
|
+
for (const anchor of anchors) {
|
|
64
|
+
for (const line of lines) {
|
|
65
|
+
const offset = line.position - anchor;
|
|
66
|
+
if (Math.abs(offset) <= reach && (best === null || Math.abs(offset) < Math.abs(best.offset) - 1e-9)) {
|
|
67
|
+
best = { offset };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return best;
|
|
72
|
+
}
|
|
73
|
+
const MATCH_EPSILON = 0.01;
|
|
74
|
+
/**
|
|
75
|
+
* The guides to draw for a box that sits where it will land: every line of the other shapes that one of
|
|
76
|
+
* its anchors is on, spanning both shapes.
|
|
77
|
+
*/
|
|
78
|
+
export function guidesFor(box, others, anchorsOf = 'both') {
|
|
79
|
+
const guides = [];
|
|
80
|
+
if (anchorsOf !== 'y') {
|
|
81
|
+
const anchors = [box.x, box.x + box.width / 2, box.x + box.width];
|
|
82
|
+
for (const line of verticalLinesOf(others)) {
|
|
83
|
+
if (anchors.some((anchor) => Math.abs(anchor - line.position) <= MATCH_EPSILON)) {
|
|
84
|
+
const top = Math.min(box.y, line.rect.y);
|
|
85
|
+
const bottom = Math.max(box.y + box.height, line.rect.y + line.rect.height);
|
|
86
|
+
mergeGuide(guides, { orientation: 'vertical', position: line.position, from: top, to: bottom });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (anchorsOf !== 'x') {
|
|
91
|
+
const anchors = [box.y, box.y + box.height / 2, box.y + box.height];
|
|
92
|
+
for (const line of horizontalLinesOf(others)) {
|
|
93
|
+
if (anchors.some((anchor) => Math.abs(anchor - line.position) <= MATCH_EPSILON)) {
|
|
94
|
+
const left = Math.min(box.x, line.rect.x);
|
|
95
|
+
const right = Math.max(box.x + box.width, line.rect.x + line.rect.width);
|
|
96
|
+
mergeGuide(guides, { orientation: 'horizontal', position: line.position, from: left, to: right });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return guides;
|
|
101
|
+
}
|
|
102
|
+
/** Adds a guide, or stretches the one already on the same line to cover it. */
|
|
103
|
+
function mergeGuide(guides, guide) {
|
|
104
|
+
const existing = guides.find((candidate) => candidate.orientation === guide.orientation && Math.abs(candidate.position - guide.position) <= MATCH_EPSILON);
|
|
105
|
+
if (!existing) {
|
|
106
|
+
guides.push(guide);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
existing.from = Math.min(existing.from, guide.from);
|
|
110
|
+
existing.to = Math.max(existing.to, guide.to);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Snaps a box that is being moved. `moving` is where the box would be without snapping; the result says how
|
|
114
|
+
* far to nudge it and which guides to show once it is there.
|
|
115
|
+
*/
|
|
116
|
+
export function computeMoveSnap(moving, others, reach) {
|
|
117
|
+
if (others.length === 0 || reach <= 0)
|
|
118
|
+
return NO_SNAP;
|
|
119
|
+
const x = nearestOffset([moving.x, moving.x + moving.width / 2, moving.x + moving.width], verticalLinesOf(others), reach);
|
|
120
|
+
const y = nearestOffset([moving.y, moving.y + moving.height / 2, moving.y + moving.height], horizontalLinesOf(others), reach);
|
|
121
|
+
const deltaX = x?.offset ?? 0;
|
|
122
|
+
const deltaY = y?.offset ?? 0;
|
|
123
|
+
if (!x && !y)
|
|
124
|
+
return NO_SNAP;
|
|
125
|
+
const landed = { ...moving, x: moving.x + deltaX, y: moving.y + deltaY };
|
|
126
|
+
return { deltaX, deltaY, guides: guidesFor(landed, others, x && y ? 'both' : x ? 'x' : 'y') };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Snaps one point (the corner or edge a handle drags, or a corner of a shape being drawn) to the lines of
|
|
130
|
+
* the other shapes. `axes` says which coordinates may snap (an edge handle moves one only).
|
|
131
|
+
*/
|
|
132
|
+
export function snapPoint(point, others, reach, axes = { x: true, y: true }) {
|
|
133
|
+
if (others.length === 0 || reach <= 0)
|
|
134
|
+
return { point, guides: [] };
|
|
135
|
+
const x = axes.x ? nearestOffset([point.x], verticalLinesOf(others), reach) : null;
|
|
136
|
+
const y = axes.y ? nearestOffset([point.y], horizontalLinesOf(others), reach) : null;
|
|
137
|
+
const snapped = { x: point.x + (x?.offset ?? 0), y: point.y + (y?.offset ?? 0) };
|
|
138
|
+
const guides = [];
|
|
139
|
+
if (x) {
|
|
140
|
+
for (const line of verticalLinesOf(others)) {
|
|
141
|
+
if (Math.abs(line.position - snapped.x) <= MATCH_EPSILON) {
|
|
142
|
+
mergeGuide(guides, {
|
|
143
|
+
orientation: 'vertical',
|
|
144
|
+
position: line.position,
|
|
145
|
+
from: Math.min(snapped.y, line.rect.y),
|
|
146
|
+
to: Math.max(snapped.y, line.rect.y + line.rect.height)
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (y) {
|
|
152
|
+
for (const line of horizontalLinesOf(others)) {
|
|
153
|
+
if (Math.abs(line.position - snapped.y) <= MATCH_EPSILON) {
|
|
154
|
+
mergeGuide(guides, {
|
|
155
|
+
orientation: 'horizontal',
|
|
156
|
+
position: line.position,
|
|
157
|
+
from: Math.min(snapped.x, line.rect.x),
|
|
158
|
+
to: Math.max(snapped.x, line.rect.x + line.rect.width)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { point: snapped, guides };
|
|
164
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Changing how selected shapes look.
|
|
3
|
+
*
|
|
4
|
+
* A style panel sends one patch for the whole selection; each shape takes the keys that mean something for
|
|
5
|
+
* its kind and ignores the rest (an image has no stroke, a rectangle no font). A shape comes back unchanged
|
|
6
|
+
* (the same object) when the patch changes nothing for it, so the renderer redraws only what is different.
|
|
7
|
+
*/
|
|
8
|
+
import type { ShapeObject, StrokeStyle, TextAlign } from './types.js';
|
|
9
|
+
export interface StylePatch {
|
|
10
|
+
/** 0..1, the whole object. */
|
|
11
|
+
opacity?: number;
|
|
12
|
+
fill?: string;
|
|
13
|
+
fillOpacity?: number;
|
|
14
|
+
stroke?: string;
|
|
15
|
+
strokeWidth?: number;
|
|
16
|
+
strokeStyle?: StrokeStyle;
|
|
17
|
+
/** 0..100 */
|
|
18
|
+
roundness?: number;
|
|
19
|
+
color?: string;
|
|
20
|
+
fontSize?: number;
|
|
21
|
+
fontFamily?: string;
|
|
22
|
+
fontWeight?: 'normal' | 'bold';
|
|
23
|
+
fontStyle?: 'normal' | 'italic';
|
|
24
|
+
underline?: boolean;
|
|
25
|
+
align?: TextAlign;
|
|
26
|
+
lineHeight?: number;
|
|
27
|
+
padding?: number;
|
|
28
|
+
backgroundColor?: string;
|
|
29
|
+
}
|
|
30
|
+
/** Whether the Roundness control means something for the path (rounded corners or round line ends). */
|
|
31
|
+
export declare function pathSupportsRoundness(shapeKey: string | null): boolean;
|
|
32
|
+
/** Whether the "Fill" colour of this shape is really the colour of its line, because it has no inside. */
|
|
33
|
+
export declare function paintsFillOnStroke(shape: ShapeObject): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The colour of the line a shape shows: its stroke, but for a brush stroke (drawn as a filled outline, so that its width can
|
|
36
|
+
* follow the pressure) the fill of that outline. The panel's Stroke colour and the shape snap read and write the line this way.
|
|
37
|
+
*/
|
|
38
|
+
export declare function lineColorOf(shape: ShapeObject): string;
|
|
39
|
+
/** Applies the keys of a patch that mean something for the shape. Same object back when nothing changes. */
|
|
40
|
+
export declare function applyStylePatch(shape: ShapeObject, requested: StylePatch): ShapeObject;
|