@widgetic/canvas 0.5.43 → 0.5.45

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 (162) hide show
  1. package/dist/canvas/DomCanvas.svelte +56 -3
  2. package/dist/canvas/DomCanvas.svelte.d.ts +59 -28
  3. package/dist/canvas/autosaveScheduler.js +89 -37
  4. package/dist/canvas/boardFrame.js +1 -1
  5. package/dist/canvas/boardSurface.js +1 -1
  6. package/dist/canvas/canvasLogger.js +19 -7
  7. package/dist/canvas/chromeLayout.js +21 -19
  8. package/dist/canvas/eventScope.js +14 -10
  9. package/dist/canvas/host/CanvasHost.d.ts +87 -2
  10. package/dist/canvas/host/CanvasHost.js +1948 -1394
  11. package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
  12. package/dist/canvas/host/CanvasHostApi.js +32 -5
  13. package/dist/canvas/host/boardFiles.d.ts +90 -0
  14. package/dist/canvas/host/boardFiles.js +155 -0
  15. package/dist/canvas/host/browserServices.d.ts +17 -0
  16. package/dist/canvas/host/browserServices.js +211 -65
  17. package/dist/canvas/host/conversion.js +170 -82
  18. package/dist/canvas/host/hostObject.js +7 -7
  19. package/dist/canvas/host/nearCards.js +21 -18
  20. package/dist/canvas/host/pageEvents.js +5 -5
  21. package/dist/canvas/host/pictureSaves.js +11 -9
  22. package/dist/canvas/host/screenRects.js +31 -28
  23. package/dist/canvas/host/storedPictures.js +243 -149
  24. package/dist/canvas/host/strokeSnap.js +143 -88
  25. package/dist/canvas/index.d.ts +2 -0
  26. package/dist/canvas/openMarks.js +2 -2
  27. package/dist/canvas/outsideClickDeselect.js +7 -6
  28. package/dist/canvas/panZoomPosition.js +2 -2
  29. package/dist/canvas/preferredTextFont.js +25 -13
  30. package/dist/canvas/shapes/ShapeLibrary.js +148 -138
  31. package/dist/canvas/shapes/shapeImageStore.js +4 -4
  32. package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
  33. package/dist/canvas/toolbarPosition.js +2 -2
  34. package/dist/canvas/types.d.ts +17 -0
  35. package/dist/canvas/ui/CanvasContextMenu.svelte +7 -0
  36. package/dist/canvas/ui/CanvasToolbar.svelte +4 -1
  37. package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +2 -0
  38. package/dist/canvas/ui/ColorField.svelte +7 -2
  39. package/dist/canvas/ui/SelectionPanel.svelte +105 -42
  40. package/dist/canvas/ui/ShapesMenu.svelte +65 -31
  41. package/dist/canvas/ui/ShapesMenu.svelte.d.ts +2 -0
  42. package/dist/canvas/ui/ShortcutTooltip.svelte +64 -0
  43. package/dist/canvas/ui/ShortcutTooltip.svelte.d.ts +24 -0
  44. package/dist/canvas/ui/ToolButton.svelte +35 -54
  45. package/dist/canvas/ui/ToolButton.svelte.d.ts +4 -1
  46. package/dist/canvas/ui/colorModel.js +32 -21
  47. package/dist/canvas/ui/colorPickerMessages.js +5 -5
  48. package/dist/canvas/ui/floatingCard.d.ts +26 -4
  49. package/dist/canvas/ui/floatingCard.js +43 -21
  50. package/dist/canvas/ui/floatingLayer.js +6 -5
  51. package/dist/canvas/ui/icons.d.ts +1 -1
  52. package/dist/canvas/ui/icons.js +1 -1
  53. package/dist/canvas/ui/keyHints.js +11 -9
  54. package/dist/canvas/ui/popoverSide.js +3 -2
  55. package/dist/canvas/ui/selectionTraits.d.ts +12 -0
  56. package/dist/canvas/ui/selectionTraits.js +66 -35
  57. package/dist/canvas/ui/sizeLabelVisibility.d.ts +4 -2
  58. package/dist/canvas/ui/sizeLabelVisibility.js +5 -5
  59. package/dist/canvas/view/CanvasStage.svelte +7 -2
  60. package/dist/canvas/view/CanvasStage.svelte.d.ts +2 -0
  61. package/dist/canvas/view/SelectionOverlay.svelte +17 -10
  62. package/dist/canvas/view/WidgetChromeLayer.svelte +15 -3
  63. package/dist/canvas/view/erasingMarks.js +7 -7
  64. package/dist/canvas/view/geometry.d.ts +1 -1
  65. package/dist/canvas/view/geometry.js +90 -71
  66. package/dist/canvas/view/liftedScene.js +36 -24
  67. package/dist/canvas/view/mountedScene.js +79 -76
  68. package/dist/components/CanvasBoardPoster.svelte +206 -0
  69. package/dist/components/CanvasBoardPoster.svelte.d.ts +17 -0
  70. package/dist/components/CanvasToolbarPlaceholder.svelte +115 -0
  71. package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +9 -0
  72. package/dist/components/index.d.ts +7 -0
  73. package/dist/components/index.js +8 -0
  74. package/dist/components/preferenceStorage.js +2 -2
  75. package/dist/engine/EngineCore.d.ts +111 -2
  76. package/dist/engine/EngineCore.js +1013 -707
  77. package/dist/engine/codec/canvasDocument.d.ts +7 -0
  78. package/dist/engine/codec/canvasDocument.js +172 -186
  79. package/dist/engine/codec/hostReadableKeys.js +1 -1
  80. package/dist/engine/core/arrange.js +38 -26
  81. package/dist/engine/core/arrow.d.ts +18 -8
  82. package/dist/engine/core/arrow.js +109 -84
  83. package/dist/engine/core/arrowBinding.d.ts +1 -1
  84. package/dist/engine/core/arrowBinding.js +126 -101
  85. package/dist/engine/core/arrowBindingRecords.d.ts +9 -3
  86. package/dist/engine/core/arrowBindingRecords.js +48 -29
  87. package/dist/engine/core/brush.d.ts +8 -0
  88. package/dist/engine/core/brush.js +98 -78
  89. package/dist/engine/core/cameraMove.d.ts +73 -0
  90. package/dist/engine/core/cameraMove.js +172 -0
  91. package/dist/engine/core/defaults.js +51 -90
  92. package/dist/engine/core/freehand.js +163 -147
  93. package/dist/engine/core/geometry.js +54 -47
  94. package/dist/engine/core/history.js +55 -38
  95. package/dist/engine/core/hitTest.js +81 -68
  96. package/dist/engine/core/matrix.js +20 -18
  97. package/dist/engine/core/pathData.js +210 -183
  98. package/dist/engine/core/rotationReset.js +6 -5
  99. package/dist/engine/core/scene.d.ts +22 -0
  100. package/dist/engine/core/scene.js +288 -139
  101. package/dist/engine/core/scribble.js +84 -59
  102. package/dist/engine/core/shapePaths.js +15 -12
  103. package/dist/engine/core/shapeTools.js +63 -72
  104. package/dist/engine/core/snapping.js +117 -65
  105. package/dist/engine/core/style.d.ts +10 -0
  106. package/dist/engine/core/style.js +82 -33
  107. package/dist/engine/core/transform.d.ts +6 -0
  108. package/dist/engine/core/transform.js +103 -83
  109. package/dist/engine/core/types.d.ts +7 -0
  110. package/dist/engine/core/types.js +4 -4
  111. package/dist/engine/core/viewport.js +36 -23
  112. package/dist/engine/input/PointerController.d.ts +15 -1
  113. package/dist/engine/input/PointerController.js +549 -466
  114. package/dist/engine/input/arrowHandles.js +12 -12
  115. package/dist/engine/input/gestures.d.ts +3 -1
  116. package/dist/engine/input/gestures.js +340 -266
  117. package/dist/engine/input/hoverCursor.js +1 -1
  118. package/dist/engine/input/keyboard.d.ts +22 -3
  119. package/dist/engine/input/keyboard.js +131 -32
  120. package/dist/engine/input/selectionHandles.js +50 -45
  121. package/dist/engine/input/types.d.ts +7 -0
  122. package/dist/engine/input/types.js +1 -1
  123. package/dist/engine/render/rasterize.js +158 -86
  124. package/dist/engine/state.d.ts +18 -0
  125. package/dist/engine/state.js +25 -4
  126. package/dist/engine/text/layoutText.js +62 -43
  127. package/dist/engine/text/textMetrics.js +10 -8
  128. package/dist/engine/widgets.js +137 -101
  129. package/dist/fonts/interFont.js +83 -30
  130. package/dist/fonts/workspaceFonts.d.ts +6 -0
  131. package/dist/fonts/workspaceFonts.js +108 -34
  132. package/dist/i18n/labels.js +4 -4
  133. package/dist/i18n/language.d.ts +3 -3
  134. package/dist/i18n/language.js +4 -4
  135. package/dist/i18n/messages.d.ts +6 -0
  136. package/dist/i18n/messages.js +7 -1
  137. package/dist/i18n/shortcuts.d.ts +6 -4
  138. package/dist/i18n/shortcuts.js +30 -26
  139. package/dist/index.d.ts +2 -1
  140. package/dist/poster/boardPlacement.d.ts +80 -0
  141. package/dist/poster/boardPlacement.js +140 -0
  142. package/dist/poster/boardPoster.d.ts +80 -0
  143. package/dist/poster/boardPoster.js +251 -0
  144. package/dist/poster/paintBoardPoster.d.ts +49 -0
  145. package/dist/poster/paintBoardPoster.js +112 -0
  146. package/dist/poster/posterPictures.d.ts +32 -0
  147. package/dist/poster/posterPictures.js +138 -0
  148. package/dist/settings/workspaceToolSettings.d.ts +6 -0
  149. package/dist/settings/workspaceToolSettings.js +58 -34
  150. package/dist/tools/components/catalog.js +74 -64
  151. package/dist/tools/components/grouping.js +28 -6
  152. package/dist/tools/definitions.js +31 -9
  153. package/dist/tools/executors.js +214 -106
  154. package/dist/tools/hostAdapter.d.ts +5 -1
  155. package/dist/tools/hostAdapter.js +15 -11
  156. package/dist/tools/sceneSummary.js +57 -43
  157. package/dist/tools/sketchIntent.js +21 -13
  158. package/dist/tools/styleArguments.d.ts +35 -0
  159. package/dist/tools/styleArguments.js +93 -0
  160. package/dist/tools/types.d.ts +26 -1
  161. package/dist/tools/types.js +2 -1
  162. package/package.json +1 -1
