@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.
Files changed (210) hide show
  1. package/README.md +105 -36
  2. package/dist/canvas/DomCanvas.svelte +454 -0
  3. package/dist/canvas/DomCanvas.svelte.d.ts +222 -0
  4. package/dist/canvas/boardFrame.d.ts +13 -0
  5. package/dist/canvas/boardFrame.js +13 -0
  6. package/dist/canvas/boardSurface.d.ts +9 -0
  7. package/dist/canvas/boardSurface.js +9 -0
  8. package/dist/canvas/chromeLayout.d.ts +76 -0
  9. package/dist/canvas/chromeLayout.js +121 -0
  10. package/dist/canvas/emptyBoardHint.d.ts +17 -0
  11. package/dist/canvas/emptyBoardHint.js +12 -0
  12. package/dist/canvas/eventScope.d.ts +1 -1
  13. package/dist/canvas/eventScope.js +1 -1
  14. package/dist/canvas/host/CanvasHost.d.ts +351 -0
  15. package/dist/canvas/host/CanvasHost.js +1740 -0
  16. package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
  17. package/dist/canvas/host/CanvasHostApi.js +95 -0
  18. package/dist/canvas/host/browserServices.d.ts +46 -0
  19. package/dist/canvas/host/browserServices.js +167 -0
  20. package/dist/canvas/host/conversion.d.ts +51 -0
  21. package/dist/canvas/host/conversion.js +123 -0
  22. package/dist/canvas/host/hostObject.d.ts +40 -0
  23. package/dist/canvas/host/hostObject.js +93 -0
  24. package/dist/canvas/host/pageEvents.d.ts +24 -0
  25. package/dist/canvas/host/pageEvents.js +35 -0
  26. package/dist/canvas/host/screenRects.d.ts +66 -0
  27. package/dist/canvas/host/screenRects.js +102 -0
  28. package/dist/canvas/host/strokeSnap.d.ts +67 -0
  29. package/dist/canvas/host/strokeSnap.js +165 -0
  30. package/dist/canvas/index.d.ts +7 -7
  31. package/dist/canvas/index.js +8 -8
  32. package/dist/canvas/openMarks.d.ts +17 -0
  33. package/dist/canvas/openMarks.js +26 -0
  34. package/dist/canvas/panZoomPosition.d.ts +15 -0
  35. package/dist/canvas/panZoomPosition.js +16 -0
  36. package/dist/canvas/preferredTextFont.d.ts +12 -0
  37. package/dist/canvas/preferredTextFont.js +38 -0
  38. package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
  39. package/dist/canvas/shapes/ShapeLibrary.js +13 -1
  40. package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
  41. package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
  42. package/dist/canvas/toolbarPosition.d.ts +11 -11
  43. package/dist/canvas/toolbarPosition.js +11 -24
  44. package/dist/canvas/types.d.ts +14 -0
  45. package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
  46. package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
  47. package/dist/canvas/ui/CanvasHint.svelte +20 -0
  48. package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
  49. package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
  50. package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
  51. package/dist/canvas/ui/ColorField.svelte +155 -0
  52. package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
  53. package/dist/canvas/ui/LoadNotice.svelte +39 -0
  54. package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
  55. package/dist/canvas/ui/SelectionPanel.svelte +300 -0
  56. package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
  57. package/dist/canvas/ui/SelectorBar.svelte +82 -0
  58. package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
  59. package/dist/canvas/ui/ShapesMenu.svelte +185 -0
  60. package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
  61. package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
  62. package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
  63. package/dist/canvas/ui/ToolButton.svelte +78 -0
  64. package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
  65. package/dist/canvas/ui/WidgetActions.svelte +152 -0
  66. package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
  67. package/dist/canvas/ui/ZoomControl.svelte +89 -0
  68. package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
  69. package/dist/canvas/ui/colorModel.d.ts +18 -0
  70. package/dist/canvas/ui/colorModel.js +85 -0
  71. package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
  72. package/dist/canvas/ui/colorPickerMessages.js +15 -0
  73. package/dist/canvas/ui/floatingCard.d.ts +107 -0
  74. package/dist/canvas/ui/floatingCard.js +107 -0
  75. package/dist/canvas/ui/floatingLayer.d.ts +21 -0
  76. package/dist/canvas/ui/floatingLayer.js +52 -0
  77. package/dist/canvas/ui/icons.d.ts +12 -0
  78. package/dist/canvas/ui/icons.js +7 -0
  79. package/dist/canvas/ui/keyHints.d.ts +27 -0
  80. package/dist/canvas/ui/keyHints.js +43 -0
  81. package/dist/canvas/ui/popoverSide.d.ts +5 -0
  82. package/dist/canvas/ui/popoverSide.js +20 -0
  83. package/dist/canvas/ui/selectionTraits.d.ts +61 -0
  84. package/dist/canvas/ui/selectionTraits.js +99 -0
  85. package/dist/canvas/view/CanvasStage.svelte +132 -0
  86. package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
  87. package/dist/canvas/view/EraserTrail.svelte +50 -0
  88. package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
  89. package/dist/canvas/view/ObjectView.svelte +245 -0
  90. package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
  91. package/dist/canvas/view/SelectionOverlay.svelte +487 -0
  92. package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
  93. package/dist/canvas/view/TextEditor.svelte +76 -0
  94. package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
  95. package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
  96. package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
  97. package/dist/canvas/view/erasingMarks.d.ts +13 -0
  98. package/dist/canvas/view/erasingMarks.js +34 -0
  99. package/dist/canvas/view/geometry.d.ts +169 -0
  100. package/dist/canvas/view/geometry.js +272 -0
  101. package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
  102. package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
  103. package/dist/components/index.js +1 -1
  104. package/dist/components/input-controls/ColorIC.svelte +2 -2
  105. package/dist/engine/EngineCore.d.ts +273 -0
  106. package/dist/engine/EngineCore.js +1051 -0
  107. package/dist/engine/codec/canvasDocument.d.ts +57 -0
  108. package/dist/engine/codec/canvasDocument.js +471 -0
  109. package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
  110. package/dist/engine/codec/hostReadableKeys.js +25 -0
  111. package/dist/engine/core/arrange.d.ts +20 -0
  112. package/dist/engine/core/arrange.js +81 -0
  113. package/dist/engine/core/arrow.d.ts +91 -0
  114. package/dist/engine/core/arrow.js +182 -0
  115. package/dist/engine/core/brush.d.ts +68 -0
  116. package/dist/engine/core/brush.js +173 -0
  117. package/dist/engine/core/defaults.d.ts +70 -0
  118. package/dist/engine/core/defaults.js +158 -0
  119. package/dist/engine/core/freehand.d.ts +130 -0
  120. package/dist/engine/core/freehand.js +430 -0
  121. package/dist/engine/core/geometry.d.ts +44 -0
  122. package/dist/engine/core/geometry.js +216 -0
  123. package/dist/engine/core/history.d.ts +39 -0
  124. package/dist/engine/core/history.js +69 -0
  125. package/dist/engine/core/hitTest.d.ts +54 -0
  126. package/dist/engine/core/hitTest.js +298 -0
  127. package/dist/engine/core/matrix.d.ts +47 -0
  128. package/dist/engine/core/matrix.js +101 -0
  129. package/dist/engine/core/pathData.d.ts +50 -0
  130. package/dist/engine/core/pathData.js +550 -0
  131. package/dist/engine/core/rotationReset.d.ts +10 -0
  132. package/dist/engine/core/rotationReset.js +17 -0
  133. package/dist/engine/core/scene.d.ts +99 -0
  134. package/dist/engine/core/scene.js +450 -0
  135. package/dist/engine/core/scribble.d.ts +64 -0
  136. package/dist/engine/core/scribble.js +121 -0
  137. package/dist/engine/core/shapePaths.d.ts +34 -0
  138. package/dist/engine/core/shapePaths.js +69 -0
  139. package/dist/engine/core/shapeTools.d.ts +80 -0
  140. package/dist/engine/core/shapeTools.js +161 -0
  141. package/dist/engine/core/snapping.d.ts +64 -0
  142. package/dist/engine/core/snapping.js +164 -0
  143. package/dist/engine/core/style.d.ts +40 -0
  144. package/dist/engine/core/style.js +151 -0
  145. package/dist/engine/core/transform.d.ts +79 -0
  146. package/dist/engine/core/transform.js +258 -0
  147. package/dist/engine/core/types.d.ts +238 -0
  148. package/dist/engine/core/types.js +31 -0
  149. package/dist/engine/core/viewport.d.ts +32 -0
  150. package/dist/engine/core/viewport.js +84 -0
  151. package/dist/engine/input/PointerController.d.ts +38 -0
  152. package/dist/engine/input/PointerController.js +545 -0
  153. package/dist/engine/input/arrowHandles.d.ts +38 -0
  154. package/dist/engine/input/arrowHandles.js +60 -0
  155. package/dist/engine/input/gestures.d.ts +79 -0
  156. package/dist/engine/input/gestures.js +769 -0
  157. package/dist/engine/input/hoverCursor.d.ts +29 -0
  158. package/dist/engine/input/hoverCursor.js +23 -0
  159. package/dist/engine/input/keyboard.d.ts +75 -0
  160. package/dist/engine/input/keyboard.js +264 -0
  161. package/dist/engine/input/selectionHandles.d.ts +89 -0
  162. package/dist/engine/input/selectionHandles.js +169 -0
  163. package/dist/engine/input/types.d.ts +82 -0
  164. package/dist/engine/input/types.js +5 -0
  165. package/dist/engine/render/rasterize.d.ts +74 -0
  166. package/dist/engine/render/rasterize.js +273 -0
  167. package/dist/engine/state.d.ts +124 -0
  168. package/dist/engine/state.js +47 -0
  169. package/dist/engine/text/layoutText.d.ts +53 -0
  170. package/dist/engine/text/layoutText.js +170 -0
  171. package/dist/engine/widgets.d.ts +124 -0
  172. package/dist/engine/widgets.js +345 -0
  173. package/dist/i18n/labels.d.ts +4 -15
  174. package/dist/i18n/labels.js +33 -53
  175. package/dist/i18n/language.d.ts +3 -3
  176. package/dist/i18n/messages.d.ts +51 -122
  177. package/dist/i18n/messages.js +58 -131
  178. package/dist/i18n/shortcuts.d.ts +0 -1
  179. package/dist/i18n/shortcuts.js +2 -11
  180. package/dist/index.d.ts +4 -2
  181. package/dist/index.js +5 -3
  182. package/package.json +30 -5
  183. package/dist/canvas/Canvas.svelte +0 -13363
  184. package/dist/canvas/Canvas.svelte.d.ts +0 -286
  185. package/dist/canvas/CanvasToolbar.svelte +0 -1498
  186. package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
  187. package/dist/canvas/ContextMenu.svelte +0 -305
  188. package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
  189. package/dist/canvas/PanZoomPanel.svelte +0 -478
  190. package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
  191. package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
  192. package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
  193. package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
  194. package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
  195. package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
  196. package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
  197. package/dist/canvas/shadowRootText.d.ts +0 -5
  198. package/dist/canvas/shadowRootText.js +0 -43
  199. package/dist/canvas/shapes/FrameShape.d.ts +0 -97
  200. package/dist/canvas/shapes/FrameShape.js +0 -953
  201. package/dist/canvas/shapes/ImageShape.d.ts +0 -47
  202. package/dist/canvas/shapes/ImageShape.js +0 -339
  203. package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
  204. package/dist/canvas/shapes/WidgetShape.js +0 -200
  205. package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
  206. package/dist/canvas/widgetPreviewGeometry.js +0 -66
  207. package/dist/i18n/shapeWords.d.ts +0 -30
  208. package/dist/i18n/shapeWords.js +0 -36
  209. package/dist/i18n/slots.d.ts +0 -20
  210. package/dist/i18n/slots.js +0 -40
