@widgetic/canvas 0.5.35 → 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/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 +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 +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 +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,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a new object starts with, and the functions that make one.
|
|
3
|
+
*
|
|
4
|
+
* Every factory takes only what differs from the defaults, so a tool says "a rectangle here, this big" and
|
|
5
|
+
* nothing else. Ids come from `createObjectId`; a test passes its own `id` and gets the same record every time.
|
|
6
|
+
*/
|
|
7
|
+
import type { EllipseObject, GroupObject, ImageObject, ObjectId, PathObject, RectObject, StrokeStyle, TextObject, WidgetObject } from './types.js';
|
|
8
|
+
/** The look of a shape in the wireframe mode (the default of the canvas) and outside it. */
|
|
9
|
+
export declare const SHAPE_STYLE_DEFAULTS: {
|
|
10
|
+
readonly wireframeFill: "#ffffff";
|
|
11
|
+
readonly wireframeStroke: "#374151";
|
|
12
|
+
readonly wireframeStrokeWidth: 2;
|
|
13
|
+
readonly stroke: "#000000";
|
|
14
|
+
readonly strokeWidth: 1;
|
|
15
|
+
/** What the Draw tool draws with. */
|
|
16
|
+
readonly inkStroke: "#1f2937";
|
|
17
|
+
readonly inkStrokeWidth: 3;
|
|
18
|
+
};
|
|
19
|
+
export declare const TEXT_DEFAULTS: {
|
|
20
|
+
readonly fontSize: 20;
|
|
21
|
+
readonly fontFamily: "Arial";
|
|
22
|
+
readonly color: "#111827";
|
|
23
|
+
/** The colour of the "Type something" prompt shown in an empty text box. */
|
|
24
|
+
readonly placeholderColor: "#9ca3af";
|
|
25
|
+
/** The narrowest a text box may be, in world units. */
|
|
26
|
+
readonly minimumWidth: 15;
|
|
27
|
+
/**
|
|
28
|
+
* Words used only to measure how wide an empty auto-width text box should be (matches the default placeholder
|
|
29
|
+
* `canvas.defaults.textBox`). A host with a custom placeholder should call `setEmptyTextMeasureStandIn` on the engine.
|
|
30
|
+
*/
|
|
31
|
+
readonly emptyTextMeasureStandIn: "Type here";
|
|
32
|
+
};
|
|
33
|
+
/** The size of a shape that is placed by a click or a button, not dragged out. */
|
|
34
|
+
export declare const DEFAULT_OBJECT_SIZE = 100;
|
|
35
|
+
/** A random vivid colour, for a new shape on a board that is not in the wireframe mode. */
|
|
36
|
+
export declare function randomShapeFill(random?: () => number): string;
|
|
37
|
+
/** A unique id: time, a counter and a random tail, so two tabs and two calls in one millisecond never collide. */
|
|
38
|
+
export declare function createObjectId(prefix?: string): ObjectId;
|
|
39
|
+
/** The dash pattern of a stroke style. Dotted uses zero-length dashes, which a round cap turns into dots. */
|
|
40
|
+
export declare function strokeDashArray(strokeStyle: StrokeStyle, strokeWidth: number): number[] | null;
|
|
41
|
+
type RectFields = Omit<RectObject, 'type' | 'id'> & {
|
|
42
|
+
id?: ObjectId;
|
|
43
|
+
};
|
|
44
|
+
type EllipseFields = Omit<EllipseObject, 'type' | 'id'> & {
|
|
45
|
+
id?: ObjectId;
|
|
46
|
+
};
|
|
47
|
+
type PathFields = Omit<PathObject, 'type' | 'id'> & {
|
|
48
|
+
id?: ObjectId;
|
|
49
|
+
};
|
|
50
|
+
type TextFields = Omit<TextObject, 'type' | 'id'> & {
|
|
51
|
+
id?: ObjectId;
|
|
52
|
+
};
|
|
53
|
+
type ImageFields = Omit<ImageObject, 'type' | 'id'> & {
|
|
54
|
+
id?: ObjectId;
|
|
55
|
+
};
|
|
56
|
+
type WidgetFields = Omit<WidgetObject, 'type' | 'id'> & {
|
|
57
|
+
id?: ObjectId;
|
|
58
|
+
};
|
|
59
|
+
export declare function createRectObject(fields?: Partial<RectFields>): RectObject;
|
|
60
|
+
export declare function createEllipseObject(fields?: Partial<EllipseFields>): EllipseObject;
|
|
61
|
+
export declare function createPathObject(fields?: Partial<PathFields>): PathObject;
|
|
62
|
+
export declare function createTextObject(fields?: Partial<TextFields>): TextObject;
|
|
63
|
+
export declare function createImageObject(fields?: Partial<ImageFields>): ImageObject;
|
|
64
|
+
export declare function createWidgetObject(fields: Partial<WidgetFields> & {
|
|
65
|
+
widgetId: string;
|
|
66
|
+
}): WidgetObject;
|
|
67
|
+
export declare function createGroupObject(fields?: Partial<Omit<GroupObject, 'type' | 'id'>> & {
|
|
68
|
+
id?: ObjectId;
|
|
69
|
+
}): GroupObject;
|
|
70
|
+
export {};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a new object starts with, and the functions that make one.
|
|
3
|
+
*
|
|
4
|
+
* Every factory takes only what differs from the defaults, so a tool says "a rectangle here, this big" and
|
|
5
|
+
* nothing else. Ids come from `createObjectId`; a test passes its own `id` and gets the same record every time.
|
|
6
|
+
*/
|
|
7
|
+
/** The look of a shape in the wireframe mode (the default of the canvas) and outside it. */
|
|
8
|
+
export const SHAPE_STYLE_DEFAULTS = {
|
|
9
|
+
wireframeFill: '#ffffff',
|
|
10
|
+
wireframeStroke: '#374151',
|
|
11
|
+
wireframeStrokeWidth: 2,
|
|
12
|
+
stroke: '#000000',
|
|
13
|
+
strokeWidth: 1,
|
|
14
|
+
/** What the Draw tool draws with. */
|
|
15
|
+
inkStroke: '#1f2937',
|
|
16
|
+
inkStrokeWidth: 3
|
|
17
|
+
};
|
|
18
|
+
export const TEXT_DEFAULTS = {
|
|
19
|
+
fontSize: 20,
|
|
20
|
+
fontFamily: 'Arial',
|
|
21
|
+
color: '#111827',
|
|
22
|
+
/** The colour of the "Type something" prompt shown in an empty text box. */
|
|
23
|
+
placeholderColor: '#9ca3af',
|
|
24
|
+
/** The narrowest a text box may be, in world units. */
|
|
25
|
+
minimumWidth: 15,
|
|
26
|
+
/**
|
|
27
|
+
* Words used only to measure how wide an empty auto-width text box should be (matches the default placeholder
|
|
28
|
+
* `canvas.defaults.textBox`). A host with a custom placeholder should call `setEmptyTextMeasureStandIn` on the engine.
|
|
29
|
+
*/
|
|
30
|
+
emptyTextMeasureStandIn: 'Type here'
|
|
31
|
+
};
|
|
32
|
+
/** The size of a shape that is placed by a click or a button, not dragged out. */
|
|
33
|
+
export const DEFAULT_OBJECT_SIZE = 100;
|
|
34
|
+
/** A random vivid colour, for a new shape on a board that is not in the wireframe mode. */
|
|
35
|
+
export function randomShapeFill(random = Math.random) {
|
|
36
|
+
return `hsl(${Math.floor(random() * 360)}, 70%, 50%)`;
|
|
37
|
+
}
|
|
38
|
+
let idCounter = 0;
|
|
39
|
+
/** A unique id: time, a counter and a random tail, so two tabs and two calls in one millisecond never collide. */
|
|
40
|
+
export function createObjectId(prefix = 'obj') {
|
|
41
|
+
idCounter += 1;
|
|
42
|
+
const randomTail = Math.random().toString(36).slice(2, 8);
|
|
43
|
+
return `${prefix}_${Date.now().toString(36)}_${idCounter.toString(36)}${randomTail}`;
|
|
44
|
+
}
|
|
45
|
+
/** The dash pattern of a stroke style. Dotted uses zero-length dashes, which a round cap turns into dots. */
|
|
46
|
+
export function strokeDashArray(strokeStyle, strokeWidth) {
|
|
47
|
+
const unit = Math.max(2, strokeWidth);
|
|
48
|
+
switch (strokeStyle) {
|
|
49
|
+
case 'dashed':
|
|
50
|
+
return [unit * 4, unit * 2];
|
|
51
|
+
case 'dotted':
|
|
52
|
+
return [0, unit * 2];
|
|
53
|
+
default:
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const BOX_DEFAULTS = {
|
|
58
|
+
parentId: null,
|
|
59
|
+
x: 0,
|
|
60
|
+
y: 0,
|
|
61
|
+
width: DEFAULT_OBJECT_SIZE,
|
|
62
|
+
height: DEFAULT_OBJECT_SIZE,
|
|
63
|
+
rotation: 0,
|
|
64
|
+
opacity: 1
|
|
65
|
+
};
|
|
66
|
+
const PAINT_DEFAULTS = {
|
|
67
|
+
fill: SHAPE_STYLE_DEFAULTS.wireframeFill,
|
|
68
|
+
fillOpacity: 1,
|
|
69
|
+
stroke: SHAPE_STYLE_DEFAULTS.wireframeStroke,
|
|
70
|
+
strokeWidth: SHAPE_STYLE_DEFAULTS.wireframeStrokeWidth,
|
|
71
|
+
strokeStyle: 'solid'
|
|
72
|
+
};
|
|
73
|
+
export function createRectObject(fields = {}) {
|
|
74
|
+
return { ...BOX_DEFAULTS, ...PAINT_DEFAULTS, roundness: 0, ...fields, id: fields.id ?? createObjectId(), type: 'rect' };
|
|
75
|
+
}
|
|
76
|
+
export function createEllipseObject(fields = {}) {
|
|
77
|
+
return {
|
|
78
|
+
...BOX_DEFAULTS,
|
|
79
|
+
...PAINT_DEFAULTS,
|
|
80
|
+
aspectLocked: false,
|
|
81
|
+
...fields,
|
|
82
|
+
id: fields.id ?? createObjectId(),
|
|
83
|
+
type: 'ellipse'
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export function createPathObject(fields = {}) {
|
|
87
|
+
return {
|
|
88
|
+
...BOX_DEFAULTS,
|
|
89
|
+
...PAINT_DEFAULTS,
|
|
90
|
+
d: '',
|
|
91
|
+
shape: null,
|
|
92
|
+
roundness: 0,
|
|
93
|
+
aspectLocked: false,
|
|
94
|
+
flipX: false,
|
|
95
|
+
flipY: false,
|
|
96
|
+
lineCap: 'round',
|
|
97
|
+
lineJoin: 'round',
|
|
98
|
+
ink: null,
|
|
99
|
+
arrow: null,
|
|
100
|
+
...fields,
|
|
101
|
+
id: fields.id ?? createObjectId(),
|
|
102
|
+
type: 'path'
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export function createTextObject(fields = {}) {
|
|
106
|
+
return {
|
|
107
|
+
...BOX_DEFAULTS,
|
|
108
|
+
width: TEXT_DEFAULTS.minimumWidth,
|
|
109
|
+
height: Math.ceil(TEXT_DEFAULTS.fontSize * 1.2),
|
|
110
|
+
text: '',
|
|
111
|
+
color: TEXT_DEFAULTS.color,
|
|
112
|
+
fontSize: TEXT_DEFAULTS.fontSize,
|
|
113
|
+
fontFamily: TEXT_DEFAULTS.fontFamily,
|
|
114
|
+
fontWeight: 'normal',
|
|
115
|
+
fontStyle: 'normal',
|
|
116
|
+
underline: false,
|
|
117
|
+
align: 'left',
|
|
118
|
+
lineHeight: 1.2,
|
|
119
|
+
padding: 0,
|
|
120
|
+
backgroundColor: 'transparent',
|
|
121
|
+
autoWidth: true,
|
|
122
|
+
...fields,
|
|
123
|
+
id: fields.id ?? createObjectId(),
|
|
124
|
+
type: 'text'
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
export function createImageObject(fields = {}) {
|
|
128
|
+
return {
|
|
129
|
+
...BOX_DEFAULTS,
|
|
130
|
+
width: 300,
|
|
131
|
+
height: 200,
|
|
132
|
+
src: null,
|
|
133
|
+
imageRef: null,
|
|
134
|
+
fileName: null,
|
|
135
|
+
naturalWidth: 0,
|
|
136
|
+
naturalHeight: 0,
|
|
137
|
+
...fields,
|
|
138
|
+
id: fields.id ?? createObjectId(),
|
|
139
|
+
type: 'image'
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
export function createWidgetObject(fields) {
|
|
143
|
+
return {
|
|
144
|
+
...BOX_DEFAULTS,
|
|
145
|
+
width: 360,
|
|
146
|
+
height: 268,
|
|
147
|
+
widgetName: null,
|
|
148
|
+
isPrimary: false,
|
|
149
|
+
thumbnail: null,
|
|
150
|
+
thumbnailRef: null,
|
|
151
|
+
...fields,
|
|
152
|
+
id: fields.id ?? createObjectId(),
|
|
153
|
+
type: 'widget'
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
export function createGroupObject(fields = {}) {
|
|
157
|
+
return { parentId: null, ...fields, id: fields.id ?? createObjectId('group'), type: 'group' };
|
|
158
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Freehand strokes: the outline of a line whose width follows the hand, the way a brush draws it.
|
|
3
|
+
*
|
|
4
|
+
* The method is the one of perfect-freehand (Steve Ruiz, MIT licence, https://github.com/steveruiz/perfect-freehand), the
|
|
5
|
+
* library tldraw draws its pencil with: the samples of the pointer are smoothed (`streamline`), every smoothed point gets a
|
|
6
|
+
* radius from a pressure, and the stroke is the polygon that runs up one side of the line and back down the other, with a
|
|
7
|
+
* cap at each end. The pressure is what a pen reports. For a mouse and a finger, which report none, it is worked out from
|
|
8
|
+
* how fast the hand moved: a hand that dwells presses, and the line swells; a quick one lifts, and the line thins (`thinning`).
|
|
9
|
+
* That is the feel of a brush that is thicker where it is pressed longer.
|
|
10
|
+
*
|
|
11
|
+
* This file is written again for this package (its own structure, names and comments), following the same method and the
|
|
12
|
+
* same constants, and it is credited to perfect-freehand's author as the licence asks:
|
|
13
|
+
*
|
|
14
|
+
* MIT License. Copyright (c) 2021 Steve Ruiz. Permission is hereby granted, free of charge, to any person obtaining a
|
|
15
|
+
* copy of this software and associated documentation files, to deal in the software without restriction, subject to the
|
|
16
|
+
* inclusion of this copyright notice and the permission notice in all copies or substantial portions of the software.
|
|
17
|
+
* The software is provided "as is", without warranty of any kind.
|
|
18
|
+
*
|
|
19
|
+
* Pure functions on plain numbers: they run in Node as they do in the browser, and the editor, the eraser's trail and the
|
|
20
|
+
* rasterizer all draw from the same outline.
|
|
21
|
+
*/
|
|
22
|
+
import type { BrushPressure, Point } from './types.js';
|
|
23
|
+
export type { BrushPressure };
|
|
24
|
+
/** One sample of the pointer. */
|
|
25
|
+
export interface FreehandSample {
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
28
|
+
/** 0 to 1 as a pen reports it. Left out (or negative) when the device reports none: the width then follows the speed. */
|
|
29
|
+
pressure?: number;
|
|
30
|
+
}
|
|
31
|
+
/** One end of a stroke: its cap, and whether it comes to a point. */
|
|
32
|
+
export interface FreehandEnd {
|
|
33
|
+
/** A round cap (the default) or a flat one. A tapered end has no cap. */
|
|
34
|
+
cap?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* How far along the line it narrows to nothing, in the units of the samples: `true` is the whole line (or `size`, when the
|
|
37
|
+
* line is shorter), a number is that length, `false` (the default) leaves the end as wide as the line.
|
|
38
|
+
*/
|
|
39
|
+
taper?: boolean | number;
|
|
40
|
+
/** How the narrowing progresses, from 0 (at the tip) to 1 (full width). */
|
|
41
|
+
easing?: (progress: number) => number;
|
|
42
|
+
}
|
|
43
|
+
export interface FreehandOptions {
|
|
44
|
+
/** The width of the line at an even pressure of one half, in the units of the samples. */
|
|
45
|
+
size: number;
|
|
46
|
+
/** How much the pressure changes the width, from -1 to 1. 0 is a line of one width. */
|
|
47
|
+
thinning?: number;
|
|
48
|
+
/** How far apart the points of the outline must be (a fraction of `size`): more smoothing, fewer points. */
|
|
49
|
+
smoothing?: number;
|
|
50
|
+
/** How far the line trails behind the hand, from 0 (follows it exactly) to 1 (smooths a lot). */
|
|
51
|
+
streamline?: number;
|
|
52
|
+
/** Shapes the pressure on its way to a width. */
|
|
53
|
+
easing?: (pressure: number) => number;
|
|
54
|
+
/** Work the pressure out from the speed of the hand instead of reading it from the samples. */
|
|
55
|
+
simulatePressure?: boolean;
|
|
56
|
+
start?: FreehandEnd;
|
|
57
|
+
end?: FreehandEnd;
|
|
58
|
+
/** The stroke is finished: the last point is the pointer's own last sample, and the end is capped. */
|
|
59
|
+
last?: boolean;
|
|
60
|
+
}
|
|
61
|
+
/** A sample after smoothing, with what the outline is made from. */
|
|
62
|
+
export interface StrokePoint {
|
|
63
|
+
x: number;
|
|
64
|
+
y: number;
|
|
65
|
+
pressure: number;
|
|
66
|
+
/** The unit vector that points back along the line, towards the previous point. */
|
|
67
|
+
vectorX: number;
|
|
68
|
+
vectorY: number;
|
|
69
|
+
/** The distance from the previous point, and from the start. */
|
|
70
|
+
distance: number;
|
|
71
|
+
runningLength: number;
|
|
72
|
+
}
|
|
73
|
+
export declare const EASE_LINEAR: (progress: number) => number;
|
|
74
|
+
export declare const EASE_OUT_SINE: (progress: number) => number;
|
|
75
|
+
/** The easing of a pen's pressure: mostly linear, with a little of the sine's softness. */
|
|
76
|
+
export declare const EASE_PEN: (progress: number) => number;
|
|
77
|
+
/**
|
|
78
|
+
* Smooths the samples into the points the outline is built on. Each point moves only a part of the way to the next sample
|
|
79
|
+
* (that is the `streamline`, and why the line trails the hand a little), the first stretch of the line, shorter than `size`,
|
|
80
|
+
* is dropped because it only makes the start wobble, and every point knows its distance from the one before and from the start.
|
|
81
|
+
* A finished line (`last`) ends exactly on the last sample.
|
|
82
|
+
*/
|
|
83
|
+
export declare function strokePointsOf(samples: readonly FreehandSample[], options: FreehandOptions): StrokePoint[];
|
|
84
|
+
/** The outline of a line, and the pressure each of its points was drawn with (read from the sample, or worked out from the speed). */
|
|
85
|
+
export interface OutlineWithPressures {
|
|
86
|
+
outline: Point[];
|
|
87
|
+
/** One per point of the line, in the same order. A point the outline leaves out (the noise at the very end) carries the pressure before it. */
|
|
88
|
+
pressures: number[];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The polygon that surrounds a line of smoothed points: up the left side, round the end, back down the right side and round
|
|
92
|
+
* the start. The points come from `strokePointsOf` with the same options.
|
|
93
|
+
*/
|
|
94
|
+
export declare function outlineOf(points: readonly StrokePoint[], options: FreehandOptions): Point[];
|
|
95
|
+
/**
|
|
96
|
+
* The same, with the pressure of every point as well. A line whose pressure was worked out from the speed can be drawn again
|
|
97
|
+
* from these (with `simulatePressure` off) and come out the same, whatever size its points are later scaled to.
|
|
98
|
+
*/
|
|
99
|
+
export declare function outlineWithPressures(points: readonly StrokePoint[], options: FreehandOptions): OutlineWithPressures;
|
|
100
|
+
/**
|
|
101
|
+
* Points that are already smooth (the line a stroke was stored as) with what the outline is made from: the way back along the
|
|
102
|
+
* line, and the distance from the point before and from the start. Nothing is smoothed again, so the line is exactly the one
|
|
103
|
+
* given; a point on the one before it adds nothing and is left out.
|
|
104
|
+
*/
|
|
105
|
+
export declare function strokePointsOfLine(line: readonly FreehandSample[]): StrokePoint[];
|
|
106
|
+
/** The outline of a line of samples: smoothed, then drawn round. */
|
|
107
|
+
export declare function freehandOutline(samples: readonly FreehandSample[], options: FreehandOptions): Point[];
|
|
108
|
+
/** The path data of a circle: two arcs, so that it fills as a dot. */
|
|
109
|
+
export declare function circlePathData(center: Point, radius: number): string;
|
|
110
|
+
/**
|
|
111
|
+
* The path data of a closed outline, smoothed: a quadratic curve through the midpoints of the polygon's sides, with the
|
|
112
|
+
* polygon's corners as the control points. Fewer than four points are no outline, and give an empty string.
|
|
113
|
+
*/
|
|
114
|
+
export declare function outlinePathData(outline: readonly Point[]): string;
|
|
115
|
+
/**
|
|
116
|
+
* The options of the pencil: the numbers tldraw gives its hand-drawn line. With a mouse or a finger the pressure follows the
|
|
117
|
+
* speed (a slow hand swells the line), with a pen it is the pen's own, and the nib is a little broader to match.
|
|
118
|
+
*/
|
|
119
|
+
export declare function brushOptions(size: number, pressure: BrushPressure, finished: boolean): FreehandOptions;
|
|
120
|
+
/**
|
|
121
|
+
* The size of the brush that goes with the width a person gave a line: the line is a little broader than its width, as a
|
|
122
|
+
* pencil's is (tldraw's `size + 1`).
|
|
123
|
+
*/
|
|
124
|
+
export declare function brushSizeFor(strokeWidth: number): number;
|
|
125
|
+
/**
|
|
126
|
+
* The widest the outline of a brush stroke reaches from the line of its samples, for a brush of `size`: the radius at full
|
|
127
|
+
* pressure, which is 0.92 of the size with the speed's easing and 0.86 of a pen's broader nib (1 + 1.2 of the size), rounded up.
|
|
128
|
+
* What hit testing and the bounds of a stroke add to the box of its samples.
|
|
129
|
+
*/
|
|
130
|
+
export declare function brushReach(size: number): number;
|