modern-path2d 0.1.0 → 0.1.2
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 +198 -129
- package/dist/index.d.cts +31 -15
- package/dist/index.d.mts +31 -15
- package/dist/index.d.ts +31 -15
- package/dist/index.js +1 -1
- package/dist/index.mjs +198 -130
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,143 @@
|
|
|
1
|
+
class Point2D {
|
|
2
|
+
constructor(x = 0, y = 0) {
|
|
3
|
+
this.x = x;
|
|
4
|
+
this.y = y;
|
|
5
|
+
}
|
|
6
|
+
static get MAX() {
|
|
7
|
+
return new Point2D(Infinity, Infinity);
|
|
8
|
+
}
|
|
9
|
+
static get MIN() {
|
|
10
|
+
return new Point2D(-Infinity, -Infinity);
|
|
11
|
+
}
|
|
12
|
+
set(x, y) {
|
|
13
|
+
this.x = x;
|
|
14
|
+
this.y = y;
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
add(point) {
|
|
18
|
+
this.x += point.x;
|
|
19
|
+
this.y += point.y;
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
sub(point) {
|
|
23
|
+
this.x -= point.x;
|
|
24
|
+
this.y -= point.y;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
distanceTo(point) {
|
|
28
|
+
return Math.sqrt(this.distanceToSquared(point));
|
|
29
|
+
}
|
|
30
|
+
distanceToSquared(point) {
|
|
31
|
+
const dx = this.x - point.x;
|
|
32
|
+
const dy = this.y - point.y;
|
|
33
|
+
return dx * dx + dy * dy;
|
|
34
|
+
}
|
|
35
|
+
length() {
|
|
36
|
+
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
37
|
+
}
|
|
38
|
+
multiplyScalar(scalar) {
|
|
39
|
+
this.x *= scalar;
|
|
40
|
+
this.y *= scalar;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
divideScalar(scalar) {
|
|
44
|
+
return this.multiplyScalar(1 / scalar);
|
|
45
|
+
}
|
|
46
|
+
subVectors(a, b) {
|
|
47
|
+
this.x = a.x - b.x;
|
|
48
|
+
this.y = a.y - b.y;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
normalize() {
|
|
52
|
+
return this.divideScalar(this.length() || 1);
|
|
53
|
+
}
|
|
54
|
+
lerpVectors(v1, v2, alpha) {
|
|
55
|
+
this.x = v1.x + (v2.x - v1.x) * alpha;
|
|
56
|
+
this.y = v1.y + (v2.y - v1.y) * alpha;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
equals(point) {
|
|
60
|
+
return this.x === point.x && this.y === point.y;
|
|
61
|
+
}
|
|
62
|
+
applyMatrix3(matrix3) {
|
|
63
|
+
const [a, c, tx, b, d, ty] = matrix3.elements;
|
|
64
|
+
const { x, y } = this;
|
|
65
|
+
this.set(
|
|
66
|
+
a * x + c * y + tx,
|
|
67
|
+
b * x + d * y + ty
|
|
68
|
+
);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
copy(point) {
|
|
72
|
+
this.x = point.x;
|
|
73
|
+
this.y = point.y;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
clone() {
|
|
77
|
+
return new Point2D(this.x, this.y);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class BoundingBox {
|
|
82
|
+
constructor(left = 0, top = 0, width = 0, height = 0) {
|
|
83
|
+
this.left = left;
|
|
84
|
+
this.top = top;
|
|
85
|
+
this.width = width;
|
|
86
|
+
this.height = height;
|
|
87
|
+
}
|
|
88
|
+
get x() {
|
|
89
|
+
return this.left;
|
|
90
|
+
}
|
|
91
|
+
set x(val) {
|
|
92
|
+
this.left = val;
|
|
93
|
+
}
|
|
94
|
+
get y() {
|
|
95
|
+
return this.top;
|
|
96
|
+
}
|
|
97
|
+
set y(val) {
|
|
98
|
+
this.top = val;
|
|
99
|
+
}
|
|
100
|
+
get right() {
|
|
101
|
+
return this.left + this.width;
|
|
102
|
+
}
|
|
103
|
+
get bottom() {
|
|
104
|
+
return this.top + this.height;
|
|
105
|
+
}
|
|
106
|
+
static from(...boxes) {
|
|
107
|
+
const firstBox = boxes[0];
|
|
108
|
+
const merged = boxes.slice(1).reduce(
|
|
109
|
+
(merged2, box) => {
|
|
110
|
+
merged2.left = Math.min(merged2.left, box.left);
|
|
111
|
+
merged2.top = Math.min(merged2.top, box.top);
|
|
112
|
+
merged2.right = Math.max(merged2.right, box.right);
|
|
113
|
+
merged2.bottom = Math.max(merged2.bottom, box.bottom);
|
|
114
|
+
return merged2;
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
left: firstBox?.left ?? 0,
|
|
118
|
+
top: firstBox?.top ?? 0,
|
|
119
|
+
right: firstBox?.right ?? 0,
|
|
120
|
+
bottom: firstBox?.bottom ?? 0
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
return new BoundingBox(merged.left, merged.top, merged.right - merged.left, merged.bottom - merged.top);
|
|
124
|
+
}
|
|
125
|
+
translate(tx, ty) {
|
|
126
|
+
this.left += tx;
|
|
127
|
+
this.top += ty;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
getCenterPoint() {
|
|
131
|
+
return new Point2D((this.left + this.right) / 2, (this.top + this.bottom) / 2);
|
|
132
|
+
}
|
|
133
|
+
clone() {
|
|
134
|
+
return new BoundingBox(this.left, this.top, this.width, this.height);
|
|
135
|
+
}
|
|
136
|
+
toArray() {
|
|
137
|
+
return [this.left, this.top, this.width, this.height];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
1
141
|
var __defProp$6 = Object.defineProperty;
|
|
2
142
|
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
143
|
var __publicField$6 = (obj, key, value) => {
|
|
@@ -200,86 +340,6 @@ class Matrix3 {
|
|
|
200
340
|
}
|
|
201
341
|
const _m3 = /* @__PURE__ */ new Matrix3();
|
|
202
342
|
|
|
203
|
-
class Point2D {
|
|
204
|
-
constructor(x = 0, y = 0) {
|
|
205
|
-
this.x = x;
|
|
206
|
-
this.y = y;
|
|
207
|
-
}
|
|
208
|
-
static get MAX() {
|
|
209
|
-
return new Point2D(Infinity, Infinity);
|
|
210
|
-
}
|
|
211
|
-
static get MIN() {
|
|
212
|
-
return new Point2D(-Infinity, -Infinity);
|
|
213
|
-
}
|
|
214
|
-
set(x, y) {
|
|
215
|
-
this.x = x;
|
|
216
|
-
this.y = y;
|
|
217
|
-
return this;
|
|
218
|
-
}
|
|
219
|
-
add(point) {
|
|
220
|
-
this.x += point.x;
|
|
221
|
-
this.y += point.y;
|
|
222
|
-
return this;
|
|
223
|
-
}
|
|
224
|
-
sub(point) {
|
|
225
|
-
this.x -= point.x;
|
|
226
|
-
this.y -= point.y;
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
distanceTo(point) {
|
|
230
|
-
return Math.sqrt(this.distanceToSquared(point));
|
|
231
|
-
}
|
|
232
|
-
distanceToSquared(point) {
|
|
233
|
-
const dx = this.x - point.x;
|
|
234
|
-
const dy = this.y - point.y;
|
|
235
|
-
return dx * dx + dy * dy;
|
|
236
|
-
}
|
|
237
|
-
length() {
|
|
238
|
-
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
239
|
-
}
|
|
240
|
-
multiplyScalar(scalar) {
|
|
241
|
-
this.x *= scalar;
|
|
242
|
-
this.y *= scalar;
|
|
243
|
-
return this;
|
|
244
|
-
}
|
|
245
|
-
divideScalar(scalar) {
|
|
246
|
-
return this.multiplyScalar(1 / scalar);
|
|
247
|
-
}
|
|
248
|
-
subVectors(a, b) {
|
|
249
|
-
this.x = a.x - b.x;
|
|
250
|
-
this.y = a.y - b.y;
|
|
251
|
-
return this;
|
|
252
|
-
}
|
|
253
|
-
normalize() {
|
|
254
|
-
return this.divideScalar(this.length() || 1);
|
|
255
|
-
}
|
|
256
|
-
lerpVectors(v1, v2, alpha) {
|
|
257
|
-
this.x = v1.x + (v2.x - v1.x) * alpha;
|
|
258
|
-
this.y = v1.y + (v2.y - v1.y) * alpha;
|
|
259
|
-
return this;
|
|
260
|
-
}
|
|
261
|
-
equals(point) {
|
|
262
|
-
return this.x === point.x && this.y === point.y;
|
|
263
|
-
}
|
|
264
|
-
applyMatrix3(matrix3) {
|
|
265
|
-
const [a, c, tx, b, d, ty] = matrix3.elements;
|
|
266
|
-
const { x, y } = this;
|
|
267
|
-
this.set(
|
|
268
|
-
a * x + c * y + tx,
|
|
269
|
-
b * x + d * y + ty
|
|
270
|
-
);
|
|
271
|
-
return this;
|
|
272
|
-
}
|
|
273
|
-
copy(point) {
|
|
274
|
-
this.x = point.x;
|
|
275
|
-
this.y = point.y;
|
|
276
|
-
return this;
|
|
277
|
-
}
|
|
278
|
-
clone() {
|
|
279
|
-
return new Point2D(this.x, this.y);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
343
|
function svgAngle(ux, uy, vx, vy) {
|
|
284
344
|
const dot = ux * vx + uy * vy;
|
|
285
345
|
const len = Math.sqrt(ux * ux + uy * uy) * Math.sqrt(vx * vx + vy * vy);
|
|
@@ -977,13 +1037,13 @@ class Curve {
|
|
|
977
1037
|
drawTo(ctx) {
|
|
978
1038
|
return this;
|
|
979
1039
|
}
|
|
980
|
-
clone() {
|
|
981
|
-
return new this.constructor().copy(this);
|
|
982
|
-
}
|
|
983
1040
|
copy(source) {
|
|
984
1041
|
this.arcLengthDivisions = source.arcLengthDivisions;
|
|
985
1042
|
return this;
|
|
986
1043
|
}
|
|
1044
|
+
clone() {
|
|
1045
|
+
return new this.constructor().copy(this);
|
|
1046
|
+
}
|
|
987
1047
|
}
|
|
988
1048
|
|
|
989
1049
|
class CircleCurve extends Curve {
|
|
@@ -1070,6 +1130,13 @@ class CubicBezierCurve extends Curve {
|
|
|
1070
1130
|
);
|
|
1071
1131
|
return output;
|
|
1072
1132
|
}
|
|
1133
|
+
transform(matrix) {
|
|
1134
|
+
this.v0.applyMatrix3(matrix);
|
|
1135
|
+
this.v1.applyMatrix3(matrix);
|
|
1136
|
+
this.v2.applyMatrix3(matrix);
|
|
1137
|
+
this.v3.applyMatrix3(matrix);
|
|
1138
|
+
return this;
|
|
1139
|
+
}
|
|
1073
1140
|
getMinMax(min = Point2D.MAX, max = Point2D.MIN) {
|
|
1074
1141
|
const { v0, v1, v2, v3 } = this;
|
|
1075
1142
|
min.x = Math.min(min.x, v0.x, v1.x, v2.x, v3.x);
|
|
@@ -1078,13 +1145,6 @@ class CubicBezierCurve extends Curve {
|
|
|
1078
1145
|
max.y = Math.max(max.y, v0.y, v1.y, v2.y, v3.y);
|
|
1079
1146
|
return { min, max };
|
|
1080
1147
|
}
|
|
1081
|
-
transform(matrix) {
|
|
1082
|
-
this.v0.applyMatrix3(matrix);
|
|
1083
|
-
this.v1.applyMatrix3(matrix);
|
|
1084
|
-
this.v2.applyMatrix3(matrix);
|
|
1085
|
-
this.v3.applyMatrix3(matrix);
|
|
1086
|
-
return this;
|
|
1087
|
-
}
|
|
1088
1148
|
getCommands() {
|
|
1089
1149
|
const { v0, v1, v2, v3 } = this;
|
|
1090
1150
|
return [
|
|
@@ -1124,9 +1184,6 @@ class EllipseCurve extends Curve {
|
|
|
1124
1184
|
this.clockwise = clockwise;
|
|
1125
1185
|
this.rotation = rotation;
|
|
1126
1186
|
}
|
|
1127
|
-
getDivisions(divisions = 12) {
|
|
1128
|
-
return divisions * 2;
|
|
1129
|
-
}
|
|
1130
1187
|
getPoint(t, output = new Point2D()) {
|
|
1131
1188
|
const twoPi = Math.PI * 2;
|
|
1132
1189
|
let deltaAngle = this.endAngle - this.startAngle;
|
|
@@ -1162,6 +1219,9 @@ class EllipseCurve extends Curve {
|
|
|
1162
1219
|
}
|
|
1163
1220
|
return output.set(_x, _y);
|
|
1164
1221
|
}
|
|
1222
|
+
getDivisions(divisions = 12) {
|
|
1223
|
+
return divisions * 2;
|
|
1224
|
+
}
|
|
1165
1225
|
getCommands() {
|
|
1166
1226
|
const { x, y, radiusX, radiusY, startAngle, endAngle, clockwise } = this;
|
|
1167
1227
|
const anticlockwise = !clockwise;
|
|
@@ -1375,9 +1435,6 @@ class LineCurve extends Curve {
|
|
|
1375
1435
|
this.v1 = v1;
|
|
1376
1436
|
this.v2 = v2;
|
|
1377
1437
|
}
|
|
1378
|
-
getDivisions() {
|
|
1379
|
-
return 1;
|
|
1380
|
-
}
|
|
1381
1438
|
getPoint(t, output = new Point2D()) {
|
|
1382
1439
|
if (t === 1) {
|
|
1383
1440
|
output.copy(this.v2);
|
|
@@ -1396,12 +1453,13 @@ class LineCurve extends Curve {
|
|
|
1396
1453
|
getTangentAt(u, output = new Point2D()) {
|
|
1397
1454
|
return this.getTangent(u, output);
|
|
1398
1455
|
}
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1456
|
+
transform(matrix) {
|
|
1457
|
+
this.v1.applyMatrix3(matrix);
|
|
1458
|
+
this.v2.applyMatrix3(matrix);
|
|
1459
|
+
return this;
|
|
1460
|
+
}
|
|
1461
|
+
getDivisions() {
|
|
1462
|
+
return 1;
|
|
1405
1463
|
}
|
|
1406
1464
|
getMinMax(min = Point2D.MAX, max = Point2D.MIN) {
|
|
1407
1465
|
const { v1, v2 } = this;
|
|
@@ -1411,10 +1469,12 @@ class LineCurve extends Curve {
|
|
|
1411
1469
|
max.y = Math.max(max.y, v1.y, v2.y);
|
|
1412
1470
|
return { min, max };
|
|
1413
1471
|
}
|
|
1414
|
-
|
|
1415
|
-
this
|
|
1416
|
-
|
|
1417
|
-
|
|
1472
|
+
getCommands() {
|
|
1473
|
+
const { v1, v2 } = this;
|
|
1474
|
+
return [
|
|
1475
|
+
{ type: "M", x: v1.x, y: v1.y },
|
|
1476
|
+
{ type: "L", x: v2.x, y: v2.y }
|
|
1477
|
+
];
|
|
1418
1478
|
}
|
|
1419
1479
|
drawTo(ctx) {
|
|
1420
1480
|
const { v1, v2 } = this;
|
|
@@ -1585,12 +1645,11 @@ class QuadraticBezierCurve extends Curve {
|
|
|
1585
1645
|
);
|
|
1586
1646
|
return output;
|
|
1587
1647
|
}
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
];
|
|
1648
|
+
transform(matrix) {
|
|
1649
|
+
this.v0.applyMatrix3(matrix);
|
|
1650
|
+
this.v1.applyMatrix3(matrix);
|
|
1651
|
+
this.v2.applyMatrix3(matrix);
|
|
1652
|
+
return this;
|
|
1594
1653
|
}
|
|
1595
1654
|
getMinMax(min = Point2D.MAX, max = Point2D.MIN) {
|
|
1596
1655
|
const { v0, v1, v2 } = this;
|
|
@@ -1604,11 +1663,12 @@ class QuadraticBezierCurve extends Curve {
|
|
|
1604
1663
|
max.y = Math.max(max.y, v0.y, v2.y, y1, y2);
|
|
1605
1664
|
return { min, max };
|
|
1606
1665
|
}
|
|
1607
|
-
|
|
1608
|
-
this
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1666
|
+
getCommands() {
|
|
1667
|
+
const { v0, v1, v2 } = this;
|
|
1668
|
+
return [
|
|
1669
|
+
{ type: "M", x: v0.x, y: v0.y },
|
|
1670
|
+
{ type: "Q", x1: v1.x, y1: v1.y, x: v2.x, y: v2.y }
|
|
1671
|
+
];
|
|
1612
1672
|
}
|
|
1613
1673
|
drawTo(ctx) {
|
|
1614
1674
|
const { v0, v1, v2 } = this;
|
|
@@ -1937,9 +1997,8 @@ class CurvePath extends Curve {
|
|
|
1937
1997
|
copy(source) {
|
|
1938
1998
|
super.copy(source);
|
|
1939
1999
|
this.curves = [];
|
|
1940
|
-
for (let i = 0,
|
|
1941
|
-
|
|
1942
|
-
this.curves.push(curve.clone());
|
|
2000
|
+
for (let i = 0, len = source.curves.length; i < len; i++) {
|
|
2001
|
+
this.curves.push(source.curves[i].clone());
|
|
1943
2002
|
}
|
|
1944
2003
|
this.autoClose = source.autoClose;
|
|
1945
2004
|
this.currentPoint.copy(source.currentPoint);
|
|
@@ -2064,21 +2123,21 @@ class Path2D {
|
|
|
2064
2123
|
this.forEachCurve((curve) => curve.getMinMax(min, max));
|
|
2065
2124
|
return { min, max };
|
|
2066
2125
|
}
|
|
2126
|
+
getBoundingBox() {
|
|
2127
|
+
const { min, max } = this.getMinMax();
|
|
2128
|
+
return new BoundingBox(
|
|
2129
|
+
min.x,
|
|
2130
|
+
min.y,
|
|
2131
|
+
max.x - min.x,
|
|
2132
|
+
max.y - min.y
|
|
2133
|
+
);
|
|
2134
|
+
}
|
|
2067
2135
|
getCommands() {
|
|
2068
2136
|
return this.paths.flatMap((path) => path.curves.flatMap((curve) => curve.getCommands()));
|
|
2069
2137
|
}
|
|
2070
2138
|
getData() {
|
|
2071
2139
|
return this.paths.map((path) => path.getData()).join(" ");
|
|
2072
2140
|
}
|
|
2073
|
-
getBoundingBox() {
|
|
2074
|
-
const { min, max } = this.getMinMax();
|
|
2075
|
-
return {
|
|
2076
|
-
x: min.x,
|
|
2077
|
-
y: min.y,
|
|
2078
|
-
width: max.x - min.x,
|
|
2079
|
-
height: max.y - min.y
|
|
2080
|
-
};
|
|
2081
|
-
}
|
|
2082
2141
|
getSvgString() {
|
|
2083
2142
|
const { x, y, width, height } = this.getBoundingBox();
|
|
2084
2143
|
return `<svg viewBox="${x} ${y} ${width} ${height}" xmlns="http://www.w3.org/2000/svg"><path fill="none" stroke="currentColor" d="${this.getData()}"></path></svg>`;
|
|
@@ -2099,6 +2158,15 @@ class Path2D {
|
|
|
2099
2158
|
this.drawTo(ctx);
|
|
2100
2159
|
ctx.fill();
|
|
2101
2160
|
}
|
|
2161
|
+
copy(source) {
|
|
2162
|
+
source.currentPath = this.currentPath.clone();
|
|
2163
|
+
source.paths = this.paths.map((path) => path.clone());
|
|
2164
|
+
source.userData = this.userData;
|
|
2165
|
+
return this;
|
|
2166
|
+
}
|
|
2167
|
+
clone() {
|
|
2168
|
+
return new Path2D().copy(this);
|
|
2169
|
+
}
|
|
2102
2170
|
}
|
|
2103
2171
|
|
|
2104
2172
|
const defaultUnit = "px";
|
|
@@ -2608,4 +2676,4 @@ function parseSvg(svg) {
|
|
|
2608
2676
|
});
|
|
2609
2677
|
}
|
|
2610
2678
|
|
|
2611
|
-
export { CircleCurve, CubicBezierCurve, Curve, CurvePath, EllipseCurve, HeartCurve, LineCurve, Matrix3, Path2D, PloygonCurve, Point2D, QuadraticBezierCurve, RectangularCurve, SplineCurve, parseSvg };
|
|
2679
|
+
export { BoundingBox, CircleCurve, CubicBezierCurve, Curve, CurvePath, EllipseCurve, HeartCurve, LineCurve, Matrix3, Path2D, PloygonCurve, Point2D, QuadraticBezierCurve, RectangularCurve, SplineCurve, parseSvg };
|
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.2",
|
|
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",
|