js-draw 0.7.2 → 0.8.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.
- package/.github/ISSUE_TEMPLATE/translation.md +1 -0
- package/CHANGELOG.md +4 -0
- package/dist/bundle.js +1 -1
- package/dist/src/components/Stroke.js +10 -3
- package/dist/src/components/builders/FreehandLineBuilder.d.ts +10 -23
- package/dist/src/components/builders/FreehandLineBuilder.js +70 -396
- package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.d.ts +36 -0
- package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.js +339 -0
- package/dist/src/components/lib.d.ts +2 -0
- package/dist/src/components/lib.js +2 -0
- package/dist/src/components/util/StrokeSmoother.d.ts +35 -0
- package/dist/src/components/util/StrokeSmoother.js +206 -0
- package/dist/src/math/Mat33.d.ts +2 -0
- package/dist/src/math/Mat33.js +4 -0
- package/dist/src/math/Path.d.ts +2 -0
- package/dist/src/math/Path.js +39 -0
- package/dist/src/rendering/renderers/CanvasRenderer.js +2 -0
- package/dist/src/rendering/renderers/SVGRenderer.d.ts +2 -0
- package/dist/src/rendering/renderers/SVGRenderer.js +20 -0
- package/dist/src/toolbar/localization.d.ts +1 -0
- package/dist/src/toolbar/localization.js +1 -0
- package/dist/src/toolbar/widgets/PenToolWidget.js +6 -1
- package/dist/src/tools/Pen.d.ts +2 -2
- package/dist/src/tools/Pen.js +2 -2
- package/dist/src/tools/SelectionTool/Selection.d.ts +1 -0
- package/dist/src/tools/SelectionTool/Selection.js +8 -1
- package/dist/src/tools/ToolController.js +2 -1
- package/package.json +1 -1
- package/src/components/Stroke.ts +16 -3
- package/src/components/builders/FreehandLineBuilder.ts +54 -495
- package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +454 -0
- package/src/components/lib.ts +3 -1
- package/src/components/util/StrokeSmoother.ts +290 -0
- package/src/math/Mat33.ts +5 -0
- package/src/math/Path.test.ts +25 -0
- package/src/math/Path.ts +45 -0
- package/src/rendering/renderers/CanvasRenderer.ts +2 -0
- package/src/rendering/renderers/SVGRenderer.ts +24 -0
- package/src/toolbar/localization.ts +2 -0
- package/src/toolbar/widgets/PenToolWidget.ts +6 -1
- package/src/tools/Pen.test.ts +2 -2
- package/src/tools/Pen.ts +1 -1
- package/src/tools/SelectionTool/Selection.ts +10 -1
- package/src/tools/ToolController.ts +2 -1
@@ -1,14 +1,14 @@
|
|
1
|
-
import { Bezier } from 'bezier-js';
|
2
1
|
import AbstractRenderer, { RenderablePathSpec } from '../../rendering/renderers/AbstractRenderer';
|
3
2
|
import { Point2, Vec2 } from '../../math/Vec2';
|
4
3
|
import Rect2 from '../../math/Rect2';
|
5
|
-
import {
|
6
|
-
import LineSegment2 from '../../math/LineSegment2';
|
4
|
+
import { PathCommand, PathCommandType } from '../../math/Path';
|
7
5
|
import Stroke from '../Stroke';
|
8
6
|
import Viewport from '../../Viewport';
|
9
7
|
import { StrokeDataPoint } from '../../types';
|
10
8
|
import { ComponentBuilder, ComponentBuilderFactory } from './types';
|
11
9
|
import RenderingStyle from '../../rendering/RenderingStyle';
|
10
|
+
import { StrokeSmoother, Curve } from '../util/StrokeSmoother';
|
11
|
+
import Color4 from '../../Color4';
|
12
12
|
|
13
13
|
export const makeFreehandLineBuilder: ComponentBuilderFactory = (initialPoint: StrokeDataPoint, viewport: Viewport) => {
|
14
14
|
// Don't smooth if input is more than ± 3 pixels from the true curve, do smooth if
|
@@ -21,73 +21,28 @@ export const makeFreehandLineBuilder: ComponentBuilderFactory = (initialPoint: S
|
|
21
21
|
);
|
22
22
|
};
|
23
23
|
|
24
|
-
type CurrentSegmentToPathResult = {
|
25
|
-
upperCurveCommand: QuadraticBezierPathCommand,
|
26
|
-
lowerToUpperConnector: PathCommand,
|
27
|
-
upperToLowerConnector: PathCommand,
|
28
|
-
lowerCurveCommand: QuadraticBezierPathCommand,
|
29
|
-
|
30
|
-
upperCurve: Bezier,
|
31
|
-
lowerCurve: Bezier,
|
32
|
-
};
|
33
|
-
|
34
24
|
// Handles stroke smoothing and creates Strokes from user/stylus input.
|
35
25
|
export default class FreehandLineBuilder implements ComponentBuilder {
|
36
26
|
private isFirstSegment: boolean = true;
|
37
|
-
private
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
// ↓
|
42
|
-
// /---pathStartConnector---/ ← Beginning of the list of upper parts
|
43
|
-
// ___/ __/
|
44
|
-
// / /
|
45
|
-
// /--Most recent connector--/ ← most recent upper part goes here
|
46
|
-
// ↑
|
47
|
-
// most recent lower part goes here
|
48
|
-
//
|
49
|
-
// The upperSegments form a path that goes in reverse from the most recent edge to the
|
50
|
-
// least recent edge.
|
51
|
-
// The lowerSegments form a path that goes from the least recent edge to the most
|
52
|
-
// recent edge.
|
53
|
-
private upperSegments: PathCommand[];
|
54
|
-
private lowerSegments: PathCommand[];
|
55
|
-
private lastUpperBezier: Bezier|null = null;
|
56
|
-
private lastLowerBezier: Bezier|null = null;
|
57
|
-
private parts: RenderablePathSpec[] = [];
|
58
|
-
|
59
|
-
private buffer: Point2[];
|
60
|
-
private lastPoint: StrokeDataPoint;
|
61
|
-
private lastExitingVec: Vec2|null = null;
|
62
|
-
private currentCurve: Bezier|null = null;
|
63
|
-
private curveStartWidth: number;
|
64
|
-
private curveEndWidth: number;
|
65
|
-
|
66
|
-
// Stroke smoothing and tangent approximation
|
67
|
-
private momentum: Vec2;
|
27
|
+
private parts: PathCommand[] = [];
|
28
|
+
|
29
|
+
private curveFitter: StrokeSmoother;
|
30
|
+
|
68
31
|
private bbox: Rect2;
|
32
|
+
private averageWidth: number;
|
33
|
+
private widthAverageNumSamples: number = 1;
|
69
34
|
|
70
35
|
public constructor(
|
71
36
|
private startPoint: StrokeDataPoint,
|
72
37
|
|
73
|
-
// Maximum distance from the actual curve (irrespective of stroke width)
|
74
|
-
// for which a point is considered 'part of the curve'.
|
75
|
-
// Note that the maximum will be smaller if the stroke width is less than
|
76
|
-
// [maxFitAllowed].
|
77
38
|
private minFitAllowed: number,
|
78
|
-
|
39
|
+
maxFitAllowed: number,
|
79
40
|
|
80
41
|
private viewport: Viewport,
|
81
42
|
) {
|
82
|
-
this.
|
83
|
-
this.upperSegments = [];
|
84
|
-
this.lowerSegments = [];
|
85
|
-
|
86
|
-
this.buffer = [this.startPoint.pos];
|
87
|
-
this.momentum = Vec2.zero;
|
88
|
-
this.currentCurve = null;
|
89
|
-
this.curveStartWidth = startPoint.width;
|
43
|
+
this.curveFitter = new StrokeSmoother(startPoint, minFitAllowed, maxFitAllowed, (curve: Curve|null) => this.addCurve(curve));
|
90
44
|
|
45
|
+
this.averageWidth = startPoint.width;
|
91
46
|
this.bbox = new Rect2(this.startPoint.pos.x, this.startPoint.pos.y, 0, 0);
|
92
47
|
}
|
93
48
|
|
@@ -95,87 +50,33 @@ export default class FreehandLineBuilder implements ComponentBuilder {
|
|
95
50
|
return this.bbox;
|
96
51
|
}
|
97
52
|
|
98
|
-
|
53
|
+
protected getRenderingStyle(): RenderingStyle {
|
99
54
|
return {
|
100
|
-
fill:
|
55
|
+
fill: Color4.transparent,
|
56
|
+
stroke: {
|
57
|
+
color: this.startPoint.color,
|
58
|
+
width: this.averageWidth,
|
59
|
+
}
|
101
60
|
};
|
102
61
|
}
|
103
62
|
|
104
|
-
|
105
|
-
const
|
106
|
-
const
|
107
|
-
|
108
|
-
let pathStartConnector: PathCommand;
|
109
|
-
if (this.currentCurve) {
|
110
|
-
const {
|
111
|
-
upperCurveCommand, lowerToUpperConnector, upperToLowerConnector, lowerCurveCommand
|
112
|
-
} = this.currentSegmentToPath();
|
113
|
-
|
114
|
-
upperPath.push(upperCurveCommand);
|
115
|
-
lowerPath.push(lowerCurveCommand);
|
116
|
-
|
117
|
-
lowerToUpperCap = lowerToUpperConnector;
|
118
|
-
pathStartConnector = this.pathStartConnector ?? upperToLowerConnector;
|
119
|
-
} else {
|
120
|
-
if (this.mostRecentConnector === null || this.pathStartConnector === null) {
|
121
|
-
return null;
|
122
|
-
}
|
123
|
-
|
124
|
-
lowerToUpperCap = this.mostRecentConnector;
|
125
|
-
pathStartConnector = this.pathStartConnector;
|
126
|
-
}
|
127
|
-
|
128
|
-
let startPoint: Point2;
|
129
|
-
const lastLowerSegment = lowerPath[lowerPath.length - 1];
|
130
|
-
if (lastLowerSegment.kind === PathCommandType.LineTo || lastLowerSegment.kind === PathCommandType.MoveTo) {
|
131
|
-
startPoint = lastLowerSegment.point;
|
132
|
-
} else {
|
133
|
-
startPoint = lastLowerSegment.endPoint;
|
134
|
-
}
|
63
|
+
protected previewCurrentPath(): RenderablePathSpec|null {
|
64
|
+
const path = this.parts.slice();
|
65
|
+
const commands = [...path, ...this.curveToPathCommands(this.curveFitter.preview())];
|
66
|
+
const startPoint = this.startPoint.pos;
|
135
67
|
|
136
68
|
return {
|
137
|
-
// Start at the end of the lower curve:
|
138
|
-
// Start point
|
139
|
-
// ↓
|
140
|
-
// __/ __/ ← Most recent points on this end
|
141
|
-
// /___ /
|
142
|
-
// ↑
|
143
|
-
// Oldest points
|
144
69
|
startPoint,
|
145
70
|
|
146
|
-
commands
|
147
|
-
// Move to the most recent point on the upperPath:
|
148
|
-
// ----→•
|
149
|
-
// __/ __/
|
150
|
-
// /___ /
|
151
|
-
lowerToUpperCap,
|
152
|
-
|
153
|
-
// Move to the beginning of the upperPath:
|
154
|
-
// __/ __/
|
155
|
-
// /___ /
|
156
|
-
// • ←-
|
157
|
-
...upperPath.reverse(),
|
158
|
-
|
159
|
-
// Move to the beginning of the lowerPath:
|
160
|
-
// __/ __/
|
161
|
-
// /___ /
|
162
|
-
// •
|
163
|
-
pathStartConnector,
|
164
|
-
|
165
|
-
// Move back to the start point:
|
166
|
-
// •
|
167
|
-
// __/ __/
|
168
|
-
// /___ /
|
169
|
-
...lowerPath,
|
170
|
-
],
|
71
|
+
commands,
|
171
72
|
style: this.getRenderingStyle(),
|
172
73
|
};
|
173
74
|
}
|
174
75
|
|
175
|
-
|
76
|
+
protected previewFullPath(): RenderablePathSpec[]|null {
|
176
77
|
const preview = this.previewCurrentPath();
|
177
78
|
if (preview) {
|
178
|
-
return [
|
79
|
+
return [ preview ];
|
179
80
|
}
|
180
81
|
return null;
|
181
82
|
}
|
@@ -202,14 +103,12 @@ export default class FreehandLineBuilder implements ComponentBuilder {
|
|
202
103
|
}
|
203
104
|
|
204
105
|
public build(): Stroke {
|
205
|
-
|
206
|
-
this.finalizeCurrentCurve();
|
207
|
-
}
|
106
|
+
this.curveFitter.finalizeCurrentCurve();
|
208
107
|
return this.previewStroke()!;
|
209
108
|
}
|
210
109
|
|
211
110
|
private roundPoint(point: Point2): Point2 {
|
212
|
-
let minFit = Math.min(this.minFitAllowed, this.
|
111
|
+
let minFit = Math.min(this.minFitAllowed, this.averageWidth / 2);
|
213
112
|
|
214
113
|
if (minFit < 1e-10) {
|
215
114
|
minFit = this.minFitAllowed;
|
@@ -218,93 +117,12 @@ export default class FreehandLineBuilder implements ComponentBuilder {
|
|
218
117
|
return Viewport.roundPoint(point, minFit);
|
219
118
|
}
|
220
119
|
|
221
|
-
|
222
|
-
private shouldStartNewSegment(lowerCurve: Bezier, upperCurve: Bezier): boolean {
|
223
|
-
if (!this.lastLowerBezier || !this.lastUpperBezier) {
|
224
|
-
return false;
|
225
|
-
}
|
226
|
-
|
227
|
-
const getIntersection = (curve1: Bezier, curve2: Bezier): Point2|null => {
|
228
|
-
const intersection = curve1.intersects(curve2) as (string[] | null | undefined);
|
229
|
-
if (!intersection || intersection.length === 0) {
|
230
|
-
return null;
|
231
|
-
}
|
232
|
-
|
233
|
-
// From http://pomax.github.io/bezierjs/#intersect-curve,
|
234
|
-
// .intersects returns an array of 't1/t2' pairs, where curve1.at(t1) gives the point.
|
235
|
-
const firstTPair = intersection[0];
|
236
|
-
const match = /^([-0-9.eE]+)\/([-0-9.eE]+)$/.exec(firstTPair);
|
237
|
-
|
238
|
-
if (!match) {
|
239
|
-
throw new Error(
|
240
|
-
`Incorrect format returned by .intersects: ${intersection} should be array of "number/number"!`
|
241
|
-
);
|
242
|
-
}
|
243
|
-
|
244
|
-
const t = parseFloat(match[1]);
|
245
|
-
return Vec2.ofXY(curve1.get(t));
|
246
|
-
};
|
247
|
-
|
248
|
-
const getExitDirection = (curve: Bezier): Vec2 => {
|
249
|
-
return Vec2.ofXY(curve.points[2]).minus(Vec2.ofXY(curve.points[1])).normalized();
|
250
|
-
};
|
251
|
-
|
252
|
-
const getEnterDirection = (curve: Bezier): Vec2 => {
|
253
|
-
return Vec2.ofXY(curve.points[1]).minus(Vec2.ofXY(curve.points[0])).normalized();
|
254
|
-
};
|
255
|
-
|
256
|
-
// Prevent
|
257
|
-
// /
|
258
|
-
// / /
|
259
|
-
// / / /|
|
260
|
-
// / / |
|
261
|
-
// / |
|
262
|
-
// where the next stroke and the previous stroke are in different directions.
|
263
|
-
//
|
264
|
-
// Are the exit/enter directions of the previous and current curves in different enough directions?
|
265
|
-
if (getEnterDirection(upperCurve).dot(getExitDirection(this.lastUpperBezier)) < 0.3
|
266
|
-
|| getEnterDirection(lowerCurve).dot(getExitDirection(this.lastLowerBezier)) < 0.3
|
267
|
-
|
268
|
-
// Also handle if the curves exit/enter directions differ
|
269
|
-
|| getEnterDirection(upperCurve).dot(getExitDirection(upperCurve)) < 0
|
270
|
-
|| getEnterDirection(lowerCurve).dot(getExitDirection(lowerCurve)) < 0) {
|
271
|
-
return true;
|
272
|
-
}
|
273
|
-
|
274
|
-
// Check whether the lower curve intersects the other wall:
|
275
|
-
// / / ← lower
|
276
|
-
// / / /
|
277
|
-
// / / /
|
278
|
-
// //
|
279
|
-
// / /
|
280
|
-
const lowerIntersection = getIntersection(lowerCurve, this.lastUpperBezier);
|
281
|
-
const upperIntersection = getIntersection(upperCurve, this.lastLowerBezier);
|
282
|
-
if (lowerIntersection || upperIntersection) {
|
283
|
-
return true;
|
284
|
-
}
|
285
|
-
|
286
|
-
return false;
|
287
|
-
}
|
288
|
-
|
289
|
-
// Returns the distance between the start, control, and end points of the curve.
|
290
|
-
private approxCurrentCurveLength() {
|
291
|
-
if (!this.currentCurve) {
|
292
|
-
return 0;
|
293
|
-
}
|
294
|
-
const startPt = Vec2.ofXY(this.currentCurve.points[0]);
|
295
|
-
const controlPt = Vec2.ofXY(this.currentCurve.points[1]);
|
296
|
-
const endPt = Vec2.ofXY(this.currentCurve.points[2]);
|
297
|
-
const toControlDist = startPt.minus(controlPt).length();
|
298
|
-
const toEndDist = endPt.minus(controlPt).length();
|
299
|
-
return toControlDist + toEndDist;
|
300
|
-
}
|
301
|
-
|
302
|
-
private finalizeCurrentCurve() {
|
120
|
+
private curveToPathCommands(curve: Curve|null): PathCommand[] {
|
303
121
|
// Case where no points have been added
|
304
|
-
if (!
|
122
|
+
if (!curve) {
|
305
123
|
// Don't create a circle around the initial point if the stroke has more than one point.
|
306
124
|
if (!this.isFirstSegment) {
|
307
|
-
return;
|
125
|
+
return [];
|
308
126
|
}
|
309
127
|
|
310
128
|
const width = Viewport.roundPoint(this.startPoint.width / 3.5, Math.min(this.minFitAllowed, this.startPoint.width / 4));
|
@@ -314,10 +132,9 @@ export default class FreehandLineBuilder implements ComponentBuilder {
|
|
314
132
|
// |
|
315
133
|
// ----- ←
|
316
134
|
// |
|
317
|
-
const startPoint = this.startPoint.pos.plus(Vec2.of(width, 0));
|
318
135
|
|
319
136
|
// Draw a circle-ish shape around the start point
|
320
|
-
|
137
|
+
return [
|
321
138
|
{
|
322
139
|
kind: PathCommandType.QuadraticBezierTo,
|
323
140
|
controlPoint: center.plus(Vec2.of(width, width)),
|
@@ -344,299 +161,41 @@ export default class FreehandLineBuilder implements ComponentBuilder {
|
|
344
161
|
controlPoint: center.plus(Vec2.of(width, -width)),
|
345
162
|
endPoint: center.plus(Vec2.of(width, 0)),
|
346
163
|
}
|
347
|
-
|
348
|
-
this.pathStartConnector = {
|
349
|
-
kind: PathCommandType.LineTo,
|
350
|
-
point: startPoint,
|
351
|
-
};
|
352
|
-
this.mostRecentConnector = this.pathStartConnector;
|
353
|
-
|
354
|
-
return;
|
355
|
-
}
|
356
|
-
|
357
|
-
const {
|
358
|
-
upperCurveCommand, lowerToUpperConnector, upperToLowerConnector, lowerCurveCommand,
|
359
|
-
lowerCurve, upperCurve,
|
360
|
-
} = this.currentSegmentToPath();
|
361
|
-
|
362
|
-
const shouldStartNew = this.shouldStartNewSegment(lowerCurve, upperCurve);
|
363
|
-
if (shouldStartNew) {
|
364
|
-
const part = this.previewCurrentPath();
|
365
|
-
|
366
|
-
if (part) {
|
367
|
-
this.parts.push(part);
|
368
|
-
this.upperSegments = [];
|
369
|
-
this.lowerSegments = [];
|
370
|
-
}
|
164
|
+
];
|
371
165
|
}
|
372
166
|
|
373
|
-
|
374
|
-
// We draw the upper path (reversed), then the lower path, so we need the
|
375
|
-
// upperToLowerConnector to join the two paths.
|
376
|
-
this.pathStartConnector = upperToLowerConnector;
|
377
|
-
this.isFirstSegment = false;
|
378
|
-
}
|
379
|
-
// With the most recent connector, we're joining the end of the lowerPath to the most recent
|
380
|
-
// upperPath:
|
381
|
-
this.mostRecentConnector = lowerToUpperConnector;
|
382
|
-
|
383
|
-
this.lowerSegments.push(lowerCurveCommand);
|
384
|
-
this.upperSegments.push(upperCurveCommand);
|
385
|
-
|
386
|
-
this.lastLowerBezier = lowerCurve;
|
387
|
-
this.lastUpperBezier = upperCurve;
|
388
|
-
|
389
|
-
const lastPoint = this.buffer[this.buffer.length - 1];
|
390
|
-
this.lastExitingVec = Vec2.ofXY(
|
391
|
-
this.currentCurve.points[2]
|
392
|
-
).minus(Vec2.ofXY(this.currentCurve.points[1]));
|
393
|
-
console.assert(this.lastExitingVec.magnitude() !== 0, 'lastExitingVec has zero length!');
|
394
|
-
|
395
|
-
// Use the last two points to start a new curve (the last point isn't used
|
396
|
-
// in the current curve and we want connected curves to share end points)
|
397
|
-
this.buffer = [
|
398
|
-
this.buffer[this.buffer.length - 2], lastPoint,
|
399
|
-
];
|
400
|
-
this.currentCurve = null;
|
401
|
-
}
|
402
|
-
|
403
|
-
// Returns [upper curve, connector, lower curve]
|
404
|
-
private currentSegmentToPath(): CurrentSegmentToPathResult {
|
405
|
-
if (this.currentCurve == null) {
|
406
|
-
throw new Error('Invalid State: currentCurve is null!');
|
407
|
-
}
|
408
|
-
|
409
|
-
let startVec = Vec2.ofXY(this.currentCurve.normal(0)).normalized();
|
410
|
-
let endVec = Vec2.ofXY(this.currentCurve.normal(1)).normalized();
|
411
|
-
|
412
|
-
startVec = startVec.times(this.curveStartWidth / 2);
|
413
|
-
endVec = endVec.times(this.curveEndWidth / 2);
|
167
|
+
const result: PathCommand[] = [];
|
414
168
|
|
415
|
-
if (
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
const startPt = Vec2.ofXY(this.currentCurve.get(0));
|
421
|
-
const endPt = Vec2.ofXY(this.currentCurve.get(1));
|
422
|
-
const controlPoint = Vec2.ofXY(this.currentCurve.points[1]);
|
423
|
-
|
424
|
-
// Approximate the normal at the location of the control point
|
425
|
-
let projectionT = this.currentCurve.project(controlPoint.xy).t;
|
426
|
-
if (!projectionT) {
|
427
|
-
if (startPt.minus(controlPoint).magnitudeSquared() < endPt.minus(controlPoint).magnitudeSquared()) {
|
428
|
-
projectionT = 0.1;
|
429
|
-
} else {
|
430
|
-
projectionT = 0.9;
|
431
|
-
}
|
169
|
+
if (this.isFirstSegment) {
|
170
|
+
result.push({
|
171
|
+
kind: PathCommandType.MoveTo,
|
172
|
+
point: this.roundPoint(curve.startPoint),
|
173
|
+
});
|
432
174
|
}
|
433
175
|
|
434
|
-
|
435
|
-
const halfVec = Vec2.ofXY(this.currentCurve.normal(halfVecT))
|
436
|
-
.normalized().times(
|
437
|
-
this.curveStartWidth / 2 * halfVecT
|
438
|
-
+ this.curveEndWidth / 2 * (1 - halfVecT)
|
439
|
-
);
|
440
|
-
|
441
|
-
// Each starts at startPt ± startVec
|
442
|
-
const lowerCurveStartPoint = this.roundPoint(startPt.plus(startVec));
|
443
|
-
const lowerCurveControlPoint = this.roundPoint(controlPoint.plus(halfVec));
|
444
|
-
const lowerCurveEndPoint = this.roundPoint(endPt.plus(endVec));
|
445
|
-
const upperCurveControlPoint = this.roundPoint(controlPoint.minus(halfVec));
|
446
|
-
const upperCurveStartPoint = this.roundPoint(endPt.minus(endVec));
|
447
|
-
const upperCurveEndPoint = this.roundPoint(startPt.minus(startVec));
|
448
|
-
|
449
|
-
const lowerCurveCommand: QuadraticBezierPathCommand = {
|
450
|
-
kind: PathCommandType.QuadraticBezierTo,
|
451
|
-
controlPoint: lowerCurveControlPoint,
|
452
|
-
endPoint: lowerCurveEndPoint,
|
453
|
-
};
|
454
|
-
|
455
|
-
// From the end of the upperCurve to the start of the lowerCurve:
|
456
|
-
const upperToLowerConnector: LinePathCommand = {
|
457
|
-
kind: PathCommandType.LineTo,
|
458
|
-
point: lowerCurveStartPoint,
|
459
|
-
};
|
460
|
-
|
461
|
-
// From the end of lowerCurve to the start of upperCurve:
|
462
|
-
const lowerToUpperConnector: LinePathCommand = {
|
463
|
-
kind: PathCommandType.LineTo,
|
464
|
-
point: upperCurveStartPoint,
|
465
|
-
};
|
466
|
-
|
467
|
-
const upperCurveCommand: QuadraticBezierPathCommand = {
|
176
|
+
result.push({
|
468
177
|
kind: PathCommandType.QuadraticBezierTo,
|
469
|
-
controlPoint:
|
470
|
-
endPoint:
|
471
|
-
};
|
472
|
-
|
473
|
-
const upperCurve = new Bezier(upperCurveStartPoint, upperCurveControlPoint, upperCurveEndPoint);
|
474
|
-
const lowerCurve = new Bezier(lowerCurveStartPoint, lowerCurveControlPoint, lowerCurveEndPoint);
|
178
|
+
controlPoint: this.roundPoint(curve.controlPoint),
|
179
|
+
endPoint: this.roundPoint(curve.endPoint),
|
180
|
+
});
|
475
181
|
|
476
|
-
return
|
477
|
-
upperCurveCommand, upperToLowerConnector, lowerToUpperConnector, lowerCurveCommand,
|
478
|
-
upperCurve, lowerCurve,
|
479
|
-
};
|
480
|
-
}
|
481
|
-
|
482
|
-
// Compute the direction of the velocity at the end of this.buffer
|
483
|
-
private computeExitingVec(): Vec2 {
|
484
|
-
return this.momentum.normalized().times(this.lastPoint.width / 2);
|
182
|
+
return result;
|
485
183
|
}
|
486
184
|
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
const fuzzEq = 1e-10;
|
491
|
-
const deltaTime = newPoint.time - this.lastPoint.time;
|
492
|
-
if (newPoint.pos.eq(this.lastPoint.pos, fuzzEq) || deltaTime === 0) {
|
493
|
-
return;
|
494
|
-
} else if (isNaN(newPoint.pos.magnitude())) {
|
495
|
-
console.warn('Discarding NaN point.', newPoint);
|
496
|
-
return;
|
497
|
-
}
|
498
|
-
|
499
|
-
const threshold = Math.min(this.lastPoint.width, newPoint.width) / 3;
|
500
|
-
const shouldSnapToInitial = this.startPoint.pos.minus(newPoint.pos).magnitude() < threshold
|
501
|
-
&& this.isFirstSegment;
|
502
|
-
|
503
|
-
// Snap to the starting point if the stroke is contained within a small ball centered
|
504
|
-
// at the starting point.
|
505
|
-
// This allows us to create a circle/dot at the start of the stroke.
|
506
|
-
if (shouldSnapToInitial) {
|
507
|
-
return;
|
508
|
-
}
|
509
|
-
|
510
|
-
const velocity = newPoint.pos.minus(this.lastPoint.pos).times(1 / (deltaTime) * 1000);
|
511
|
-
this.momentum = this.momentum.lerp(velocity, 0.9);
|
512
|
-
}
|
513
|
-
|
514
|
-
const lastPoint = this.lastPoint ?? newPoint;
|
515
|
-
this.lastPoint = newPoint;
|
516
|
-
|
517
|
-
this.buffer.push(newPoint.pos);
|
518
|
-
const pointRadius = newPoint.width / 2;
|
519
|
-
const prevEndWidth = this.curveEndWidth;
|
520
|
-
this.curveEndWidth = pointRadius;
|
185
|
+
private addCurve(curve: Curve|null) {
|
186
|
+
const parts = this.curveToPathCommands(curve);
|
187
|
+
this.parts.push(...parts);
|
521
188
|
|
522
189
|
if (this.isFirstSegment) {
|
523
|
-
|
524
|
-
this.curveStartWidth = (this.curveStartWidth + pointRadius) / 2;
|
525
|
-
}
|
526
|
-
|
527
|
-
// recompute bbox
|
528
|
-
this.bbox = this.bbox.grownToPoint(newPoint.pos, pointRadius);
|
529
|
-
|
530
|
-
if (this.currentCurve === null) {
|
531
|
-
const p1 = lastPoint.pos;
|
532
|
-
const p2 = lastPoint.pos.plus(this.lastExitingVec ?? Vec2.unitX);
|
533
|
-
const p3 = newPoint.pos;
|
534
|
-
|
535
|
-
// Quadratic Bézier curve
|
536
|
-
this.currentCurve = new Bezier(
|
537
|
-
p1.xy, p2.xy, p3.xy
|
538
|
-
);
|
539
|
-
this.curveStartWidth = lastPoint.width / 2;
|
540
|
-
console.assert(!isNaN(p1.magnitude()) && !isNaN(p2.magnitude()) && !isNaN(p3.magnitude()), 'Expected !NaN');
|
541
|
-
}
|
542
|
-
|
543
|
-
// If there isn't an entering vector (e.g. because this.isFirstCurve), approximate it.
|
544
|
-
let enteringVec = this.lastExitingVec;
|
545
|
-
if (!enteringVec) {
|
546
|
-
let sampleIdx = Math.ceil(this.buffer.length / 2);
|
547
|
-
if (sampleIdx === 0 || sampleIdx >= this.buffer.length) {
|
548
|
-
sampleIdx = this.buffer.length - 1;
|
549
|
-
}
|
550
|
-
|
551
|
-
enteringVec = this.buffer[sampleIdx].minus(this.buffer[0]);
|
552
|
-
}
|
553
|
-
|
554
|
-
let exitingVec = this.computeExitingVec();
|
555
|
-
|
556
|
-
// Find the intersection between the entering vector and the exiting vector
|
557
|
-
const maxRelativeLength = 2;
|
558
|
-
const segmentStart = this.buffer[0];
|
559
|
-
const segmentEnd = newPoint.pos;
|
560
|
-
const startEndDist = segmentEnd.minus(segmentStart).magnitude();
|
561
|
-
const maxControlPointDist = maxRelativeLength * startEndDist;
|
562
|
-
|
563
|
-
// Exit in cases where we would divide by zero
|
564
|
-
if (maxControlPointDist === 0 || exitingVec.magnitude() === 0 || !isFinite(exitingVec.magnitude())) {
|
565
|
-
return;
|
566
|
-
}
|
567
|
-
|
568
|
-
console.assert(isFinite(enteringVec.magnitude()), 'Pre-normalized enteringVec has NaN or ∞ magnitude!');
|
569
|
-
|
570
|
-
enteringVec = enteringVec.normalized();
|
571
|
-
exitingVec = exitingVec.normalized();
|
572
|
-
|
573
|
-
console.assert(isFinite(enteringVec.magnitude()), 'Normalized enteringVec has NaN or ∞ magnitude!');
|
574
|
-
|
575
|
-
const lineFromStart = new LineSegment2(
|
576
|
-
segmentStart,
|
577
|
-
segmentStart.plus(enteringVec.times(maxControlPointDist))
|
578
|
-
);
|
579
|
-
const lineFromEnd = new LineSegment2(
|
580
|
-
segmentEnd.minus(exitingVec.times(maxControlPointDist)),
|
581
|
-
segmentEnd
|
582
|
-
);
|
583
|
-
const intersection = lineFromEnd.intersection(lineFromStart);
|
584
|
-
|
585
|
-
// Position the control point at this intersection
|
586
|
-
let controlPoint: Point2|null = null;
|
587
|
-
if (intersection) {
|
588
|
-
controlPoint = intersection.point;
|
589
|
-
}
|
590
|
-
|
591
|
-
// No intersection or the intersection is one of the end points?
|
592
|
-
if (!controlPoint || segmentStart.eq(controlPoint) || segmentEnd.eq(controlPoint)) {
|
593
|
-
// Position the control point closer to the first -- the connecting
|
594
|
-
// segment will be roughly a line.
|
595
|
-
controlPoint = segmentStart.plus(enteringVec.times(startEndDist / 4));
|
596
|
-
}
|
597
|
-
|
598
|
-
console.assert(!segmentStart.eq(controlPoint, 1e-11), 'Start and control points are equal!');
|
599
|
-
console.assert(!controlPoint.eq(segmentEnd, 1e-11), 'Control and end points are equal!');
|
600
|
-
|
601
|
-
const prevCurve = this.currentCurve;
|
602
|
-
this.currentCurve = new Bezier(segmentStart.xy, controlPoint.xy, segmentEnd.xy);
|
603
|
-
|
604
|
-
if (isNaN(Vec2.ofXY(this.currentCurve.normal(0)).magnitude())) {
|
605
|
-
console.error('NaN normal at 0. Curve:', this.currentCurve);
|
606
|
-
this.currentCurve = prevCurve;
|
190
|
+
this.isFirstSegment = false;
|
607
191
|
}
|
192
|
+
}
|
608
193
|
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
const dist = proj.minus(point).magnitude();
|
616
|
-
|
617
|
-
const minFit = Math.max(
|
618
|
-
Math.min(this.curveStartWidth, this.curveEndWidth) / 3,
|
619
|
-
this.minFitAllowed
|
620
|
-
);
|
621
|
-
if (dist > minFit || dist > this.maxFitAllowed) {
|
622
|
-
return false;
|
623
|
-
}
|
624
|
-
}
|
625
|
-
return true;
|
626
|
-
};
|
627
|
-
|
628
|
-
if (this.buffer.length > 3 && this.approxCurrentCurveLength() > this.curveStartWidth) {
|
629
|
-
if (!curveMatchesPoints(this.currentCurve)) {
|
630
|
-
// Use a curve that better fits the points
|
631
|
-
this.currentCurve = prevCurve;
|
632
|
-
this.curveEndWidth = prevEndWidth;
|
633
|
-
|
634
|
-
// Reset the last point -- the current point was not added to the curve.
|
635
|
-
this.lastPoint = lastPoint;
|
636
|
-
|
637
|
-
this.finalizeCurrentCurve();
|
638
|
-
return;
|
639
|
-
}
|
640
|
-
}
|
194
|
+
public addPoint(newPoint: StrokeDataPoint) {
|
195
|
+
this.curveFitter.addPoint(newPoint);
|
196
|
+
this.widthAverageNumSamples ++;
|
197
|
+
this.averageWidth =
|
198
|
+
this.averageWidth * (this.widthAverageNumSamples - 1) / this.widthAverageNumSamples
|
199
|
+
+ newPoint.width / this.widthAverageNumSamples;
|
641
200
|
}
|
642
201
|
}
|