js-draw 0.7.2 → 0.9.0

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 (82) hide show
  1. package/.github/ISSUE_TEMPLATE/translation.md +1 -0
  2. package/CHANGELOG.md +12 -0
  3. package/dist/bundle.js +1 -1
  4. package/dist/src/Color4.js +3 -0
  5. package/dist/src/SVGLoader.js +5 -7
  6. package/dist/src/components/Stroke.js +10 -3
  7. package/dist/src/components/builders/FreehandLineBuilder.d.ts +10 -23
  8. package/dist/src/components/builders/FreehandLineBuilder.js +70 -396
  9. package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.d.ts +36 -0
  10. package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.js +339 -0
  11. package/dist/src/components/lib.d.ts +2 -0
  12. package/dist/src/components/lib.js +2 -0
  13. package/dist/src/components/util/StrokeSmoother.d.ts +35 -0
  14. package/dist/src/components/util/StrokeSmoother.js +206 -0
  15. package/dist/src/math/Mat33.d.ts +2 -0
  16. package/dist/src/math/Mat33.js +4 -0
  17. package/dist/src/math/Path.d.ts +2 -0
  18. package/dist/src/math/Path.js +44 -0
  19. package/dist/src/rendering/renderers/CanvasRenderer.js +2 -0
  20. package/dist/src/rendering/renderers/SVGRenderer.d.ts +2 -0
  21. package/dist/src/rendering/renderers/SVGRenderer.js +20 -0
  22. package/dist/src/toolbar/HTMLToolbar.d.ts +3 -0
  23. package/dist/src/toolbar/HTMLToolbar.js +24 -0
  24. package/dist/src/toolbar/IconProvider.d.ts +1 -0
  25. package/dist/src/toolbar/IconProvider.js +43 -1
  26. package/dist/src/toolbar/localization.d.ts +2 -0
  27. package/dist/src/toolbar/localization.js +2 -0
  28. package/dist/src/toolbar/makeColorInput.d.ts +2 -1
  29. package/dist/src/toolbar/makeColorInput.js +13 -2
  30. package/dist/src/toolbar/widgets/ActionButtonWidget.d.ts +1 -1
  31. package/dist/src/toolbar/widgets/ActionButtonWidget.js +2 -2
  32. package/dist/src/toolbar/widgets/BaseToolWidget.d.ts +1 -2
  33. package/dist/src/toolbar/widgets/BaseToolWidget.js +2 -3
  34. package/dist/src/toolbar/widgets/BaseWidget.d.ts +32 -2
  35. package/dist/src/toolbar/widgets/BaseWidget.js +67 -6
  36. package/dist/src/toolbar/widgets/EraserToolWidget.d.ts +4 -0
  37. package/dist/src/toolbar/widgets/EraserToolWidget.js +3 -0
  38. package/dist/src/toolbar/widgets/HandToolWidget.d.ts +3 -1
  39. package/dist/src/toolbar/widgets/HandToolWidget.js +22 -13
  40. package/dist/src/toolbar/widgets/PenToolWidget.d.ts +7 -1
  41. package/dist/src/toolbar/widgets/PenToolWidget.js +83 -12
  42. package/dist/src/toolbar/widgets/SelectionToolWidget.d.ts +1 -1
  43. package/dist/src/toolbar/widgets/SelectionToolWidget.js +7 -7
  44. package/dist/src/toolbar/widgets/TextToolWidget.d.ts +4 -1
  45. package/dist/src/toolbar/widgets/TextToolWidget.js +17 -3
  46. package/dist/src/tools/PanZoom.d.ts +4 -1
  47. package/dist/src/tools/PanZoom.js +24 -1
  48. package/dist/src/tools/Pen.d.ts +2 -2
  49. package/dist/src/tools/Pen.js +2 -2
  50. package/dist/src/tools/SelectionTool/Selection.d.ts +1 -0
  51. package/dist/src/tools/SelectionTool/Selection.js +8 -1
  52. package/dist/src/tools/ToolController.js +2 -1
  53. package/package.json +1 -1
  54. package/src/Color4.ts +2 -0
  55. package/src/SVGLoader.ts +8 -8
  56. package/src/components/Stroke.ts +16 -3
  57. package/src/components/builders/FreehandLineBuilder.ts +54 -495
  58. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +454 -0
  59. package/src/components/lib.ts +3 -1
  60. package/src/components/util/StrokeSmoother.ts +290 -0
  61. package/src/math/Mat33.ts +5 -0
  62. package/src/math/Path.test.ts +49 -0
  63. package/src/math/Path.ts +51 -0
  64. package/src/rendering/renderers/CanvasRenderer.ts +2 -0
  65. package/src/rendering/renderers/SVGRenderer.ts +24 -0
  66. package/src/toolbar/HTMLToolbar.ts +33 -0
  67. package/src/toolbar/IconProvider.ts +49 -1
  68. package/src/toolbar/localization.ts +4 -0
  69. package/src/toolbar/makeColorInput.ts +21 -3
  70. package/src/toolbar/widgets/ActionButtonWidget.ts +6 -3
  71. package/src/toolbar/widgets/BaseToolWidget.ts +4 -3
  72. package/src/toolbar/widgets/BaseWidget.ts +83 -5
  73. package/src/toolbar/widgets/EraserToolWidget.ts +11 -0
  74. package/src/toolbar/widgets/HandToolWidget.ts +48 -17
  75. package/src/toolbar/widgets/PenToolWidget.ts +110 -13
  76. package/src/toolbar/widgets/SelectionToolWidget.ts +8 -5
  77. package/src/toolbar/widgets/TextToolWidget.ts +29 -4
  78. package/src/tools/PanZoom.ts +28 -1
  79. package/src/tools/Pen.test.ts +2 -2
  80. package/src/tools/Pen.ts +1 -1
  81. package/src/tools/SelectionTool/Selection.ts +10 -1
  82. package/src/tools/ToolController.ts +2 -1
