js-draw 1.0.1 → 1.0.2

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 (182) hide show
  1. package/LICENSE +21 -0
  2. package/dist/bundle.js +1 -1
  3. package/dist/cjs/version.js +1 -1
  4. package/dist/mjs/version.mjs +1 -1
  5. package/docs/img/readme-images/js-draw.jpg +0 -0
  6. package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
  7. package/package.json +5 -4
  8. package/dist-test/test_imports/package-lock.json +0 -13
  9. package/dist-test/test_imports/package.json +0 -12
  10. package/dist-test/test_imports/test-imports.js +0 -11
  11. package/dist-test/test_imports/test-require.cjs +0 -14
  12. package/src/Editor.loadFrom.test.ts +0 -24
  13. package/src/Editor.test.ts +0 -107
  14. package/src/Editor.toSVG.test.ts +0 -294
  15. package/src/Editor.ts +0 -1443
  16. package/src/EditorImage.test.ts +0 -117
  17. package/src/EditorImage.ts +0 -609
  18. package/src/EventDispatcher.test.ts +0 -123
  19. package/src/EventDispatcher.ts +0 -72
  20. package/src/Pointer.ts +0 -183
  21. package/src/SVGLoader.test.ts +0 -114
  22. package/src/SVGLoader.ts +0 -672
  23. package/src/UndoRedoHistory.test.ts +0 -34
  24. package/src/UndoRedoHistory.ts +0 -102
  25. package/src/Viewport.ts +0 -322
  26. package/src/bundle/bundled.ts +0 -7
  27. package/src/commands/Command.ts +0 -45
  28. package/src/commands/Duplicate.ts +0 -75
  29. package/src/commands/Erase.ts +0 -95
  30. package/src/commands/SerializableCommand.ts +0 -49
  31. package/src/commands/UnresolvedCommand.ts +0 -37
  32. package/src/commands/invertCommand.ts +0 -58
  33. package/src/commands/lib.ts +0 -16
  34. package/src/commands/localization.ts +0 -47
  35. package/src/commands/uniteCommands.test.ts +0 -23
  36. package/src/commands/uniteCommands.ts +0 -140
  37. package/src/components/AbstractComponent.transformBy.test.ts +0 -23
  38. package/src/components/AbstractComponent.ts +0 -383
  39. package/src/components/BackgroundComponent.test.ts +0 -44
  40. package/src/components/BackgroundComponent.ts +0 -348
  41. package/src/components/ImageComponent.ts +0 -176
  42. package/src/components/RestylableComponent.ts +0 -161
  43. package/src/components/SVGGlobalAttributesObject.ts +0 -79
  44. package/src/components/Stroke.test.ts +0 -137
  45. package/src/components/Stroke.ts +0 -294
  46. package/src/components/TextComponent.test.ts +0 -202
  47. package/src/components/TextComponent.ts +0 -429
  48. package/src/components/UnknownSVGObject.test.ts +0 -10
  49. package/src/components/UnknownSVGObject.ts +0 -60
  50. package/src/components/builders/ArrowBuilder.ts +0 -106
  51. package/src/components/builders/CircleBuilder.ts +0 -100
  52. package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
  53. package/src/components/builders/FreehandLineBuilder.ts +0 -210
  54. package/src/components/builders/LineBuilder.ts +0 -77
  55. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
  56. package/src/components/builders/RectangleBuilder.ts +0 -73
  57. package/src/components/builders/types.ts +0 -15
  58. package/src/components/lib.ts +0 -31
  59. package/src/components/localization.ts +0 -24
  60. package/src/components/util/StrokeSmoother.ts +0 -302
  61. package/src/components/util/describeComponentList.ts +0 -18
  62. package/src/dialogs/makeAboutDialog.ts +0 -82
  63. package/src/inputEvents.ts +0 -143
  64. package/src/lib.ts +0 -91
  65. package/src/localization.ts +0 -34
  66. package/src/localizations/de.ts +0 -146
  67. package/src/localizations/en.ts +0 -8
  68. package/src/localizations/es.ts +0 -74
  69. package/src/localizations/getLocalizationTable.test.ts +0 -27
  70. package/src/localizations/getLocalizationTable.ts +0 -74
  71. package/src/rendering/Display.ts +0 -247
  72. package/src/rendering/RenderablePathSpec.ts +0 -88
  73. package/src/rendering/RenderingStyle.test.ts +0 -68
  74. package/src/rendering/RenderingStyle.ts +0 -55
  75. package/src/rendering/TextRenderingStyle.ts +0 -55
  76. package/src/rendering/caching/CacheRecord.test.ts +0 -48
  77. package/src/rendering/caching/CacheRecord.ts +0 -76
  78. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  79. package/src/rendering/caching/RenderingCache.test.ts +0 -43
  80. package/src/rendering/caching/RenderingCache.ts +0 -66
  81. package/src/rendering/caching/RenderingCacheNode.ts +0 -404
  82. package/src/rendering/caching/testUtils.ts +0 -35
  83. package/src/rendering/caching/types.ts +0 -34
  84. package/src/rendering/lib.ts +0 -8
  85. package/src/rendering/localization.ts +0 -20
  86. package/src/rendering/renderers/AbstractRenderer.ts +0 -232
  87. package/src/rendering/renderers/CanvasRenderer.ts +0 -312
  88. package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
  89. package/src/rendering/renderers/DummyRenderer.ts +0 -142
  90. package/src/rendering/renderers/SVGRenderer.ts +0 -434
  91. package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
  92. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
  93. package/src/shortcuts/KeyBinding.test.ts +0 -61
  94. package/src/shortcuts/KeyBinding.ts +0 -257
  95. package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
  96. package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
  97. package/src/shortcuts/lib.ts +0 -3
  98. package/src/testing/createEditor.ts +0 -11
  99. package/src/testing/getUniquePointerId.ts +0 -18
  100. package/src/testing/lib.ts +0 -3
  101. package/src/testing/sendPenEvent.ts +0 -36
  102. package/src/testing/sendTouchEvent.ts +0 -71
  103. package/src/toolbar/AbstractToolbar.ts +0 -542
  104. package/src/toolbar/DropdownToolbar.ts +0 -220
  105. package/src/toolbar/EdgeToolbar.test.ts +0 -54
  106. package/src/toolbar/EdgeToolbar.ts +0 -543
  107. package/src/toolbar/IconProvider.ts +0 -861
  108. package/src/toolbar/constants.ts +0 -1
  109. package/src/toolbar/lib.ts +0 -6
  110. package/src/toolbar/localization.ts +0 -136
  111. package/src/toolbar/types.ts +0 -13
  112. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  113. package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
  114. package/src/toolbar/widgets/BaseWidget.ts +0 -495
  115. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
  116. package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
  117. package/src/toolbar/widgets/HandToolWidget.ts +0 -239
  118. package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
  119. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  120. package/src/toolbar/widgets/PenToolWidget.ts +0 -369
  121. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
  122. package/src/toolbar/widgets/TextToolWidget.ts +0 -149
  123. package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
  124. package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
  125. package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
  126. package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
  127. package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
  128. package/src/toolbar/widgets/keybindings.ts +0 -19
  129. package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
  130. package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
  131. package/src/toolbar/widgets/layout/types.ts +0 -74
  132. package/src/toolbar/widgets/lib.ts +0 -13
  133. package/src/tools/BaseTool.ts +0 -169
  134. package/src/tools/Eraser.test.ts +0 -103
  135. package/src/tools/Eraser.ts +0 -173
  136. package/src/tools/FindTool.test.ts +0 -67
  137. package/src/tools/FindTool.ts +0 -153
  138. package/src/tools/InputFilter/FunctionMapper.ts +0 -17
  139. package/src/tools/InputFilter/InputMapper.ts +0 -41
  140. package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
  141. package/src/tools/InputFilter/InputPipeline.ts +0 -34
  142. package/src/tools/InputFilter/InputStabilizer.ts +0 -254
  143. package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
  144. package/src/tools/PanZoom.test.ts +0 -339
  145. package/src/tools/PanZoom.ts +0 -525
  146. package/src/tools/PasteHandler.ts +0 -94
  147. package/src/tools/Pen.test.ts +0 -260
  148. package/src/tools/Pen.ts +0 -284
  149. package/src/tools/PipetteTool.ts +0 -84
  150. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
  151. package/src/tools/SelectionTool/Selection.ts +0 -647
  152. package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
  153. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
  154. package/src/tools/SelectionTool/SelectionTool.ts +0 -510
  155. package/src/tools/SelectionTool/TransformMode.ts +0 -112
  156. package/src/tools/SelectionTool/types.ts +0 -11
  157. package/src/tools/SoundUITool.ts +0 -221
  158. package/src/tools/TextTool.ts +0 -339
  159. package/src/tools/ToolController.ts +0 -224
  160. package/src/tools/ToolEnabledGroup.ts +0 -14
  161. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  162. package/src/tools/ToolbarShortcutHandler.ts +0 -39
  163. package/src/tools/UndoRedoShortcut.test.ts +0 -62
  164. package/src/tools/UndoRedoShortcut.ts +0 -24
  165. package/src/tools/keybindings.ts +0 -85
  166. package/src/tools/lib.ts +0 -22
  167. package/src/tools/localization.ts +0 -76
  168. package/src/types.ts +0 -151
  169. package/src/util/ReactiveValue.test.ts +0 -168
  170. package/src/util/ReactiveValue.ts +0 -241
  171. package/src/util/assertions.ts +0 -55
  172. package/src/util/fileToBase64.ts +0 -18
  173. package/src/util/guessKeyCodeFromKey.ts +0 -36
  174. package/src/util/listPrefixMatch.ts +0 -19
  175. package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
  176. package/src/util/untilNextAnimationFrame.ts +0 -9
  177. package/src/util/waitForAll.ts +0 -18
  178. package/src/util/waitForTimeout.ts +0 -9
  179. package/src/version.test.ts +0 -12
  180. package/src/version.ts +0 -3
  181. package/tools/allLocales.js +0 -4
  182. package/tools/copyREADME.ts +0 -62
