modern-path2d 0.1.8 → 0.1.10
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/README.md +1 -1
- package/dist/index.cjs +226 -225
- package/dist/index.d.cts +21 -12
- package/dist/index.d.mts +21 -12
- package/dist/index.d.ts +21 -12
- package/dist/index.js +1 -1
- package/dist/index.mjs +226 -225
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -384,221 +384,175 @@ function parseArcCommand(path, rx, ry, xAxisRotation, largeArcFlag, sweepFlag, s
|
|
|
384
384
|
const cy = Math.sin(xAxisRotation) * cxp + Math.cos(xAxisRotation) * cyp + (start.y + end.y) / 2;
|
|
385
385
|
const theta = svgAngle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
|
|
386
386
|
const delta = svgAngle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry) % (Math.PI * 2);
|
|
387
|
-
path.
|
|
387
|
+
path.ellipse(cx, cy, rx, ry, xAxisRotation, theta, theta + delta, sweepFlag === 1);
|
|
388
388
|
}
|
|
389
389
|
|
|
390
390
|
function getReflection(a, b) {
|
|
391
391
|
return a - (b - a);
|
|
392
392
|
}
|
|
393
393
|
function addPathCommandsToPath2D(commands, path) {
|
|
394
|
-
const
|
|
394
|
+
const current = new Vector2();
|
|
395
395
|
const control = new Vector2();
|
|
396
|
-
const firstPoint = new Vector2();
|
|
397
|
-
let isFirstPoint = true;
|
|
398
|
-
let doSetFirstPoint = false;
|
|
399
396
|
for (let i = 0, l = commands.length; i < l; i++) {
|
|
400
|
-
const
|
|
401
|
-
if (
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
if (command.type === "m" || command.type === "M") {
|
|
406
|
-
if (command.type === "m") {
|
|
407
|
-
point.x += command.x;
|
|
408
|
-
point.y += command.y;
|
|
397
|
+
const cmd = commands[i];
|
|
398
|
+
if (cmd.type === "m" || cmd.type === "M") {
|
|
399
|
+
if (cmd.type === "m") {
|
|
400
|
+
current.add(cmd);
|
|
409
401
|
} else {
|
|
410
|
-
|
|
411
|
-
point.y = command.y;
|
|
402
|
+
current.copy(cmd);
|
|
412
403
|
}
|
|
413
|
-
|
|
414
|
-
control.
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
if (command.type === "h") {
|
|
419
|
-
point.x += command.x;
|
|
404
|
+
path.moveTo(current.x, current.y);
|
|
405
|
+
control.copy(current);
|
|
406
|
+
} else if (cmd.type === "h" || cmd.type === "H") {
|
|
407
|
+
if (cmd.type === "h") {
|
|
408
|
+
current.x += cmd.x;
|
|
420
409
|
} else {
|
|
421
|
-
|
|
410
|
+
current.x = cmd.x;
|
|
422
411
|
}
|
|
423
|
-
|
|
424
|
-
control.
|
|
425
|
-
|
|
426
|
-
if (
|
|
427
|
-
|
|
428
|
-
} else if (command.type === "v" || command.type === "V") {
|
|
429
|
-
if (command.type === "v") {
|
|
430
|
-
point.y += command.y;
|
|
412
|
+
path.lineTo(current.x, current.y);
|
|
413
|
+
control.copy(current);
|
|
414
|
+
} else if (cmd.type === "v" || cmd.type === "V") {
|
|
415
|
+
if (cmd.type === "v") {
|
|
416
|
+
current.y += cmd.y;
|
|
431
417
|
} else {
|
|
432
|
-
|
|
418
|
+
current.y = cmd.y;
|
|
433
419
|
}
|
|
434
|
-
|
|
435
|
-
control.
|
|
436
|
-
|
|
437
|
-
if (
|
|
438
|
-
|
|
439
|
-
} else if (command.type === "l" || command.type === "L") {
|
|
440
|
-
if (command.type === "l") {
|
|
441
|
-
point.x += command.x;
|
|
442
|
-
point.y += command.y;
|
|
420
|
+
path.lineTo(current.x, current.y);
|
|
421
|
+
control.copy(current);
|
|
422
|
+
} else if (cmd.type === "l" || cmd.type === "L") {
|
|
423
|
+
if (cmd.type === "l") {
|
|
424
|
+
current.add(cmd);
|
|
443
425
|
} else {
|
|
444
|
-
|
|
445
|
-
point.y = command.y;
|
|
426
|
+
current.copy(cmd);
|
|
446
427
|
}
|
|
447
|
-
|
|
448
|
-
control.
|
|
449
|
-
|
|
450
|
-
if (
|
|
451
|
-
firstPoint.copy(point);
|
|
452
|
-
} else if (command.type === "c" || command.type === "C") {
|
|
453
|
-
if (command.type === "c") {
|
|
428
|
+
path.lineTo(current.x, current.y);
|
|
429
|
+
control.copy(current);
|
|
430
|
+
} else if (cmd.type === "c" || cmd.type === "C") {
|
|
431
|
+
if (cmd.type === "c") {
|
|
454
432
|
path.bezierCurveTo(
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
433
|
+
current.x + cmd.x1,
|
|
434
|
+
current.y + cmd.y1,
|
|
435
|
+
current.x + cmd.x2,
|
|
436
|
+
current.y + cmd.y2,
|
|
437
|
+
current.x + cmd.x,
|
|
438
|
+
current.y + cmd.y
|
|
461
439
|
);
|
|
462
|
-
control.x =
|
|
463
|
-
control.y =
|
|
464
|
-
|
|
465
|
-
point.y += command.y;
|
|
440
|
+
control.x = current.x + cmd.x2;
|
|
441
|
+
control.y = current.y + cmd.y2;
|
|
442
|
+
current.add(cmd);
|
|
466
443
|
} else {
|
|
467
444
|
path.bezierCurveTo(
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
445
|
+
cmd.x1,
|
|
446
|
+
cmd.y1,
|
|
447
|
+
cmd.x2,
|
|
448
|
+
cmd.y2,
|
|
449
|
+
cmd.x,
|
|
450
|
+
cmd.y
|
|
474
451
|
);
|
|
475
|
-
control.x =
|
|
476
|
-
control.y =
|
|
477
|
-
|
|
478
|
-
point.y = command.y;
|
|
452
|
+
control.x = cmd.x2;
|
|
453
|
+
control.y = cmd.y2;
|
|
454
|
+
current.copy(cmd);
|
|
479
455
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
} else if (command.type === "s" || command.type === "S") {
|
|
483
|
-
if (command.type === "s") {
|
|
456
|
+
} else if (cmd.type === "s" || cmd.type === "S") {
|
|
457
|
+
if (cmd.type === "s") {
|
|
484
458
|
path.bezierCurveTo(
|
|
485
|
-
getReflection(
|
|
486
|
-
getReflection(
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
459
|
+
getReflection(current.x, control.x),
|
|
460
|
+
getReflection(current.y, control.y),
|
|
461
|
+
current.x + cmd.x2,
|
|
462
|
+
current.y + cmd.y2,
|
|
463
|
+
current.x + cmd.x,
|
|
464
|
+
current.y + cmd.y
|
|
491
465
|
);
|
|
492
|
-
control.x =
|
|
493
|
-
control.y =
|
|
494
|
-
|
|
495
|
-
point.y += command.y;
|
|
466
|
+
control.x = current.x + cmd.x2;
|
|
467
|
+
control.y = current.y + cmd.y2;
|
|
468
|
+
current.add(cmd);
|
|
496
469
|
} else {
|
|
497
470
|
path.bezierCurveTo(
|
|
498
|
-
getReflection(
|
|
499
|
-
getReflection(
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
471
|
+
getReflection(current.x, control.x),
|
|
472
|
+
getReflection(current.y, control.y),
|
|
473
|
+
cmd.x2,
|
|
474
|
+
cmd.y2,
|
|
475
|
+
cmd.x,
|
|
476
|
+
cmd.y
|
|
504
477
|
);
|
|
505
|
-
control.x =
|
|
506
|
-
control.y =
|
|
507
|
-
|
|
508
|
-
point.y = command.y;
|
|
478
|
+
control.x = cmd.x2;
|
|
479
|
+
control.y = cmd.y2;
|
|
480
|
+
current.copy(cmd);
|
|
509
481
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
} else if (command.type === "q" || command.type === "Q") {
|
|
513
|
-
if (command.type === "q") {
|
|
482
|
+
} else if (cmd.type === "q" || cmd.type === "Q") {
|
|
483
|
+
if (cmd.type === "q") {
|
|
514
484
|
path.quadraticCurveTo(
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
485
|
+
current.x + cmd.x1,
|
|
486
|
+
current.y + cmd.y1,
|
|
487
|
+
current.x + cmd.x,
|
|
488
|
+
current.y + cmd.y
|
|
519
489
|
);
|
|
520
|
-
control.x =
|
|
521
|
-
control.y =
|
|
522
|
-
|
|
523
|
-
point.y += command.y;
|
|
490
|
+
control.x = current.x + cmd.x1;
|
|
491
|
+
control.y = current.y + cmd.y1;
|
|
492
|
+
current.add(cmd);
|
|
524
493
|
} else {
|
|
525
494
|
path.quadraticCurveTo(
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
495
|
+
cmd.x1,
|
|
496
|
+
cmd.y1,
|
|
497
|
+
cmd.x,
|
|
498
|
+
cmd.y
|
|
530
499
|
);
|
|
531
|
-
control.x =
|
|
532
|
-
control.y =
|
|
533
|
-
|
|
534
|
-
point.y = command.y;
|
|
500
|
+
control.x = cmd.x1;
|
|
501
|
+
control.y = cmd.y1;
|
|
502
|
+
current.copy(cmd);
|
|
535
503
|
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
const rx = getReflection(point.x, control.x);
|
|
540
|
-
const ry = getReflection(point.y, control.y);
|
|
504
|
+
} else if (cmd.type === "t" || cmd.type === "T") {
|
|
505
|
+
const rx = getReflection(current.x, control.x);
|
|
506
|
+
const ry = getReflection(current.y, control.y);
|
|
541
507
|
control.x = rx;
|
|
542
508
|
control.y = ry;
|
|
543
|
-
if (
|
|
509
|
+
if (cmd.type === "t") {
|
|
544
510
|
path.quadraticCurveTo(
|
|
545
511
|
rx,
|
|
546
512
|
ry,
|
|
547
|
-
|
|
548
|
-
|
|
513
|
+
current.x + cmd.x,
|
|
514
|
+
current.y + cmd.y
|
|
549
515
|
);
|
|
550
|
-
|
|
551
|
-
point.y += command.y;
|
|
516
|
+
current.add(cmd);
|
|
552
517
|
} else {
|
|
553
518
|
path.quadraticCurveTo(
|
|
554
519
|
rx,
|
|
555
520
|
ry,
|
|
556
|
-
|
|
557
|
-
|
|
521
|
+
cmd.x,
|
|
522
|
+
cmd.y
|
|
558
523
|
);
|
|
559
|
-
|
|
560
|
-
point.y = command.y;
|
|
524
|
+
current.copy(cmd);
|
|
561
525
|
}
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
if (command.x === 0 && command.y === 0)
|
|
526
|
+
} else if (cmd.type === "a" || cmd.type === "A") {
|
|
527
|
+
const start = current.clone();
|
|
528
|
+
if (cmd.type === "a") {
|
|
529
|
+
if (cmd.x === 0 && cmd.y === 0)
|
|
567
530
|
continue;
|
|
568
|
-
|
|
569
|
-
point.y += command.y;
|
|
531
|
+
current.add(cmd);
|
|
570
532
|
} else {
|
|
571
|
-
if (
|
|
533
|
+
if (current.equals(cmd))
|
|
572
534
|
continue;
|
|
573
|
-
|
|
574
|
-
point.y = command.y;
|
|
535
|
+
current.copy(cmd);
|
|
575
536
|
}
|
|
576
|
-
|
|
577
|
-
control.x = point.x;
|
|
578
|
-
control.y = point.y;
|
|
537
|
+
control.copy(current);
|
|
579
538
|
parseArcCommand(
|
|
580
539
|
path,
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
540
|
+
cmd.rx,
|
|
541
|
+
cmd.ry,
|
|
542
|
+
cmd.angle,
|
|
543
|
+
cmd.largeArcFlag,
|
|
544
|
+
cmd.sweepFlag,
|
|
586
545
|
start,
|
|
587
|
-
|
|
546
|
+
current
|
|
588
547
|
);
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
path.currentPath.autoClose = true;
|
|
593
|
-
if (path.currentPath.curves.length > 0) {
|
|
594
|
-
point.copy(firstPoint);
|
|
595
|
-
path.currentPath.currentPoint.copy(point);
|
|
596
|
-
isFirstPoint = true;
|
|
548
|
+
} else if (cmd.type === "z" || cmd.type === "Z") {
|
|
549
|
+
if (path.startPoint) {
|
|
550
|
+
current.copy(path.startPoint);
|
|
597
551
|
}
|
|
552
|
+
path.closePath();
|
|
598
553
|
} else {
|
|
599
|
-
console.warn("Unsupported commands",
|
|
554
|
+
console.warn("Unsupported commands", cmd);
|
|
600
555
|
}
|
|
601
|
-
doSetFirstPoint = false;
|
|
602
556
|
}
|
|
603
557
|
}
|
|
604
558
|
|
|
@@ -1189,15 +1143,15 @@ const tempTransform1$1 = new Matrix3();
|
|
|
1189
1143
|
const tempTransform2$1 = new Matrix3();
|
|
1190
1144
|
const tempV2 = new Vector2();
|
|
1191
1145
|
class EllipseCurve extends Curve {
|
|
1192
|
-
constructor(center = new Vector2(), radiusX = 1, radiusY = 1, startAngle = 0, endAngle = Math.PI * 2, clockwise = false
|
|
1146
|
+
constructor(center = new Vector2(), radiusX = 1, radiusY = 1, rotation = 0, startAngle = 0, endAngle = Math.PI * 2, clockwise = false) {
|
|
1193
1147
|
super();
|
|
1194
1148
|
this.center = center;
|
|
1195
1149
|
this.radiusX = radiusX;
|
|
1196
1150
|
this.radiusY = radiusY;
|
|
1151
|
+
this.rotation = rotation;
|
|
1197
1152
|
this.startAngle = startAngle;
|
|
1198
1153
|
this.endAngle = endAngle;
|
|
1199
1154
|
this.clockwise = clockwise;
|
|
1200
|
-
this.rotation = rotation;
|
|
1201
1155
|
}
|
|
1202
1156
|
getPoint(t, output = new Vector2()) {
|
|
1203
1157
|
const twoPi = Math.PI * 2;
|
|
@@ -1881,27 +1835,36 @@ class CurvePath extends Curve {
|
|
|
1881
1835
|
constructor(points) {
|
|
1882
1836
|
super();
|
|
1883
1837
|
__publicField$1(this, "curves", []);
|
|
1838
|
+
__publicField$1(this, "startPoint");
|
|
1884
1839
|
__publicField$1(this, "currentPoint", new Vector2());
|
|
1885
1840
|
__publicField$1(this, "autoClose", false);
|
|
1886
1841
|
__publicField$1(this, "_cacheLengths", []);
|
|
1887
1842
|
if (points) {
|
|
1888
|
-
this.
|
|
1843
|
+
this.addPoints(points);
|
|
1889
1844
|
}
|
|
1890
1845
|
}
|
|
1891
1846
|
addCurve(curve) {
|
|
1892
1847
|
this.curves.push(curve);
|
|
1893
1848
|
return this;
|
|
1894
1849
|
}
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
this.
|
|
1850
|
+
addPoints(points) {
|
|
1851
|
+
this.moveTo(points[0].x, points[0].y);
|
|
1852
|
+
for (let i = 1, len = points.length; i < len; i++) {
|
|
1853
|
+
const { x, y } = points[i];
|
|
1854
|
+
this.lineTo(x, y);
|
|
1900
1855
|
}
|
|
1901
1856
|
return this;
|
|
1902
1857
|
}
|
|
1903
|
-
|
|
1904
|
-
|
|
1858
|
+
addCommands(commands) {
|
|
1859
|
+
addPathCommandsToPath2D(commands, this);
|
|
1860
|
+
return this;
|
|
1861
|
+
}
|
|
1862
|
+
addData(data) {
|
|
1863
|
+
this.addCommands(pathDataToPathCommands(data));
|
|
1864
|
+
return this;
|
|
1865
|
+
}
|
|
1866
|
+
getPoint(t, output = new Vector2()) {
|
|
1867
|
+
const d = t * this.getLength();
|
|
1905
1868
|
const curveLengths = this.getCurveLengths();
|
|
1906
1869
|
let i = 0;
|
|
1907
1870
|
while (i < curveLengths.length) {
|
|
@@ -1966,23 +1929,28 @@ class CurvePath extends Curve {
|
|
|
1966
1929
|
}
|
|
1967
1930
|
return points;
|
|
1968
1931
|
}
|
|
1969
|
-
|
|
1970
|
-
this.
|
|
1971
|
-
|
|
1972
|
-
this.
|
|
1932
|
+
_setCurrentPoint(point) {
|
|
1933
|
+
this.currentPoint.copy(point);
|
|
1934
|
+
if (!this.startPoint) {
|
|
1935
|
+
this.startPoint = this.currentPoint.clone();
|
|
1973
1936
|
}
|
|
1974
1937
|
return this;
|
|
1975
1938
|
}
|
|
1976
|
-
|
|
1977
|
-
this.
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
new
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1939
|
+
closePath() {
|
|
1940
|
+
const start = this.startPoint;
|
|
1941
|
+
if (start) {
|
|
1942
|
+
const end = this.currentPoint;
|
|
1943
|
+
if (!start.equals(end)) {
|
|
1944
|
+
this.curves.push(new LineCurve(end, start));
|
|
1945
|
+
this.currentPoint.copy(start);
|
|
1946
|
+
}
|
|
1947
|
+
this.startPoint = void 0;
|
|
1948
|
+
}
|
|
1949
|
+
return this;
|
|
1950
|
+
}
|
|
1951
|
+
moveTo(x, y) {
|
|
1985
1952
|
this.currentPoint.set(x, y);
|
|
1953
|
+
this.startPoint = this.currentPoint.clone();
|
|
1986
1954
|
return this;
|
|
1987
1955
|
}
|
|
1988
1956
|
lineTo(x, y) {
|
|
@@ -1992,64 +1960,56 @@ class CurvePath extends Curve {
|
|
|
1992
1960
|
new Vector2(x, y)
|
|
1993
1961
|
)
|
|
1994
1962
|
);
|
|
1995
|
-
this.
|
|
1963
|
+
this._setCurrentPoint({ x, y });
|
|
1996
1964
|
return this;
|
|
1997
1965
|
}
|
|
1998
|
-
|
|
1999
|
-
this.currentPoint.set(x, y);
|
|
2000
|
-
return this;
|
|
2001
|
-
}
|
|
2002
|
-
quadraticCurveTo(cpx, cpy, x, y) {
|
|
1966
|
+
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
2003
1967
|
this.curves.push(
|
|
2004
|
-
new
|
|
1968
|
+
new CubicBezierCurve(
|
|
2005
1969
|
this.currentPoint.clone(),
|
|
2006
|
-
new Vector2(
|
|
1970
|
+
new Vector2(cp1x, cp1y),
|
|
1971
|
+
new Vector2(cp2x, cp2y),
|
|
2007
1972
|
new Vector2(x, y)
|
|
2008
1973
|
)
|
|
2009
1974
|
);
|
|
2010
|
-
this.
|
|
1975
|
+
this._setCurrentPoint({ x, y });
|
|
2011
1976
|
return this;
|
|
2012
1977
|
}
|
|
2013
|
-
|
|
1978
|
+
quadraticCurveTo(cpx, cpy, x, y) {
|
|
2014
1979
|
this.curves.push(
|
|
2015
|
-
new
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
1980
|
+
new QuadraticBezierCurve(
|
|
1981
|
+
this.currentPoint.clone(),
|
|
1982
|
+
new Vector2(cpx, cpy),
|
|
1983
|
+
new Vector2(x, y)
|
|
2019
1984
|
)
|
|
2020
1985
|
);
|
|
2021
|
-
this.
|
|
1986
|
+
this._setCurrentPoint({ x, y });
|
|
2022
1987
|
return this;
|
|
2023
1988
|
}
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
this.curves.push(new SplineCurve(npts));
|
|
2027
|
-
this.currentPoint.copy(points[points.length - 1]);
|
|
1989
|
+
arc(x, y, radius, startAngle, endAngle, counterclockwise) {
|
|
1990
|
+
this.ellipse(x, y, radius, radius, 0, startAngle, endAngle, counterclockwise);
|
|
2028
1991
|
return this;
|
|
2029
1992
|
}
|
|
2030
|
-
|
|
1993
|
+
relativeArc(x, y, radius, startAngle, endAngle, counterclockwise) {
|
|
2031
1994
|
const point = this.currentPoint;
|
|
2032
|
-
this.
|
|
2033
|
-
return this;
|
|
2034
|
-
}
|
|
2035
|
-
absarc(x, y, radius, startAngle, endAngle, clockwise = false) {
|
|
2036
|
-
this.absellipse(x, y, radius, radius, startAngle, endAngle, clockwise);
|
|
1995
|
+
this.arc(x + point.x, y + point.y, radius, startAngle, endAngle, counterclockwise);
|
|
2037
1996
|
return this;
|
|
2038
1997
|
}
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
1998
|
+
// TODO
|
|
1999
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
2000
|
+
arcTo(x1, y1, x2, y2, radius) {
|
|
2001
|
+
console.warn("Method arcTo not supported yet");
|
|
2042
2002
|
return this;
|
|
2043
2003
|
}
|
|
2044
|
-
|
|
2004
|
+
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise = true) {
|
|
2045
2005
|
const curve = new EllipseCurve(
|
|
2046
2006
|
new Vector2(x, y),
|
|
2047
2007
|
radiusX,
|
|
2048
2008
|
radiusY,
|
|
2009
|
+
rotation,
|
|
2049
2010
|
startAngle,
|
|
2050
2011
|
endAngle,
|
|
2051
|
-
|
|
2052
|
-
rotation
|
|
2012
|
+
!counterclockwise
|
|
2053
2013
|
);
|
|
2054
2014
|
if (this.curves.length > 0) {
|
|
2055
2015
|
const first = curve.getPoint(0);
|
|
@@ -2058,16 +2018,45 @@ class CurvePath extends Curve {
|
|
|
2058
2018
|
}
|
|
2059
2019
|
}
|
|
2060
2020
|
this.curves.push(curve);
|
|
2061
|
-
this.
|
|
2021
|
+
this._setCurrentPoint(curve.getPoint(1));
|
|
2062
2022
|
return this;
|
|
2063
2023
|
}
|
|
2064
|
-
|
|
2065
|
-
|
|
2024
|
+
relativeEllipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
2025
|
+
const point = this.currentPoint;
|
|
2026
|
+
this.ellipse(x + point.x, y + point.y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise);
|
|
2027
|
+
return this;
|
|
2028
|
+
}
|
|
2029
|
+
rect(x, y, w, h) {
|
|
2030
|
+
this.curves.push(
|
|
2031
|
+
new RectangularCurve(
|
|
2032
|
+
new Vector2(x + w / 2, y + h / 2),
|
|
2033
|
+
w / 2,
|
|
2034
|
+
w / h
|
|
2035
|
+
)
|
|
2036
|
+
);
|
|
2037
|
+
this._setCurrentPoint({ x, y });
|
|
2038
|
+
return this;
|
|
2039
|
+
}
|
|
2040
|
+
splineThru(points) {
|
|
2041
|
+
this.curves.push(new SplineCurve([this.currentPoint.clone()].concat(points)));
|
|
2042
|
+
this._setCurrentPoint(points[points.length - 1]);
|
|
2043
|
+
return this;
|
|
2044
|
+
}
|
|
2045
|
+
transformPoint(cb) {
|
|
2046
|
+
this.curves.forEach((curve) => curve.transformPoint(cb));
|
|
2047
|
+
return this;
|
|
2066
2048
|
}
|
|
2067
2049
|
getMinMax(min = Vector2.MAX, max = Vector2.MIN) {
|
|
2068
2050
|
this.curves.forEach((curve) => curve.getMinMax(min, max));
|
|
2069
2051
|
return { min, max };
|
|
2070
2052
|
}
|
|
2053
|
+
getBoundingBox() {
|
|
2054
|
+
const { min, max } = this.getMinMax();
|
|
2055
|
+
return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
|
|
2056
|
+
}
|
|
2057
|
+
getCommands() {
|
|
2058
|
+
return this.curves.flatMap((curve) => curve.getCommands());
|
|
2059
|
+
}
|
|
2071
2060
|
drawTo(ctx) {
|
|
2072
2061
|
const point = this.curves[0]?.getPoint(0);
|
|
2073
2062
|
if (point) {
|
|
@@ -2112,6 +2101,12 @@ class Path2D {
|
|
|
2112
2101
|
}
|
|
2113
2102
|
}
|
|
2114
2103
|
}
|
|
2104
|
+
get startPoint() {
|
|
2105
|
+
return this.currentPath.startPoint;
|
|
2106
|
+
}
|
|
2107
|
+
get currentPoint() {
|
|
2108
|
+
return this.currentPath.currentPoint;
|
|
2109
|
+
}
|
|
2115
2110
|
addPath(path) {
|
|
2116
2111
|
if (path instanceof Path2D) {
|
|
2117
2112
|
this.paths.push(...path.paths.map((v) => v.clone()));
|
|
@@ -2121,12 +2116,19 @@ class Path2D {
|
|
|
2121
2116
|
return this;
|
|
2122
2117
|
}
|
|
2123
2118
|
closePath() {
|
|
2124
|
-
this.
|
|
2119
|
+
const startPoint = this.startPoint;
|
|
2120
|
+
if (startPoint) {
|
|
2121
|
+
this.currentPath.closePath();
|
|
2122
|
+
if (this.currentPath.curves.length > 0) {
|
|
2123
|
+
this.currentPath = new CurvePath().moveTo(startPoint.x, startPoint.y);
|
|
2124
|
+
this.paths.push(this.currentPath);
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2125
2127
|
return this;
|
|
2126
2128
|
}
|
|
2127
2129
|
moveTo(x, y) {
|
|
2128
2130
|
const { currentPoint, curves } = this.currentPath;
|
|
2129
|
-
if (currentPoint.
|
|
2131
|
+
if (!currentPoint.equals({ x, y })) {
|
|
2130
2132
|
if (curves.length) {
|
|
2131
2133
|
this.currentPath = new CurvePath().moveTo(x, y);
|
|
2132
2134
|
this.paths.push(this.currentPath);
|
|
@@ -2149,17 +2151,15 @@ class Path2D {
|
|
|
2149
2151
|
return this;
|
|
2150
2152
|
}
|
|
2151
2153
|
arc(x, y, radius, startAngle, endAngle, counterclockwise) {
|
|
2152
|
-
this.currentPath.
|
|
2154
|
+
this.currentPath.arc(x, y, radius, startAngle, endAngle, counterclockwise);
|
|
2153
2155
|
return this;
|
|
2154
2156
|
}
|
|
2155
|
-
// TODO
|
|
2156
|
-
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
2157
2157
|
arcTo(x1, y1, x2, y2, radius) {
|
|
2158
|
-
|
|
2158
|
+
this.currentPath.arcTo(x1, y1, x2, y2, radius);
|
|
2159
2159
|
return this;
|
|
2160
2160
|
}
|
|
2161
2161
|
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
2162
|
-
this.currentPath.
|
|
2162
|
+
this.currentPath.ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise);
|
|
2163
2163
|
return this;
|
|
2164
2164
|
}
|
|
2165
2165
|
rect(x, y, w, h) {
|
|
@@ -2199,7 +2199,7 @@ class Path2D {
|
|
|
2199
2199
|
return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
|
|
2200
2200
|
}
|
|
2201
2201
|
getCommands() {
|
|
2202
|
-
return this.paths.flatMap((path) => path.
|
|
2202
|
+
return this.paths.flatMap((path) => path.getCommands());
|
|
2203
2203
|
}
|
|
2204
2204
|
getData() {
|
|
2205
2205
|
return this.paths.map((path) => path.getData()).join(" ");
|
|
@@ -2452,7 +2452,7 @@ function parseNodeTransform(node) {
|
|
|
2452
2452
|
|
|
2453
2453
|
function parseCircleNode(node) {
|
|
2454
2454
|
return new Path2D().addPath(
|
|
2455
|
-
new CurvePath().
|
|
2455
|
+
new CurvePath().arc(
|
|
2456
2456
|
parseFloatWithUnits(node.getAttribute("cx") || 0),
|
|
2457
2457
|
parseFloatWithUnits(node.getAttribute("cy") || 0),
|
|
2458
2458
|
parseFloatWithUnits(node.getAttribute("r") || 0),
|
|
@@ -2484,12 +2484,13 @@ function parseCSSStylesheet(node, stylesheets) {
|
|
|
2484
2484
|
|
|
2485
2485
|
function parseEllipseNode(node) {
|
|
2486
2486
|
return new Path2D().addPath(
|
|
2487
|
-
new CurvePath().
|
|
2487
|
+
new CurvePath().ellipse(
|
|
2488
2488
|
parseFloatWithUnits(node.getAttribute("cx") || 0),
|
|
2489
2489
|
parseFloatWithUnits(node.getAttribute("cy") || 0),
|
|
2490
2490
|
parseFloatWithUnits(node.getAttribute("rx") || 0),
|
|
2491
2491
|
parseFloatWithUnits(node.getAttribute("ry") || 0),
|
|
2492
2492
|
0,
|
|
2493
|
+
0,
|
|
2493
2494
|
Math.PI * 2
|
|
2494
2495
|
)
|
|
2495
2496
|
);
|