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