js-draw 0.7.1 → 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 +10 -0
- package/CONTRIBUTING.md +75 -0
- package/dist/bundle.js +1 -1
- package/dist/src/SVGLoader.js +1 -0
- 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 +39 -7
- 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/TextTool.js +4 -2
- package/dist/src/tools/ToolController.js +2 -1
- package/package.json +1 -1
- package/src/SVGLoader.ts +1 -0
- 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 +47 -7
- 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/TextTool.ts +5 -2
- package/src/tools/ToolController.ts +2 -1
package/dist/src/SVGLoader.js
CHANGED
@@ -177,6 +177,7 @@ export default class SVGLoader {
|
|
177
177
|
else if (child.nodeType === Node.ELEMENT_NODE) {
|
178
178
|
const subElem = child;
|
179
179
|
if (subElem.tagName.toLowerCase() === 'tspan') {
|
180
|
+
// FIXME: tspan's (x, y) components are absolute, not relative to the parent.
|
180
181
|
contentList.push(this.makeText(subElem));
|
181
182
|
}
|
182
183
|
else {
|
@@ -36,6 +36,7 @@ export default class Stroke extends AbstractComponent {
|
|
36
36
|
return false;
|
37
37
|
}
|
38
38
|
render(canvas, visibleRect) {
|
39
|
+
var _a;
|
39
40
|
canvas.startObject(this.getBBox());
|
40
41
|
for (const part of this.parts) {
|
41
42
|
const bbox = this.bboxForPart(part.path.bbox, part.style);
|
@@ -44,7 +45,7 @@ export default class Stroke extends AbstractComponent {
|
|
44
45
|
continue;
|
45
46
|
}
|
46
47
|
const muchBiggerThanVisible = bbox.size.x > visibleRect.size.x * 2 || bbox.size.y > visibleRect.size.y * 2;
|
47
|
-
if (muchBiggerThanVisible && !part.path.
|
48
|
+
if (muchBiggerThanVisible && !part.path.roughlyIntersects(visibleRect, (_a = part.style.stroke) === null || _a === void 0 ? void 0 : _a.width)) {
|
48
49
|
continue;
|
49
50
|
}
|
50
51
|
}
|
@@ -65,7 +66,13 @@ export default class Stroke extends AbstractComponent {
|
|
65
66
|
// Update each part
|
66
67
|
this.parts = this.parts.map((part) => {
|
67
68
|
const newPath = part.path.transformedBy(affineTransfm);
|
68
|
-
const
|
69
|
+
const newStyle = Object.assign(Object.assign({}, part.style), { stroke: part.style.stroke ? Object.assign({}, part.style.stroke) : undefined });
|
70
|
+
// Approximate the scale factor.
|
71
|
+
if (newStyle.stroke) {
|
72
|
+
const scaleFactor = affineTransfm.getScaleFactor();
|
73
|
+
newStyle.stroke.width *= scaleFactor;
|
74
|
+
}
|
75
|
+
const newBBox = this.bboxForPart(newPath.bbox, newStyle);
|
69
76
|
if (isFirstPart) {
|
70
77
|
this.contentBBox = newBBox;
|
71
78
|
isFirstPart = false;
|
@@ -77,7 +84,7 @@ export default class Stroke extends AbstractComponent {
|
|
77
84
|
path: newPath,
|
78
85
|
startPoint: newPath.startPoint,
|
79
86
|
commands: newPath.parts,
|
80
|
-
style:
|
87
|
+
style: newStyle,
|
81
88
|
};
|
82
89
|
});
|
83
90
|
}
|
@@ -1,44 +1,31 @@
|
|
1
|
-
import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
|
1
|
+
import AbstractRenderer, { RenderablePathSpec } from '../../rendering/renderers/AbstractRenderer';
|
2
2
|
import Rect2 from '../../math/Rect2';
|
3
3
|
import Stroke from '../Stroke';
|
4
4
|
import Viewport from '../../Viewport';
|
5
5
|
import { StrokeDataPoint } from '../../types';
|
6
6
|
import { ComponentBuilder, ComponentBuilderFactory } from './types';
|
7
|
+
import RenderingStyle from '../../rendering/RenderingStyle';
|
7
8
|
export declare const makeFreehandLineBuilder: ComponentBuilderFactory;
|
8
9
|
export default class FreehandLineBuilder implements ComponentBuilder {
|
9
10
|
private startPoint;
|
10
11
|
private minFitAllowed;
|
11
|
-
private maxFitAllowed;
|
12
12
|
private viewport;
|
13
13
|
private isFirstSegment;
|
14
|
-
private pathStartConnector;
|
15
|
-
private mostRecentConnector;
|
16
|
-
private upperSegments;
|
17
|
-
private lowerSegments;
|
18
|
-
private lastUpperBezier;
|
19
|
-
private lastLowerBezier;
|
20
14
|
private parts;
|
21
|
-
private
|
22
|
-
private lastPoint;
|
23
|
-
private lastExitingVec;
|
24
|
-
private currentCurve;
|
25
|
-
private curveStartWidth;
|
26
|
-
private curveEndWidth;
|
27
|
-
private momentum;
|
15
|
+
private curveFitter;
|
28
16
|
private bbox;
|
17
|
+
private averageWidth;
|
18
|
+
private widthAverageNumSamples;
|
29
19
|
constructor(startPoint: StrokeDataPoint, minFitAllowed: number, maxFitAllowed: number, viewport: Viewport);
|
30
20
|
getBBox(): Rect2;
|
31
|
-
|
32
|
-
|
33
|
-
|
21
|
+
protected getRenderingStyle(): RenderingStyle;
|
22
|
+
protected previewCurrentPath(): RenderablePathSpec | null;
|
23
|
+
protected previewFullPath(): RenderablePathSpec[] | null;
|
34
24
|
private previewStroke;
|
35
25
|
preview(renderer: AbstractRenderer): void;
|
36
26
|
build(): Stroke;
|
37
27
|
private roundPoint;
|
38
|
-
private
|
39
|
-
private
|
40
|
-
private finalizeCurrentCurve;
|
41
|
-
private currentSegmentToPath;
|
42
|
-
private computeExitingVec;
|
28
|
+
private curveToPathCommands;
|
29
|
+
private addCurve;
|
43
30
|
addPoint(newPoint: StrokeDataPoint): void;
|
44
31
|
}
|
@@ -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.
|
34
|
-
this.
|
35
|
-
this.
|
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:
|
33
|
+
fill: Color4.transparent,
|
34
|
+
stroke: {
|
35
|
+
color: this.startPoint.color,
|
36
|
+
width: this.averageWidth,
|
37
|
+
}
|
51
38
|
};
|
52
39
|
}
|
53
40
|
previewCurrentPath() {
|
54
|
-
|
55
|
-
const
|
56
|
-
const
|
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 [
|
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
|
-
|
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.
|
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
|
-
|
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 (!
|
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
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
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
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
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
|
-
|
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:
|
352
|
-
endPoint:
|
353
|
-
};
|
354
|
-
|
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
|
-
|
362
|
-
|
363
|
-
|
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
|
-
|
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
|
}
|