@@ -11,24 +11,35 @@
11
11
  * hand, and scaling the line must not change how thick it is where the hand dwelled. Everything here is plain functions over
12
12
  * plain records, so the same stroke is drawn in the editor, the screenshot rasterizer and the tests.
13
13
  */
14
+ var __assign = (this && this.__assign) || function () {
15
+ __assign = Object.assign || function(t) {
16
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
17
+ s = arguments[i];
18
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
19
+ t[p] = s[p];
20
+ }
21
+ return t;
22
+ };
23
+ return __assign.apply(this, arguments);
24
+ };
14
25
  import { SHAPE_STYLE_DEFAULTS, createObjectId, createPathObject } from './defaults.js';
15
26
  import { brushOptions, brushReach, brushSizeFor, circlePathData, outlineOf, outlinePathData, outlineWithPressures, strokePointsOf, strokePointsOfLine } from './freehand.js';
16
27
  import { MIN_OBJECT_SIZE } from './types.js';
17
28
  /** The most points of a line a stroke keeps; a longer line is thinned further, so a stroke never grows without bound. */
18
- export const BRUSH_MAX_POINTS = 4000;
29
+ export var BRUSH_MAX_POINTS = 4000;
19
30
  // The points of a stroke closer together than this fraction of the brush (and than a unit of the board) add nothing the outline
