modern-path2d 1.3.1 → 1.3.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/dist/index.cjs CHANGED
@@ -1035,7 +1035,7 @@ function cubicBezier(t, p0, p1, p2, p3) {
1035
1035
  return cubicBezierP0(t, p0) + cubicBezierP1(t, p1) + cubicBezierP2(t, p2) + cubicBezierP3(t, p3);
1036
1036
  }
1037
1037
 
1038
- function fillTriangulate(points, options = {}) {
1038
+ function fillTriangulate(pointArray, options = {}) {
1039
1039
  let {
1040
1040
  vertices = [],
1041
1041
  indices = [],
@@ -1044,17 +1044,17 @@ function fillTriangulate(points, options = {}) {
1044
1044
  verticesOffset = vertices.length / verticesStride,
1045
1045
  indicesOffset = indices.length
1046
1046
  } = options;
1047
- const triangles = earcut__default(points, holes, 2);
1048
- if (triangles) {
1047
+ const triangles = earcut__default(pointArray, holes, 2);
1048
+ if (triangles.length) {
1049
1049
  for (let i = 0; i < triangles.length; i += 3) {
1050
1050
  indices[indicesOffset++] = triangles[i] + verticesOffset;
1051
1051
  indices[indicesOffset++] = triangles[i + 1] + verticesOffset;
1052
1052
  indices[indicesOffset++] = triangles[i + 2] + verticesOffset;
1053
1053
  }
1054
1054
  let index = verticesOffset * verticesStride;
1055
- for (let i = 0; i < points.length; i += 2) {
1056
- vertices[index] = points[i];
1057
- vertices[index + 1] = points[i + 1];
1055
+ for (let i = 0; i < pointArray.length; i += 2) {
1056
+ vertices[index] = pointArray[i];
1057
+ vertices[index + 1] = pointArray[i + 1];
1058
1058
  index += verticesStride;
1059
1059
  }
1060
1060
  }
@@ -1181,6 +1181,35 @@ function recursive(points, x1, y1, x2, y2, x3, y3, distanceTolerance, level) {
1181
1181
  recursive(points, x123, y123, x23, y23, x3, y3, distanceTolerance, level + 1);
1182
1182
  }
1183
1183
 
1184
+ function getDirectedArea(vertices) {
1185
+ let area = 0;
1186
+ const n = vertices.length;
1187
+ for (let i = 0; i < n; i += 2) {
1188
+ const x0 = vertices[i];
1189
+ const y0 = vertices[i + 1];
1190
+ const x1 = vertices[(i + 2) % (n - 1)];
1191
+ const y1 = vertices[(i + 3) % n];
1192
+ area += x0 * y1 - x1 * y0;
1193
+ }
1194
+ return area / 2;
1195
+ }
1196
+
1197
+ function pointInPolygon(point, polygon) {
1198
+ let inside = false;
1199
+ const [x, y] = point;
1200
+ const len = polygon.length / 2;
1201
+ for (let i = 0, j = len - 1; i < len; j = i++) {
1202
+ const xi = polygon[i * 2];
1203
+ const yi = polygon[i * 2 + 1];
1204
+ const xj = polygon[j * 2];
1205
+ const yj = polygon[j * 2 + 1];
1206
+ if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
1207
+ inside = !inside;
1208
+ }
1209
+ }
1210
+ return inside;
1211
+ }
1212
+
1184
1213
  function quadraticBezierP0(t, p) {
1185
1214
  const k = 1 - t;
1186
1215
  return k * k * p;
@@ -1609,7 +1638,7 @@ class Curve {
1609
1638
  });
1610
1639
  return this;
1611
1640
  }
1612
- getUnevenPointArray(count = 5, output = []) {
1641
+ getUnevenVertices(count = 5, output = []) {
1613
1642
  const p = new Vector2();
1614
1643
  for (let i = 0, len = Math.max(1, count) - 1; i <= len; i++) {
1615
1644
  this.getPoint(i / len, p);
@@ -1617,7 +1646,7 @@ class Curve {
1617
1646
  }
1618
1647
  return output;
1619
1648
  }
1620
- getSpacedPointArray(count = 5, output = []) {
1649
+ getSpacedVertices(count = 5, output = []) {
1621
1650
  const p = new Vector2();
1622
1651
  for (let i = 0, len = Math.max(1, count) - 1; i <= len; i++) {
1623
1652
  this.getPointAt(i / len, p);
@@ -1625,40 +1654,40 @@ class Curve {
1625
1654
  }
1626
1655
  return output;
1627
1656
  }
1628
- getAdaptivePointArray(output = []) {
1629
- return this.getUnevenPointArray(5, output);
1657
+ getAdaptiveVertices(output = []) {
1658
+ return this.getUnevenVertices(5, output);
1630
1659
  }
1631
- _pointArrayToPoint(array, output = []) {
1632
- for (let i = 0, len = array.length; i < len; i += 2) {
1633
- const x = array[i];
1634
- const y = array[i + 1];
1660
+ _verticesToPoints(vertices, output = []) {
1661
+ for (let i = 0, len = vertices.length; i < len; i += 2) {
1662
+ const x = vertices[i];
1663
+ const y = vertices[i + 1];
1635
1664
  output.push(new Vector2(x, y));
1636
1665
  }
1637
1666
  return output;
1638
1667
  }
1639
1668
  getSpacedPoints(count, output = []) {
1640
- const array = this.getSpacedPointArray(count);
1641
- this._pointArrayToPoint(array, output);
1669
+ const array = this.getSpacedVertices(count);
1670
+ this._verticesToPoints(array, output);
1642
1671
  return output;
1643
1672
  }
1644
1673
  getUnevenPoints(count, output = []) {
1645
- const array = this.getUnevenPointArray(count);
1646
- this._pointArrayToPoint(array, output);
1674
+ const array = this.getUnevenVertices(count);
1675
+ this._verticesToPoints(array, output);
1647
1676
  return output;
1648
1677
  }
1649
1678
  getAdaptivePoints(output = []) {
1650
- const array = this.getAdaptivePointArray();
1651
- this._pointArrayToPoint(array, output);
1679
+ const array = this.getAdaptiveVertices();
1680
+ this._verticesToPoints(array, output);
1652
1681
  return output;
1653
1682
  }
1654
1683
  getPoints(count, output = []) {
1655
1684
  let array;
1656
1685
  if (count) {
1657
- array = this.getUnevenPointArray(count);
1686
+ array = this.getUnevenVertices(count);
1658
1687
  } else {
1659
- array = this.getAdaptivePointArray();
1688
+ array = this.getAdaptiveVertices();
1660
1689
  }
1661
- this._pointArrayToPoint(array, output);
1690
+ this._verticesToPoints(array, output);
1662
1691
  return output;
1663
1692
  }
1664
1693
  getLength() {
@@ -1763,44 +1792,21 @@ class Curve {
1763
1792
  const { min, max } = this.getMinMax();
1764
1793
  return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
1765
1794
  }
1795
+ getFillVertices(_options) {
1796
+ return this.getAdaptiveVertices();
1797
+ }
1766
1798
  fillTriangulate(options) {
1767
1799
  return fillTriangulate(
1768
- this.getAdaptivePointArray(),
1800
+ this.getFillVertices(options),
1769
1801
  options
1770
1802
  );
1771
1803
  }
1772
1804
  strokeTriangulate(options) {
1773
1805
  return strokeTriangulate(
1774
- this.getAdaptivePointArray(),
1806
+ this.getAdaptiveVertices(),
1775
1807
  options
1776
1808
  );
1777
1809
  }
1778
- toTriangulatedSVGString(result = this.fillTriangulate(), padding = 0) {
1779
- const { vertices, indices } = result;
1780
- const min = { x: -padding, y: -padding };
1781
- const max = { x: padding, y: padding };
1782
- const getPoint = (indice) => {
1783
- const x = vertices[indice * 2];
1784
- const y = vertices[indice * 2 + 1];
1785
- min.x = Math.min(min.x, x + padding);
1786
- max.x = Math.max(max.x, x + padding);
1787
- min.y = Math.min(min.y, y + padding);
1788
- max.y = Math.max(max.y, y + padding);
1789
- return [x, y];
1790
- };
1791
- let polygonStr = "";
1792
- for (let i = 0, len = indices.length; i < len; i += 3) {
1793
- const p1 = getPoint(indices[i]);
1794
- const p2 = getPoint(indices[i + 1]);
1795
- const p3 = getPoint(indices[i + 2]);
1796
- polygonStr += `<polygon points="${p1.join(",")} ${p2.join(",")} ${p3.join(",")}" fill="none" stroke="black" stroke-width="0.5" stroke-linecap="round" stroke-linejoin="round" />`;
1797
- }
1798
- const viewBox = [min.x, min.y, max.x - min.x, max.y - min.y];
1799
- return `<svg width="${viewBox[2]}" height="${viewBox[3]}" viewBox="${viewBox.join(" ")}" xmlns="http://www.w3.org/2000/svg">${polygonStr}</svg>`;
1800
- }
1801
- toTriangulatedSVG(result, padding) {
1802
- return new DOMParser().parseFromString(this.toTriangulatedSVGString(result, padding), "image/svg+xml").documentElement;
1803
- }
1804
1810
  toCommands() {
1805
1811
  const comds = [];
1806
1812
  const potins = this.getPoints();
@@ -1975,69 +1981,114 @@ class RoundCurve extends Curve {
1975
1981
  getControlPointRefs() {
1976
1982
  return [this._center];
1977
1983
  }
1978
- getAdaptivePointArray(output = []) {
1979
- const { cx, cy, rx, ry, startAngle, endAngle, clockwise } = this;
1980
- if (!(rx >= 0 && ry >= 0)) {
1981
- return output;
1982
- }
1983
- let deltaAngle = endAngle - startAngle;
1984
- if (!clockwise && deltaAngle > 0) {
1985
- deltaAngle -= 2 * Math.PI;
1986
- } else if (clockwise && deltaAngle < 0) {
1987
- deltaAngle += 2 * Math.PI;
1984
+ _getAdaptiveVerticesByArc(output = []) {
1985
+ const { cx, cy, rx, ry, dx, dy, startAngle, endAngle, clockwise: _clockwise } = this;
1986
+ const clockwise = !_clockwise;
1987
+ const x = cx;
1988
+ const y = cy;
1989
+ const start = startAngle;
1990
+ const end = endAngle;
1991
+ let dist = Math.abs(start - end);
1992
+ if (!clockwise && start > end) {
1993
+ dist = 2 * Math.PI - dist;
1994
+ } else if (clockwise && end > start) {
1995
+ dist = 2 * Math.PI - dist;
1988
1996
  }
1989
- const arcLength = Math.abs(deltaAngle);
1990
- const n = Math.max(1, Math.ceil(arcLength / (Math.PI / 16)));
1991
- for (let i = 0; i <= n; i++) {
1992
- const t = i / n;
1993
- const angle = startAngle + deltaAngle * t;
1994
- const x = cx + Math.cos(angle) * rx;
1995
- const y = cy + Math.sin(angle) * ry;
1996
- output.push(x, y);
1997
+ let steps = Math.max(6, Math.floor(6 * rx ** (1 / 3) * (dist / Math.PI)));
1998
+ steps = Math.max(steps, 3);
1999
+ let f = dist / steps;
2000
+ let t = start;
2001
+ f *= clockwise ? -1 : 1;
2002
+ for (let i = 0; i < steps + 1; i++) {
2003
+ const cs = Math.cos(t);
2004
+ const sn = Math.sin(t);
2005
+ const nx = x + dx + cs * rx;
2006
+ const ny = y + dy + sn * ry;
2007
+ output.push(nx, ny);
2008
+ t += f;
1997
2009
  }
1998
2010
  return output;
1999
2011
  }
2000
- fillTriangulate(options = {}) {
2001
- let {
2002
- vertices = [],
2003
- indices = [],
2004
- verticesStride = 2,
2005
- verticesOffset = vertices.length / verticesStride,
2006
- indicesOffset = indices.length
2007
- } = options;
2008
- const points = this.getAdaptivePointArray();
2009
- if (points.length === 0) {
2010
- return { vertices, indices };
2012
+ _getAdaptiveVerticesByCircle(output = []) {
2013
+ const { cx, cy, rx, ry, dx, dy } = this;
2014
+ if (!(rx >= 0 && ry >= 0 && dx >= 0 && dy >= 0)) {
2015
+ return output;
2011
2016
  }
2012
- let centerX = 0;
2013
- let centerY = 0;
2014
- for (let i = 0; i < points.length; i += 2) {
2015
- centerX += points[i];
2016
- centerY += points[i + 1];
2017
+ const n = Math.ceil(2.3 * Math.sqrt(rx + ry));
2018
+ const x = cx;
2019
+ const y = cy;
2020
+ const m = n * 8 + (dx ? 4 : 0) + (dy ? 4 : 0);
2021
+ if (m === 0) {
2022
+ return output;
2017
2023
  }
2018
- centerX /= points.length / 2;
2019
- centerY /= points.length / 2;
2020
- let count = verticesOffset;
2021
- vertices[count * verticesStride] = centerX;
2022
- vertices[count * verticesStride + 1] = centerY;
2023
- const centerIndex = count++;
2024
- for (let i = 0; i < points.length; i += 2) {
2025
- vertices[count * verticesStride] = points[i];
2026
- vertices[count * verticesStride + 1] = points[i + 1];
2027
- if (i > 0) {
2028
- indices[indicesOffset++] = count;
2029
- indices[indicesOffset++] = centerIndex;
2030
- indices[indicesOffset++] = count - 1;
2031
- }
2032
- count++;
2024
+ const start = output.length;
2025
+ if (n === 0) {
2026
+ output[start] = output[start + 6] = x + dx;
2027
+ output[start + 1] = output[start + 3] = y + dy;
2028
+ output[start + 2] = output[start + 4] = x - dx;
2029
+ output[start + 5] = output[start + 7] = y - dy;
2030
+ return output;
2033
2031
  }
2034
- indices[indicesOffset++] = centerIndex + 1;
2035
- indices[indicesOffset++] = centerIndex;
2036
- indices[indicesOffset++] = count - 1;
2037
- return {
2038
- vertices,
2039
- indices
2040
- };
2032
+ let j1 = start;
2033
+ let j2 = start + n * 4 + (dx ? 2 : 0) + 2;
2034
+ let j3 = j2;
2035
+ let j4 = m;
2036
+ let x0 = dx + rx;
2037
+ let y0 = dy;
2038
+ let x1 = x + x0;
2039
+ let x2 = x - x0;
2040
+ let y1 = y + y0;
2041
+ output[j1++] = x1;
2042
+ output[j1++] = y1;
2043
+ output[--j2] = y1;
2044
+ output[--j2] = x2;
2045
+ if (dy) {
2046
+ const y22 = y - y0;
2047
+ output[j3++] = x2;
2048
+ output[j3++] = y22;
2049
+ output[--j4] = y22;
2050
+ output[--j4] = x1;
2051
+ }
2052
+ for (let i = 1; i < n; i++) {
2053
+ const a = Math.PI / 2 * (i / n);
2054
+ const x02 = dx + Math.cos(a) * rx;
2055
+ const y02 = dy + Math.sin(a) * ry;
2056
+ const x12 = x + x02;
2057
+ const x22 = x - x02;
2058
+ const y12 = y + y02;
2059
+ const y22 = y - y02;
2060
+ output[j1++] = x12;
2061
+ output[j1++] = y12;
2062
+ output[--j2] = y12;
2063
+ output[--j2] = x22;
2064
+ output[j3++] = x22;
2065
+ output[j3++] = y22;
2066
+ output[--j4] = y22;
2067
+ output[--j4] = x12;
2068
+ }
2069
+ x0 = dx;
2070
+ y0 = dy + ry;
2071
+ x1 = x + x0;
2072
+ x2 = x - x0;
2073
+ y1 = y + y0;
2074
+ const y2 = y - y0;
2075
+ output[j1++] = x1;
2076
+ output[j1++] = y1;
2077
+ output[--j4] = y2;
2078
+ output[--j4] = x1;
2079
+ if (dx) {
2080
+ output[j1++] = x2;
2081
+ output[j1++] = y1;
2082
+ output[--j4] = y2;
2083
+ output[--j4] = x2;
2084
+ }
2085
+ return output;
2086
+ }
2087
+ getAdaptiveVertices(output = []) {
2088
+ if (this.startAngle === 0 && this.endAngle === Math.PI * 2) {
2089
+ return this._getAdaptiveVerticesByCircle(output);
2090
+ }
2091
+ return this._getAdaptiveVerticesByArc(output);
2041
2092
  }
2042
2093
  getMinMax(min = Vector2.MAX, max = Vector2.MIN) {
2043
2094
  const { cx, cy, rx, ry, rotate } = this;
@@ -2234,7 +2285,7 @@ class ArcCurve extends RoundCurve {
2234
2285
  );
2235
2286
  return this;
2236
2287
  }
2237
- getAdaptivePointArray(output = []) {
2288
+ getAdaptiveVertices(output = []) {
2238
2289
  const { cx, cy, rx, startAngle, endAngle, clockwise } = this;
2239
2290
  let dist = Math.abs(startAngle - endAngle);
2240
2291
  if (!clockwise && startAngle > endAngle) {
@@ -2259,6 +2310,96 @@ class ArcCurve extends RoundCurve {
2259
2310
  }
2260
2311
  }
2261
2312
 
2313
+ class LineCurve extends Curve {
2314
+ constructor(p1 = new Vector2(), p2 = new Vector2()) {
2315
+ super();
2316
+ this.p1 = p1;
2317
+ this.p2 = p2;
2318
+ }
2319
+ static from(p1x, p1y, p2x, p2y) {
2320
+ return new LineCurve(
2321
+ new Vector2(p1x, p1y),
2322
+ new Vector2(p2x, p2y)
2323
+ );
2324
+ }
2325
+ getPoint(t, output = new Vector2()) {
2326
+ if (t === 1) {
2327
+ output.copy(this.p2);
2328
+ } else {
2329
+ output.copy(this.p2).sub(this.p1).scale(t).add(this.p1);
2330
+ }
2331
+ return output;
2332
+ }
2333
+ getPointAt(u, output = new Vector2()) {
2334
+ return this.getPoint(u, output);
2335
+ }
2336
+ getTangent(_t, output = new Vector2()) {
2337
+ return output.subVectors(this.p2, this.p1).normalize();
2338
+ }
2339
+ getTangentAt(u, output = new Vector2()) {
2340
+ return this.getTangent(u, output);
2341
+ }
2342
+ getControlPointRefs() {
2343
+ return [this.p1, this.p2];
2344
+ }
2345
+ getAdaptiveVertices(output = []) {
2346
+ output.push(
2347
+ this.p1.x,
2348
+ this.p1.y,
2349
+ this.p2.x,
2350
+ this.p2.y
2351
+ );
2352
+ return output;
2353
+ }
2354
+ getMinMax(min = Vector2.MAX, max = Vector2.MIN) {
2355
+ const { p1, p2 } = this;
2356
+ min.x = Math.min(min.x, p1.x, p2.x);
2357
+ min.y = Math.min(min.y, p1.y, p2.y);
2358
+ max.x = Math.max(max.x, p1.x, p2.x);
2359
+ max.y = Math.max(max.y, p1.y, p2.y);
2360
+ return { min: min.finite(), max: max.finite() };
2361
+ }
2362
+ toCommands() {
2363
+ const { p1, p2 } = this;
2364
+ return [
2365
+ { type: "M", x: p1.x, y: p1.y },
2366
+ { type: "L", x: p2.x, y: p2.y }
2367
+ ];
2368
+ }
2369
+ getFillVertices(options = {}) {
2370
+ const minX = Math.min(this.p1.x, this.p2.x);
2371
+ const maxX = Math.max(this.p1.x, this.p2.x);
2372
+ const minY = Math.min(this.p1.y, this.p2.y);
2373
+ const maxY = Math.max(this.p1.y, this.p2.y);
2374
+ const x = minX;
2375
+ const y = minY;
2376
+ const width = maxX - minX || options.style?.strokeWidth || 0;
2377
+ const height = maxY - minY || options.style?.strokeWidth || 0;
2378
+ return [
2379
+ x,
2380
+ y,
2381
+ x + width,
2382
+ y,
2383
+ x + width,
2384
+ y + height,
2385
+ x,
2386
+ y + height
2387
+ ];
2388
+ }
2389
+ drawTo(ctx) {
2390
+ const { p1, p2 } = this;
2391
+ ctx.lineTo(p1.x, p1.y);
2392
+ ctx.lineTo(p2.x, p2.y);
2393
+ return this;
2394
+ }
2395
+ copy(source) {
2396
+ super.copy(source);
2397
+ this.p1.copy(source.p1);
2398
+ this.p2.copy(source.p2);
2399
+ return this;
2400
+ }
2401
+ }
2402
+
2262
2403
  class CompositeCurve extends Curve {
2263
2404
  constructor(curves = []) {
2264
2405
  super();
@@ -2306,15 +2447,17 @@ class CompositeCurve extends Curve {
2306
2447
  return this.curves.flatMap((curve) => curve.getControlPointRefs());
2307
2448
  }
2308
2449
  _removeNextPointIfEqualPrevPoint(output, offset) {
2309
- if (output[offset - 1] === output[offset + 1] && output[offset] === output[offset + 2]) {
2450
+ const p1 = [output[offset - 1], output[offset]];
2451
+ const p2 = [output[offset + 1], output[offset + 2]];
2452
+ if (p1[0] === p2[0] && p1[1] === p2[1]) {
2310
2453
  output.splice(offset + 1, 2);
2311
2454
  }
2312
2455
  return output;
2313
2456
  }
2314
- getSpacedPointArray(count = 5, output = []) {
2457
+ getSpacedVertices(count = 5, output = []) {
2315
2458
  let offset;
2316
2459
  this.curves.forEach((curve) => {
2317
- curve.getSpacedPointArray(count, output);
2460
+ curve.getSpacedVertices(count, output);
2318
2461
  if (offset) {
2319
2462
  this._removeNextPointIfEqualPrevPoint(output, offset);
2320
2463
  }
@@ -2322,10 +2465,10 @@ class CompositeCurve extends Curve {
2322
2465
  });
2323
2466
  return output;
2324
2467
  }
2325
- getAdaptivePointArray(output = []) {
2468
+ getAdaptiveVertices(output = []) {
2326
2469
  let offset;
2327
2470
  this.curves.forEach((curve) => {
2328
- curve.getAdaptivePointArray(output);
2471
+ curve.getAdaptiveVertices(output);
2329
2472
  if (offset) {
2330
2473
  this._removeNextPointIfEqualPrevPoint(output, offset);
2331
2474
  }
@@ -2333,17 +2476,34 @@ class CompositeCurve extends Curve {
2333
2476
  });
2334
2477
  return output;
2335
2478
  }
2336
- fillTriangulate(options) {
2337
- const indices = options?.indices ?? [];
2338
- const vertices = options?.vertices ?? [];
2339
- this.curves.forEach((curve) => {
2340
- curve.fillTriangulate({
2341
- ...options,
2342
- indices,
2343
- vertices
2479
+ strokeTriangulate(options) {
2480
+ if (this.curves.length === 1) {
2481
+ return this.curves[0].strokeTriangulate(options);
2482
+ } else {
2483
+ return super.strokeTriangulate(options);
2484
+ }
2485
+ }
2486
+ getFillVertices(options) {
2487
+ if (this.curves.length === 1) {
2488
+ return this.curves[0].getFillVertices(options);
2489
+ } else {
2490
+ const output = [];
2491
+ let offset;
2492
+ this.curves.forEach((curve) => {
2493
+ let arr;
2494
+ if (curve instanceof LineCurve) {
2495
+ arr = curve.getAdaptiveVertices();
2496
+ } else {
2497
+ arr = curve.getFillVertices(options);
2498
+ }
2499
+ output.push(...arr);
2500
+ if (offset) {
2501
+ this._removeNextPointIfEqualPrevPoint(output, offset);
2502
+ }
2503
+ offset = output.length - 1;
2344
2504
  });
2345
- });
2346
- return { indices, vertices };
2505
+ return output;
2506
+ }
2347
2507
  }
2348
2508
  applyTransform(transform) {
2349
2509
  this.curves.forEach((curve) => curve.applyTransform(transform));
@@ -2398,7 +2558,7 @@ class CubicBezierCurve extends Curve {
2398
2558
  cubicBezier(t, p1.y, cp1.y, cp2.y, p2.y)
2399
2559
  );
2400
2560
  }
2401
- getAdaptivePointArray(output = []) {
2561
+ getAdaptiveVertices(output = []) {
2402
2562
  return getAdaptiveCubicBezierCurvePoints(
2403
2563
  this.p1.x,
2404
2564
  this.p1.y,
@@ -2503,125 +2663,6 @@ class EllipseCurve extends RoundCurve {
2503
2663
  }
2504
2664
  }
2505
2665
 
2506
- class LineCurve extends Curve {
2507
- constructor(p1 = new Vector2(), p2 = new Vector2()) {
2508
- super();
2509
- this.p1 = p1;
2510
- this.p2 = p2;
2511
- }
2512
- static from(p1x, p1y, p2x, p2y) {
2513
- return new LineCurve(
2514
- new Vector2(p1x, p1y),
2515
- new Vector2(p2x, p2y)
2516
- );
2517
- }
2518
- getPoint(t, output = new Vector2()) {
2519
- if (t === 1) {
2520
- output.copy(this.p2);
2521
- } else {
2522
- output.copy(this.p2).sub(this.p1).scale(t).add(this.p1);
2523
- }
2524
- return output;
2525
- }
2526
- getPointAt(u, output = new Vector2()) {
2527
- return this.getPoint(u, output);
2528
- }
2529
- getTangent(_t, output = new Vector2()) {
2530
- return output.subVectors(this.p2, this.p1).normalize();
2531
- }
2532
- getTangentAt(u, output = new Vector2()) {
2533
- return this.getTangent(u, output);
2534
- }
2535
- getControlPointRefs() {
2536
- return [this.p1, this.p2];
2537
- }
2538
- getAdaptivePointArray(output = []) {
2539
- output.push(
2540
- this.p1.x,
2541
- this.p1.y,
2542
- this.p2.x,
2543
- this.p2.y
2544
- );
2545
- return output;
2546
- }
2547
- getMinMax(min = Vector2.MAX, max = Vector2.MIN) {
2548
- const { p1, p2 } = this;
2549
- min.x = Math.min(min.x, p1.x, p2.x);
2550
- min.y = Math.min(min.y, p1.y, p2.y);
2551
- max.x = Math.max(max.x, p1.x, p2.x);
2552
- max.y = Math.max(max.y, p1.y, p2.y);
2553
- return { min: min.finite(), max: max.finite() };
2554
- }
2555
- toCommands() {
2556
- const { p1, p2 } = this;
2557
- return [
2558
- { type: "M", x: p1.x, y: p1.y },
2559
- { type: "L", x: p2.x, y: p2.y }
2560
- ];
2561
- }
2562
- fillTriangulate(options = {}) {
2563
- let {
2564
- vertices = [],
2565
- indices = [],
2566
- verticesStride = 2,
2567
- verticesOffset = vertices.length / verticesStride,
2568
- indicesOffset = indices.length
2569
- } = options;
2570
- const minX = Math.min(this.p1.x, this.p2.x);
2571
- const maxX = Math.max(this.p1.x, this.p2.x);
2572
- const minY = Math.min(this.p1.y, this.p2.y);
2573
- const maxY = Math.max(this.p1.y, this.p2.y);
2574
- const x = minX;
2575
- const y = minY;
2576
- const width = maxX - minX;
2577
- const height = maxY - minY;
2578
- const points = [
2579
- x,
2580
- y,
2581
- x + width,
2582
- y,
2583
- x + width,
2584
- y + height,
2585
- x,
2586
- y + height
2587
- ];
2588
- let count = 0;
2589
- verticesOffset *= verticesStride;
2590
- vertices[verticesOffset + count] = points[0];
2591
- vertices[verticesOffset + count + 1] = points[1];
2592
- count += verticesStride;
2593
- vertices[verticesOffset + count] = points[2];
2594
- vertices[verticesOffset + count + 1] = points[3];
2595
- count += verticesStride;
2596
- vertices[verticesOffset + count] = points[6];
2597
- vertices[verticesOffset + count + 1] = points[7];
2598
- count += verticesStride;
2599
- vertices[verticesOffset + count] = points[4];
2600
- vertices[verticesOffset + count + 1] = points[5];
2601
- count += verticesStride;
2602
- const verticesIndex = verticesOffset / verticesStride;
2603
- indices[indicesOffset++] = verticesIndex;
2604
- indices[indicesOffset++] = verticesIndex + 1;
2605
- indices[indicesOffset++] = verticesIndex + 2;
2606
- indices[indicesOffset++] = verticesIndex + 1;
2607
- indices[indicesOffset++] = verticesIndex + 3;
2608
- indices[indicesOffset++] = verticesIndex + 2;
2609
- return { vertices, indices };
2610
- }
2611
- drawTo(ctx) {
2612
- const { p1, p2 } = this;
2613
- ctx.lineTo(p1.x, p1.y);
2614
- ctx.lineTo(p2.x, p2.y);
2615
- return this;
2616
- }
2617
- copy(source) {
2618
- super.copy(source);
2619
- this.p1.copy(source.p1);
2620
- this.p2.copy(source.p2);
2621
- return this;
2622
- }
2623
- }
2624
-
2625
2666
  class PloygonCurve extends CompositeCurve {
2626
2667
  //
2627
2668
  }
@@ -2695,7 +2736,7 @@ class QuadraticBezierCurve extends Curve {
2695
2736
  getControlPointRefs() {
2696
2737
  return [this.p1, this.cp, this.p2];
2697
2738
  }
2698
- getAdaptivePointArray(output = []) {
2739
+ getAdaptiveVertices(output = []) {
2699
2740
  return getAdaptiveQuadraticBezierCurvePoints(
2700
2741
  this.p1.x,
2701
2742
  this.p1.y,
@@ -2770,16 +2811,9 @@ class RectangleCurve extends PloygonCurve {
2770
2811
  ctx.rect(this.x, this.y, this.width, this.height);
2771
2812
  return this;
2772
2813
  }
2773
- fillTriangulate(options = {}) {
2774
- let {
2775
- vertices = [],
2776
- indices = [],
2777
- verticesStride = 2,
2778
- verticesOffset = vertices.length / verticesStride,
2779
- indicesOffset = indices.length
2780
- } = options;
2814
+ getFillVertices(_options = {}) {
2781
2815
  const { x, y, width, height } = this;
2782
- const points = [
2816
+ return [
2783
2817
  x,
2784
2818
  y,
2785
2819
  x + width,
@@ -2789,28 +2823,6 @@ class RectangleCurve extends PloygonCurve {
2789
2823
  x,
2790
2824
  y + height
2791
2825
  ];
2792
- let count = 0;
2793
- verticesOffset *= verticesStride;
2794
- vertices[verticesOffset + count] = points[0];
2795
- vertices[verticesOffset + count + 1] = points[1];
2796
- count += verticesStride;
2797
- vertices[verticesOffset + count] = points[2];
2798
- vertices[verticesOffset + count + 1] = points[3];
2799
- count += verticesStride;
2800
- vertices[verticesOffset + count] = points[6];
2801
- vertices[verticesOffset + count + 1] = points[7];
2802
- count += verticesStride;
2803
- vertices[verticesOffset + count] = points[4];
2804
- vertices[verticesOffset + count + 1] = points[5];
2805
- count += verticesStride;
2806
- const verticesIndex = verticesOffset / verticesStride;
2807
- indices[indicesOffset++] = verticesIndex;
2808
- indices[indicesOffset++] = verticesIndex + 1;
2809
- indices[indicesOffset++] = verticesIndex + 2;
2810
- indices[indicesOffset++] = verticesIndex + 1;
2811
- indices[indicesOffset++] = verticesIndex + 3;
2812
- indices[indicesOffset++] = verticesIndex + 2;
2813
- return { vertices, indices };
2814
2826
  }
2815
2827
  copy(source) {
2816
2828
  super.copy(source);
@@ -2922,25 +2934,30 @@ class CurvePath extends CompositeCurve {
2922
2934
  this.addCommands(svgPathDataToCommands(data));
2923
2935
  return this;
2924
2936
  }
2925
- _closePointArray(output) {
2937
+ _closeVertices(output) {
2926
2938
  if (this.autoClose && output.length >= 4 && (output[0] !== output[output.length - 2] && output[1] !== output[output.length - 1])) {
2927
2939
  output.push(output[0], output[1]);
2928
2940
  }
2929
2941
  return output;
2930
2942
  }
2931
- getUnevenPointArray(count = 40, output = []) {
2932
- return this._closePointArray(
2933
- super.getUnevenPointArray(count, output)
2943
+ getUnevenVertices(count = 40, output = []) {
2944
+ return this._closeVertices(
2945
+ super.getUnevenVertices(count, output)
2934
2946
  );
2935
2947
  }
2936
- getSpacedPointArray(count = 40, output = []) {
2937
- return this._closePointArray(
2938
- super.getSpacedPointArray(count, output)
2948
+ getSpacedVertices(count = 40, output = []) {
2949
+ return this._closeVertices(
2950
+ super.getSpacedVertices(count, output)
2939
2951
  );
2940
2952
  }
2941
- getAdaptivePointArray(output = []) {
2942
- return this._closePointArray(
2943
- super.getAdaptivePointArray(output)
2953
+ getAdaptiveVertices(output = []) {
2954
+ return this._closeVertices(
2955
+ super.getAdaptiveVertices(output)
2956
+ );
2957
+ }
2958
+ getFillVertices(options) {
2959
+ return this._closeVertices(
2960
+ super.getFillVertices(options)
2944
2961
  );
2945
2962
  }
2946
2963
  _setCurrentPoint(point) {
@@ -3368,11 +3385,78 @@ class Path2D extends CompositeCurve {
3368
3385
  curve.strokeTriangulate({
3369
3386
  ...options,
3370
3387
  indices,
3371
- vertices
3388
+ vertices,
3389
+ style: { ...this.style }
3372
3390
  });
3373
3391
  });
3374
3392
  return { indices, vertices };
3375
3393
  }
3394
+ fillTriangulate(options) {
3395
+ const _options = {
3396
+ ...options,
3397
+ style: {
3398
+ ...this.style,
3399
+ ...options?.style
3400
+ }
3401
+ };
3402
+ const indices = _options.indices ?? [];
3403
+ const vertices = _options.vertices ?? [];
3404
+ const fillRule = _options.style.fillRule ?? "nonzero";
3405
+ if (fillRule === "nonzero") {
3406
+ const pointArrays = this.curves.map((curve) => curve.getFillVertices(_options));
3407
+ const parentMap = /* @__PURE__ */ new Map();
3408
+ const parentd = /* @__PURE__ */ new Set();
3409
+ for (let i = 0; i < pointArrays.length; i++) {
3410
+ const parents = [];
3411
+ for (let j = 0; j < pointArrays.length; j++) {
3412
+ if (i === j)
3413
+ continue;
3414
+ if (pointInPolygon([pointArrays[i][0], pointArrays[i][1]], pointArrays[j])) {
3415
+ parents.push(j);
3416
+ }
3417
+ }
3418
+ if (parents.length) {
3419
+ parents.forEach((pi) => {
3420
+ let set = parentMap.get(pi);
3421
+ if (!set) {
3422
+ set = /* @__PURE__ */ new Set();
3423
+ parentMap.set(pi, set);
3424
+ }
3425
+ set.add(i);
3426
+ });
3427
+ parentd.add(i);
3428
+ }
3429
+ }
3430
+ pointArrays.forEach((pointArray, i) => {
3431
+ if (parentd.has(i) || !pointArray.length) {
3432
+ return;
3433
+ }
3434
+ const _pointArray = pointArray.slice();
3435
+ const holes = [];
3436
+ parentMap.get(i)?.forEach((ci) => {
3437
+ holes.push(_pointArray.length / 2);
3438
+ _pointArray.push(...pointArrays[ci]);
3439
+ });
3440
+ fillTriangulate(_pointArray, {
3441
+ ...options,
3442
+ indices,
3443
+ vertices,
3444
+ holes,
3445
+ style: { ...this.style }
3446
+ });
3447
+ });
3448
+ } else {
3449
+ this.curves.forEach((curve) => {
3450
+ curve.fillTriangulate({
3451
+ ...options,
3452
+ indices,
3453
+ vertices,
3454
+ style: { ...this.style }
3455
+ });
3456
+ });
3457
+ }
3458
+ return { indices, vertices };
3459
+ }
3376
3460
  getBoundingBox(withStyle = true) {
3377
3461
  const { min, max } = this.getMinMax(undefined, undefined, withStyle);
3378
3462
  return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
@@ -3464,6 +3548,34 @@ class Path2DSet {
3464
3548
  this.paths.forEach((path) => path.getMinMax(min, max, withStyle));
3465
3549
  return new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
3466
3550
  }
3551
+ toTriangulatedSVGString(result = this.paths.map((p) => p.fillTriangulate()), padding = 0) {
3552
+ let polygonStr = "";
3553
+ const min = { x: -padding, y: -padding };
3554
+ const max = { x: padding, y: padding };
3555
+ const results = Array.isArray(result) ? result : [result];
3556
+ results.forEach(({ vertices, indices }) => {
3557
+ const getPoint = (indice) => {
3558
+ const x = vertices[indice * 2];
3559
+ const y = vertices[indice * 2 + 1];
3560
+ min.x = Math.min(min.x, x + padding);
3561
+ max.x = Math.max(max.x, x + padding);
3562
+ min.y = Math.min(min.y, y + padding);
3563
+ max.y = Math.max(max.y, y + padding);
3564
+ return [x, y];
3565
+ };
3566
+ for (let i = 0, len = indices.length; i < len; i += 3) {
3567
+ const p1 = getPoint(indices[i]);
3568
+ const p2 = getPoint(indices[i + 1]);
3569
+ const p3 = getPoint(indices[i + 2]);
3570
+ polygonStr += `<polygon points="${p1.join(",")} ${p2.join(",")} ${p3.join(",")}" fill="black" />`;
3571
+ }
3572
+ });
3573
+ const viewBox = [min.x, min.y, max.x - min.x, max.y - min.y];
3574
+ return `<svg width="${viewBox[2]}" height="${viewBox[3]}" viewBox="${viewBox.join(" ")}" xmlns="http://www.w3.org/2000/svg">${polygonStr}</svg>`;
3575
+ }
3576
+ toTriangulatedSVG(result, padding) {
3577
+ return new DOMParser().parseFromString(this.toTriangulatedSVGString(result, padding), "image/svg+xml").documentElement;
3578
+ }
3467
3579
  toSVGString() {
3468
3580
  const { x, y, width, height } = this.getBoundingBox();
3469
3581
  const content = this.paths.map((path) => path.toSVGPathString()).join("");
@@ -4105,8 +4217,10 @@ exports.drawPoint = drawPoint;
4105
4217
  exports.fillTriangulate = fillTriangulate;
4106
4218
  exports.getAdaptiveCubicBezierCurvePoints = getAdaptiveCubicBezierCurvePoints;
4107
4219
  exports.getAdaptiveQuadraticBezierCurvePoints = getAdaptiveQuadraticBezierCurvePoints;
4220
+ exports.getDirectedArea = getDirectedArea;
4108
4221
  exports.parseArcCommand = parseArcCommand;
4109
4222
  exports.parsePathDataArgs = parsePathDataArgs;
4223
+ exports.pointInPolygon = pointInPolygon;
4110
4224
  exports.quadraticBezier = quadraticBezier;
4111
4225
  exports.setCanvasContext = setCanvasContext;
4112
4226
  exports.strokeTriangulate = strokeTriangulate;