@widgetic/canvas 0.5.34 → 0.5.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) 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/autosaveScheduler.d.ts +58 -0
  5. package/dist/canvas/autosaveScheduler.js +157 -0
  6. package/dist/canvas/boardFrame.d.ts +13 -0
  7. package/dist/canvas/boardFrame.js +13 -0
  8. package/dist/canvas/boardSurface.d.ts +9 -0
  9. package/dist/canvas/boardSurface.js +9 -0
  10. package/dist/canvas/chromeLayout.d.ts +76 -0
  11. package/dist/canvas/chromeLayout.js +121 -0
  12. package/dist/canvas/emptyBoardHint.d.ts +17 -0
  13. package/dist/canvas/emptyBoardHint.js +12 -0
  14. package/dist/canvas/eventScope.d.ts +1 -1
  15. package/dist/canvas/eventScope.js +1 -1
  16. package/dist/canvas/host/CanvasHost.d.ts +351 -0
  17. package/dist/canvas/host/CanvasHost.js +1740 -0
  18. package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
  19. package/dist/canvas/host/CanvasHostApi.js +95 -0
  20. package/dist/canvas/host/browserServices.d.ts +46 -0
  21. package/dist/canvas/host/browserServices.js +167 -0
  22. package/dist/canvas/host/conversion.d.ts +51 -0
  23. package/dist/canvas/host/conversion.js +123 -0
  24. package/dist/canvas/host/hostObject.d.ts +40 -0
  25. package/dist/canvas/host/hostObject.js +93 -0
  26. package/dist/canvas/host/pageEvents.d.ts +24 -0
  27. package/dist/canvas/host/pageEvents.js +35 -0
  28. package/dist/canvas/host/screenRects.d.ts +66 -0
  29. package/dist/canvas/host/screenRects.js +102 -0
  30. package/dist/canvas/host/strokeSnap.d.ts +67 -0
  31. package/dist/canvas/host/strokeSnap.js +165 -0
  32. package/dist/canvas/index.d.ts +9 -7
  33. package/dist/canvas/index.js +9 -7
  34. package/dist/canvas/openMarks.d.ts +17 -0
  35. package/dist/canvas/openMarks.js +26 -0
  36. package/dist/canvas/panZoomPosition.d.ts +15 -0
  37. package/dist/canvas/panZoomPosition.js +16 -0
  38. package/dist/canvas/preferredTextFont.d.ts +12 -0
  39. package/dist/canvas/preferredTextFont.js +38 -0
  40. package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
  41. package/dist/canvas/shapes/ShapeLibrary.js +13 -1
  42. package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
  43. package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
  44. package/dist/canvas/toolbarPosition.d.ts +24 -0
  45. package/dist/canvas/toolbarPosition.js +24 -0
  46. package/dist/canvas/types.d.ts +14 -0
  47. package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
  48. package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
  49. package/dist/canvas/ui/CanvasHint.svelte +20 -0
  50. package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
  51. package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
  52. package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
  53. package/dist/canvas/ui/ColorField.svelte +155 -0
  54. package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
  55. package/dist/canvas/ui/LoadNotice.svelte +39 -0
  56. package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
  57. package/dist/canvas/ui/SelectionPanel.svelte +300 -0
  58. package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
  59. package/dist/canvas/ui/SelectorBar.svelte +82 -0
  60. package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
  61. package/dist/canvas/ui/ShapesMenu.svelte +185 -0
  62. package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
  63. package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
  64. package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
  65. package/dist/canvas/ui/ToolButton.svelte +78 -0
  66. package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
  67. package/dist/canvas/ui/WidgetActions.svelte +152 -0
  68. package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
  69. package/dist/canvas/ui/ZoomControl.svelte +89 -0
  70. package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
  71. package/dist/canvas/ui/colorModel.d.ts +18 -0
  72. package/dist/canvas/ui/colorModel.js +85 -0
  73. package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
  74. package/dist/canvas/ui/colorPickerMessages.js +15 -0
  75. package/dist/canvas/ui/floatingCard.d.ts +107 -0
  76. package/dist/canvas/ui/floatingCard.js +107 -0
  77. package/dist/canvas/ui/floatingLayer.d.ts +21 -0
  78. package/dist/canvas/ui/floatingLayer.js +52 -0
  79. package/dist/canvas/ui/icons.d.ts +12 -0
  80. package/dist/canvas/ui/icons.js +7 -0
  81. package/dist/canvas/ui/keyHints.d.ts +27 -0
  82. package/dist/canvas/ui/keyHints.js +43 -0
  83. package/dist/canvas/ui/popoverSide.d.ts +5 -0
  84. package/dist/canvas/ui/popoverSide.js +20 -0
  85. package/dist/canvas/ui/selectionTraits.d.ts +61 -0
  86. package/dist/canvas/ui/selectionTraits.js +99 -0
  87. package/dist/canvas/view/CanvasStage.svelte +132 -0
  88. package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
  89. package/dist/canvas/view/EraserTrail.svelte +50 -0
  90. package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
  91. package/dist/canvas/view/ObjectView.svelte +245 -0
  92. package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
  93. package/dist/canvas/view/SelectionOverlay.svelte +487 -0
  94. package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
  95. package/dist/canvas/view/TextEditor.svelte +76 -0
  96. package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
  97. package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
  98. package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
  99. package/dist/canvas/view/erasingMarks.d.ts +13 -0
  100. package/dist/canvas/view/erasingMarks.js +34 -0
  101. package/dist/canvas/view/geometry.d.ts +169 -0
  102. package/dist/canvas/view/geometry.js +272 -0
  103. package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
  104. package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
  105. package/dist/components/index.js +1 -1
  106. package/dist/components/input-controls/ColorIC.svelte +2 -2
  107. package/dist/engine/EngineCore.d.ts +273 -0
  108. package/dist/engine/EngineCore.js +1051 -0
  109. package/dist/engine/codec/canvasDocument.d.ts +57 -0
  110. package/dist/engine/codec/canvasDocument.js +471 -0
  111. package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
  112. package/dist/engine/codec/hostReadableKeys.js +25 -0
  113. package/dist/engine/core/arrange.d.ts +20 -0
  114. package/dist/engine/core/arrange.js +81 -0
  115. package/dist/engine/core/arrow.d.ts +91 -0
  116. package/dist/engine/core/arrow.js +182 -0
  117. package/dist/engine/core/brush.d.ts +68 -0
  118. package/dist/engine/core/brush.js +173 -0
  119. package/dist/engine/core/defaults.d.ts +70 -0
  120. package/dist/engine/core/defaults.js +158 -0
  121. package/dist/engine/core/freehand.d.ts +130 -0
  122. package/dist/engine/core/freehand.js +430 -0
  123. package/dist/engine/core/geometry.d.ts +44 -0
  124. package/dist/engine/core/geometry.js +216 -0
  125. package/dist/engine/core/history.d.ts +39 -0
  126. package/dist/engine/core/history.js +69 -0
  127. package/dist/engine/core/hitTest.d.ts +54 -0
  128. package/dist/engine/core/hitTest.js +298 -0
  129. package/dist/engine/core/matrix.d.ts +47 -0
  130. package/dist/engine/core/matrix.js +101 -0
  131. package/dist/engine/core/pathData.d.ts +50 -0
  132. package/dist/engine/core/pathData.js +550 -0
  133. package/dist/engine/core/rotationReset.d.ts +10 -0
  134. package/dist/engine/core/rotationReset.js +17 -0
  135. package/dist/engine/core/scene.d.ts +99 -0
  136. package/dist/engine/core/scene.js +450 -0
  137. package/dist/engine/core/scribble.d.ts +64 -0
  138. package/dist/engine/core/scribble.js +121 -0
  139. package/dist/engine/core/shapePaths.d.ts +34 -0
  140. package/dist/engine/core/shapePaths.js +69 -0
  141. package/dist/engine/core/shapeTools.d.ts +80 -0
  142. package/dist/engine/core/shapeTools.js +161 -0
  143. package/dist/engine/core/snapping.d.ts +64 -0
  144. package/dist/engine/core/snapping.js +164 -0
  145. package/dist/engine/core/style.d.ts +40 -0
  146. package/dist/engine/core/style.js +151 -0
  147. package/dist/engine/core/transform.d.ts +79 -0
  148. package/dist/engine/core/transform.js +258 -0
  149. package/dist/engine/core/types.d.ts +238 -0
  150. package/dist/engine/core/types.js +31 -0
  151. package/dist/engine/core/viewport.d.ts +32 -0
  152. package/dist/engine/core/viewport.js +84 -0
  153. package/dist/engine/input/PointerController.d.ts +38 -0
  154. package/dist/engine/input/PointerController.js +545 -0
  155. package/dist/engine/input/arrowHandles.d.ts +38 -0
  156. package/dist/engine/input/arrowHandles.js +60 -0
  157. package/dist/engine/input/gestures.d.ts +79 -0
  158. package/dist/engine/input/gestures.js +769 -0
  159. package/dist/engine/input/hoverCursor.d.ts +29 -0
  160. package/dist/engine/input/hoverCursor.js +23 -0
  161. package/dist/engine/input/keyboard.d.ts +75 -0
  162. package/dist/engine/input/keyboard.js +264 -0
  163. package/dist/engine/input/selectionHandles.d.ts +89 -0
  164. package/dist/engine/input/selectionHandles.js +169 -0
  165. package/dist/engine/input/types.d.ts +82 -0
  166. package/dist/engine/input/types.js +5 -0
  167. package/dist/engine/render/rasterize.d.ts +74 -0
  168. package/dist/engine/render/rasterize.js +273 -0
  169. package/dist/engine/state.d.ts +124 -0
  170. package/dist/engine/state.js +47 -0
  171. package/dist/engine/text/layoutText.d.ts +53 -0
  172. package/dist/engine/text/layoutText.js +170 -0
  173. package/dist/engine/widgets.d.ts +124 -0
  174. package/dist/engine/widgets.js +345 -0
  175. package/dist/i18n/labels.d.ts +4 -15
  176. package/dist/i18n/labels.js +33 -53
  177. package/dist/i18n/language.d.ts +3 -3
  178. package/dist/i18n/messages.d.ts +51 -122
  179. package/dist/i18n/messages.js +58 -131
  180. package/dist/i18n/shortcuts.d.ts +0 -1
  181. package/dist/i18n/shortcuts.js +2 -11
  182. package/dist/index.d.ts +6 -2
  183. package/dist/index.js +6 -2
  184. package/package.json +30 -5
  185. package/dist/canvas/Canvas.svelte +0 -13299
  186. package/dist/canvas/Canvas.svelte.d.ts +0 -281
  187. package/dist/canvas/CanvasToolbar.svelte +0 -1498
  188. package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
  189. package/dist/canvas/ContextMenu.svelte +0 -305
  190. package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
  191. package/dist/canvas/PanZoomPanel.svelte +0 -478
  192. package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
  193. package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
  194. package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
  195. package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
  196. package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
  197. package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
  198. package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
  199. package/dist/canvas/shadowRootText.d.ts +0 -5
  200. package/dist/canvas/shadowRootText.js +0 -43
  201. package/dist/canvas/shapes/FrameShape.d.ts +0 -97
  202. package/dist/canvas/shapes/FrameShape.js +0 -953
  203. package/dist/canvas/shapes/ImageShape.d.ts +0 -47
  204. package/dist/canvas/shapes/ImageShape.js +0 -339
  205. package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
  206. package/dist/canvas/shapes/WidgetShape.js +0 -200
  207. package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
  208. package/dist/canvas/widgetPreviewGeometry.js +0 -66
  209. package/dist/i18n/shapeWords.d.ts +0 -30
  210. package/dist/i18n/shapeWords.js +0 -36
  211. package/dist/i18n/slots.d.ts +0 -20
  212. package/dist/i18n/slots.js +0 -40