@@ -1,453 +0,0 @@
1
- import { Bezier } from 'bezier-js';
2
- import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
- import RenderablePathSpec from '../../rendering/RenderablePathSpec';
4
- import { Point2, Vec2, Rect2, PathCommand, PathCommandType, QuadraticBezierPathCommand, LinePathCommand } from '@js-draw/math';
5
- import Stroke from '../Stroke';
6
- import Viewport from '../../Viewport';
7
- import { StrokeDataPoint } from '../../types';
8
- import { ComponentBuilder, ComponentBuilderFactory } from './types';
9
- import RenderingStyle from '../../rendering/RenderingStyle';
10
- import { StrokeSmoother, Curve } from '../util/StrokeSmoother';
11
-
12
- export const makePressureSensitiveFreehandLineBuilder: ComponentBuilderFactory = (initialPoint: StrokeDataPoint, viewport: Viewport) => {
13
- // Don't smooth if input is more than ± 3 pixels from the true curve, do smooth if
14
- // less than ±1 px from the curve.
15
- const maxSmoothingDist = viewport.getSizeOfPixelOnCanvas() * 3;
16
- const minSmoothingDist = viewport.getSizeOfPixelOnCanvas();
17
-
18
- return new PressureSensitiveFreehandLineBuilder(
19
- initialPoint, minSmoothingDist, maxSmoothingDist, viewport
20
- );
21
- };
22
-
23
- type CurrentSegmentToPathResult = {
24
- upperCurveCommand: QuadraticBezierPathCommand,
25
- lowerToUpperConnector: PathCommand,
26
- upperToLowerConnector: PathCommand,
27
- lowerCurveCommand: QuadraticBezierPathCommand,
28
-
29
- upperCurve: Bezier,
30
- lowerCurve: Bezier,
31
- };
32
-
33
- // Handles stroke smoothing and creates Strokes from user/stylus input.
34
- export default class PressureSensitiveFreehandLineBuilder implements ComponentBuilder {
35
- private isFirstSegment: boolean = true;
36
- private pathStartConnector: PathCommand|null = null;
37
- private mostRecentConnector: PathCommand|null = null;
38
-
39
- // Beginning of the list of lower parts
40
- // ↓
41
- // /---pathStartConnector---/ ← Beginning of the list of upper parts
42
- // ___/ __/
43
- // / /
44
- // /--Most recent connector--/ ← most recent upper part goes here
45
- // ↑
46
- // most recent lower part goes here
47
- //
48
- // The upperSegments form a path that goes in reverse from the most recent edge to the
49
- // least recent edge.
50
- // The lowerSegments form a path that goes from the least recent edge to the most
51
- // recent edge.
52
- private upperSegments: PathCommand[];
53
- private lowerSegments: PathCommand[];
54
- private lastUpperBezier: Bezier|null = null;
55
- private lastLowerBezier: Bezier|null = null;
56
- private parts: RenderablePathSpec[] = [];
57
- private curveFitter: StrokeSmoother;
58
-
59
- private curveStartWidth: number;
60
-
61
- private bbox: Rect2;
62
-
63
- public constructor(
64
- private startPoint: StrokeDataPoint,
65
-
66
- // Maximum distance from the actual curve (irrespective of stroke width)
67
- // for which a point is considered 'part of the curve'.
68
- // Note that the maximum will be smaller if the stroke width is less than
69
- // [maxFitAllowed].
70
- private minFitAllowed: number,
71
- maxFitAllowed: number,
72
-
73
- private viewport: Viewport,
74
- ) {
75
- this.upperSegments = [];
76
- this.lowerSegments = [];
77
-
78
- this.curveFitter = new StrokeSmoother(startPoint, minFitAllowed, maxFitAllowed, curve => this.addCurve(curve));
79
-
80
- this.curveStartWidth = startPoint.width;
81
- this.bbox = new Rect2(this.startPoint.pos.x, this.startPoint.pos.y, 0, 0);
82
- }
83
-
84
- public getBBox(): Rect2 {
85
- return this.bbox;
86
- }
87
-
88
- private getRenderingStyle(): RenderingStyle {
89
- return {
90
- fill: this.startPoint.color ?? null,
91
- };
92
- }
93
-
94
- private previewCurrentPath(): RenderablePathSpec|null {
95
- const upperPath = this.upperSegments.slice();
96
- const lowerPath = this.lowerSegments.slice();
97
- let lowerToUpperCap: PathCommand;
98
- let pathStartConnector: PathCommand;
99
-
100
- const currentCurve = this.curveFitter.preview();
101
- if (currentCurve) {
102
- const {
103
- upperCurveCommand, lowerToUpperConnector, upperToLowerConnector, lowerCurveCommand
104
- } = this.segmentToPath(currentCurve);
105
-
106
- upperPath.push(upperCurveCommand);
107
- lowerPath.push(lowerCurveCommand);
108
-
109
- lowerToUpperCap = lowerToUpperConnector;
110
- pathStartConnector = this.pathStartConnector ?? upperToLowerConnector;
111
- } else {
112
- if (this.mostRecentConnector === null || this.pathStartConnector === null) {
113
- return null;
114
- }
115
-
116
- lowerToUpperCap = this.mostRecentConnector;
117
- pathStartConnector = this.pathStartConnector;
118
- }
119
-
120
- let startPoint: Point2;
121
- const lastLowerSegment = lowerPath[lowerPath.length - 1];
122
- if (lastLowerSegment.kind === PathCommandType.LineTo || lastLowerSegment.kind === PathCommandType.MoveTo) {
123
- startPoint = lastLowerSegment.point;
124
- } else {
125
- startPoint = lastLowerSegment.endPoint;
126
- }
127
-
128
- return {
129
- // Start at the end of the lower curve:
130
- // Start point
131
- // ↓
132
- // __/ __/ ← Most recent points on this end
133
- // /___ /
134
- // ↑
135
- // Oldest points
136
- startPoint,
137
-
138
- commands: [
139
- // Move to the most recent point on the upperPath:
140
- // ----→•
141
- // __/ __/
142
- // /___ /
143
- lowerToUpperCap,
144
-
145
- // Move to the beginning of the upperPath:
146
- // __/ __/
147
- // /___ /
148
- // • ←-
149
- ...upperPath.reverse(),
150
-
151
- // Move to the beginning of the lowerPath:
152
- // __/ __/
153
- // /___ /
154
- // •
155
- pathStartConnector,
156
-
157
- // Move back to the start point:
158
- // •
159
- // __/ __/
160
- // /___ /
161
- ...lowerPath,
162
- ],
163
- style: this.getRenderingStyle(),
164
- };
165
- }
166
-
167
- private previewFullPath(): RenderablePathSpec[]|null {
168
- const preview = this.previewCurrentPath();
169
- if (preview) {
170
- return [ ...this.parts, preview ];
171
- }
172
- return null;
173
- }
174
-
175
- private previewStroke(): Stroke|null {
176
- const pathPreview = this.previewFullPath();
177
-
178
- if (pathPreview) {
179
- return new Stroke(pathPreview);
180
- }
181
- return null;
182
- }
183
-
184
- public preview(renderer: AbstractRenderer) {
185
- const paths = this.previewFullPath();
186
- if (paths) {
187
- const approxBBox = this.viewport.visibleRect;
188
- renderer.startObject(approxBBox);
189
- for (const path of paths) {
190
- renderer.drawPath(path);
191
- }
192
- renderer.endObject();
193
- }
194
- }
195
-
196
- public build(): Stroke {
197
- this.curveFitter.finalizeCurrentCurve();
198
- if (this.isFirstSegment) {
199
- // Ensure we have something.
200
- this.addCurve(null);
201
- }
202
-
203
- return this.previewStroke()!;
204
- }
205
-
206
- private roundPoint(point: Point2): Point2 {
207
- let minFit = Math.min(this.minFitAllowed, this.curveStartWidth / 3);
208
-
209
- if (minFit < 1e-10) {
210
- minFit = this.minFitAllowed;
211
- }
212
-
213
- return Viewport.roundPoint(point, minFit);
214
- }
215
-
216
- // Returns true if, due to overlap with previous segments, a new RenderablePathSpec should be created.
217
- private shouldStartNewSegment(lowerCurve: Bezier, upperCurve: Bezier): boolean {
218
- if (!this.lastLowerBezier || !this.lastUpperBezier) {
219
- return false;
220
- }
221
-
222
- const getIntersection = (curve1: Bezier, curve2: Bezier): Point2|null => {
223
- const intersection = curve1.intersects(curve2) as (string[] | null | undefined);
224
- if (!intersection || intersection.length === 0) {
225
- return null;
226
- }
227
-
228
- // From http://pomax.github.io/bezierjs/#intersect-curve,
229
- // .intersects returns an array of 't1/t2' pairs, where curve1.at(t1) gives the point.
230
- const firstTPair = intersection[0];
231
- const match = /^([-0-9.eE]+)\/([-0-9.eE]+)$/.exec(firstTPair);
232
-
233
- if (!match) {
234
- throw new Error(
235
- `Incorrect format returned by .intersects: ${intersection} should be array of "number/number"!`
236
- );
237
- }
238
-
239
- const t = parseFloat(match[1]);
240
- return Vec2.ofXY(curve1.get(t));
241
- };
242
-
243
- const getExitDirection = (curve: Bezier): Vec2 => {
244
- return Vec2.ofXY(curve.points[2]).minus(Vec2.ofXY(curve.points[1])).normalized();
245
- };
246
-
247
- const getEnterDirection = (curve: Bezier): Vec2 => {
248
- return Vec2.ofXY(curve.points[1]).minus(Vec2.ofXY(curve.points[0])).normalized();
249
- };
250
-
251
- // Prevent
252
- // /
253
- // / /
254
- // / / /|
255
- // / / |
256
- // / |
257
- // where the next stroke and the previous stroke are in different directions.
258
- //
259
- // Are the exit/enter directions of the previous and current curves in different enough directions?
260
- if (getEnterDirection(upperCurve).dot(getExitDirection(this.lastUpperBezier)) < 0.3
261
- || getEnterDirection(lowerCurve).dot(getExitDirection(this.lastLowerBezier)) < 0.3
262
-
263
- // Also handle if the curves exit/enter directions differ
264
- || getEnterDirection(upperCurve).dot(getExitDirection(upperCurve)) < 0
265
- || getEnterDirection(lowerCurve).dot(getExitDirection(lowerCurve)) < 0) {
266
- return true;
267
- }
268
-
269
- // Check whether the lower curve intersects the other wall:
270
- // / / ← lower
271
- // / / /
272
- // / / /
273
- // //
274
- // / /
275
- const lowerIntersection = getIntersection(lowerCurve, this.lastUpperBezier);
276
- const upperIntersection = getIntersection(upperCurve, this.lastLowerBezier);
277
- if (lowerIntersection || upperIntersection) {
278
- return true;
279
- }
280
-
281
- return false;
282
- }
283
-
284
- private addCurve(curve: Curve|null) {
285
- // Case where no points have been added
286
- if (!curve) {
287
- // Don't create a circle around the initial point if the stroke has more than one point.
288
- if (!this.isFirstSegment) {
289
- return;
290
- }
291
-
292
- const width = Viewport.roundPoint(this.startPoint.width / 2.2, Math.min(this.minFitAllowed, this.startPoint.width / 4));
293
- const center = this.roundPoint(this.startPoint.pos);
294
-
295
- // Start on the right, cycle clockwise:
296
- // |
297
- // ----- ←
298
- // |
299
- const startPoint = this.startPoint.pos.plus(Vec2.of(width, 0));
300
-
301
- // Draw a circle-ish shape around the start point
302
- this.lowerSegments.push(
303
- {
304
- kind: PathCommandType.QuadraticBezierTo,
305
- controlPoint: center.plus(Vec2.of(width, width)),
306
-
307
- // Bottom of the circle
308
- // |
309
- // -----
310
- // |
311
- // ↑
312
- endPoint: center.plus(Vec2.of(0, width)),
313
- },
314
- {
315
- kind: PathCommandType.QuadraticBezierTo,
316
- controlPoint: center.plus(Vec2.of(-width, width)),
317
- endPoint: center.plus(Vec2.of(-width, 0)),
318
- },
319
- {
320
- kind: PathCommandType.QuadraticBezierTo,
321
- controlPoint: center.plus(Vec2.of(-width, -width)),
322
- endPoint: center.plus(Vec2.of(0, -width)),
323
- },
324
- {
325
- kind: PathCommandType.QuadraticBezierTo,
326
- controlPoint: center.plus(Vec2.of(width, -width)),
327
- endPoint: center.plus(Vec2.of(width, 0)),
328
- }
329
- );
330
- this.pathStartConnector = {
331
- kind: PathCommandType.LineTo,
332
- point: startPoint,
333
- };
334
- this.mostRecentConnector = this.pathStartConnector;
335
-
336
- return;
337
- }
338
-
339
- const {
340
- upperCurveCommand, lowerToUpperConnector, upperToLowerConnector, lowerCurveCommand,
341
- lowerCurve, upperCurve,
342
- } = this.segmentToPath(curve);
343
-
344
- const shouldStartNew = this.shouldStartNewSegment(lowerCurve, upperCurve);
345
-
346
- if (shouldStartNew) {
347
- const part = this.previewCurrentPath();
348
-
349
- if (part) {
350
- this.parts.push(part);
351
- this.upperSegments = [];
352
- this.lowerSegments = [];
353
- }
354
- }
355
-
356
- if (this.isFirstSegment || shouldStartNew) {
357
- // We draw the upper path (reversed), then the lower path, so we need the
358
- // upperToLowerConnector to join the two paths.
359
- this.pathStartConnector = upperToLowerConnector;
360
- this.isFirstSegment = false;
361
- }
362
- // With the most recent connector, we're joining the end of the lowerPath to the most recent
363
- // upperPath:
364
- this.mostRecentConnector = lowerToUpperConnector;
365
-
366
- this.lowerSegments.push(lowerCurveCommand);
367
- this.upperSegments.push(upperCurveCommand);
368
-
369
- this.lastLowerBezier = lowerCurve;
370
- this.lastUpperBezier = upperCurve;
371
- this.curveStartWidth = curve.startWidth;
372
- }
373
-
374
- // Returns [upper curve, connector, lower curve]
375
- private segmentToPath(curve: Curve): CurrentSegmentToPathResult {
376
- const bezier = new Bezier(curve.startPoint.xy, curve.controlPoint.xy, curve.endPoint.xy);
377
- let startVec = Vec2.ofXY(bezier.normal(0)).normalized();
378
- let endVec = Vec2.ofXY(bezier.normal(1)).normalized();
379
-
380
- startVec = startVec.times(curve.startWidth / 2);
381
- endVec = endVec.times(curve.endWidth / 2);
382
-
383
- if (!isFinite(startVec.magnitude())) {
384
- console.error('Warning: startVec is NaN or ∞', startVec, endVec, curve);
385
- startVec = endVec;
386
- }
387
-
388
- const startPt = curve.startPoint;
389
- const endPt = curve.endPoint;
390
- const controlPoint = curve.controlPoint;
391
-
392
- // Approximate the normal at the location of the control point
393
- let projectionT = bezier.project(controlPoint.xy).t;
394
- if (!projectionT) {
395
- if (startPt.minus(controlPoint).magnitudeSquared() < endPt.minus(controlPoint).magnitudeSquared()) {
396
- projectionT = 0.1;
397
- } else {
398
- projectionT = 0.9;
399
- }
400
- }
401
-
402
- const halfVecT = projectionT;
403
- const halfVec = Vec2.ofXY(bezier.normal(halfVecT))
404
- .normalized().times(
405
- curve.startWidth / 2 * halfVecT
406
- + curve.endWidth / 2 * (1 - halfVecT)
407
- );
408
-
409
- // Each starts at startPt ± startVec
410
- const lowerCurveStartPoint = this.roundPoint(startPt.plus(startVec));
411
- const lowerCurveControlPoint = this.roundPoint(controlPoint.plus(halfVec));
412
- const lowerCurveEndPoint = this.roundPoint(endPt.plus(endVec));
413
- const upperCurveControlPoint = this.roundPoint(controlPoint.minus(halfVec));
414
- const upperCurveStartPoint = this.roundPoint(endPt.minus(endVec));
415
- const upperCurveEndPoint = this.roundPoint(startPt.minus(startVec));
416
-
417
- const lowerCurveCommand: QuadraticBezierPathCommand = {
418
- kind: PathCommandType.QuadraticBezierTo,
419
- controlPoint: lowerCurveControlPoint,
420
- endPoint: lowerCurveEndPoint,
421
- };
422
-
423
- // From the end of the upperCurve to the start of the lowerCurve:
424
- const upperToLowerConnector: LinePathCommand = {
425
- kind: PathCommandType.LineTo,
426
- point: lowerCurveStartPoint,
427
- };
428
-
429
- // From the end of lowerCurve to the start of upperCurve:
430
- const lowerToUpperConnector: LinePathCommand = {
431
- kind: PathCommandType.LineTo,
432
- point: upperCurveStartPoint,
433
- };
434
-
435
- const upperCurveCommand: QuadraticBezierPathCommand = {
436
- kind: PathCommandType.QuadraticBezierTo,
437
- controlPoint: upperCurveControlPoint,
438
- endPoint: upperCurveEndPoint,
439
- };
440
-
441
- const upperCurve = new Bezier(upperCurveStartPoint, upperCurveControlPoint, upperCurveEndPoint);
442
- const lowerCurve = new Bezier(lowerCurveStartPoint, lowerCurveControlPoint, lowerCurveEndPoint);
443
-
444
- return {
445
- upperCurveCommand, upperToLowerConnector, lowerToUpperConnector, lowerCurveCommand,
446
- upperCurve, lowerCurve,
447
- };
448
- }
449
-
450
- public addPoint(newPoint: StrokeDataPoint) {
451
- this.curveFitter.addPoint(newPoint);
452
- }
453
- }
@@ -1,73 +0,0 @@
1
- import { Mat33, Rect2, Path } from '@js-draw/math';
2
- import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
- import { pathToRenderable } from '../../rendering/RenderablePathSpec';
4
- import { StrokeDataPoint } from '../../types';
5
- import Viewport from '../../Viewport';
6
- import AbstractComponent from '../AbstractComponent';
7
- import Stroke from '../Stroke';
8
- import { ComponentBuilder, ComponentBuilderFactory } from './types';
9
-
10
- export const makeFilledRectangleBuilder: ComponentBuilderFactory = (initialPoint: StrokeDataPoint, viewport: Viewport) => {
11
- return new RectangleBuilder(initialPoint, true, viewport);
12
- };
13
-
14
- export const makeOutlinedRectangleBuilder: ComponentBuilderFactory = (initialPoint: StrokeDataPoint, viewport: Viewport) => {
15
- return new RectangleBuilder(initialPoint, false, viewport);
16
- };
17
-
18
- export default class RectangleBuilder implements ComponentBuilder {
19
- private endPoint: StrokeDataPoint;
20
-
21
- public constructor(
22
- private readonly startPoint: StrokeDataPoint,
23
- private filled: boolean,
24
- private viewport: Viewport,
25
- ) {
26
- // Initially, the start and end points are the same.
27
- this.endPoint = startPoint;
28
- }
29
-
30
- public getBBox(): Rect2 {
31
- const preview = this.buildPreview();
32
- return preview.getBBox();
33
- }
34
-
35
- private buildPreview(): Stroke {
36
- const canvasAngle = this.viewport.getRotationAngle();
37
- const rotationMat = Mat33.zRotation(-canvasAngle);
38
-
39
- // Adjust startPoint and endPoint such that applying [rotationMat] to them
40
- // brings them to this.startPoint and this.endPoint.
41
- const startPoint = rotationMat.inverse().transformVec2(this.startPoint.pos);
42
- const endPoint = rotationMat.inverse().transformVec2(this.endPoint.pos);
43
-
44
- const rect = Rect2.fromCorners(startPoint, endPoint);
45
- const path = Path.fromRect(
46
- rect,
47
- this.filled ? null : this.endPoint.width,
48
- ).transformedBy(
49
- // Rotate the canvas rectangle so that its rotation matches the screen
50
- rotationMat
51
- ).mapPoints(point => this.viewport.roundPoint(point));
52
-
53
- const preview = new Stroke([
54
- pathToRenderable(path, {
55
- fill: this.endPoint.color
56
- }),
57
- ]);
58
-
59
- return preview;
60
- }
61
-
62
- public build(): AbstractComponent {
63
- return this.buildPreview();
64
- }
65
-
66
- public preview(renderer: AbstractRenderer): void {
67
- this.buildPreview().render(renderer);
68
- }
69
-
70
- public addPoint(point: StrokeDataPoint): void {
71
- this.endPoint = point;
72
- }
73
- }
@@ -1,15 +0,0 @@
1
- import { Rect2 } from '@js-draw/math';
2
- import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
- import { StrokeDataPoint } from '../../types';
4
- import Viewport from '../../Viewport';
5
- import AbstractComponent from '../AbstractComponent';
6
-
7
- export interface ComponentBuilder {
8
- getBBox(): Rect2;
9
- build(): AbstractComponent;
10
- preview(renderer: AbstractRenderer): void;
11
-
12
- addPoint(point: StrokeDataPoint): void;
13
- }
14
-
15
- export type ComponentBuilderFactory = (startPoint: StrokeDataPoint, viewport: Viewport)=> ComponentBuilder;
@@ -1,31 +0,0 @@
1
- export * from './builders/types';
2
- export { makeFreehandLineBuilder } from './builders/FreehandLineBuilder';
3
- export { makePressureSensitiveFreehandLineBuilder } from './builders/PressureSensitiveFreehandLineBuilder';
4
- export { makeOutlinedCircleBuilder } from './builders/CircleBuilder';
5
- export { default as StrokeSmoother, Curve as StrokeSmootherCurve } from './util/StrokeSmoother';
6
-
7
- export * from './AbstractComponent';
8
- export { default as AbstractComponent } from './AbstractComponent';
9
- import Stroke from './Stroke';
10
- import TextComponent from './TextComponent';
11
- import ImageComponent from './ImageComponent';
12
- import RestyleableComponent from './RestylableComponent';
13
- import { createRestyleComponentCommand, isRestylableComponent, ComponentStyle as RestyleableComponentStyle } from './RestylableComponent';
14
- import BackgroundComponent from './BackgroundComponent';
15
-
16
- export {
17
- Stroke,
18
- RestyleableComponent,
19
- createRestyleComponentCommand,
20
- isRestylableComponent,
21
- RestyleableComponentStyle,
22
-
23
- TextComponent,
24
-
25
- /** @deprecated use {@link TextComponent} */
26
- TextComponent as Text,
27
-
28
- Stroke as StrokeComponent,
29
- BackgroundComponent,
30
- ImageComponent,
31
- };
@@ -1,24 +0,0 @@
1
- export interface ImageComponentLocalization {
2
- unlabeledImageNode: string;
3
- text: (text: string)=> string;
4
- imageNode: (description: string)=> string;
5
- stroke: string;
6
- svgObject: string;
7
- emptyBackground: string;
8
- gridBackground: string;
9
- filledBackgroundWithColor: (color: string)=> string;
10
-
11
- restyledElement: (elementDescription: string) => string;
12
- }
13
-
14
- export const defaultComponentLocalization: ImageComponentLocalization = {
15
- unlabeledImageNode: 'Unlabeled image node',
16
- stroke: 'Stroke',
17
- svgObject: 'SVG Object',
18
- emptyBackground: 'Empty background',
19
- gridBackground: 'Grid background',
20
- filledBackgroundWithColor: (color) => `Filled background (${color})`,
21
- text: (text) => `Text object: ${text}`,
22
- imageNode: (description: string) => `Image: ${description}`,
23
- restyledElement: (elementDescription: string) => `Restyled ${elementDescription}`,
24
- };