@@ -0,0 +1,151 @@
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 { getShapeByKey } from '../../canvas/shapes/ShapeLibrary.js';
9
+ import { isArrow, reshapeArrow } from './arrow.js';
10
+ import { MIN_BRUSH_WIDTH, isBrushStroke, reshapeBrushStroke } from './brush.js';
11
+ import { clampNumber } from './geometry.js';
12
+ import { mirrorPathCommands, serializePathData } from './pathData.js';
13
+ import { regeneratedPathCommands } from './shapePaths.js';
14
+ const PAINT_KEYS = ['fill', 'fillOpacity', 'stroke', 'strokeWidth', 'strokeStyle'];
15
+ const TEXT_KEYS = [
16
+ 'color',
17
+ 'fontSize',
18
+ 'fontFamily',
19
+ 'fontWeight',
20
+ 'fontStyle',
21
+ 'underline',
22
+ 'align',
23
+ 'lineHeight',
24
+ 'padding',
25
+ 'backgroundColor'
26
+ ];
27
+ /** The keys of a patch that apply to a kind of shape (a brush stroke has a patch of its own, below). */
28
+ function keysFor(shape) {
29
+ switch (shape.type) {
30
+ case 'rect':
31
+ return ['opacity', 'roundness', ...PAINT_KEYS];
32
+ case 'ellipse':
33
+ return ['opacity', ...PAINT_KEYS];
34
+ case 'path':
35
+ // An arrow is a line and a head: nothing to fill, no corner to round.
36
+ if (isArrow(shape))
37
+ return ['opacity', 'stroke', 'strokeWidth', 'strokeStyle'];
38
+ return ['opacity', 'roundness', ...PAINT_KEYS];
39
+ case 'text':
40
+ return ['opacity', ...TEXT_KEYS];
41
+ default:
42
+ return ['opacity'];
43
+ }
44
+ }
45
+ /** Whether the Roundness control means something for the path (rounded corners or round line ends). */
46
+ export function pathSupportsRoundness(shapeKey) {
47
+ if (shapeKey === 'triangle')
48
+ return true;
49
+ const shape = getShapeByKey(shapeKey);
50
+ return !!shape && (shape.cornerRadiusMode === 'pathRegen' || shape.cornerRadiusMode === 'strokeRound');
51
+ }
52
+ /** Whether the shape's roundness is the shape of its line ends rather than of its corners. */
53
+ function roundnessIsLineEnds(shapeKey) {
54
+ return getShapeByKey(shapeKey)?.cornerRadiusMode === 'strokeRound';
55
+ }
56
+ /** The widest a line may be made with the Width control. */
57
+ const MAX_STROKE_WIDTH = 200;
58
+ const RANGES = {
59
+ opacity: [0, 1],
60
+ fillOpacity: [0, 1],
61
+ strokeWidth: [0, MAX_STROKE_WIDTH],
62
+ roundness: [0, 100],
63
+ fontSize: [4, 600],
64
+ lineHeight: [0.5, 4],
65
+ padding: [0, 200]
66
+ };
67
+ /** The line-only icons of the library (a check, a cross, a plus, a minus): their colour is the colour of the line. */
68
+ const LINE_ICON_KEYS = new Set(['cross', 'plus', 'minus', 'check']);
69
+ /** Whether the "Fill" colour of this shape is really the colour of its line, because it has no inside. */
70
+ export function paintsFillOnStroke(shape) {
71
+ if (shape.type !== 'path' || !shape.shape)
72
+ return false;
73
+ return roundnessIsLineEnds(shape.shape) || LINE_ICON_KEYS.has(shape.shape);
74
+ }
75
+ /**
76
+ * 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
77
+ * follow the pressure) the fill of that outline. The panel's Stroke colour and the shape snap read and write the line this way.
78
+ */
79
+ export function lineColorOf(shape) {
80
+ if (isBrushStroke(shape))
81
+ return shape.fill;
82
+ return shape.type === 'rect' || shape.type === 'ellipse' || shape.type === 'path' ? shape.stroke : '';
83
+ }
84
+ /**
85
+ * A patch for a brush stroke: the Stroke colour is the colour of the outline, the Width draws the brush again at the new width.
86
+ * Everything else the patch carries means nothing to a stroke and is left out. Same object back when nothing changes.
87
+ */
88
+ function applyBrushStylePatch(shape, patch) {
89
+ let next = shape;
90
+ if (patch.opacity !== undefined) {
91
+ const opacity = clampNumber(patch.opacity, 0, 1);
92
+ if (opacity !== next.opacity)
93
+ next = { ...next, opacity };
94
+ }
95
+ if (patch.stroke !== undefined && patch.stroke !== next.fill)
96
+ next = { ...next, fill: patch.stroke };
97
+ if (patch.strokeWidth !== undefined) {
98
+ const strokeWidth = clampNumber(patch.strokeWidth, MIN_BRUSH_WIDTH, MAX_STROKE_WIDTH);
99
+ if (strokeWidth !== next.strokeWidth)
100
+ next = reshapeBrushStroke(next, { strokeWidth });
101
+ }
102
+ return next;
103
+ }
104
+ /** Applies the keys of a patch that mean something for the shape. Same object back when nothing changes. */
105
+ export function applyStylePatch(shape, requested) {
106
+ if (isBrushStroke(shape))
107
+ return applyBrushStylePatch(shape, requested);
108
+ let patch = requested;
109
+ if (patch.fill !== undefined && paintsFillOnStroke(shape)) {
110
+ // The panel's Fill colour is the colour of the line for these icons.
111
+ const { fill, ...rest } = patch;
112
+ patch = { ...rest, stroke: fill };
113
+ }
114
+ const allowed = keysFor(shape);
115
+ const changes = {};
116
+ for (const key of allowed) {
117
+ if (!(key in patch))
118
+ continue;
119
+ let value = patch[key];
120
+ if (value === undefined)
121
+ continue;
122
+ const range = RANGES[key];
123
+ if (range && typeof value === 'number')
124
+ value = clampNumber(value, range[0], range[1]);
125
+ if (shape[key] !== value)
126
+ changes[key] = value;
127
+ }
128
+ if (Object.keys(changes).length === 0)
129
+ return shape;
130
+ let next = { ...shape, ...changes };
131
+ // The head of an arrow is sized by the width of its line, so a new width draws the arrow again.
132
+ if (isArrow(next) && 'strokeWidth' in changes)
133
+ next = reshapeArrow(next);
134
+ if (next.type === 'path' && 'roundness' in changes) {
135
+ if (roundnessIsLineEnds(next.shape)) {
136
+ const rounded = next.roundness > 0;
137
+ next = { ...next, lineCap: (rounded ? 'round' : 'butt'), lineJoin: (rounded ? 'round' : 'miter') };
138
+ }
139
+ else {
140
+ const commands = regeneratedPathCommands(next, next.width, next.height);
141
+ if (commands) {
142
+ next = { ...next, d: serializePathData(mirrorPathCommands(commands, next.width, next.height, next.flipX, next.flipY)) };
143
+ }
144
+ else {
145
+ // Nothing to rebuild (the path is drawn, not generated): the roundness stays as it was.
146
+ next = { ...next, roundness: shape.type === 'path' ? shape.roundness : 0 };
147
+ }
148
+ }
149
+ }
150
+ return next;
151
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Resizing and rotating what is selected.
3
+ *
4
+ * The selection has a *frame*: for one shape, the shape's own box and rotation; for several shapes or a
5
+ * group, the upright rectangle around them. Dragging a handle computes a new frame from the pointer, and
6
+ * the shapes are then mapped from the old frame onto the new one.
7
+ *
8
+ * A handle is dragged in the frame's own (unrotated) space, so a turned shape resizes along its own edges.
9
+ * The edge opposite the handle (or the centre, with Alt) stays where it is in the world.
10
+ */
11
+ import type { Point, ShapeObject } from './types.js';
12
+ export type ResizeHandle = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w';
13
+ /** The oriented rectangle a selection is resized and rotated by. */
14
+ export interface SelectionFrame {
15
+ x: number;
16
+ y: number;
17
+ width: number;
18
+ height: number;
19
+ rotation: number;
20
+ }
21
+ export interface ResizeOptions {
22
+ /** Alt: grow and shrink about the centre instead of the opposite edge. */
23
+ fromCenter?: boolean;
24
+ /** Shift, or a shape that must keep its proportions. */
25
+ keepAspect?: boolean;
26
+ /** Whether dragging past the opposite edge mirrors the shape (otherwise the size stops at the minimum). */
27
+ allowFlip?: boolean;
28
+ minimumSize?: number;
29
+ }
30
+ export interface ResizedFrame extends SelectionFrame {
31
+ /** The pointer went past the opposite edge horizontally: the shapes are mirrored left to right. */
32
+ flipX: boolean;
33
+ flipY: boolean;
34
+ }
35
+ export declare function handleDirection(handle: ResizeHandle): {
36
+ x: -1 | 0 | 1;
37
+ y: -1 | 0 | 1;
38
+ };
39
+ /** The frame of a selection: the shape's own box for one, the upright rectangle around them for several. */
40
+ export declare function selectionFrameOf(shapes: readonly ShapeObject[]): SelectionFrame | null;
41
+ /** The centre of a frame in the world. */
42
+ export declare function frameCenter(frame: SelectionFrame): Point;
43
+ /**
44
+ * The frame that results from dragging `handle` to a world point.
45
+ * `frame` is the frame when the drag began (not the one the last pointer move produced).
46
+ */
47
+ export declare function computeResizedFrame(frame: SelectionFrame, handle: ResizeHandle, pointerWorld: Point, options?: ResizeOptions): ResizedFrame;
48
+ /**
49
+ * Gives one shape a new box, rewriting what depends on its size: the path of a drawn shape is scaled (and
50
+ * mirrored when the box was flipped), the path of a generated shape is rebuilt, a brush stroke has its line
51
+ * scaled and its outline drawn again at the brush's own width, an arrow has its points moved with the box and
52
+ * is drawn again (its head keeps its size), text keeps its font size when it is resized from a side
53
+ * (wrapping) and scales it from a corner.
54
+ */
55
+ export declare function resizeShapeToBox(shape: ShapeObject, box: {
56
+ x: number;
57
+ y: number;
58
+ width: number;
59
+ height: number;
60
+ rotation?: number;
61
+ }, flip?: {
62
+ flipX: boolean;
63
+ flipY: boolean;
64
+ }, textScale?: number | null): ShapeObject;
65
+ /**
66
+ * Maps shapes from the frame they had when the drag began onto the frame the drag produced.
67
+ * A single shape takes the new box as it is; several shapes keep their place inside the frame (their
68
+ * centres move with it, their sizes scale with it, along their own axes).
69
+ */
70
+ export declare function applyFrameResize(shapes: readonly ShapeObject[], startFrame: SelectionFrame, resized: ResizedFrame, isCornerHandle: boolean): ShapeObject[];
71
+ /**
72
+ * Turns shapes about a point by an angle: each centre goes round the point and each shape turns with it. An arrow does not
73
+ * turn (it is always upright): its points go round the point and it is drawn again between them.
74
+ */
75
+ export declare function rotateShapesAbout(shapes: readonly ShapeObject[], pivot: Point, deltaDegrees: number): ShapeObject[];
76
+ /** The angle, in degrees, of the line from `center` to `point` (0 is to the right, growing clockwise on screen). */
77
+ export declare function angleFromCenter(center: Point, point: Point): number;
78
+ /** Rounds an angle to the nearest multiple of `step`. */
79
+ export declare function snapAngle(degrees: number, step: number): number;
@@ -0,0 +1,258 @@
1
+ /**
2
+ * Resizing and rotating what is selected.
3
+ *
4
+ * The selection has a *frame*: for one shape, the shape's own box and rotation; for several shapes or a
5
+ * group, the upright rectangle around them. Dragging a handle computes a new frame from the pointer, and
6
+ * the shapes are then mapped from the old frame onto the new one.
7
+ *
8
+ * A handle is dragged in the frame's own (unrotated) space, so a turned shape resizes along its own edges.
9
+ * The edge opposite the handle (or the centre, with Alt) stays where it is in the world.
10
+ */
11
+ import { isArrow, transformArrow } from './arrow.js';
12
+ import { isBrushStroke, reshapeBrushStroke } from './brush.js';
13
+ import { rotatedBoxBounds, unionRects, worldToBoxLocal, clampNumber } from './geometry.js';
14
+ import { applyMatrixToPoint, boxToWorldMatrix, degreesToRadians, normalizeDegrees } from './matrix.js';
15
+ import { parsePathData, scalePathCommands, mirrorPathCommands, serializePathData } from './pathData.js';
16
+ import { regeneratedPathCommands } from './shapePaths.js';
17
+ import { MIN_OBJECT_SIZE } from './types.js';
18
+ const HANDLE_DIRECTIONS = {
19
+ nw: { x: -1, y: -1 },
20
+ n: { x: 0, y: -1 },
21
+ ne: { x: 1, y: -1 },
22
+ e: { x: 1, y: 0 },
23
+ se: { x: 1, y: 1 },
24
+ s: { x: 0, y: 1 },
25
+ sw: { x: -1, y: 1 },
26
+ w: { x: -1, y: 0 }
27
+ };
28
+ export function handleDirection(handle) {
29
+ return HANDLE_DIRECTIONS[handle];
30
+ }
31
+ /** The frame of a selection: the shape's own box for one, the upright rectangle around them for several. */
32
+ export function selectionFrameOf(shapes) {
33
+ if (shapes.length === 0)
34
+ return null;
35
+ if (shapes.length === 1) {
36
+ const [shape] = shapes;
37
+ return { x: shape.x, y: shape.y, width: shape.width, height: shape.height, rotation: shape.rotation };
38
+ }
39
+ const union = unionRects(shapes.map((shape) => rotatedBoxBounds(shape)));
40
+ return union ? { ...union, rotation: 0 } : null;
41
+ }
42
+ /** The centre of a frame in the world. */
43
+ export function frameCenter(frame) {
44
+ return { x: frame.x + frame.width / 2, y: frame.y + frame.height / 2 };
45
+ }
46
+ /**
47
+ * The frame that results from dragging `handle` to a world point.
48
+ * `frame` is the frame when the drag began (not the one the last pointer move produced).
49
+ */
50
+ export function computeResizedFrame(frame, handle, pointerWorld, options = {}) {
51
+ const minimumSize = options.minimumSize ?? MIN_OBJECT_SIZE;
52
+ const direction = HANDLE_DIRECTIONS[handle];
53
+ const local = worldToBoxLocal(frame, pointerWorld);
54
+ if (!local)
55
+ return { ...frame, flipX: false, flipY: false };
56
+ const { width, height } = frame;
57
+ // How far the handle's edge is from the anchor (the opposite edge, or the centre), signed: negative means
58
+ // the pointer has crossed the anchor.
59
+ const extentX = direction.x === 0
60
+ ? width
61
+ : options.fromCenter
62
+ ? (local.x - width / 2) * direction.x * 2
63
+ : (local.x - (direction.x > 0 ? 0 : width)) * direction.x;
64
+ const extentY = direction.y === 0
65
+ ? height
66
+ : options.fromCenter
67
+ ? (local.y - height / 2) * direction.y * 2
68
+ : (local.y - (direction.y > 0 ? 0 : height)) * direction.y;
69
+ let signedWidth = extentX;
70
+ let signedHeight = extentY;
71
+ if (options.keepAspect && width > 0 && height > 0) {
72
+ let scale;
73
+ if (direction.x !== 0 && direction.y !== 0) {
74
+ scale = Math.max(Math.abs(extentX) / width, Math.abs(extentY) / height);
75
+ }
76
+ else if (direction.x !== 0) {
77
+ scale = Math.abs(extentX) / width;
78
+ }
79
+ else {
80
+ scale = Math.abs(extentY) / height;
81
+ }
82
+ const smallestScale = minimumSize / Math.min(width, height);
83
+ if (!options.allowFlip || scale < smallestScale)
84
+ scale = Math.max(scale, smallestScale);
85
+ signedWidth = (direction.x === 0 || extentX >= 0 ? 1 : -1) * width * scale;
86
+ signedHeight = (direction.y === 0 || extentY >= 0 ? 1 : -1) * height * scale;
87
+ }
88
+ let flipX = false;
89
+ let flipY = false;
90
+ if (signedWidth < 0) {
91
+ if (options.allowFlip)
92
+ flipX = true;
93
+ else
94
+ signedWidth = minimumSize;
95
+ }
96
+ if (signedHeight < 0) {
97
+ if (options.allowFlip)
98
+ flipY = true;
99
+ else
100
+ signedHeight = minimumSize;
101
+ }
102
+ const newWidth = Math.max(minimumSize, Math.abs(signedWidth));
103
+ const newHeight = Math.max(minimumSize, Math.abs(signedHeight));
104
+ // The centre of the new box in the frame's own space: half an extent from the anchor, towards the handle.
105
+ const anchorX = direction.x > 0 ? 0 : width;
106
+ const anchorY = direction.y > 0 ? 0 : height;
107
+ const centerLocalX = direction.x === 0 || options.fromCenter ? width / 2 : anchorX + (direction.x * (flipX ? -newWidth : newWidth)) / 2;
108
+ const centerLocalY = direction.y === 0 || options.fromCenter ? height / 2 : anchorY + (direction.y * (flipY ? -newHeight : newHeight)) / 2;
109
+ const centerWorld = applyMatrixToPoint(boxToWorldMatrix(frame), { x: centerLocalX, y: centerLocalY });
110
+ return {
111
+ x: centerWorld.x - newWidth / 2,
112
+ y: centerWorld.y - newHeight / 2,
113
+ width: newWidth,
114
+ height: newHeight,
115
+ rotation: frame.rotation,
116
+ flipX,
117
+ flipY
118
+ };
119
+ }
120
+ /**
121
+ * Gives one shape a new box, rewriting what depends on its size: the path of a drawn shape is scaled (and
122
+ * mirrored when the box was flipped), the path of a generated shape is rebuilt, a brush stroke has its line
123
+ * scaled and its outline drawn again at the brush's own width, an arrow has its points moved with the box and
124
+ * is drawn again (its head keeps its size), text keeps its font size when it is resized from a side
125
+ * (wrapping) and scales it from a corner.
126
+ */
127
+ export function resizeShapeToBox(shape, box, flip = { flipX: false, flipY: false }, textScale = null) {
128
+ const width = Math.max(MIN_OBJECT_SIZE, box.width);
129
+ const height = Math.max(MIN_OBJECT_SIZE, box.height);
130
+ const rotation = box.rotation ?? shape.rotation;
131
+ const base = { ...shape, x: box.x, y: box.y, width, height, rotation };
132
+ switch (shape.type) {
133
+ case 'path': {
134
+ if (isArrow(shape)) {
135
+ // The points of the arrow go where the box goes (mirrored by a flip), and the arrow is drawn again from them.
136
+ const mapPoint = (point) => {
137
+ let unitX = shape.width > 0 ? (point.x - shape.x) / shape.width : 0.5;
138
+ let unitY = shape.height > 0 ? (point.y - shape.y) / shape.height : 0.5;
139
+ if (flip.flipX)
140
+ unitX = 1 - unitX;
141
+ if (flip.flipY)
142
+ unitY = 1 - unitY;
143
+ return { x: box.x + unitX * width, y: box.y + unitY * height };
144
+ };
145
+ return transformArrow(shape, mapPoint);
146
+ }
147
+ // A flip toggles the mirror the shape already has.
148
+ const flipX = shape.flipX !== flip.flipX;
149
+ const flipY = shape.flipY !== flip.flipY;
150
+ if (isBrushStroke(shape)) {
151
+ // The line goes where the box goes (and is mirrored by this flip); the brush stays as wide as it was.
152
+ const reshaped = reshapeBrushStroke(shape, { width, height, flipX: flip.flipX, flipY: flip.flipY });
153
+ return { ...base, d: reshaped.d, ink: reshaped.ink, flipX, flipY };
154
+ }
155
+ const regenerated = regeneratedPathCommands(shape, width, height);
156
+ let commands;
157
+ if (regenerated) {
158
+ // Rebuilt from its key, so the mirror the shape had is applied again.
159
+ commands = mirrorPathCommands(regenerated, width, height, flipX, flipY);
160
+ }
161
+ else {
162
+ // Scaled from the path it has, which already holds the earlier mirrors: only this flip is applied.
163
+ const scaleX = shape.width > 0 ? width / shape.width : 1;
164
+ const scaleY = shape.height > 0 ? height / shape.height : 1;
165
+ commands = mirrorPathCommands(scalePathCommands(parsePathData(shape.d), scaleX, scaleY), width, height, flip.flipX, flip.flipY);
166
+ }
167
+ return { ...base, d: serializePathData(commands), flipX, flipY };
168
+ }
169
+ case 'text': {
170
+ const text = shape;
171
+ if (textScale !== null && textScale > 0) {
172
+ // A corner drag scales the letters with the box.
173
+ return { ...base, fontSize: clampNumber(text.fontSize * textScale, 4, 600) };
174
+ }
175
+ // A side drag only sets the width the text wraps at.
176
+ return { ...base, autoWidth: false };
177
+ }
178
+ default:
179
+ return base;
180
+ }
181
+ }
182
+ /**
183
+ * Maps shapes from the frame they had when the drag began onto the frame the drag produced.
184
+ * A single shape takes the new box as it is; several shapes keep their place inside the frame (their
185
+ * centres move with it, their sizes scale with it, along their own axes).
186
+ */
187
+ export function applyFrameResize(shapes, startFrame, resized, isCornerHandle) {
188
+ if (shapes.length === 1) {
189
+ const [shape] = shapes;
190
+ const cornerTextScale = shape.type === 'text' && isCornerHandle && startFrame.width > 0 ? resized.width / startFrame.width : null;
191
+ return [
192
+ resizeShapeToBox(shape, resized, { flipX: resized.flipX, flipY: resized.flipY }, cornerTextScale)
193
+ ];
194
+ }
195
+ const scaleX = startFrame.width > 0 ? resized.width / startFrame.width : 1;
196
+ const scaleY = startFrame.height > 0 ? resized.height / startFrame.height : 1;
197
+ const mapPoint = (point) => {
198
+ let unitX = startFrame.width > 0 ? (point.x - startFrame.x) / startFrame.width : 0.5;
199
+ let unitY = startFrame.height > 0 ? (point.y - startFrame.y) / startFrame.height : 0.5;
200
+ if (resized.flipX)
201
+ unitX = 1 - unitX;
202
+ if (resized.flipY)
203
+ unitY = 1 - unitY;
204
+ return { x: resized.x + unitX * resized.width, y: resized.y + unitY * resized.height };
205
+ };
206
+ return shapes.map((shape) => {
207
+ const radians = degreesToRadians(shape.rotation);
208
+ const cosine = Math.cos(radians);
209
+ const sine = Math.sin(radians);
210
+ // The shape's own axes are stretched by the frame; the lengths along them give its new size.
211
+ const widthFactor = Math.hypot(scaleX * cosine, scaleY * sine);
212
+ const heightFactor = Math.hypot(scaleX * sine, scaleY * cosine);
213
+ const newWidth = Math.max(MIN_OBJECT_SIZE, shape.width * widthFactor);
214
+ const newHeight = Math.max(MIN_OBJECT_SIZE, shape.height * heightFactor);
215
+ const center = mapPoint({ x: shape.x + shape.width / 2, y: shape.y + shape.height / 2 });
216
+ // A mirrored shape turns the other way.
217
+ const mirrors = (resized.flipX ? 1 : 0) + (resized.flipY ? 1 : 0);
218
+ const rotation = mirrors % 2 === 1 ? normalizeDegrees(-shape.rotation) : shape.rotation;
219
+ const box = { x: center.x - newWidth / 2, y: center.y - newHeight / 2, width: newWidth, height: newHeight, rotation };
220
+ const textScale = shape.type === 'text' ? (widthFactor + heightFactor) / 2 : null;
221
+ return resizeShapeToBox(shape, box, { flipX: resized.flipX, flipY: resized.flipY }, textScale);
222
+ });
223
+ }
224
+ /**
225
+ * Turns shapes about a point by an angle: each centre goes round the point and each shape turns with it. An arrow does not
226
+ * turn (it is always upright): its points go round the point and it is drawn again between them.
227
+ */
228
+ export function rotateShapesAbout(shapes, pivot, deltaDegrees) {
229
+ const radians = degreesToRadians(deltaDegrees);
230
+ const cosine = Math.cos(radians);
231
+ const sine = Math.sin(radians);
232
+ return shapes.map((shape) => {
233
+ if (isArrow(shape)) {
234
+ return transformArrow(shape, (point) => ({
235
+ x: pivot.x + (point.x - pivot.x) * cosine - (point.y - pivot.y) * sine,
236
+ y: pivot.y + (point.x - pivot.x) * sine + (point.y - pivot.y) * cosine
237
+ }));
238
+ }
239
+ const centerX = shape.x + shape.width / 2 - pivot.x;
240
+ const centerY = shape.y + shape.height / 2 - pivot.y;
241
+ const rotatedX = pivot.x + centerX * cosine - centerY * sine;
242
+ const rotatedY = pivot.y + centerX * sine + centerY * cosine;
243
+ return {
244
+ ...shape,
245
+ x: rotatedX - shape.width / 2,
246
+ y: rotatedY - shape.height / 2,
247
+ rotation: normalizeDegrees(shape.rotation + deltaDegrees)
248
+ };
249
+ });
250
+ }
251
+ /** The angle, in degrees, of the line from `center` to `point` (0 is to the right, growing clockwise on screen). */
252
+ export function angleFromCenter(center, point) {
253
+ return (Math.atan2(point.y - center.y, point.x - center.x) * 180) / Math.PI;
254
+ }
255
+ /** Rounds an angle to the nearest multiple of `step`. */
256
+ export function snapAngle(degrees, step) {
257
+ return Math.round(degrees / step) * step;
258
+ }