canvas-recorder-lib 0.8.1 → 0.8.3
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/dev/canvas-recorder.css +6 -6
- package/dev/canvas-recorder.ts +10 -9
- package/dev/delay-files.ts +3 -1
- package/dev/player-body.ts +3 -3
- package/dev/transcribe-core.ts +7 -4
- package/dev/transcript-align.ts +20 -17
- package/dist/dev/canvas-recorder.css +6 -6
- package/dist/dev/canvas-recorder.js +10 -9
- package/dist/dev/canvas-recorder.js.map +1 -1
- package/dist/dev/delay-files.d.ts.map +1 -1
- package/dist/dev/delay-files.js +3 -1
- package/dist/dev/delay-files.js.map +1 -1
- package/dist/dev/player-body.d.ts +1 -1
- package/dist/dev/player-body.d.ts.map +1 -1
- package/dist/dev/player-body.js +3 -3
- package/dist/dev/transcribe-core.d.ts.map +1 -1
- package/dist/dev/transcribe-core.js +7 -4
- package/dist/dev/transcribe-core.js.map +1 -1
- package/dist/dev/transcript-align.d.ts +3 -3
- package/dist/dev/transcript-align.d.ts.map +1 -1
- package/dist/dev/transcript-align.js +16 -13
- package/dist/dev/transcript-align.js.map +1 -1
- package/dist/src/glib/path-shape.d.ts +7 -0
- package/dist/src/glib/path-shape.d.ts.map +1 -1
- package/dist/src/glib/path-shape.js +43 -221
- package/dist/src/glib/path-shape.js.map +1 -1
- package/dist/src/glib/svg-arc.d.ts +41 -0
- package/dist/src/glib/svg-arc.d.ts.map +1 -0
- package/dist/src/glib/svg-arc.js +114 -0
- package/dist/src/glib/svg-arc.js.map +1 -0
- package/dist/src/glib/svg-path-parser.d.ts +60 -0
- package/dist/src/glib/svg-path-parser.d.ts.map +1 -0
- package/dist/src/glib/svg-path-parser.js +258 -0
- package/dist/src/glib/svg-path-parser.js.map +1 -0
- package/dist/src/slide-components/fourier.d.ts.map +1 -1
- package/dist/src/slide-components/fourier.js +14 -1
- package/dist/src/slide-components/fourier.js.map +1 -1
- package/dist/src/slide-components/halftone-shadow.d.ts.map +1 -1
- package/dist/src/slide-components/halftone-shadow.js +12 -9
- package/dist/src/slide-components/halftone-shadow.js.map +1 -1
- package/dist/src/slide-components/show-error.d.ts.map +1 -1
- package/dist/src/slide-components/show-error.js +9 -2
- package/dist/src/slide-components/show-error.js.map +1 -1
- package/package.json +1 -1
- package/src/glib/path-shape.ts +66 -245
- package/src/glib/svg-arc.ts +141 -0
- package/src/glib/svg-path-parser.ts +308 -0
- package/src/slide-components/fourier.ts +15 -3
- package/src/slide-components/halftone-shadow.ts +12 -9
- package/src/slide-components/show-error.ts +9 -2
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { canvasRecorder } from "../../lib/main-control.js";
|
|
1
2
|
import { ColorScheduleInfo, NumberScheduleInfo } from "../schedule-helper.js";
|
|
2
3
|
import { InParallelComponent } from "./in-parallel.js";
|
|
3
4
|
/** At 100% opacity the dot radius reaches this fraction of the half-cell width. */
|
|
@@ -50,16 +51,19 @@ export class HalftoneShadowComponent extends InParallelComponent {
|
|
|
50
51
|
const dx = this.dxSchedule.at(timeInMs);
|
|
51
52
|
const dy = this.dySchedule.at(timeInMs);
|
|
52
53
|
const period = this.periodSchedule.at(timeInMs);
|
|
53
|
-
|
|
54
|
-
const
|
|
54
|
+
// Read the size every frame: it can change at any time.
|
|
55
|
+
const { width, height } = canvasRecorder;
|
|
56
|
+
// One small pixel per dot. The last row and column can hang past the edge.
|
|
57
|
+
const cols = Math.ceil(width / period);
|
|
58
|
+
const rows = Math.ceil(height / period);
|
|
55
59
|
// Low-power mode or CLI context: skip the halftone pass.
|
|
56
60
|
if (options.quality === "Low Power" || typeof document === "undefined") {
|
|
57
61
|
context.fillStyle = backgroundColor;
|
|
58
|
-
context.fillRect(0, 0,
|
|
62
|
+
context.fillRect(0, 0, width, height);
|
|
59
63
|
super.show(options);
|
|
60
64
|
return;
|
|
61
65
|
}
|
|
62
|
-
// Resize the small canvas when period changes.
|
|
66
|
+
// Resize the small canvas when the period or the canvas size changes.
|
|
63
67
|
if (!this.#smallCanvas ||
|
|
64
68
|
this.#smallCols !== cols ||
|
|
65
69
|
this.#smallRows !== rows) {
|
|
@@ -69,21 +73,20 @@ export class HalftoneShadowComponent extends InParallelComponent {
|
|
|
69
73
|
this.#smallCtx = this.#smallCanvas.getContext("2d", {
|
|
70
74
|
willReadFrequently: true,
|
|
71
75
|
});
|
|
72
|
-
// Apply the 16×9 root transform once — it stays until the canvas is replaced.
|
|
73
|
-
this.#smallCtx.scale(cols / 16, rows / 9);
|
|
74
76
|
this.#smallCols = cols;
|
|
75
77
|
this.#smallRows = rows;
|
|
76
78
|
}
|
|
77
79
|
const smallCtx = this.#smallCtx;
|
|
78
80
|
// Render children into the small canvas for alpha sampling.
|
|
79
|
-
smallCtx.save();
|
|
80
81
|
smallCtx.resetTransform();
|
|
81
82
|
smallCtx.clearRect(0, 0, cols, rows);
|
|
82
|
-
|
|
83
|
+
// Each small pixel is exactly one cell, so it samples where its dot is drawn.
|
|
84
|
+
// Set every frame: the period can change without changing cols or rows.
|
|
85
|
+
smallCtx.scale(1 / period, 1 / period);
|
|
83
86
|
super.show({ ...options, context: smallCtx, registerTransform: undefined });
|
|
84
87
|
// Draw background.
|
|
85
88
|
context.fillStyle = backgroundColor;
|
|
86
|
-
context.fillRect(0, 0,
|
|
89
|
+
context.fillRect(0, 0, width, height);
|
|
87
90
|
// Build halftone dot path from the alpha channel and draw as the shadow.
|
|
88
91
|
const { data } = smallCtx.getImageData(0, 0, cols, rows);
|
|
89
92
|
const path = new Path2D();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"halftone-shadow.js","sourceRoot":"","sources":["../../../src/slide-components/halftone-shadow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAE9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAGvD,mFAAmF;AACnF,MAAM,0BAA0B,GAAG,GAAG,CAAC;AAEvC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AAG,QAAQ,CAAC;QAC7B,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,OAAO,CAAC,OAAO,EAAE,CAAC;QAElB,wDAAwD;QACxD,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACtB,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"show-error.d.ts","sourceRoot":"","sources":["../../../src/slide-components/show-error.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"show-error.d.ts","sourceRoot":"","sources":["../../../src/slide-components/show-error.ts"],"names":[],"mappings":"AAMA;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,wBAAwB,EAAE,IAAI,EAAE,MAAM,QAsBxE"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { canvasRecorder } from "../../lib/main-control.js";
|
|
1
2
|
import { makeLineFont } from "../glib/line-font.js";
|
|
2
3
|
import { ParagraphLayout } from "../glib/paragraph-layout.js";
|
|
3
4
|
const errorFont = makeLineFont(1);
|
|
@@ -9,12 +10,18 @@ const errorFont = makeLineFont(1);
|
|
|
9
10
|
*/
|
|
10
11
|
export function showError(context, text) {
|
|
11
12
|
const font = errorFont;
|
|
13
|
+
/** Space on the left and right. */
|
|
14
|
+
const margin = 0.25;
|
|
15
|
+
// Read the width every time: it can change at any time.
|
|
16
|
+
const width = Math.max(1, canvasRecorder.width - 2 * margin);
|
|
12
17
|
const path = ParagraphLayout.singlePathShape({
|
|
13
18
|
font,
|
|
14
19
|
text,
|
|
15
20
|
alignment: "center",
|
|
16
|
-
width
|
|
17
|
-
})
|
|
21
|
+
width,
|
|
22
|
+
})
|
|
23
|
+
// Center on the canvas, not on the paragraph.
|
|
24
|
+
.translate((canvasRecorder.width - width) / 2, 0).canvasPath;
|
|
18
25
|
context.lineCap = "round";
|
|
19
26
|
context.lineJoin = "round";
|
|
20
27
|
context.lineWidth = font.strokeWidth * 3;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"show-error.js","sourceRoot":"","sources":["../../../src/slide-components/show-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,MAAM,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;AAElC;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,OAAiC,EAAE,IAAY;IACvE,MAAM,IAAI,GAAG,SAAS,CAAC;IACvB,MAAM,IAAI,GAAG,eAAe,CAAC,eAAe,CAAC;QAC3C,IAAI;QACJ,IAAI;QACJ,SAAS,EAAE,QAAQ;QACnB,KAAK,
|
|
1
|
+
{"version":3,"file":"show-error.js","sourceRoot":"","sources":["../../../src/slide-components/show-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,MAAM,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;AAElC;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,OAAiC,EAAE,IAAY;IACvE,MAAM,IAAI,GAAG,SAAS,CAAC;IACvB,mCAAmC;IACnC,MAAM,MAAM,GAAG,IAAI,CAAC;IACpB,yDAAyD;IACzD,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,cAAc,CAAC,KAAK,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;IAC7D,MAAM,IAAI,GAAG,eAAe,CAAC,eAAe,CAAC;QAC3C,IAAI;QACJ,IAAI;QACJ,SAAS,EAAE,QAAQ;QACnB,KAAK;KACN,CAAC;QACA,8CAA8C;SAC7C,SAAS,CAAC,CAAC,cAAc,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;IAC/D,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC;IAC1B,OAAO,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC3B,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;IACzC,OAAO,CAAC,WAAW,GAAG,OAAO,CAAC;IAC9B,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACrB,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC;IACrC,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC;IAC5B,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AACvB,CAAC"}
|
package/package.json
CHANGED
package/src/glib/path-shape.ts
CHANGED
|
@@ -16,6 +16,11 @@ import {
|
|
|
16
16
|
sum,
|
|
17
17
|
} from "phil-lib/misc";
|
|
18
18
|
import { panAndZoom, transform } from "./transforms.ts";
|
|
19
|
+
import {
|
|
20
|
+
SvgPathSegment,
|
|
21
|
+
SvgPathSyntaxError,
|
|
22
|
+
svgPathSegments,
|
|
23
|
+
} from "./svg-path-parser.ts";
|
|
19
24
|
|
|
20
25
|
import { Bezier, MinMax } from "bezier-js";
|
|
21
26
|
import { mapEachPair, philDebug } from "../utility.ts";
|
|
@@ -1336,64 +1341,15 @@ const between = "(?: *[, ] *|(?=-))";
|
|
|
1336
1341
|
const mCommand = new RegExp(
|
|
1337
1342
|
`^M${afterCommand}${number}${between}${number}(.*)$`,
|
|
1338
1343
|
);
|
|
1339
|
-
const mCommandRelative = new RegExp(
|
|
1340
|
-
`^m${afterCommand}${number}${between}${number}(.*)$`,
|
|
1341
|
-
);
|
|
1342
1344
|
const lCommand = new RegExp(
|
|
1343
1345
|
`^L${afterCommand}${number}${between}${number}(.*)$`,
|
|
1344
1346
|
);
|
|
1345
|
-
const lCommandContinuation = new RegExp(
|
|
1346
|
-
`^${between}${number}${between}${number}(.*)$`,
|
|
1347
|
-
);
|
|
1348
|
-
const lCommandRelative = new RegExp(
|
|
1349
|
-
`^l${afterCommand}${number}${between}${number}(.*)$`,
|
|
1350
|
-
);
|
|
1351
|
-
const hCommand = new RegExp(`^H${afterCommand}${number}(.*)$`);
|
|
1352
|
-
const hCommandRelative = new RegExp(`^h${afterCommand}${number}(.*)$`);
|
|
1353
|
-
const vCommand = new RegExp(`^V${afterCommand}${number}(.*)$`);
|
|
1354
|
-
const vCommandRelative = new RegExp(`^v${afterCommand}${number}(.*)$`);
|
|
1355
|
-
const vCommandContinuation = new RegExp(`^${between}${number}(.*)$`);
|
|
1356
|
-
const hCommandContinuation = vCommandContinuation;
|
|
1357
1347
|
const qCommand = new RegExp(
|
|
1358
1348
|
`^Q${afterCommand}${number}${between}${number}${between}${number}${between}${number}(.*)$`,
|
|
1359
1349
|
);
|
|
1360
|
-
const qCommandContinuation = new RegExp(
|
|
1361
|
-
`^${between}${number}${between}${number}${between}${number}${between}${number}(.*)$`,
|
|
1362
|
-
);
|
|
1363
|
-
const qCommandRelative = new RegExp(
|
|
1364
|
-
`^q${afterCommand}${number}${between}${number}${between}${number}${between}${number}(.*)$`,
|
|
1365
|
-
);
|
|
1366
1350
|
const cCommand = new RegExp(
|
|
1367
1351
|
`^C${afterCommand}${number}${between}${number}${between}${number}${between}${number}${between}${number}${between}${number}(.*)$`,
|
|
1368
1352
|
);
|
|
1369
|
-
const cCommandContinuation = new RegExp(
|
|
1370
|
-
`^${between}${number}${between}${number}${between}${number}${between}${number}${between}${number}${between}${number}(.*)$`,
|
|
1371
|
-
);
|
|
1372
|
-
const cCommandRelative = new RegExp(
|
|
1373
|
-
`^c${afterCommand}${number}${between}${number}${between}${number}${between}${number}${between}${number}${between}${number}(.*)$`,
|
|
1374
|
-
);
|
|
1375
|
-
/**
|
|
1376
|
-
* Four numbers: dx2, dy2, dx3, dy3
|
|
1377
|
-
* x1 and y1 are computed based on the previous command.
|
|
1378
|
-
* Example: s4.187,5.581,5.582,5.581
|
|
1379
|
-
*/
|
|
1380
|
-
const sCommandRelative = new RegExp(
|
|
1381
|
-
`^s${afterCommand}${number}${between}${number}${between}${number}${between}${number}(.*)$`,
|
|
1382
|
-
);
|
|
1383
|
-
/**
|
|
1384
|
-
* Four more numbers: (x2, y2, x3, y3) or (dx2, dy2, dx3, dy3)
|
|
1385
|
-
* x1 and y1 are computed based on the previous command.
|
|
1386
|
-
*/
|
|
1387
|
-
const sCommandContinuation = qCommandContinuation;
|
|
1388
|
-
const zCommand = new RegExp("^[zZ](.*)$");
|
|
1389
|
-
|
|
1390
|
-
function reflect(reflectThis: number, overThis: number) {
|
|
1391
|
-
return overThis + (overThis - reflectThis);
|
|
1392
|
-
// Example:
|
|
1393
|
-
// reflectThis = 2, overThis= 3, motion = (overThis - reflectThis) = (3-2) = 1,
|
|
1394
|
-
// result = start + motion = 3 + 1 = 4
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
1353
|
/**
|
|
1398
1354
|
* If the user enters a path string (like in `path-debugger.html`) that is invalid
|
|
1399
1355
|
* we want to give a good error message back to the user.
|
|
@@ -1524,220 +1480,85 @@ export class PathShape {
|
|
|
1524
1480
|
* Convert a string into a `PathShape`.
|
|
1525
1481
|
* @param d A valid d attribute for an `SVGPathElement`.
|
|
1526
1482
|
* E.g "M1,2 L3,4".
|
|
1483
|
+
* Every command is understood, arcs included. See {@link svgPathSegments}.
|
|
1484
|
+
*
|
|
1485
|
+
* Arcs become cubic Bézier curves, each covering at most a quarter turn.
|
|
1486
|
+
* One arc can't be a full circle, because the start and end are the same point and that draws nothing.
|
|
1487
|
+
* Use two half circles. A unit circle, starting at the top and going clockwise on the screen, like
|
|
1488
|
+
* `makePolygon()` after `rotate(-90)`: `"M0-1A1 1 0 0 1 0 1A1 1 0 0 1 0-1Z"`.
|
|
1489
|
+
* That's four cubics, each off from a true circle by about 0.03% of the radius.
|
|
1527
1490
|
* @returns The path segment generated by the string.
|
|
1528
1491
|
* @throws An invalid input will throw a PathShapeError.
|
|
1529
1492
|
*/
|
|
1530
1493
|
static fromRawString(d: string): PathShape {
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
let remaining = d;
|
|
1538
|
-
let found: RegExpExecArray | null = null;
|
|
1539
|
-
const fail = (message: string): PathShapeError => {
|
|
1540
|
-
return new PathShapeError(message, remaining);
|
|
1541
|
-
};
|
|
1542
|
-
const parseOrThrow = (s: string): number => {
|
|
1543
|
-
const result = parseFloatX(s);
|
|
1544
|
-
if (result === undefined) {
|
|
1545
|
-
throw fail(`Invalid number: "${s}"`);
|
|
1494
|
+
let segments: SvgPathSegment[];
|
|
1495
|
+
try {
|
|
1496
|
+
segments = svgPathSegments(d);
|
|
1497
|
+
} catch (reason) {
|
|
1498
|
+
if (reason instanceof SvgPathSyntaxError) {
|
|
1499
|
+
throw new PathShapeError(reason.message, d.slice(reason.position));
|
|
1546
1500
|
}
|
|
1547
|
-
|
|
1548
|
-
}
|
|
1501
|
+
throw reason;
|
|
1502
|
+
}
|
|
1503
|
+
const commands: Command[] = [];
|
|
1504
|
+
// The current point, and where the current subpath started.
|
|
1505
|
+
// A move doesn't make a command. The next command just starts somewhere else.
|
|
1549
1506
|
let x0 = 0;
|
|
1550
1507
|
let y0 = 0;
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
* Some commands depend on the previous command.
|
|
1554
|
-
*
|
|
1555
|
-
* This will be undefine if there were no previous commands
|
|
1556
|
-
* or if the previous command did not create a `Command` object.
|
|
1557
|
-
*/
|
|
1558
|
-
let previousCommand: Command | undefined;
|
|
1508
|
+
let startX = 0;
|
|
1509
|
+
let startY = 0;
|
|
1559
1510
|
const push = (command: Command) => {
|
|
1560
1511
|
commands.push(command);
|
|
1561
1512
|
x0 = command.x;
|
|
1562
1513
|
y0 = command.y;
|
|
1563
|
-
previousCommand = command;
|
|
1564
1514
|
};
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
}
|
|
1572
|
-
if ((found = mCommand.exec(remaining))) {
|
|
1573
|
-
x0 = parseOrThrow(found[1]);
|
|
1574
|
-
y0 = parseOrThrow(found[2]);
|
|
1575
|
-
recentMove = { x0, y0 };
|
|
1576
|
-
remaining = found[3];
|
|
1577
|
-
previousCommand = undefined;
|
|
1578
|
-
// TODO remaining arguments should be interpreted as L command arguments.
|
|
1579
|
-
continue;
|
|
1580
|
-
}
|
|
1581
|
-
if ((found = mCommandRelative.exec(remaining))) {
|
|
1582
|
-
x0 += parseOrThrow(found[1]);
|
|
1583
|
-
y0 += parseOrThrow(found[2]);
|
|
1584
|
-
recentMove = { x0, y0 };
|
|
1585
|
-
remaining = found[3];
|
|
1586
|
-
previousCommand = undefined;
|
|
1587
|
-
while ((found = lCommandContinuation.exec(remaining))) {
|
|
1588
|
-
const current = LCommand.relative(
|
|
1589
|
-
x0,
|
|
1590
|
-
y0,
|
|
1591
|
-
parseOrThrow(found[1]),
|
|
1592
|
-
parseOrThrow(found[2]),
|
|
1593
|
-
);
|
|
1594
|
-
push(current);
|
|
1595
|
-
remaining = found[3];
|
|
1596
|
-
}
|
|
1597
|
-
continue;
|
|
1598
|
-
}
|
|
1599
|
-
if (!recentMove) {
|
|
1600
|
-
throw fail("Must start with an M command.");
|
|
1601
|
-
}
|
|
1602
|
-
if ((found = zCommand.exec(remaining))) {
|
|
1603
|
-
if (x0 != recentMove.x0 || y0 != recentMove.y0) {
|
|
1604
|
-
const current = new LCommand(x0, y0, recentMove.x0, recentMove.y0);
|
|
1605
|
-
push(current);
|
|
1515
|
+
for (const segment of segments) {
|
|
1516
|
+
switch (segment.type) {
|
|
1517
|
+
case "M": {
|
|
1518
|
+
x0 = startX = segment.x;
|
|
1519
|
+
y0 = startY = segment.y;
|
|
1520
|
+
break;
|
|
1606
1521
|
}
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
}
|
|
1611
|
-
if ((found = lCommand.exec(remaining))) {
|
|
1612
|
-
const x = parseOrThrow(found[1]);
|
|
1613
|
-
const y = parseOrThrow(found[2]);
|
|
1614
|
-
const current = new LCommand(x0, y0, x, y);
|
|
1615
|
-
push(current);
|
|
1616
|
-
remaining = found[3];
|
|
1617
|
-
continue;
|
|
1618
|
-
}
|
|
1619
|
-
if ((found = lCommandRelative.exec(remaining))) {
|
|
1620
|
-
while (found) {
|
|
1621
|
-
const dx = parseOrThrow(found[1]);
|
|
1622
|
-
const dy = parseOrThrow(found[2]);
|
|
1623
|
-
const current = LCommand.relative(x0, y0, dx, dy);
|
|
1624
|
-
push(current);
|
|
1625
|
-
remaining = found[3];
|
|
1626
|
-
found = lCommandContinuation.exec(remaining);
|
|
1522
|
+
case "L": {
|
|
1523
|
+
push(new LCommand(x0, y0, segment.x, segment.y));
|
|
1524
|
+
break;
|
|
1627
1525
|
}
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
const current = new LCommand(x0, y0, x0 + dx, y0);
|
|
1641
|
-
push(current);
|
|
1642
|
-
remaining = found[2];
|
|
1643
|
-
found = hCommandContinuation.exec(remaining);
|
|
1644
|
-
}
|
|
1645
|
-
continue;
|
|
1646
|
-
}
|
|
1647
|
-
if ((found = vCommand.exec(remaining))) {
|
|
1648
|
-
const y = parseOrThrow(found[1]);
|
|
1649
|
-
const current = new LCommand(x0, y0, x0, y);
|
|
1650
|
-
push(current);
|
|
1651
|
-
remaining = found[2];
|
|
1652
|
-
continue;
|
|
1653
|
-
}
|
|
1654
|
-
if ((found = vCommandRelative.exec(remaining))) {
|
|
1655
|
-
while (found) {
|
|
1656
|
-
const dy = parseOrThrow(found[1]);
|
|
1657
|
-
const current = new LCommand(x0, y0, x0, y0 + dy);
|
|
1658
|
-
push(current);
|
|
1659
|
-
remaining = found[2];
|
|
1660
|
-
found = vCommandContinuation.exec(remaining);
|
|
1661
|
-
}
|
|
1662
|
-
continue;
|
|
1663
|
-
}
|
|
1664
|
-
if ((found = qCommand.exec(remaining))) {
|
|
1665
|
-
const x1 = parseOrThrow(found[1]);
|
|
1666
|
-
const y1 = parseOrThrow(found[2]);
|
|
1667
|
-
const x = parseOrThrow(found[3]);
|
|
1668
|
-
const y = parseOrThrow(found[4]);
|
|
1669
|
-
const current = QCommand.controlPoints(x0, y0, x1, y1, x, y);
|
|
1670
|
-
push(current);
|
|
1671
|
-
remaining = found[5];
|
|
1672
|
-
continue;
|
|
1673
|
-
}
|
|
1674
|
-
if ((found = qCommandRelative.exec(remaining))) {
|
|
1675
|
-
while (found) {
|
|
1676
|
-
const dx1 = parseOrThrow(found[1]);
|
|
1677
|
-
const dy1 = parseOrThrow(found[2]);
|
|
1678
|
-
const dx = parseOrThrow(found[3]);
|
|
1679
|
-
const dy = parseOrThrow(found[4]);
|
|
1680
|
-
const current = QCommand.relative(x0, y0, dx1, dy1, dx, dy);
|
|
1681
|
-
push(current);
|
|
1682
|
-
remaining = found[5];
|
|
1683
|
-
found = qCommandContinuation.exec(remaining);
|
|
1684
|
-
}
|
|
1685
|
-
continue;
|
|
1686
|
-
}
|
|
1687
|
-
if ((found = cCommand.exec(remaining))) {
|
|
1688
|
-
while (found) {
|
|
1689
|
-
const x1 = parseOrThrow(found[1]);
|
|
1690
|
-
const y1 = parseOrThrow(found[2]);
|
|
1691
|
-
const x2 = parseOrThrow(found[3]);
|
|
1692
|
-
const y2 = parseOrThrow(found[4]);
|
|
1693
|
-
const x3 = parseOrThrow(found[5]);
|
|
1694
|
-
const y3 = parseOrThrow(found[6]);
|
|
1695
|
-
const current = new CCommand(x0, y0, x1, y1, x2, y2, x3, y3);
|
|
1696
|
-
push(current);
|
|
1697
|
-
remaining = found[7];
|
|
1698
|
-
found = cCommandContinuation.exec(remaining);
|
|
1526
|
+
case "Q": {
|
|
1527
|
+
push(
|
|
1528
|
+
QCommand.controlPoints(
|
|
1529
|
+
x0,
|
|
1530
|
+
y0,
|
|
1531
|
+
segment.x1,
|
|
1532
|
+
segment.y1,
|
|
1533
|
+
segment.x,
|
|
1534
|
+
segment.y,
|
|
1535
|
+
),
|
|
1536
|
+
);
|
|
1537
|
+
break;
|
|
1699
1538
|
}
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1539
|
+
case "C": {
|
|
1540
|
+
push(
|
|
1541
|
+
new CCommand(
|
|
1542
|
+
x0,
|
|
1543
|
+
y0,
|
|
1544
|
+
segment.x1,
|
|
1545
|
+
segment.y1,
|
|
1546
|
+
segment.x2,
|
|
1547
|
+
segment.y2,
|
|
1548
|
+
segment.x,
|
|
1549
|
+
segment.y,
|
|
1550
|
+
),
|
|
1551
|
+
);
|
|
1552
|
+
break;
|
|
1714
1553
|
}
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
? [
|
|
1722
|
-
reflect(previousCommand.x2, x0),
|
|
1723
|
-
reflect(previousCommand.y2, y0),
|
|
1724
|
-
]
|
|
1725
|
-
: [x0, y0];
|
|
1726
|
-
const x2 = x0 + parseOrThrow(found[1]);
|
|
1727
|
-
const y2 = y0 + parseOrThrow(found[2]);
|
|
1728
|
-
const x3 = x0 + parseOrThrow(found[3]);
|
|
1729
|
-
const y3 = y0 + parseOrThrow(found[4]);
|
|
1730
|
-
const current = new CCommand(x0, y0, x1, y1, x2, y2, x3, y3);
|
|
1731
|
-
push(current);
|
|
1732
|
-
remaining = found[5];
|
|
1733
|
-
found = sCommandContinuation.exec(remaining);
|
|
1554
|
+
case "Z": {
|
|
1555
|
+
// PathShape has no Z. Close the gap with a line, unless there isn't one.
|
|
1556
|
+
if (x0 !== startX || y0 !== startY) {
|
|
1557
|
+
push(new LCommand(x0, y0, startX, startY));
|
|
1558
|
+
}
|
|
1559
|
+
break;
|
|
1734
1560
|
}
|
|
1735
|
-
continue;
|
|
1736
1561
|
}
|
|
1737
|
-
// This could be a faulty path string.
|
|
1738
|
-
// This could be a command that I just haven't implemented yet.
|
|
1739
|
-
// And sometimes it's a small bit of syntax that I missed.
|
|
1740
|
-
throw fail("Confused.");
|
|
1741
1562
|
}
|
|
1742
1563
|
return new this(commands);
|
|
1743
1564
|
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An SVG arc (the `A` and `a` path commands), as cubic Bézier curves.
|
|
3
|
+
*
|
|
4
|
+
* Pure math, with no dependencies, so it can be tested on its own.
|
|
5
|
+
* See {@link svgPathSegments} for the caller.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** One cubic Bézier curve. It starts where the previous one ended. */
|
|
10
|
+
export type CubicCurve = {
|
|
11
|
+
readonly x1: number;
|
|
12
|
+
readonly y1: number;
|
|
13
|
+
readonly x2: number;
|
|
14
|
+
readonly y2: number;
|
|
15
|
+
readonly x: number;
|
|
16
|
+
readonly y: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Turn one SVG elliptical arc into cubic curves, each covering at most a quarter turn.
|
|
21
|
+
* A full circle is four curves, off by about 0.03% of the radius.
|
|
22
|
+
*
|
|
23
|
+
* This is the endpoint to center conversion from the SVG specification, section F.6.5, and the
|
|
24
|
+
* out of range rules from F.6.6.
|
|
25
|
+
* Both are mostly translated directly, so the names match the specification.
|
|
26
|
+
*
|
|
27
|
+
* Out of range input is handled the way the specification says, not as an error:
|
|
28
|
+
* * Negative radii are made positive.
|
|
29
|
+
* * Radii too small to reach the end point are scaled up until they just reach it.
|
|
30
|
+
* * Both ends in the same place means there's no arc, and an empty array.
|
|
31
|
+
* * A radius of 0 means a straight line. The caller has to check for that. Not allowed here.
|
|
32
|
+
* @param x0 Where the arc starts.
|
|
33
|
+
* @param y0 Where the arc starts.
|
|
34
|
+
* @param rx The ellipse's radius, before rotating it.
|
|
35
|
+
* @param ry The ellipse's radius, before rotating it. Not 0.
|
|
36
|
+
* @param xAxisRotationDegrees How far the ellipse is turned, clockwise on the screen.
|
|
37
|
+
* @param largeArc Of the two arcs that fit, choose the one that's longer than a half turn.
|
|
38
|
+
* @param sweep Go clockwise on the screen (y down), as `true`.
|
|
39
|
+
* @param x Where the arc ends.
|
|
40
|
+
* @param y Where the arc ends.
|
|
41
|
+
*/
|
|
42
|
+
export function arcToCubics(
|
|
43
|
+
x0: number,
|
|
44
|
+
y0: number,
|
|
45
|
+
rx: number,
|
|
46
|
+
ry: number,
|
|
47
|
+
xAxisRotationDegrees: number,
|
|
48
|
+
largeArc: boolean,
|
|
49
|
+
sweep: boolean,
|
|
50
|
+
x: number,
|
|
51
|
+
y: number,
|
|
52
|
+
): CubicCurve[] {
|
|
53
|
+
if (x0 === x && y0 === y) {
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
rx = Math.abs(rx);
|
|
57
|
+
ry = Math.abs(ry);
|
|
58
|
+
if (!(rx > 0 && ry > 0)) {
|
|
59
|
+
throw new Error("arcToCubics(): a radius of 0 is a straight line.");
|
|
60
|
+
}
|
|
61
|
+
const φ = (xAxisRotationDegrees * Math.PI) / 180;
|
|
62
|
+
const cosφ = Math.cos(φ);
|
|
63
|
+
const sinφ = Math.sin(φ);
|
|
64
|
+
|
|
65
|
+
// F.6.5 step 1: the start point, in a frame centered halfway between the ends, aligned with the ellipse.
|
|
66
|
+
const dx2 = (x0 - x) / 2;
|
|
67
|
+
const dy2 = (y0 - y) / 2;
|
|
68
|
+
const x1p = cosφ * dx2 + sinφ * dy2;
|
|
69
|
+
const y1p = -sinφ * dx2 + cosφ * dy2;
|
|
70
|
+
|
|
71
|
+
// F.6.6 step 3: if the radii can't reach, grow them. The center is then halfway between the ends.
|
|
72
|
+
const λ = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
73
|
+
if (λ > 1) {
|
|
74
|
+
const scale = Math.sqrt(λ);
|
|
75
|
+
rx *= scale;
|
|
76
|
+
ry *= scale;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Step 2: the center, in that frame.
|
|
80
|
+
const rx2 = rx * rx;
|
|
81
|
+
const ry2 = ry * ry;
|
|
82
|
+
const numerator = rx2 * ry2 - rx2 * y1p * y1p - ry2 * x1p * x1p;
|
|
83
|
+
const denominator = rx2 * y1p * y1p + ry2 * x1p * x1p;
|
|
84
|
+
// Rounding can make this a tiny negative number when the radii were just scaled up.
|
|
85
|
+
const root = Math.sqrt(Math.max(0, numerator / denominator));
|
|
86
|
+
const coefficient = largeArc === sweep ? -root : root;
|
|
87
|
+
const cxp = (coefficient * rx * y1p) / ry;
|
|
88
|
+
const cyp = (-coefficient * ry * x1p) / rx;
|
|
89
|
+
|
|
90
|
+
// Step 3: the center, in the real coordinates.
|
|
91
|
+
const cx = cosφ * cxp - sinφ * cyp + (x0 + x) / 2;
|
|
92
|
+
const cy = sinφ * cxp + cosφ * cyp + (y0 + y) / 2;
|
|
93
|
+
|
|
94
|
+
// Step 4: the start angle, and how far to sweep.
|
|
95
|
+
const ux = (x1p - cxp) / rx;
|
|
96
|
+
const uy = (y1p - cyp) / ry;
|
|
97
|
+
const vx = (-x1p - cxp) / rx;
|
|
98
|
+
const vy = (-y1p - cyp) / ry;
|
|
99
|
+
const θ1 = Math.atan2(uy, ux);
|
|
100
|
+
let Δθ = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
101
|
+
if (!sweep && Δθ > 0) {
|
|
102
|
+
Δθ -= 2 * Math.PI;
|
|
103
|
+
} else if (sweep && Δθ < 0) {
|
|
104
|
+
Δθ += 2 * Math.PI;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Each curve covers at most a quarter turn. The small allowance keeps 90.00000001° from making two.
|
|
108
|
+
const count = Math.max(1, Math.ceil(Math.abs(Δθ) / (Math.PI / 2) - 1e-9));
|
|
109
|
+
const δ = Δθ / count;
|
|
110
|
+
// The distance from each end to its control point, as a fraction of the tangent's length.
|
|
111
|
+
const k = (4 / 3) * Math.tan(δ / 4);
|
|
112
|
+
|
|
113
|
+
const pointAt = (θ: number) => {
|
|
114
|
+
const cosθ = Math.cos(θ);
|
|
115
|
+
const sinθ = Math.sin(θ);
|
|
116
|
+
return {
|
|
117
|
+
x: cx + rx * cosθ * cosφ - ry * sinθ * sinφ,
|
|
118
|
+
y: cy + rx * cosθ * sinφ + ry * sinθ * cosφ,
|
|
119
|
+
// The derivative with respect to θ.
|
|
120
|
+
dx: -rx * sinθ * cosφ - ry * cosθ * sinφ,
|
|
121
|
+
dy: -rx * sinθ * sinφ + ry * cosθ * cosφ,
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const result: CubicCurve[] = [];
|
|
126
|
+
let start = pointAt(θ1);
|
|
127
|
+
for (let i = 1; i <= count; i++) {
|
|
128
|
+
const end = pointAt(θ1 + i * δ);
|
|
129
|
+
result.push({
|
|
130
|
+
x1: start.x + k * start.dx,
|
|
131
|
+
y1: start.y + k * start.dy,
|
|
132
|
+
x2: end.x - k * end.dx,
|
|
133
|
+
y2: end.y - k * end.dy,
|
|
134
|
+
// The last one lands exactly where the command said, not where the arithmetic did.
|
|
135
|
+
x: i === count ? x : end.x,
|
|
136
|
+
y: i === count ? y : end.y,
|
|
137
|
+
});
|
|
138
|
+
start = end;
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|