20
31
  // shows: it leaves out points closer than 0.62 of the brush on both of its sides already.
21
- const POINT_GAP_OF_SIZE = 0.3;
22
- const MINIMUM_POINT_GAP = 0.75;
32
+ var POINT_GAP_OF_SIZE = 0.3;
33
+ var MINIMUM_POINT_GAP = 0.75;
23
34
  // The decimals a stored point keeps: a hundredth of a unit is far finer than a pixel at the zoom that goes up to eight.
24
- const COORDINATE_DECIMALS = 2;
35
+ var COORDINATE_DECIMALS = 2;
25
36
  // A line that never leaves this distance (in units of the board) from where it began is a click: a dot at the pointer.
26
- const DOT_REACH = 1;
37
+ var DOT_REACH = 1;
27
38
  // The pressure of a dot made with a mouse or a finger: the middle of the range, as a line drawn at an even pressure has.
28
- const DOT_PRESSURE = 0.5;
39
+ var DOT_PRESSURE = 0.5;
29
40
  /** The narrowest width the Width control may give a brush stroke. */
30
- export const MIN_BRUSH_WIDTH = 1;
31
- const roundTo = (value, decimals) => Math.round(value * 10 ** decimals) / 10 ** decimals;
41
+ export var MIN_BRUSH_WIDTH = 1;
42
+ var roundTo = function (value, decimals) { return Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals); };
32
43
  /** Whether an object is a brush stroke (a path that was drawn with the Draw tool and holds its line). */