@@ -1,10 +1,10 @@
1
- import { Bezier } from 'bezier-js';
2
1
  import { Vec2 } from '../../math/Vec2';
3
2
  import Rect2 from '../../math/Rect2';
4
3
  import { PathCommandType } from '../../math/Path';
5
- import LineSegment2 from '../../math/LineSegment2';
6
4
  import Stroke from '../Stroke';
7
5
  import Viewport from '../../Viewport';
6
+ import { StrokeSmoother } from '../util/StrokeSmoother';
7
+ import Color4 from '../../Color4';
8
8
  export const makeFreehandLineBuilder = (initialPoint, viewport) => {
9
9
  // Don't smooth if input is more than ± 3 pixels from the true curve, do smooth if
10
10
  // less than ±1 px from the curve.
@@ -14,108 +14,43 @@ export const makeFreehandLineBuilder = (initialPoint, viewport) => {
14
14
  };
15
15
  // Handles stroke smoothing and creates Strokes from user/stylus input.
16
16
  export default class FreehandLineBuilder {
17
- constructor(startPoint,
18
- // Maximum distance from the actual curve (irrespective of stroke width)
19
- // for which a point is considered 'part of the curve'.
20
- // Note that the maximum will be smaller if the stroke width is less than
21
- // [maxFitAllowed].
22
- minFitAllowed, maxFitAllowed, viewport) {
17
+ constructor(startPoint, minFitAllowed, maxFitAllowed, viewport) {
23
18
  this.startPoint = startPoint;
24
19
  this.minFitAllowed = minFitAllowed;
25
- this.maxFitAllowed = maxFitAllowed;
26
20
  this.viewport = viewport;
27
21
  this.isFirstSegment = true;
28
- this.pathStartConnector = null;
29
- this.mostRecentConnector = null;
30
- this.lastUpperBezier = null;
31
- this.lastLowerBezier = null;
32
22
  this.parts = [];
33
- this.lastExitingVec = null;
34
- this.currentCurve = null;
35
- this.lastPoint = this.startPoint;
36
- this.upperSegments = [];
37
- this.lowerSegments = [];
38
- this.buffer = [this.startPoint.pos];
39
- this.momentum = Vec2.zero;
40
- this.currentCurve = null;
41
- this.curveStartWidth = startPoint.width;
23
+ this.widthAverageNumSamples = 1;
24
+ this.curveFitter = new StrokeSmoother(startPoint, minFitAllowed, maxFitAllowed, (curve) => this.addCurve(curve));
25
+ this.averageWidth = startPoint.width;
42
26
  this.bbox = new Rect2(this.startPoint.pos.x, this.startPoint.pos.y, 0, 0);
43
27
  }
44
28
  getBBox() {
45
29
  return this.bbox;
46
30
  }
47
31
  getRenderingStyle() {
48
- var _a;
49
32
  return {
50
- fill: (_a = this.lastPoint.color) !== null && _a !== void 0 ? _a : null,
33
+ fill: Color4.transparent,
34
+ stroke: {
35
+ color: this.startPoint.color,
36
+ width: this.averageWidth,
37
+ }
51
38
  };
52
39
  }
53
40
  previewCurrentPath() {
54
- var _a;
55
- const upperPath = this.upperSegments.slice();
56
- const lowerPath = this.lowerSegments.slice();
57
- let lowerToUpperCap;
58
- let pathStartConnector;
59
- if (this.currentCurve) {
60
- const { upperCurveCommand, lowerToUpperConnector, upperToLowerConnector, lowerCurveCommand } = this.currentSegmentToPath();
61
- upperPath.push(upperCurveCommand);
62
- lowerPath.push(lowerCurveCommand);
63
- lowerToUpperCap = lowerToUpperConnector;
64
- pathStartConnector = (_a = this.pathStartConnector) !== null && _a !== void 0 ? _a : upperToLowerConnector;
65
- }
66
- else {
67
- if (this.mostRecentConnector === null || this.pathStartConnector === null) {
68
- return null;
69
- }
70
- lowerToUpperCap = this.mostRecentConnector;
71
- pathStartConnector = this.pathStartConnector;
72
- }
73
- let startPoint;
74
- const lastLowerSegment = lowerPath[lowerPath.length - 1];
75
- if (lastLowerSegment.kind === PathCommandType.LineTo || lastLowerSegment.kind === PathCommandType.MoveTo) {
76
- startPoint = lastLowerSegment.point;
77
- }
78
- else {
79
- startPoint = lastLowerSegment.endPoint;
80
- }
41
+ const path = this.parts.slice();
42
+ const commands = [...path, ...this.curveToPathCommands(this.curveFitter.preview())];
43
+ const startPoint = this.startPoint.pos;
81
44
  return {
82
- // Start at the end of the lower curve:
83
- // Start point
84
- // ↓
85
- // __/ __/ ← Most recent points on this end
86
- // /___ /
87
- // ↑
88
- // Oldest points
89
45
  startPoint,
90
- commands: [
91
- // Move to the most recent point on the upperPath:
92
- // ----→•
93
- // __/ __/
94
- // /___ /
95
- lowerToUpperCap,
96
- // Move to the beginning of the upperPath:
97
- // __/ __/
98
- // /___ /
99
- // • ←-
100
- ...upperPath.reverse(),
101
- // Move to the beginning of the lowerPath:
102
- // __/ __/
103
- // /___ /
104
- // •
105
- pathStartConnector,
106
- // Move back to the start point:
107
- // •
108
- // __/ __/
109
- // /___ /
110
- ...lowerPath,
111
- ],
46
+ commands,
112
47
  style: this.getRenderingStyle(),
113
48
  };
114
49
  }
115
50
  previewFullPath() {
116
51
  const preview = this.previewCurrentPath();
117
52
  if (preview) {
118
- return [...this.parts, preview];
53
+ return [preview];
119
54
  }
120
55
  return null;
121
56
  }
@@ -138,91 +73,22 @@ export default class FreehandLineBuilder {
138
73
  }
139
74
  }
140
75
  build() {
141
- if (this.lastPoint) {
142
- this.finalizeCurrentCurve();
143
- }
76
+ this.curveFitter.finalizeCurrentCurve();
144
77
  return this.previewStroke();
145
78
  }
146
79
  roundPoint(point) {
147
- let minFit = Math.min(this.minFitAllowed, this.curveStartWidth / 2);
80
+ let minFit = Math.min(this.minFitAllowed, this.averageWidth / 2);
148
81
  if (minFit < 1e-10) {
149
82
  minFit = this.minFitAllowed;
150
83
  }
151
84
  return Viewport.roundPoint(point, minFit);
152
85
  }
153
- // Returns true if, due to overlap with previous segments, a new RenderablePathSpec should be created.
154
- shouldStartNewSegment(lowerCurve, upperCurve) {
155
- if (!this.lastLowerBezier || !this.lastUpperBezier) {
156
- return false;
157
- }
158
- const getIntersection = (curve1, curve2) => {
159
- const intersection = curve1.intersects(curve2);
160
- if (!intersection || intersection.length === 0) {
161
- return null;
162
- }
163
- // From http://pomax.github.io/bezierjs/#intersect-curve,
164
- // .intersects returns an array of 't1/t2' pairs, where curve1.at(t1) gives the point.
165
- const firstTPair = intersection[0];
166
- const match = /^([-0-9.eE]+)\/([-0-9.eE]+)$/.exec(firstTPair);
167
- if (!match) {
168
- throw new Error(`Incorrect format returned by .intersects: ${intersection} should be array of "number/number"!`);
169
- }
170
- const t = parseFloat(match[1]);
171
- return Vec2.ofXY(curve1.get(t));
172
- };
173
- const getExitDirection = (curve) => {
174
- return Vec2.ofXY(curve.points[2]).minus(Vec2.ofXY(curve.points[1])).normalized();
175
- };
176
- const getEnterDirection = (curve) => {
177
- return Vec2.ofXY(curve.points[1]).minus(Vec2.ofXY(curve.points[0])).normalized();
178
- };
179
- // Prevent
180
- // /
181
- // / /
182
- // / / /|
183
- // / / |
184
- // / |
185
- // where the next stroke and the previous stroke are in different directions.
186
- //
187
- // Are the exit/enter directions of the previous and current curves in different enough directions?
188
- if (getEnterDirection(upperCurve).dot(getExitDirection(this.lastUpperBezier)) < 0.3
189
- || getEnterDirection(lowerCurve).dot(getExitDirection(this.lastLowerBezier)) < 0.3
190
- // Also handle if the curves exit/enter directions differ
191
- || getEnterDirection(upperCurve).dot(getExitDirection(upperCurve)) < 0
192
- || getEnterDirection(lowerCurve).dot(getExitDirection(lowerCurve)) < 0) {
193
- return true;
194
- }
195
- // Check whether the lower curve intersects the other wall:
196
- // / / ← lower
197
- // / / /
198
- // / / /
199
- // //
200
- // / /
201
- const lowerIntersection = getIntersection(lowerCurve, this.lastUpperBezier);
202
- const upperIntersection = getIntersection(upperCurve, this.lastLowerBezier);
203
- if (lowerIntersection || upperIntersection) {
204
- return true;
205
- }
206
- return false;
207
- }
208
- // Returns the distance between the start, control, and end points of the curve.
209
- approxCurrentCurveLength() {
210
- if (!this.currentCurve) {
211
- return 0;
212
- }
213
- const startPt = Vec2.ofXY(this.currentCurve.points[0]);
214
- const controlPt = Vec2.ofXY(this.currentCurve.points[1]);
215
- const endPt = Vec2.ofXY(this.currentCurve.points[2]);
216
- const toControlDist = startPt.minus(controlPt).length();
217
- const toEndDist = endPt.minus(controlPt).length();
218
- return toControlDist + toEndDist;
219
- }
220
- finalizeCurrentCurve() {
86
+ curveToPathCommands(curve) {
221
87
  // Case where no points have been added
222
- if (!this.currentCurve) {
88
+ if (!curve) {
223
89
  // Don't create a circle around the initial point if the stroke has more than one point.
224
90
  if (!this.isFirstSegment) {
225
- return;
91
+ return [];
226
92
  }
227
93
  const width = Viewport.roundPoint(this.startPoint.width / 3.5, Math.min(this.minFitAllowed, this.startPoint.width / 4));
228
94
  const center = this.roundPoint(this.startPoint.pos);
@@ -230,253 +96,61 @@ export default class FreehandLineBuilder {
230
96
  // |
231
97
  // ----- ←
232
98
  // |
233
- const startPoint = this.startPoint.pos.plus(Vec2.of(width, 0));
234
99
  // Draw a circle-ish shape around the start point
235
- this.lowerSegments.push({
236
- kind: PathCommandType.QuadraticBezierTo,
237
- controlPoint: center.plus(Vec2.of(width, width)),
238
- // Bottom of the circle
239
- // |
240
- // -----
241
- // |
242
- //
243
- endPoint: center.plus(Vec2.of(0, width)),
244
- }, {
245
- kind: PathCommandType.QuadraticBezierTo,
246
- controlPoint: center.plus(Vec2.of(-width, width)),
247
- endPoint: center.plus(Vec2.of(-width, 0)),
248
- }, {
249
- kind: PathCommandType.QuadraticBezierTo,
250
- controlPoint: center.plus(Vec2.of(-width, -width)),
251
- endPoint: center.plus(Vec2.of(0, -width)),
252
- }, {
253
- kind: PathCommandType.QuadraticBezierTo,
254
- controlPoint: center.plus(Vec2.of(width, -width)),
255
- endPoint: center.plus(Vec2.of(width, 0)),
256
- });
257
- this.pathStartConnector = {
258
- kind: PathCommandType.LineTo,
259
- point: startPoint,
260
- };
261
- this.mostRecentConnector = this.pathStartConnector;
262
- return;
263
- }
264
- const { upperCurveCommand, lowerToUpperConnector, upperToLowerConnector, lowerCurveCommand, lowerCurve, upperCurve, } = this.currentSegmentToPath();
265
- const shouldStartNew = this.shouldStartNewSegment(lowerCurve, upperCurve);
266
- if (shouldStartNew) {
267
- const part = this.previewCurrentPath();
268
- if (part) {
269
- this.parts.push(part);
270
- this.upperSegments = [];
271
- this.lowerSegments = [];
272
- }
273
- }
274
- if (this.isFirstSegment || shouldStartNew) {
275
- // We draw the upper path (reversed), then the lower path, so we need the
276
- // upperToLowerConnector to join the two paths.
277
- this.pathStartConnector = upperToLowerConnector;
278
- this.isFirstSegment = false;
279
- }
280
- // With the most recent connector, we're joining the end of the lowerPath to the most recent
281
- // upperPath:
282
- this.mostRecentConnector = lowerToUpperConnector;
283
- this.lowerSegments.push(lowerCurveCommand);
284
- this.upperSegments.push(upperCurveCommand);
285
- this.lastLowerBezier = lowerCurve;
286
- this.lastUpperBezier = upperCurve;
287
- const lastPoint = this.buffer[this.buffer.length - 1];
288
- this.lastExitingVec = Vec2.ofXY(this.currentCurve.points[2]).minus(Vec2.ofXY(this.currentCurve.points[1]));
289
- console.assert(this.lastExitingVec.magnitude() !== 0, 'lastExitingVec has zero length!');
290
- // Use the last two points to start a new curve (the last point isn't used
291
- // in the current curve and we want connected curves to share end points)
292
- this.buffer = [
293
- this.buffer[this.buffer.length - 2], lastPoint,
294
- ];
295
- this.currentCurve = null;
296
- }
297
- // Returns [upper curve, connector, lower curve]
298
- currentSegmentToPath() {
299
- if (this.currentCurve == null) {
300
- throw new Error('Invalid State: currentCurve is null!');
301
- }
302
- let startVec = Vec2.ofXY(this.currentCurve.normal(0)).normalized();
303
- let endVec = Vec2.ofXY(this.currentCurve.normal(1)).normalized();
304
- startVec = startVec.times(this.curveStartWidth / 2);
305
- endVec = endVec.times(this.curveEndWidth / 2);
306
- if (!isFinite(startVec.magnitude())) {
307
- console.error('Warning: startVec is NaN or ∞', startVec, endVec, this.currentCurve);
308
- startVec = endVec;
100
+ return [
101
+ {
102
+ kind: PathCommandType.QuadraticBezierTo,
103
+ controlPoint: center.plus(Vec2.of(width, width)),
104
+ // Bottom of the circle
105
+ // |
106
+ // -----
107
+ // |
108
+ // ↑
109
+ endPoint: center.plus(Vec2.of(0, width)),
110
+ },
111
+ {
112
+ kind: PathCommandType.QuadraticBezierTo,
113
+ controlPoint: center.plus(Vec2.of(-width, width)),
114
+ endPoint: center.plus(Vec2.of(-width, 0)),
115
+ },
116
+ {
117
+ kind: PathCommandType.QuadraticBezierTo,
118
+ controlPoint: center.plus(Vec2.of(-width, -width)),
119
+ endPoint: center.plus(Vec2.of(0, -width)),
120
+ },
121
+ {
122
+ kind: PathCommandType.QuadraticBezierTo,
123
+ controlPoint: center.plus(Vec2.of(width, -width)),
124
+ endPoint: center.plus(Vec2.of(width, 0)),
125
+ }
126
+ ];
309
127
  }
310
- const startPt = Vec2.ofXY(this.currentCurve.get(0));
311
- const endPt = Vec2.ofXY(this.currentCurve.get(1));
312
- const controlPoint = Vec2.ofXY(this.currentCurve.points[1]);
313
- // Approximate the normal at the location of the control point
314
- let projectionT = this.currentCurve.project(controlPoint.xy).t;
315
- if (!projectionT) {
316
- if (startPt.minus(controlPoint).magnitudeSquared() < endPt.minus(controlPoint).magnitudeSquared()) {
317
- projectionT = 0.1;
318
- }
319
- else {
320
- projectionT = 0.9;
321
- }
128
+ const result = [];
129
+ if (this.isFirstSegment) {
130
+ result.push({
131
+ kind: PathCommandType.MoveTo,
132
+ point: this.roundPoint(curve.startPoint),
133
+ });
322
134
  }
323
- const halfVecT = projectionT;
324
- const halfVec = Vec2.ofXY(this.currentCurve.normal(halfVecT))
325
- .normalized().times(this.curveStartWidth / 2 * halfVecT
326
- + this.curveEndWidth / 2 * (1 - halfVecT));
327
- // Each starts at startPt ± startVec
328
- const lowerCurveStartPoint = this.roundPoint(startPt.plus(startVec));
329
- const lowerCurveControlPoint = this.roundPoint(controlPoint.plus(halfVec));
330
- const lowerCurveEndPoint = this.roundPoint(endPt.plus(endVec));
331
- const upperCurveControlPoint = this.roundPoint(controlPoint.minus(halfVec));
332
- const upperCurveStartPoint = this.roundPoint(endPt.minus(endVec));
333
- const upperCurveEndPoint = this.roundPoint(startPt.minus(startVec));
334
- const lowerCurveCommand = {
335
- kind: PathCommandType.QuadraticBezierTo,
336
- controlPoint: lowerCurveControlPoint,
337
- endPoint: lowerCurveEndPoint,
338
- };
339
- // From the end of the upperCurve to the start of the lowerCurve:
340
- const upperToLowerConnector = {
341
- kind: PathCommandType.LineTo,
342
- point: lowerCurveStartPoint,
343
- };
344
- // From the end of lowerCurve to the start of upperCurve:
345
- const lowerToUpperConnector = {
346
- kind: PathCommandType.LineTo,
347
- point: upperCurveStartPoint,
348
- };
349
- const upperCurveCommand = {
135
+ result.push({
350
136
  kind: PathCommandType.QuadraticBezierTo,
351
- controlPoint: upperCurveControlPoint,
352
- endPoint: upperCurveEndPoint,
353
- };
354
- const upperCurve = new Bezier(upperCurveStartPoint, upperCurveControlPoint, upperCurveEndPoint);
355
- const lowerCurve = new Bezier(lowerCurveStartPoint, lowerCurveControlPoint, lowerCurveEndPoint);
356
- return {
357
- upperCurveCommand, upperToLowerConnector, lowerToUpperConnector, lowerCurveCommand,
358
- upperCurve, lowerCurve,
359
- };
137
+ controlPoint: this.roundPoint(curve.controlPoint),
138
+ endPoint: this.roundPoint(curve.endPoint),
139
+ });
140
+ return result;
360
141
  }
361
- // Compute the direction of the velocity at the end of this.buffer
362
- computeExitingVec() {
363
- return this.momentum.normalized().times(this.lastPoint.width / 2);
364
- }
365
- addPoint(newPoint) {
366
- var _a, _b;
367
- if (this.lastPoint) {
368
- // Ignore points that are identical
369
- const fuzzEq = 1e-10;
370
- const deltaTime = newPoint.time - this.lastPoint.time;
371
- if (newPoint.pos.eq(this.lastPoint.pos, fuzzEq) || deltaTime === 0) {
372
- return;
373
- }
374
- else if (isNaN(newPoint.pos.magnitude())) {
375
- console.warn('Discarding NaN point.', newPoint);
376
- return;
377
- }
378
- const threshold = Math.min(this.lastPoint.width, newPoint.width) / 3;
379
- const shouldSnapToInitial = this.startPoint.pos.minus(newPoint.pos).magnitude() < threshold
380
- && this.isFirstSegment;
381
- // Snap to the starting point if the stroke is contained within a small ball centered
382
- // at the starting point.
383
- // This allows us to create a circle/dot at the start of the stroke.
384
- if (shouldSnapToInitial) {
385
- return;
386
- }
387
- const velocity = newPoint.pos.minus(this.lastPoint.pos).times(1 / (deltaTime) * 1000);
388
- this.momentum = this.momentum.lerp(velocity, 0.9);
389
- }
390
- const lastPoint = (_a = this.lastPoint) !== null && _a !== void 0 ? _a : newPoint;
391
- this.lastPoint = newPoint;
392
- this.buffer.push(newPoint.pos);
393
- const pointRadius = newPoint.width / 2;
394
- const prevEndWidth = this.curveEndWidth;
395
- this.curveEndWidth = pointRadius;
142
+ addCurve(curve) {
143
+ const parts = this.curveToPathCommands(curve);
144
+ this.parts.push(...parts);
396
145
  if (this.isFirstSegment) {
397
- // The start of a curve often lacks accurate pressure information. Update it.
398
- this.curveStartWidth = (this.curveStartWidth + pointRadius) / 2;
399
- }
400
- // recompute bbox
401
- this.bbox = this.bbox.grownToPoint(newPoint.pos, pointRadius);
402
- if (this.currentCurve === null) {
403
- const p1 = lastPoint.pos;
404
- const p2 = lastPoint.pos.plus((_b = this.lastExitingVec) !== null && _b !== void 0 ? _b : Vec2.unitX);
405
- const p3 = newPoint.pos;
406
- // Quadratic Bézier curve
407
- this.currentCurve = new Bezier(p1.xy, p2.xy, p3.xy);
408
- this.curveStartWidth = lastPoint.width / 2;
409
- console.assert(!isNaN(p1.magnitude()) && !isNaN(p2.magnitude()) && !isNaN(p3.magnitude()), 'Expected !NaN');
410
- }
411
- // If there isn't an entering vector (e.g. because this.isFirstCurve), approximate it.
412
- let enteringVec = this.lastExitingVec;
413
- if (!enteringVec) {
414
- let sampleIdx = Math.ceil(this.buffer.length / 2);
415
- if (sampleIdx === 0 || sampleIdx >= this.buffer.length) {
416
- sampleIdx = this.buffer.length - 1;
417
- }
418
- enteringVec = this.buffer[sampleIdx].minus(this.buffer[0]);
419
- }
420
- let exitingVec = this.computeExitingVec();
421
- // Find the intersection between the entering vector and the exiting vector
422
- const maxRelativeLength = 2;
423
- const segmentStart = this.buffer[0];
424
- const segmentEnd = newPoint.pos;
425
- const startEndDist = segmentEnd.minus(segmentStart).magnitude();
426
- const maxControlPointDist = maxRelativeLength * startEndDist;
427
- // Exit in cases where we would divide by zero
428
- if (maxControlPointDist === 0 || exitingVec.magnitude() === 0 || !isFinite(exitingVec.magnitude())) {
429
- return;
430
- }
431
- console.assert(isFinite(enteringVec.magnitude()), 'Pre-normalized enteringVec has NaN or ∞ magnitude!');
432
- enteringVec = enteringVec.normalized();
433
- exitingVec = exitingVec.normalized();
434
- console.assert(isFinite(enteringVec.magnitude()), 'Normalized enteringVec has NaN or ∞ magnitude!');
435
- const lineFromStart = new LineSegment2(segmentStart, segmentStart.plus(enteringVec.times(maxControlPointDist)));
436
- const lineFromEnd = new LineSegment2(segmentEnd.minus(exitingVec.times(maxControlPointDist)), segmentEnd);
437
- const intersection = lineFromEnd.intersection(lineFromStart);
438
- // Position the control point at this intersection
439
- let controlPoint = null;
440
- if (intersection) {
441
- controlPoint = intersection.point;
442
- }
443
- // No intersection or the intersection is one of the end points?
444
- if (!controlPoint || segmentStart.eq(controlPoint) || segmentEnd.eq(controlPoint)) {
445
- // Position the control point closer to the first -- the connecting
446
- // segment will be roughly a line.
447
- controlPoint = segmentStart.plus(enteringVec.times(startEndDist / 4));
448
- }
449
- console.assert(!segmentStart.eq(controlPoint, 1e-11), 'Start and control points are equal!');
450
- console.assert(!controlPoint.eq(segmentEnd, 1e-11), 'Control and end points are equal!');
451
- const prevCurve = this.currentCurve;
452
- this.currentCurve = new Bezier(segmentStart.xy, controlPoint.xy, segmentEnd.xy);
453
- if (isNaN(Vec2.ofXY(this.currentCurve.normal(0)).magnitude())) {
454
- console.error('NaN normal at 0. Curve:', this.currentCurve);
455
- this.currentCurve = prevCurve;
456
- }
457
- // Should we start making a new curve? Check whether all buffer points are within
458
- // ±strokeWidth of the curve.
459
- const curveMatchesPoints = (curve) => {
460
- for (const point of this.buffer) {
461
- const proj = Vec2.ofXY(curve.project(point.xy));
462
- const dist = proj.minus(point).magnitude();
463
- const minFit = Math.max(Math.min(this.curveStartWidth, this.curveEndWidth) / 3, this.minFitAllowed);
464
- if (dist > minFit || dist > this.maxFitAllowed) {
465
- return false;
466
- }
467
- }
468
- return true;
469
- };
470
- if (this.buffer.length > 3 && this.approxCurrentCurveLength() > this.curveStartWidth) {
471
- if (!curveMatchesPoints(this.currentCurve)) {
472
- // Use a curve that better fits the points
473
- this.currentCurve = prevCurve;
474
- this.curveEndWidth = prevEndWidth;
475
- // Reset the last point -- the current point was not added to the curve.
476
- this.lastPoint = lastPoint;
477
- this.finalizeCurrentCurve();
478
- return;
479
- }
146
+ this.isFirstSegment = false;
480
147
  }
481
148
  }
149
+ addPoint(newPoint) {
150
+ this.curveFitter.addPoint(newPoint);
151
+ this.widthAverageNumSamples++;
152
+ this.averageWidth =
153
+ this.averageWidth * (this.widthAverageNumSamples - 1) / this.widthAverageNumSamples
154
+ + newPoint.width / this.widthAverageNumSamples;
155
+ }
482
156
  }
@@ -0,0 +1,36 @@
1
+ import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
2
+ import Rect2 from '../../math/Rect2';
3
+ import Stroke from '../Stroke';
4
+ import Viewport from '../../Viewport';
5
+ import { StrokeDataPoint } from '../../types';
6
+ import { ComponentBuilder, ComponentBuilderFactory } from './types';
7
+ export declare const makePressureSensitiveFreehandLineBuilder: ComponentBuilderFactory;
8
+ export default class PressureSensitiveFreehandLineBuilder implements ComponentBuilder {
9
+ private startPoint;
10
+ private minFitAllowed;
11
+ private viewport;
12
+ private isFirstSegment;
13
+ private pathStartConnector;
14
+ private mostRecentConnector;
15
+ private upperSegments;
16
+ private lowerSegments;
17
+ private lastUpperBezier;
18
+ private lastLowerBezier;
19
+ private parts;
20
+ private curveFitter;
21
+ private curveStartWidth;
22
+ private bbox;
23
+ constructor(startPoint: StrokeDataPoint, minFitAllowed: number, maxFitAllowed: number, viewport: Viewport);
24
+ getBBox(): Rect2;
25
+ private getRenderingStyle;
26
+ private previewCurrentPath;
27
+ private previewFullPath;
28
+ private previewStroke;
29
+ preview(renderer: AbstractRenderer): void;
30
+ build(): Stroke;
31
+ private roundPoint;
32
+ private shouldStartNewSegment;
33
+ private addCurve;
34
+ private segmentToPath;
35
+ addPoint(newPoint: StrokeDataPoint): void;
36
+ }