@@ -0,0 +1,430 @@
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
+ // How fast a simulated pressure follows the speed of the hand.
23
+ const RATE_OF_PRESSURE_CHANGE = 0.275;
24
+ // A half turn that is a hair more than π: a cap built from exactly π leaves a visible seam in browsers.
25
+ const HALF_TURN = Math.PI + 0.0001;
26
+ // The outline of a corner is built from this many steps of a half turn, and a round end cap from this many of one and a half.
27
+ const CORNER_STEPS = 13;
28
+ const END_CAP_STEPS = 29;
29
+ // A turn sharper than this (the dot product of two directions) is a corner that gets a round join.
30
+ const SHARP_CORNER = 0;
31
+ // The end of a line within this distance of its last point is dropped from the outline: it is only noise.
32
+ const END_NOISE_DISTANCE = 3;
33
+ // ───────────────────────────────────────────────────────────────────────── easings
34
+ export const EASE_LINEAR = (progress) => progress;
35
+ export const EASE_OUT_SINE = (progress) => Math.sin((progress * Math.PI) / 2);
36
+ /** The easing of a pen's pressure: mostly linear, with a little of the sine's softness. */
37
+ export const EASE_PEN = (progress) => progress * 0.65 + Math.sin((progress * Math.PI) / 2) * 0.35;
38
+ const EASE_OUT_QUADRATIC = (progress) => progress * (2 - progress);
39
+ const EASE_OUT_CUBIC = (progress) => (progress - 1) * (progress - 1) * (progress - 1) + 1;
40
+ // ───────────────────────────────────────────────────────────────────────── the line of points
41
+ /** The samples that can be used: finite coordinates, and a pressure that is either 0 to 1 or negative for unknown. */
42
+ function usableSamples(samples) {
43
+ const usable = [];
44
+ for (const sample of samples) {
45
+ if (!Number.isFinite(sample.x) || !Number.isFinite(sample.y))
46
+ continue;
47
+ const pressure = sample.pressure !== undefined && Number.isFinite(sample.pressure) && sample.pressure >= 0 ? Math.min(1, sample.pressure) : -1;
48
+ usable.push({ x: sample.x, y: sample.y, pressure });
49
+ }
50
+ return usable;
51
+ }
52
+ /**
53
+ * Smooths the samples into the points the outline is built on. Each point moves only a part of the way to the next sample
54
+ * (that is the `streamline`, and why the line trails the hand a little), the first stretch of the line, shorter than `size`,
55
+ * is dropped because it only makes the start wobble, and every point knows its distance from the one before and from the start.
56
+ * A finished line (`last`) ends exactly on the last sample.
57
+ */
58
+ export function strokePointsOf(samples, options) {
59
+ const size = options.size;
60
+ const streamline = options.streamline ?? 0.5;
61
+ const finished = options.last === true;
62
+ let line = usableSamples(samples);
63
+ if (line.length === 0)
64
+ return [];
65
+ // How far a point moves towards the next sample: 1 would follow the hand exactly.
66
+ const follow = 0.15 + (1 - streamline) * 0.85;
67
+ if (line.length === 2) {
68
+ // Two samples are a straight line: points are put between them so that it is drawn as a longer line is.
69
+ const [first, second] = line;
70
+ line = [first];
71
+ for (let step = 1; step < 5; step += 1) {
72
+ const progress = step / 4;
73
+ line.push({
74
+ x: first.x + (second.x - first.x) * progress,
75
+ y: first.y + (second.y - first.y) * progress,
76
+ pressure: first.pressure >= 0 && second.pressure >= 0 ? first.pressure + (second.pressure - first.pressure) * progress : -1
77
+ });
78
+ }
79
+ }
80
+ if (line.length === 1) {
81
+ // A click is a dot: a second point a pixel away gives it a direction.
82
+ line = [line[0], { ...line[0], x: line[0].x + 1, y: line[0].y + 1 }];
83
+ }
84
+ const points = [
85
+ { x: line[0].x, y: line[0].y, pressure: line[0].pressure >= 0 ? line[0].pressure : 0.25, vectorX: 1, vectorY: 1, distance: 0, runningLength: 0 }
86
+ ];
87
+ let reachedMinimumLength = false;
88
+ let runningLength = 0;
89
+ let previous = points[0];
90
+ const lastIndex = line.length - 1;
91
+ for (let index = 1; index < line.length; index += 1) {
92
+ const sample = line[index];
93
+ const atEnd = finished && index === lastIndex;
94
+ const x = atEnd ? sample.x : previous.x + (sample.x - previous.x) * follow;
95
+ const y = atEnd ? sample.y : previous.y + (sample.y - previous.y) * follow;
96
+ if (x === previous.x && y === previous.y)
97
+ continue;
98
+ const distance = Math.hypot(x - previous.x, y - previous.y);
99
+ runningLength += distance;
100
+ // The start of the line, shorter than the brush is wide, only makes it wobble.
101
+ if (index < lastIndex && !reachedMinimumLength) {
102
+ if (runningLength < size)
103
+ continue;
104
+ reachedMinimumLength = true;
105
+ }
106
+ const next = {
107
+ x,
108
+ y,
109
+ pressure: sample.pressure >= 0 ? sample.pressure : 0.5,
110
+ vectorX: (previous.x - x) / distance,
111
+ vectorY: (previous.y - y) / distance,
112
+ distance,
113
+ runningLength
114
+ };
115
+ points.push(next);
116
+ previous = next;
117
+ }
118
+ // The first point looks the way the second one does.
119
+ points[0].vectorX = points.length > 1 ? points[1].vectorX : 0;
120
+ points[0].vectorY = points.length > 1 ? points[1].vectorY : 0;
121
+ return points;
122
+ }
123
+ // ───────────────────────────────────────────────────────────────────────── the outline
124
+ /** The radius for a pressure: the width at an even pressure of one half, swelling above it and thinning below it. */
125
+ function radiusFor(size, thinning, pressure, easing) {
126
+ return size * easing(0.5 - thinning * (0.5 - pressure));
127
+ }
128
+ /** A pressure that follows the speed of the hand: a point close to the one before presses, a far one lifts. */
129
+ function simulatedPressure(previousPressure, distance, size) {
130
+ // How fast the hand moves, in brush widths per point, and what is left of the stillness.
131
+ const speed = Math.min(1, distance / size);
132
+ const stillness = Math.min(1, 1 - speed);
133
+ return Math.min(1, previousPressure + (stillness - previousPressure) * (speed * RATE_OF_PRESSURE_CHANGE));
134
+ }
135
+ function rotateAbout(point, center, angle) {
136
+ const cosine = Math.cos(angle);
137
+ const sine = Math.sin(angle);
138
+ const offsetX = point.x - center.x;
139
+ const offsetY = point.y - center.y;
140
+ return { x: center.x + offsetX * cosine - offsetY * sine, y: center.y + offsetX * sine + offsetY * cosine };
141
+ }
142
+ /** The length a tapered end narrows over. */
143
+ function taperLength(taper, size, totalLength) {
144
+ if (taper === undefined || taper === false)
145
+ return 0;
146
+ if (taper === true)
147
+ return Math.max(size, totalLength);
148
+ return Math.max(0, taper);
149
+ }
150
+ /**
151
+ * The polygon that surrounds a line of smoothed points: up the left side, round the end, back down the right side and round
152
+ * the start. The points come from `strokePointsOf` with the same options.
153
+ */
154
+ export function outlineOf(points, options) {
155
+ return outlineWithPressures(points, options).outline;
156
+ }
157
+ /**
158
+ * The same, with the pressure of every point as well. A line whose pressure was worked out from the speed can be drawn again
159
+ * from these (with `simulatePressure` off) and come out the same, whatever size its points are later scaled to.
160
+ */
161
+ export function outlineWithPressures(points, options) {
162
+ const size = options.size;
163
+ const thinning = options.thinning ?? 0.5;
164
+ const smoothing = options.smoothing ?? 0.5;
165
+ const simulate = options.simulatePressure ?? true;
166
+ const easing = options.easing ?? EASE_LINEAR;
167
+ const finished = options.last === true;
168
+ const startCap = options.start?.cap ?? true;
169
+ const endCap = options.end?.cap ?? true;
170
+ const startEasing = options.start?.easing ?? EASE_OUT_QUADRATIC;
171
+ const endEasing = options.end?.easing ?? EASE_OUT_CUBIC;
172
+ if (points.length === 0 || !(size > 0))
173
+ return { outline: [], pressures: [] };
174
+ const pressures = points.map((point) => point.pressure);
175
+ const totalLength = points[points.length - 1].runningLength;
176
+ const taperStart = taperLength(options.start?.taper, size, totalLength);
177
+ const taperEnd = taperLength(options.end?.taper, size, totalLength);
178
+ // The least distance between two points of one side of the outline (squared): closer points add a wobble and nothing else.
179
+ const minimumGapSquared = (size * smoothing) ** 2;
180
+ const left = [];
181
+ const right = [];
182
+ // The pressure the line starts from: the mean of its first ten points.
183
+ let previousPressure = points[0].pressure;
184
+ for (const point of points.slice(0, 10)) {
185
+ const pressure = simulate ? simulatedPressure(previousPressure, point.distance, size) : point.pressure;
186
+ previousPressure = (previousPressure + pressure) / 2;
187
+ }
188
+ let radius = radiusFor(size, thinning, points[points.length - 1].pressure, easing);
189
+ let firstRadius;
190
+ let previousVectorX = points[0].vectorX;
191
+ let previousVectorY = points[0].vectorY;
192
+ let previousLeft = { x: points[0].x, y: points[0].y };
193
+ let previousRight = previousLeft;
194
+ let previousWasSharpCorner = false;
195
+ for (let index = 0; index < points.length; index += 1) {
196
+ const point = points[index];
197
+ let pressure = point.pressure;
198
+ const isLast = index === points.length - 1;
199
+ // The very end of a line is noise: leave it out and let the last point finish the line.
200
+ if (!isLast && totalLength - point.runningLength < END_NOISE_DISTANCE) {
201
+ pressures[index] = previousPressure;
202
+ continue;
203
+ }
204
+ // The radius: half the size for a line of one width, otherwise from the pressure (read, or worked out from the speed).
205
+ if (thinning) {
206
+ if (simulate)
207
+ pressure = simulatedPressure(previousPressure, point.distance, size);
208
+ radius = radiusFor(size, thinning, pressure, easing);
209
+ }
210
+ else {
211
+ radius = size / 2;
212
+ }
213
+ pressures[index] = pressure;
214
+ if (firstRadius === undefined)
215
+ firstRadius = radius;
216
+ // A tapered end narrows over its length, and the narrower of the two ends wins where they meet.
217
+ const startTaper = point.runningLength < taperStart ? startEasing(point.runningLength / taperStart) : 1;
218
+ const endTaper = totalLength - point.runningLength < taperEnd ? endEasing((totalLength - point.runningLength) / taperEnd) : 1;
219
+ radius = Math.max(0.01, radius * Math.min(startTaper, endTaper));
220
+ const next = isLast ? point : points[index + 1];
221
+ const nextDot = isLast ? 1 : point.vectorX * next.vectorX + point.vectorY * next.vectorY;
222
+ const previousDot = point.vectorX * previousVectorX + point.vectorY * previousVectorY;
223
+ const isSharpCorner = previousDot < SHARP_CORNER && !previousWasSharpCorner;
224
+ const nextIsSharpCorner = nextDot < SHARP_CORNER;
225
+ if (isSharpCorner || nextIsSharpCorner) {
226
+ // A sharp corner is a half turn round the point, on both sides, and the line goes on from there.
227
+ const offsetX = previousVectorY * radius;
228
+ const offsetY = -previousVectorX * radius;
229
+ let lastLeft = previousLeft;
230
+ let lastRight = previousRight;
231
+ for (let step = 0; step <= CORNER_STEPS; step += 1) {
232
+ const turn = HALF_TURN * (step / CORNER_STEPS);
233
+ lastLeft = rotateAbout({ x: point.x - offsetX, y: point.y - offsetY }, point, turn);
234
+ left.push(lastLeft);
235
+ lastRight = rotateAbout({ x: point.x + offsetX, y: point.y + offsetY }, point, -turn);
236
+ right.push(lastRight);
237
+ }
238
+ previousLeft = lastLeft;
239
+ previousRight = lastRight;
240
+ if (nextIsSharpCorner)
241
+ previousWasSharpCorner = true;
242
+ continue;
243
+ }
244
+ previousWasSharpCorner = false;
245
+ if (isLast) {
246
+ // The last point: straight out to both sides of the way the line was going.
247
+ const offsetX = point.vectorY * radius;
248
+ const offsetY = -point.vectorX * radius;
249
+ left.push({ x: point.x - offsetX, y: point.y - offsetY });
250
+ right.push({ x: point.x + offsetX, y: point.y + offsetY });
251
+ continue;
252
+ }
253
+ // An ordinary point: out to both sides along the middle of the way in and the way out.
254
+ const middleX = next.vectorX + (point.vectorX - next.vectorX) * nextDot;
255
+ const middleY = next.vectorY + (point.vectorY - next.vectorY) * nextDot;
256
+ const offsetX = middleY * radius;
257
+ const offsetY = -middleX * radius;
258
+ const leftPoint = { x: point.x - offsetX, y: point.y - offsetY };
259
+ if (index <= 1 || (leftPoint.x - previousLeft.x) ** 2 + (leftPoint.y - previousLeft.y) ** 2 > minimumGapSquared) {
260
+ left.push(leftPoint);
261
+ previousLeft = leftPoint;
262
+ }
263
+ const rightPoint = { x: point.x + offsetX, y: point.y + offsetY };
264
+ if (index <= 1 || (rightPoint.x - previousRight.x) ** 2 + (rightPoint.y - previousRight.y) ** 2 > minimumGapSquared) {
265
+ right.push(rightPoint);
266
+ previousRight = rightPoint;
267
+ }
268
+ previousPressure = pressure;
269
+ previousVectorX = point.vectorX;
270
+ previousVectorY = point.vectorY;
271
+ }
272
+ const first = { x: points[0].x, y: points[0].y };
273
+ const last = points.length > 1 ? { x: points[points.length - 1].x, y: points[points.length - 1].y } : { x: points[0].x + 1, y: points[0].y + 1 };
274
+ const startCapPoints = [];
275
+ const endCapPoints = [];
276
+ if (points.length === 1) {
277
+ // A lone point is a dot, unless its taper would make it nothing and the stroke is not finished.
278
+ if (!(taperStart || taperEnd) || finished) {
279
+ const around = { x: first.x - last.x, y: first.y - last.y };
280
+ const length = Math.hypot(around.x, around.y) || 1;
281
+ const dotRadius = firstRadius ?? radius;
282
+ // A point a radius away, at right angles to the (arbitrary) direction of the dot, turned a full circle.
283
+ const start = { x: first.x - (around.y / length) * dotRadius, y: first.y + (around.x / length) * dotRadius };
284
+ const dot = [];
285
+ for (let step = 1; step <= CORNER_STEPS; step += 1)
286
+ dot.push(rotateAbout(start, first, HALF_TURN * 2 * (step / CORNER_STEPS)));
287
+ return { outline: dot, pressures };
288
+ }
289
+ }
290
+ else {
291
+ if (taperStart || (taperEnd && points.length === 1)) {
292
+ // A tapered start comes to a point and has no cap.
293
+ }
294
+ else if (startCap) {
295
+ // A round cap: the right side's first point turned half a circle about the start, to the left side's.
296
+ for (let step = 1; step <= CORNER_STEPS; step += 1)
297
+ startCapPoints.push(rotateAbout(right[0], first, HALF_TURN * (step / CORNER_STEPS)));
298
+ }
299
+ else {
300
+ const cornersX = left[0].x - right[0].x;
301
+ const cornersY = left[0].y - right[0].y;
302
+ startCapPoints.push({ x: first.x - cornersX * 0.5, y: first.y - cornersY * 0.5 }, { x: first.x - cornersX * 0.51, y: first.y - cornersY * 0.51 }, { x: first.x + cornersX * 0.51, y: first.y + cornersY * 0.51 }, { x: first.x + cornersX * 0.5, y: first.y + cornersY * 0.5 });
303
+ }
304
+ const lastPoint = points[points.length - 1];
305
+ // At right angles to the way the line was going when it ended (its vector points back along it).
306
+ const directionX = -lastPoint.vectorY;
307
+ const directionY = lastPoint.vectorX;
308
+ if (taperEnd || (taperStart && points.length === 1)) {
309
+ endCapPoints.push(last);
310
+ }
311
+ else if (endCap) {
312
+ // A round end cap goes a turn and a half round the last point, so that a sharp turn at the end is not cut short.
313
+ const start = { x: last.x + directionX * radius, y: last.y + directionY * radius };
314
+ for (let step = 1; step < END_CAP_STEPS; step += 1)
315
+ endCapPoints.push(rotateAbout(start, last, HALF_TURN * 3 * (step / END_CAP_STEPS)));
316
+ }
317
+ else {
318
+ endCapPoints.push({ x: last.x + directionX * radius, y: last.y + directionY * radius }, { x: last.x + directionX * radius * 0.99, y: last.y + directionY * radius * 0.99 }, { x: last.x - directionX * radius * 0.99, y: last.y - directionY * radius * 0.99 }, { x: last.x - directionX * radius, y: last.y - directionY * radius });
319
+ }
320
+ }
321
+ // Up the left, round the end, down the right, round the start.
322
+ return { outline: left.concat(endCapPoints, right.reverse(), startCapPoints), pressures };
323
+ }
324
+ /**
325
+ * Points that are already smooth (the line a stroke was stored as) with what the outline is made from: the way back along the
326
+ * line, and the distance from the point before and from the start. Nothing is smoothed again, so the line is exactly the one
327
+ * given; a point on the one before it adds nothing and is left out.
328
+ */
329
+ export function strokePointsOfLine(line) {
330
+ const points = [];
331
+ let runningLength = 0;
332
+ for (const sample of usableSamples(line)) {
333
+ const pressure = sample.pressure >= 0 ? sample.pressure : 0.5;
334
+ const previous = points[points.length - 1];
335
+ if (!previous) {
336
+ points.push({ x: sample.x, y: sample.y, pressure, vectorX: 1, vectorY: 1, distance: 0, runningLength: 0 });
337
+ continue;
338
+ }
339
+ const distance = Math.hypot(sample.x - previous.x, sample.y - previous.y);
340
+ if (distance === 0)
341
+ continue;
342
+ runningLength += distance;
343
+ points.push({
344
+ x: sample.x,
345
+ y: sample.y,
346
+ pressure,
347
+ vectorX: (previous.x - sample.x) / distance,
348
+ vectorY: (previous.y - sample.y) / distance,
349
+ distance,
350
+ runningLength
351
+ });
352
+ }
353
+ // The first point looks the way the second one does.
354
+ if (points.length > 1) {
355
+ points[0].vectorX = points[1].vectorX;
356
+ points[0].vectorY = points[1].vectorY;
357
+ }
358
+ else if (points.length === 1) {
359
+ points[0].vectorX = 0;
360
+ points[0].vectorY = 0;
361
+ }
362
+ return points;
363
+ }
364
+ /** The outline of a line of samples: smoothed, then drawn round. */
365
+ export function freehandOutline(samples, options) {
366
+ return outlineOf(strokePointsOf(samples, options), options);
367
+ }
368
+ // ───────────────────────────────────────────────────────────────────────── as path data
369
+ const FIXED_DECIMALS = 2;
370
+ const fixed = (value) => (Math.round(value * 10 ** FIXED_DECIMALS) / 10 ** FIXED_DECIMALS).toString();
371
+ /** The path data of a circle: two arcs, so that it fills as a dot. */
372
+ export function circlePathData(center, radius) {
373
+ const r = Math.max(radius, 0.01);
374
+ return `M${fixed(center.x - r)},${fixed(center.y)}a${fixed(r)},${fixed(r)} 0 1,0 ${fixed(r * 2)},0a${fixed(r)},${fixed(r)} 0 1,0 ${fixed(-r * 2)},0Z`;
375
+ }
376
+ /**
377
+ * The path data of a closed outline, smoothed: a quadratic curve through the midpoints of the polygon's sides, with the
378
+ * polygon's corners as the control points. Fewer than four points are no outline, and give an empty string.
379
+ */
380
+ export function outlinePathData(outline) {
381
+ if (outline.length < 4)
382
+ return '';
383
+ const [first, second, third] = outline;
384
+ let data = `M${fixed(first.x)},${fixed(first.y)}Q${fixed(second.x)},${fixed(second.y)} ${fixed((second.x + third.x) / 2)},${fixed((second.y + third.y) / 2)}T`;
385
+ for (let index = 2; index < outline.length - 1; index += 1) {
386
+ const current = outline[index];
387
+ const next = outline[index + 1];
388
+ data += `${fixed((current.x + next.x) / 2)},${fixed((current.y + next.y) / 2)} `;
389
+ }
390
+ return `${data.trimEnd()}Z`;
391
+ }
392
+ // ───────────────────────────────────────────────────────────────────────── the brush
393
+ /** Maps a value from one range to another, held inside it. */
394
+ function modulate(value, from, to) {
395
+ const progress = Math.min(1, Math.max(0, (value - from[0]) / (from[1] - from[0])));
396
+ return to[0] + (to[1] - to[0]) * progress;
397
+ }
398
+ /**
399
+ * The options of the pencil: the numbers tldraw gives its hand-drawn line. With a mouse or a finger the pressure follows the
400
+ * 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.
401
+ */
402
+ export function brushOptions(size, pressure, finished) {
403
+ if (pressure === 'pen') {
404
+ return { size: 1 + size * 1.2, thinning: 0.62, streamline: 0.62, smoothing: 0.62, simulatePressure: false, easing: EASE_PEN, last: finished };
405
+ }
406
+ return {
407
+ size,
408
+ thinning: 0.5,
409
+ streamline: modulate(size, [9, 16], [0.64, 0.74]),
410
+ smoothing: 0.62,
411
+ simulatePressure: true,
412
+ easing: EASE_OUT_SINE,
413
+ last: finished
414
+ };
415
+ }
416
+ /**
417
+ * 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
418
+ * pencil's is (tldraw's `size + 1`).
419
+ */
420
+ export function brushSizeFor(strokeWidth) {
421
+ return Math.max(0.5, strokeWidth) + 1;
422
+ }
423
+ /**
424
+ * The widest the outline of a brush stroke reaches from the line of its samples, for a brush of `size`: the radius at full
425
+ * 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.
426
+ * What hit testing and the bounds of a stroke add to the box of its samples.
427
+ */
428
+ export function brushReach(size) {
429
+ return 1 + size * 1.1;
430
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Plane geometry for boxes, points and polylines: what hit testing, snapping, selection and the
3
+ * rasterizer are built from. Pure functions on plain numbers.
4
+ */
5
+ import type { Point, Rect } from './types.js';
6
+ /** A box in world space: the geometry box plus the rotation about its centre. */
7
+ export interface RotatedBox extends Rect {
8
+ rotation: number;
9
+ }
10
+ export declare function rectRight(rect: Rect): number;
11
+ export declare function rectBottom(rect: Rect): number;
12
+ export declare function rectCenter(rect: Rect): Point;
13
+ /** The rectangle spanned by two corner points, in any order. */
14
+ export declare function rectFromPoints(first: Point, second: Point): Rect;
15
+ export declare function rectContainsPoint(rect: Rect, point: Point): boolean;
16
+ export declare function rectsIntersect(first: Rect, second: Rect): boolean;
17
+ export declare function rectContainsRect(outer: Rect, inner: Rect): boolean;
18
+ export declare function inflateRect(rect: Rect, amount: number): Rect;
19
+ /** The smallest rectangle that holds all of them, or null for none. */
20
+ export declare function unionRects(rects: readonly Rect[]): Rect | null;
21
+ /** The smallest rectangle that holds all the points, or null for none. */
22
+ export declare function boundsOfPoints(points: readonly Point[]): Rect | null;
23
+ /** The four corners of a box in world space, clockwise from the top-left (before rotation). */
24
+ export declare function boxCorners(box: RotatedBox): [Point, Point, Point, Point];
25
+ /** The axis-aligned rectangle around a rotated box. */
26
+ export declare function rotatedBoxBounds(box: RotatedBox): Rect;
27
+ /** Whether a world point is inside a (possibly rotated) box, with `slop` world units of leeway on every side. */
28
+ export declare function pointInBox(box: RotatedBox, point: Point, slop?: number): boolean;
29
+ /** A world point in the local space of a box. Null when the box is degenerate. */
30
+ export declare function worldToBoxLocal(box: RotatedBox, point: Point): Point | null;
31
+ export declare function distanceBetween(first: Point, second: Point): number;
32
+ /** The distance from a point to the segment a-b. */
33
+ export declare function distanceToSegment(point: Point, a: Point, b: Point): number;
34
+ /** The distance from a point to a polyline (closed: the last point joins the first). */
35
+ export declare function distanceToPolyline(point: Point, vertices: readonly Point[], closed: boolean): number;
36
+ /** Even-odd test: whether the point lies inside the polygon. */
37
+ export declare function pointInPolygon(point: Point, vertices: readonly Point[]): boolean;
38
+ /** Whether segment p1-p2 crosses segment p3-p4. */
39
+ export declare function segmentsIntersect(p1: Point, p2: Point, p3: Point, p4: Point): boolean;
40
+ /** Whether a polygon (a rotated box, say) and an axis-aligned rectangle touch. */
41
+ export declare function polygonIntersectsRect(vertices: readonly Point[], rect: Rect): boolean;
42
+ /** Rounds to a number of decimals (default 2): for stored geometry, so JSON does not carry noise digits. */
43
+ export declare function roundTo(value: number, decimals?: number): number;
44
+ export declare function clampNumber(value: number, minimum: number, maximum: number): number;