33
44
  export function isBrushStroke(object) {
34
45
  return object.type === 'path' && !!object.ink;
@@ -42,30 +53,37 @@ export function brushOverflow(shape) {
42
53
  }
43
54
  /** The line a brush stroke follows, in the coordinates of its box (what the shape snap measures). */
44
55
  export function brushCentreline(shape) {
45
- return shape.ink.points.map(([x, y]) => ({ x, y }));
56
+ return shape.ink.points.map(function (_a) {
57
+ var x = _a[0], y = _a[1];
58
+ return ({ x: x, y: y });
59
+ });
46
60
  }
47
61
  /**
48
62
  * The outline of a stored line, as path data: drawn again from the points and the pressures it holds, at the width of the brush.
49
63
  * Always something to draw: a line the outline cannot be made of (one point, or none) is a dot.
50
64
  */
51
65
  export function brushOutlineData(ink, strokeWidth) {
52
- const size = brushSizeFor(strokeWidth);
66
+ var _a;
67
+ var size = brushSizeFor(strokeWidth);
53
68
  // The pressure is already in the points, so it is read, whatever it came from.
54
- const options = { ...brushOptions(size, ink.pressure, true), simulatePressure: false };
55
- const points = strokePointsOfLine(ink.points.map(([x, y, pressure]) => ({ x, y, pressure })));
56
- const data = outlinePathData(outlineOf(points, options));
69
+ var options = __assign(__assign({}, brushOptions(size, ink.pressure, true)), { simulatePressure: false });
70
+ var points = strokePointsOfLine(ink.points.map(function (_a) {
71
+ var x = _a[0], y = _a[1], pressure = _a[2];
72
+ return ({ x: x, y: y, pressure: pressure });
73
+ }));
74
+ var data = outlinePathData(outlineOf(points, options));
57
75
  if (data)
58
76
  return data;
59
- const center = points[0] ?? { x: 0, y: 0 };
77
+ var center = (_a = points[0]) !== null && _a !== void 0 ? _a : { x: 0, y: 0 };
60
78
  return circlePathData(center, Math.max(0.5, size / 2));
61
79
  }
62
80
  /** The points of a smoothed line that the outline needs, with the pressure each was drawn with. */
63
81
  function keptPoints(points, pressures, size) {
64
- let gap = Math.max(MINIMUM_POINT_GAP, size * POINT_GAP_OF_SIZE);
82
+ var gap = Math.max(MINIMUM_POINT_GAP, size * POINT_GAP_OF_SIZE);
65
83
  for (;;) {
66
- const kept = [{ point: points[0], pressure: pressures[0] }];
67
- for (let index = 1; index < points.length - 1; index += 1) {
68
- const previous = kept[kept.length - 1].point;
84
+ var kept = [{ point: points[0], pressure: pressures[0] }];
85
+ for (var index = 1; index < points.length - 1; index += 1) {
86
+ var previous = kept[kept.length - 1].point;
69
87
  if (Math.hypot(points[index].x - previous.x, points[index].y - previous.y) >= gap) {
70
88
  kept.push({ point: points[index], pressure: pressures[index] });
71
89
  }
@@ -82,92 +100,94 @@ function keptPoints(points, pressures, size) {
82
100
  * The stroke for the samples of a pointer, in world units: the box of its line, the line itself with its pressure, and the
83
101
  * outline to draw. Null when there is nothing to draw.
84
102
  */
85
- export function buildBrushStroke(samples, options = {}) {
86
- const strokeWidth = options.strokeWidth ?? SHAPE_STYLE_DEFAULTS.inkStrokeWidth;
87
- const pressure = options.pressure ?? 'speed';
88
- const size = brushSizeFor(strokeWidth);
89
- const lineOptions = brushOptions(size, pressure, options.finished === true);
90
- const usable = samples.filter((sample) => Number.isFinite(sample.x) && Number.isFinite(sample.y));
103
+ export function buildBrushStroke(samples, options) {
104
+ var _a, _b, _c, _d;
105
+ if (options === void 0) { options = {}; }
106
+ var strokeWidth = (_a = options.strokeWidth) !== null && _a !== void 0 ? _a : SHAPE_STYLE_DEFAULTS.inkStrokeWidth;
107
+ var pressure = (_b = options.pressure) !== null && _b !== void 0 ? _b : 'speed';
108
+ var size = brushSizeFor(strokeWidth);
109
+ // A line of one pressure reads that pressure at every sample, and is not smoothed (smoothing is what makes a line trail the hand).
110
+ var steadyPressure = typeof options.steadyPressure === 'number' && Number.isFinite(options.steadyPressure) ? Math.min(1, Math.max(0, options.steadyPressure)) : null;
111
+ var brushLine = brushOptions(size, pressure, options.finished === true);
112
+ var lineOptions = steadyPressure === null ? brushLine : __assign(__assign({}, brushLine), { streamline: 0, simulatePressure: false });
113
+ var usable = samples
114
+ .filter(function (sample) { return Number.isFinite(sample.x) && Number.isFinite(sample.y); })
115
+ .map(function (sample) { return (steadyPressure === null ? sample : { x: sample.x, y: sample.y, pressure: steadyPressure }); });
91
116
  if (usable.length === 0)
92
117
  return null;
93
- let kept;
94
- if (usable.every((sample) => Math.hypot(sample.x - usable[0].x, sample.y - usable[0].y) < DOT_REACH)) {
118
+ var kept;
119
+ if (usable.every(function (sample) { return Math.hypot(sample.x - usable[0].x, sample.y - usable[0].y) < DOT_REACH; })) {
95
120
  // A click is a dot where the pointer went down: the line has one point, so nothing is drawn a pixel away from it.
96
- const penPressure = usable.reduce((most, sample) => Math.max(most, sample.pressure ?? 0), 0);
97
- const dotPressure = pressure === 'pen' && penPressure > 0 ? Math.min(1, penPressure) : DOT_PRESSURE;
98
- const point = { x: usable[0].x, y: usable[0].y, pressure: dotPressure, vectorX: 0, vectorY: 0, distance: 0, runningLength: 0 };
99
- kept = [{ point, pressure: dotPressure }];
121
+ var penPressure = usable.reduce(function (most, sample) { var _a; return Math.max(most, (_a = sample.pressure) !== null && _a !== void 0 ? _a : 0); }, 0);
122
+ var dotPressure = pressure === 'pen' && penPressure > 0 ? Math.min(1, penPressure) : DOT_PRESSURE;
123
+ var point = { x: usable[0].x, y: usable[0].y, pressure: dotPressure, vectorX: 0, vectorY: 0, distance: 0, runningLength: 0 };
124
+ kept = [{ point: point, pressure: dotPressure }];
100
125
  }
101
126
  else {
102
- const smoothed = strokePointsOf(usable, lineOptions);
127
+ var smoothed = strokePointsOf(usable, lineOptions);
103
128
  // The pass that works the pressure out (from the speed, or read from the pen) so that it can be kept.
104
- const { pressures } = outlineWithPressures(smoothed, lineOptions);
129
+ var pressures = outlineWithPressures(smoothed, lineOptions).pressures;
105
130
  kept = keptPoints(smoothed, pressures, size);
106
131
  }
107
- let minX = Infinity;
108
- let minY = Infinity;
109
- let maxX = -Infinity;
110
- let maxY = -Infinity;
111
- for (const { point } of kept) {
132
+ var minX = Infinity;
133
+ var minY = Infinity;
134
+ var maxX = -Infinity;
135
+ var maxY = -Infinity;
136
+ for (var _i = 0, kept_1 = kept; _i < kept_1.length; _i++) {
137
+ var point = kept_1[_i].point;
112
138
  minX = Math.min(minX, point.x);
113
139
  minY = Math.min(minY, point.y);
114
140
  maxX = Math.max(maxX, point.x);
115
141
  maxY = Math.max(maxY, point.y);
116
142
  }
117
143
  // A line that is flat on one axis (a horizontal line, a dot) gets a box one unit thick, with the line in its middle.
118
- const flatX = maxX - minX < MIN_OBJECT_SIZE;
119
- const flatY = maxY - minY < MIN_OBJECT_SIZE;
120
- const boxX = flatX ? (minX + maxX) / 2 - MIN_OBJECT_SIZE / 2 : minX;
121
- const boxY = flatY ? (minY + maxY) / 2 - MIN_OBJECT_SIZE / 2 : minY;
122
- const width = flatX ? MIN_OBJECT_SIZE : maxX - minX;
123
- const height = flatY ? MIN_OBJECT_SIZE : maxY - minY;
124
- const ink = {
125
- points: kept.map(({ point, pressure: pointPressure }) => [
126
- roundTo(point.x - boxX, COORDINATE_DECIMALS),
127
- roundTo(point.y - boxY, COORDINATE_DECIMALS),
128
- roundTo(pointPressure, COORDINATE_DECIMALS)
129
- ]),
130
- pressure
144
+ var flatX = maxX - minX < MIN_OBJECT_SIZE;
145
+ var flatY = maxY - minY < MIN_OBJECT_SIZE;
146
+ var boxX = flatX ? (minX + maxX) / 2 - MIN_OBJECT_SIZE / 2 : minX;
147
+ var boxY = flatY ? (minY + maxY) / 2 - MIN_OBJECT_SIZE / 2 : minY;
148
+ var width = flatX ? MIN_OBJECT_SIZE : maxX - minX;
149
+ var height = flatY ? MIN_OBJECT_SIZE : maxY - minY;
150
+ var ink = {
151
+ points: kept.map(function (_a) {
152
+ var point = _a.point, pointPressure = _a.pressure;
153
+ return [
154
+ roundTo(point.x - boxX, COORDINATE_DECIMALS),
155
+ roundTo(point.y - boxY, COORDINATE_DECIMALS),
156
+ roundTo(pointPressure, COORDINATE_DECIMALS)
157
+ ];
158
+ }),
159
+ pressure: pressure
131
160
  };
132
- const createId = options.createId ?? createObjectId;
133
- return createPathObject({
134
- id: createId('obj'),
135
- x: boxX,
136
- y: boxY,
137
- width,
138
- height,
139
- d: brushOutlineData(ink, strokeWidth),
140
- shape: null,
161
+ var createId = (_c = options.createId) !== null && _c !== void 0 ? _c : createObjectId;
162
+ // The whole line shows through as much as it was asked to; a line that was not asked anything is fully opaque, as an object is.
163
+ var opacity = typeof options.opacity === 'number' && Number.isFinite(options.opacity) ? Math.min(1, Math.max(0, options.opacity)) : null;
164
+ return createPathObject(__assign(__assign({ id: createId('obj'), x: boxX, y: boxY, width: width, height: height, d: brushOutlineData(ink, strokeWidth), shape: null,
141
165
  // The brush is filled: its colour is the fill of the outline, and the outline has no stroke of its own.
142
- fill: options.color ?? SHAPE_STYLE_DEFAULTS.inkStroke,
143
- fillOpacity: 1,
144
- stroke: 'transparent',
145
- strokeWidth,
146
- lineCap: 'round',
147
- lineJoin: 'round',
148
- ink
149
- });
166
+ fill: (_d = options.color) !== null && _d !== void 0 ? _d : SHAPE_STYLE_DEFAULTS.inkStroke, fillOpacity: 1, stroke: 'transparent', strokeWidth: strokeWidth, lineCap: 'round', lineJoin: 'round' }, (opacity === null ? {} : { opacity: opacity })), { ink: ink }));
150
167
  }
151
168
  /**
152
169
  * The stroke with its line scaled (and mirrored) into a new box and its outline drawn again at the brush width: the line goes
153
170
  * where the box goes, the brush stays as wide as it was. Only `d`, `ink`, the size of the box and `strokeWidth` are written;
154
171
  * where the box is, and how it is turned, is the caller's.
155
172
  */
156
- export function reshapeBrushStroke(shape, change = {}) {
157
- const width = Math.max(MIN_OBJECT_SIZE, change.width ?? shape.width);
158
- const height = Math.max(MIN_OBJECT_SIZE, change.height ?? shape.height);
159
- const strokeWidth = change.strokeWidth ?? shape.strokeWidth;
160
- const scaleX = shape.width > 0 ? width / shape.width : 1;
161
- const scaleY = shape.height > 0 ? height / shape.height : 1;
162
- const points = shape.ink.points.map(([x, y, pressure]) => {
163
- const scaledX = x * scaleX;
164
- const scaledY = y * scaleY;
173
+ export function reshapeBrushStroke(shape, change) {
174
+ var _a, _b, _c;
175
+ if (change === void 0) { change = {}; }
176
+ var width = Math.max(MIN_OBJECT_SIZE, (_a = change.width) !== null && _a !== void 0 ? _a : shape.width);
177
+ var height = Math.max(MIN_OBJECT_SIZE, (_b = change.height) !== null && _b !== void 0 ? _b : shape.height);
178
+ var strokeWidth = (_c = change.strokeWidth) !== null && _c !== void 0 ? _c : shape.strokeWidth;
179
+ var scaleX = shape.width > 0 ? width / shape.width : 1;
180
+ var scaleY = shape.height > 0 ? height / shape.height : 1;
181
+ var points = shape.ink.points.map(function (_a) {
182
+ var x = _a[0], y = _a[1], pressure = _a[2];
183
+ var scaledX = x * scaleX;
184
+ var scaledY = y * scaleY;
165
185
  return [
166
186
  roundTo(change.flipX ? width - scaledX : scaledX, COORDINATE_DECIMALS),
167
187
  roundTo(change.flipY ? height - scaledY : scaledY, COORDINATE_DECIMALS),
168
188
  pressure
169
189
  ];
170
190
  });
171
- const ink = { points, pressure: shape.ink.pressure };
172
- return { ...shape, width, height, strokeWidth, ink, d: brushOutlineData(ink, strokeWidth) };
191
+ var ink = { points: points, pressure: shape.ink.pressure };
192
+ return __assign(__assign({}, shape), { width: width, height: height, strokeWidth: strokeWidth, ink: ink, d: brushOutlineData(ink, strokeWidth) });
173
193
  }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Moving the view the way a camera moves: from where it looks now to a place it is asked to look at, with the position and the
3
+ * zoom changing together, on one eased progress, and ending exactly on the place.
4
+ *
5
+ * A view is what the board looks at (`CameraView`): the point of the world in the middle of the board element, and the zoom (1 is
6
+ * 100%). The viewport the engine keeps (`x`, `y`, `zoom`) is worked out from it for the size of the board at that moment, so a board
7
+ * that is resized on the way still ends with the place in its middle.
8
+ *
9
+ * There are two shapes of path between two views (`CameraPathShape`):
10
+ *
11
+ * - `'ease'`, the default: the position and the zoom ease together. The centre goes in a straight line and the zoom changes by the
12
+ * same factor at every step, both on the one eased progress, so they start, speed up, slow down and arrive at the same moment, and
13
+ * the zoom never goes beyond its two ends. This is what "ease the position and the zoom at the same time" asks for.
14
+ * - `'fly'`: the path of van Wijk and Nuij, "Smooth and efficient zooming and panning" (2003), the one d3's `interpolateZoom` draws.
15
+ * A view that only zooms moves like a plain zoom; a view that has to go far zooms out on the way and in again at the end, so what
16
+ * the person looks at does not rush past. The position and the zoom still ride on one progress, but not at the same rate (the view
17
+ * zooms out while it pans and in as it arrives), so it is not the plain ease above.
18
+ *
19
+ * Both last as long as the length of the van Wijk path says, so a move takes the same time whichever shape it has.
20
+ *
21
+ * Nothing here knows the clock or the page: the engine asks for the view at a progress, and a test asks for the same.
22
+ */
23
+ import type { Point, Size, Viewport } from './types.js';
24
+ /**
25
+ * The way a move goes between two views: `'ease'` (the default) changes the position and the zoom together at the same rate, and
26
+ * `'fly'` is the path of van Wijk and Nuij, which zooms out on the way when the move is long. Any other value is taken for `'ease'`.
27
+ */
28
+ export type CameraPathShape = 'ease' | 'fly';
29
+ /** What the board looks at: the point of the world in the middle of the board element, and the zoom. */
30
+ export interface CameraView {
31
+ center: Point;
32
+ /** The scale of the world on screen (1 is 100%). */
33
+ zoom: number;
34
+ }
35
+ /** The path of a move between two views. */
36
+ export interface CameraPath {
37
+ /** How far the view travels, in the units of the path: a plain zoom of about four times is 1, and a longer way is more. */
38
+ length: number;
39
+ /** How long the move should last, in milliseconds, worked out from its length (never under the shortest or over the longest). */
40
+ duration: number;
41
+ /** The view `progress` of the way along the path: 0 is the start, 1 is exactly the end. */
42
+ at(progress: number): CameraView;
43
+ }
44
+ /** The shape a move has when nobody chose one: the position and the zoom ease together. */
45
+ export declare const DEFAULT_CAMERA_PATH_SHAPE: CameraPathShape;
46
+ /** The shortest a move lasts when nobody gave its length, in milliseconds: a shorter one is too quick for the eye to follow. */
47
+ export declare const CAMERA_MOVE_MIN_DURATION_MS = 380;
48
+ /** The longest a move lasts when nobody gave its length, in milliseconds: a long way across the board still takes under a second. */
49
+ export declare const CAMERA_MOVE_MAX_DURATION_MS = 1000;
50
+ /** The time for each unit of the length of a path, in milliseconds. */
51
+ export declare const CAMERA_MOVE_MS_PER_UNIT = 800;
52
+ /**
53
+ * The share of the time a move takes that a quick move takes (`durationScale` of the move options). The double click on the empty
54
+ * board and Ctrl/Cmd+0 use it: they answer something the person has just done and should feel brisk, while a move a page asks for
55
+ * keeps the time its length gives.
56
+ */
57
+ export declare const QUICK_CAMERA_MOVE_SCALE = 0.65;
58
+ /** The view that a viewport shows in a board element of `viewSize`. */
59
+ export declare function cameraOfViewport(viewport: Viewport, viewSize: Size): CameraView;
60
+ /** The viewport that shows `camera` in a board element of `viewSize`. */
61
+ export declare function viewportOfCamera(camera: CameraView, viewSize: Size): Viewport;
62
+ /** Whether two viewports look the same to a person: less than half a pixel apart, with a zoom that differs by less than 0.01%. */
63
+ export declare function viewportsMatch(first: Viewport, second: Viewport): boolean;
64
+ /** Slow start, quick middle, slow end: the way a person expects a camera to move. `progress` and the answer are between 0 and 1. */
65
+ export declare function easeInOutCubic(progress: number): number;
66
+ /**
67
+ * The path from one view to another, or `null` when there is no path to draw: the board has no size yet, or one of the views is not
68
+ * a view (a centre that is not a point, a zoom that is not above 0). The caller then puts the view in place at once.
69
+ *
70
+ * `shape` is how the view goes (see `CameraPathShape`): by default the position and the zoom ease together, and `'fly'` takes the
71
+ * path of van Wijk and Nuij. How long the move lasts comes from the length of that path whichever shape is asked for.
72
+ */
73
+ export declare function createCameraPath(from: CameraView, to: CameraView, viewSize: Size, shape?: CameraPathShape): CameraPath | null;
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Moving the view the way a camera moves: from where it looks now to a place it is asked to look at, with the position and the
3
+ * zoom changing together, on one eased progress, and ending exactly on the place.
4
+ *
5
+ * A view is what the board looks at (`CameraView`): the point of the world in the middle of the board element, and the zoom (1 is
6
+ * 100%). The viewport the engine keeps (`x`, `y`, `zoom`) is worked out from it for the size of the board at that moment, so a board
7
+ * that is resized on the way still ends with the place in its middle.
8
+ *
9
+ * There are two shapes of path between two views (`CameraPathShape`):
10
+ *
11
+ * - `'ease'`, the default: the position and the zoom ease together. The centre goes in a straight line and the zoom changes by the
12
+ * same factor at every step, both on the one eased progress, so they start, speed up, slow down and arrive at the same moment, and
13
+ * the zoom never goes beyond its two ends. This is what "ease the position and the zoom at the same time" asks for.
14
+ * - `'fly'`: the path of van Wijk and Nuij, "Smooth and efficient zooming and panning" (2003), the one d3's `interpolateZoom` draws.
15
+ * A view that only zooms moves like a plain zoom; a view that has to go far zooms out on the way and in again at the end, so what
16
+ * the person looks at does not rush past. The position and the zoom still ride on one progress, but not at the same rate (the view
17
+ * zooms out while it pans and in as it arrives), so it is not the plain ease above.
18
+ *
19
+ * Both last as long as the length of the van Wijk path says, so a move takes the same time whichever shape it has.
20
+ *
21
+ * Nothing here knows the clock or the page: the engine asks for the view at a progress, and a test asks for the same.
22
+ */
23
+ var __assign = (this && this.__assign) || function () {
24
+ __assign = Object.assign || function(t) {
25
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
26
+ s = arguments[i];
27
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
28
+ t[p] = s[p];
29
+ }
30
+ return t;
31
+ };
32
+ return __assign.apply(this, arguments);
33
+ };
34
+ import { clampNumber } from './geometry.js';
35
+ import { clampZoom, screenToWorld } from './viewport.js';
36
+ /** The shape a move has when nobody chose one: the position and the zoom ease together. */
37
+ export var DEFAULT_CAMERA_PATH_SHAPE = 'ease';
38
+ /** The shortest a move lasts when nobody gave its length, in milliseconds: a shorter one is too quick for the eye to follow. */
39
+ export var CAMERA_MOVE_MIN_DURATION_MS = 380;
40
+ /** The longest a move lasts when nobody gave its length, in milliseconds: a long way across the board still takes under a second. */
41
+ export var CAMERA_MOVE_MAX_DURATION_MS = 1000;
42
+ /** The time for each unit of the length of a path, in milliseconds. */
43
+ export var CAMERA_MOVE_MS_PER_UNIT = 800;
44
+ /**
45
+ * The share of the time a move takes that a quick move takes (`durationScale` of the move options). The double click on the empty
46
+ * board and Ctrl/Cmd+0 use it: they answer something the person has just done and should feel brisk, while a move a page asks for
47
+ * keeps the time its length gives.
48
+ */
49
+ export var QUICK_CAMERA_MOVE_SCALE = 0.65;
50
+ /**
51
+ * How much the path zooms out on its way when the move is long (ρ in the paper). The square root of 2 is the value its authors
52
+ * found the most pleasing: a larger one zooms out more, and 0 would be a straight line.
53
+ */
54
+ var PATH_CURVATURE = Math.SQRT2;
55
+ /** A move whose centres are closer than this fraction of the narrower view is a plain zoom: there is nothing to pan. */
56
+ var PLAIN_ZOOM_DISTANCE_RATIO = 1e-6;
57
+ /** Two viewports less than this many pixels apart look the same to a person. */
58
+ var SAME_VIEWPORT_PIXELS = 0.5;
59
+ /** Two zooms that differ by less than this fraction look the same to a person. */
60
+ var SAME_ZOOM_RATIO = 1e-4;
61
+ /** The view that a viewport shows in a board element of `viewSize`. */
62
+ export function cameraOfViewport(viewport, viewSize) {
63
+ return { center: screenToWorld(viewport, { x: viewSize.width / 2, y: viewSize.height / 2 }), zoom: viewport.zoom };
64
+ }
65
+ /** The viewport that shows `camera` in a board element of `viewSize`. */
66
+ export function viewportOfCamera(camera, viewSize) {
67
+ return {
68
+ x: viewSize.width / 2 - camera.center.x * camera.zoom,
69
+ y: viewSize.height / 2 - camera.center.y * camera.zoom,
70
+ zoom: camera.zoom
71
+ };
72
+ }
73
+ /** Whether two viewports look the same to a person: less than half a pixel apart, with a zoom that differs by less than 0.01%. */
74
+ export function viewportsMatch(first, second) {
75
+ return (Math.abs(first.x - second.x) < SAME_VIEWPORT_PIXELS &&
76
+ Math.abs(first.y - second.y) < SAME_VIEWPORT_PIXELS &&
77
+ Math.abs(first.zoom - second.zoom) < SAME_ZOOM_RATIO * Math.max(first.zoom, second.zoom));
78
+ }
79
+ /** Slow start, quick middle, slow end: the way a person expects a camera to move. `progress` and the answer are between 0 and 1. */
80
+ export function easeInOutCubic(progress) {
81
+ var clampedProgress = clampNumber(progress, 0, 1);
82
+ return clampedProgress < 0.5 ? 4 * Math.pow(clampedProgress, 3) : 1 - Math.pow((-2 * clampedProgress + 2), 3) / 2;
83
+ }
84
+ function isUsableView(view) {
85
+ return Number.isFinite(view.center.x) && Number.isFinite(view.center.y) && Number.isFinite(view.zoom) && view.zoom > 0;
86
+ }
87
+ /** A zoom ratio this close to 1 (its logarithm is this close to 0) is no zoom: the centre then goes by the plain share of the progress. */
88
+ var NO_ZOOM_LOGARITHM = 1e-9;
89
+ /**
90
+ * The view between two views with no detour: the zoom changes by the same factor at every step, and the centre goes along the
91
+ * straight line between the two centres, the way it does when a person zooms about one fixed point of the board and pans nothing
92
+ * else.
93
+ *
94
+ * The centre is not simply `progress` of the way along that line. The world grows on screen by the same factor at every step, so
95
+ * once the zoom grows by more than about 2.7 times in all, the place would first be carried away from the middle of the board, and
96
+ * only then come back to it (the swing of a plain tween). Here the share of the line is
97
+ * `(1 - zoomRatio^-progress) / (1 - 1 / zoomRatio)`, with which the place only ever comes closer to the middle of the board, on
98
+ * screen, as the zoom goes on; with no change of zoom it is `progress` itself, and zooming out it is the same formula.
99
+ */
100
+ function straightView(from, to, progress) {
101
+ var zoomLogarithm = Math.log(to.zoom / from.zoom);
102
+ var lineShare = Math.abs(zoomLogarithm) < NO_ZOOM_LOGARITHM ? progress : Math.expm1(-progress * zoomLogarithm) / Math.expm1(-zoomLogarithm);
103
+ return {
104
+ center: { x: from.center.x + (to.center.x - from.center.x) * lineShare, y: from.center.y + (to.center.y - from.center.y) * lineShare },
105
+ zoom: clampZoom(from.zoom * Math.exp(zoomLogarithm * progress))
106
+ };
107
+ }
108
+ /**
109
+ * The path from one view to another, or `null` when there is no path to draw: the board has no size yet, or one of the views is not
110
+ * a view (a centre that is not a point, a zoom that is not above 0). The caller then puts the view in place at once.
111
+ *
112
+ * `shape` is how the view goes (see `CameraPathShape`): by default the position and the zoom ease together, and `'fly'` takes the
113
+ * path of van Wijk and Nuij. How long the move lasts comes from the length of that path whichever shape is asked for.
114
+ */
115
+ export function createCameraPath(from, to, viewSize, shape) {
116
+ if (shape === void 0) { shape = DEFAULT_CAMERA_PATH_SHAPE; }
117
+ // The path measures a view by the width of the world it shows along the longer side of the board.
118
+ var extent = Math.max(viewSize.width, viewSize.height);
119
+ if (!(extent > 0) || !isUsableView(from) || !isUsableView(to))
120
+ return null;
121
+ var startWidth = extent / from.zoom;
122
+ var endWidth = extent / to.zoom;
123
+ var deltaX = to.center.x - from.center.x;
124
+ var deltaY = to.center.y - from.center.y;
125
+ var distance = Math.hypot(deltaX, deltaY);
126
+ var length;
127
+ var sampleAt;
128
+ if (distance < Math.min(startWidth, endWidth) * PLAIN_ZOOM_DISTANCE_RATIO) {
129
+ // Nothing to pan: a plain zoom, by the same factor at every step.
130
+ length = Math.log(endWidth / startWidth) / PATH_CURVATURE;
131
+ sampleAt = function (progress) { return ({ fraction: progress, width: startWidth * Math.exp(PATH_CURVATURE * length * progress) }); };
132
+ }
133
+ else {
134
+ var curvatureSquared_1 = Math.pow(PATH_CURVATURE, 2);
135
+ var widthGrowth = Math.pow(endWidth, 2) - Math.pow(startWidth, 2);
136
+ var panPart = Math.pow(curvatureSquared_1, 2) * Math.pow(distance, 2);
137
+ // `asinh` rather than the logarithm of the square root: for a view that zooms in a long way with a short pan, the
138
+ // difference of two nearly equal numbers would come out as 0.
139
+ var startAngle_1 = -Math.asinh((widthGrowth + panPart) / (2 * startWidth * curvatureSquared_1 * distance));
140
+ var endAngle = -Math.asinh((widthGrowth - panPart) / (2 * endWidth * curvatureSquared_1 * distance));
141
+ length = (endAngle - startAngle_1) / PATH_CURVATURE;
142
+ sampleAt = function (progress) {
143
+ var along = PATH_CURVATURE * length * progress + startAngle_1;
144
+ return {
145
+ fraction: (startWidth / (curvatureSquared_1 * distance)) * (Math.cosh(startAngle_1) * Math.tanh(along) - Math.sinh(startAngle_1)),
146
+ width: (startWidth * Math.cosh(startAngle_1)) / Math.cosh(along)
147
+ };
148
+ };
149
+ }
150
+ var pathLength = Number.isFinite(length) ? Math.abs(length) : 1;
151
+ return {
152
+ length: pathLength,
153
+ duration: clampNumber(pathLength * CAMERA_MOVE_MS_PER_UNIT, CAMERA_MOVE_MIN_DURATION_MS, CAMERA_MOVE_MAX_DURATION_MS),
154
+ at: function (progress) {
155
+ if (!Number.isFinite(progress) || progress <= 0)
156
+ return { center: __assign({}, from.center), zoom: from.zoom };
157
+ if (progress >= 1)
158
+ return { center: __assign({}, to.center), zoom: to.zoom };
159
+ // No detour: the centre and the zoom change at the same rate on the one progress, and arrive together.
160
+ if (shape !== 'fly')
161
+ return straightView(from, to, progress);
162
+ var sample = sampleAt(progress);
163
+ // A path that came out as something that is not a number (a move across a distance no board has) is walked in a straight line.
164
+ if (!Number.isFinite(sample.fraction) || !(sample.width > 0) || !Number.isFinite(sample.width))
165
+ return straightView(from, to, progress);
166
+ return {
167
+ center: { x: from.center.x + deltaX * sample.fraction, y: from.center.y + deltaY * sample.fraction },
168
+ zoom: clampZoom(extent / sample.width)
169
+ };
170
+ }
171
+ };
172
+ }