modern-path2d 0.1.10 → 0.1.13
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 +120 -41
- package/dist/index.d.cts +191 -139
- package/dist/index.d.mts +191 -139
- package/dist/index.d.ts +191 -139
- package/dist/index.js +1 -1
- package/dist/index.mjs +115 -42
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const lineJoinMap = {
|
|
4
|
+
"arcs": "bevel",
|
|
5
|
+
"bevel": "bevel",
|
|
6
|
+
"miter": "miter",
|
|
7
|
+
"miter-clip": "miter",
|
|
8
|
+
"round": "round"
|
|
9
|
+
};
|
|
10
|
+
function setCanvasContext(ctx, style) {
|
|
11
|
+
const {
|
|
12
|
+
fill = "#000",
|
|
13
|
+
stroke = "none",
|
|
14
|
+
strokeWidth = stroke === "none" ? 0 : 1,
|
|
15
|
+
strokeLinecap = "round",
|
|
16
|
+
strokeLinejoin = "miter",
|
|
17
|
+
strokeMiterlimit = 0,
|
|
18
|
+
strokeDasharray,
|
|
19
|
+
strokeDashoffset = 0,
|
|
20
|
+
shadowOffsetX = 0,
|
|
21
|
+
shadowOffsetY = 0,
|
|
22
|
+
shadowBlur = 0,
|
|
23
|
+
shadowColor = "rgba(0, 0, 0, 0)"
|
|
24
|
+
} = style;
|
|
25
|
+
ctx.fillStyle = fill;
|
|
26
|
+
ctx.strokeStyle = stroke;
|
|
27
|
+
ctx.lineWidth = strokeWidth;
|
|
28
|
+
ctx.lineCap = strokeLinecap;
|
|
29
|
+
ctx.lineJoin = lineJoinMap[strokeLinejoin];
|
|
30
|
+
ctx.miterLimit = strokeMiterlimit;
|
|
31
|
+
strokeDasharray && ctx.setLineDash(strokeDasharray);
|
|
32
|
+
ctx.lineDashOffset = strokeDashoffset;
|
|
33
|
+
ctx.shadowOffsetX = shadowOffsetX;
|
|
34
|
+
ctx.shadowOffsetY = shadowOffsetY;
|
|
35
|
+
ctx.shadowBlur = shadowBlur;
|
|
36
|
+
ctx.shadowColor = shadowColor;
|
|
37
|
+
}
|
|
38
|
+
|
|
3
39
|
class Vector2 {
|
|
4
40
|
constructor(x = 0, y = 0) {
|
|
5
41
|
this.x = x;
|
|
@@ -2080,6 +2116,10 @@ class CurvePath extends Curve {
|
|
|
2080
2116
|
}
|
|
2081
2117
|
}
|
|
2082
2118
|
|
|
2119
|
+
function toKebabCase(str) {
|
|
2120
|
+
return str.replace(/[^a-z0-9]/gi, "-").replace(/\B([A-Z])/g, "-$1").toLowerCase();
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2083
2123
|
var __defProp = Object.defineProperty;
|
|
2084
2124
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2085
2125
|
var __publicField = (obj, key, value) => {
|
|
@@ -2090,7 +2130,7 @@ class Path2D {
|
|
|
2090
2130
|
constructor(path) {
|
|
2091
2131
|
__publicField(this, "currentPath", new CurvePath());
|
|
2092
2132
|
__publicField(this, "paths", [this.currentPath]);
|
|
2093
|
-
__publicField(this, "
|
|
2133
|
+
__publicField(this, "style", {});
|
|
2094
2134
|
if (path) {
|
|
2095
2135
|
if (path instanceof Path2D) {
|
|
2096
2136
|
this.addPath(path);
|
|
@@ -2107,6 +2147,9 @@ class Path2D {
|
|
|
2107
2147
|
get currentPoint() {
|
|
2108
2148
|
return this.currentPath.currentPoint;
|
|
2109
2149
|
}
|
|
2150
|
+
get strokeWidth() {
|
|
2151
|
+
return this.style.strokeWidth ?? ((this.style.stroke ?? "none") === "none" ? 0 : 1);
|
|
2152
|
+
}
|
|
2110
2153
|
addPath(path) {
|
|
2111
2154
|
if (path instanceof Path2D) {
|
|
2112
2155
|
this.paths.push(...path.paths.map((v) => v.clone()));
|
|
@@ -2194,9 +2237,17 @@ class Path2D {
|
|
|
2194
2237
|
this.forEachCurve((curve) => curve.getMinMax(min, max));
|
|
2195
2238
|
return { min, max };
|
|
2196
2239
|
}
|
|
2197
|
-
getBoundingBox() {
|
|
2240
|
+
getBoundingBox(withStrokeWidth = true) {
|
|
2198
2241
|
const { min, max } = this.getMinMax();
|
|
2199
|
-
|
|
2242
|
+
const bbox = new BoundingBox(min.x, min.y, max.x - min.x, max.y - min.y);
|
|
2243
|
+
if (withStrokeWidth) {
|
|
2244
|
+
const strokeWidth = this.strokeWidth;
|
|
2245
|
+
bbox.left -= strokeWidth / 2;
|
|
2246
|
+
bbox.top -= strokeWidth / 2;
|
|
2247
|
+
bbox.width += strokeWidth;
|
|
2248
|
+
bbox.height += strokeWidth;
|
|
2249
|
+
}
|
|
2250
|
+
return bbox;
|
|
2200
2251
|
}
|
|
2201
2252
|
getCommands() {
|
|
2202
2253
|
return this.paths.flatMap((path) => path.getCommands());
|
|
@@ -2204,46 +2255,73 @@ class Path2D {
|
|
|
2204
2255
|
getData() {
|
|
2205
2256
|
return this.paths.map((path) => path.getData()).join(" ");
|
|
2206
2257
|
}
|
|
2207
|
-
|
|
2258
|
+
getSvgPathXml() {
|
|
2259
|
+
const style = {
|
|
2260
|
+
...this.style,
|
|
2261
|
+
fill: this.style.fill ?? "#000",
|
|
2262
|
+
stroke: this.style.stroke ?? "none"
|
|
2263
|
+
};
|
|
2264
|
+
const cssStyle = {};
|
|
2265
|
+
for (const key in style) {
|
|
2266
|
+
if (style[key] !== void 0) {
|
|
2267
|
+
cssStyle[toKebabCase(key)] = style[key];
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
Object.assign(cssStyle, {
|
|
2271
|
+
"stroke-width": `${this.strokeWidth}px`
|
|
2272
|
+
});
|
|
2273
|
+
let cssText = "";
|
|
2274
|
+
for (const key in cssStyle) {
|
|
2275
|
+
if (cssStyle[key] !== void 0) {
|
|
2276
|
+
cssText += `${key}:${cssStyle[key]};`;
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
return `<path d="${this.getData()}" style="${cssText}"></path>`;
|
|
2280
|
+
}
|
|
2281
|
+
getSvgXml() {
|
|
2208
2282
|
const { x, y, width, height } = this.getBoundingBox();
|
|
2209
|
-
const
|
|
2210
|
-
return `<svg viewBox="${x
|
|
2283
|
+
const path = this.getSvgPathXml();
|
|
2284
|
+
return `<svg viewBox="${x} ${y} ${width} ${height}" width="${width}px" height="${height}px" xmlns="http://www.w3.org/2000/svg">${path}</svg>`;
|
|
2211
2285
|
}
|
|
2212
2286
|
getSvgDataUri() {
|
|
2213
|
-
return `data:image/svg+xml;base64,${btoa(this.
|
|
2287
|
+
return `data:image/svg+xml;base64,${btoa(this.getSvgXml())}`;
|
|
2214
2288
|
}
|
|
2215
|
-
drawTo(ctx) {
|
|
2289
|
+
drawTo(ctx, withStyle = true) {
|
|
2290
|
+
const { fill = "#000", stroke = "none" } = this.style;
|
|
2291
|
+
if (withStyle) {
|
|
2292
|
+
setCanvasContext(ctx, this.style);
|
|
2293
|
+
}
|
|
2216
2294
|
this.paths.forEach((path) => {
|
|
2217
2295
|
path.drawTo(ctx);
|
|
2218
2296
|
});
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
this.drawTo(ctx);
|
|
2226
|
-
ctx.fill();
|
|
2297
|
+
if (fill !== "none") {
|
|
2298
|
+
ctx.fill();
|
|
2299
|
+
}
|
|
2300
|
+
if (stroke !== "none") {
|
|
2301
|
+
ctx.stroke();
|
|
2302
|
+
}
|
|
2227
2303
|
}
|
|
2228
2304
|
copy(source) {
|
|
2229
2305
|
this.currentPath = source.currentPath.clone();
|
|
2230
2306
|
this.paths = source.paths.map((path) => path.clone());
|
|
2231
|
-
this.
|
|
2307
|
+
this.style = { ...source.style };
|
|
2232
2308
|
return this;
|
|
2233
2309
|
}
|
|
2234
|
-
|
|
2235
|
-
|
|
2310
|
+
toSvg() {
|
|
2311
|
+
return new DOMParser().parseFromString(this.getSvgXml(), "image/svg+xml").documentElement;
|
|
2312
|
+
}
|
|
2313
|
+
toCanvas(pixelRatio = 2) {
|
|
2236
2314
|
const { left, top, width, height } = this.getBoundingBox();
|
|
2237
|
-
canvas
|
|
2238
|
-
canvas.
|
|
2315
|
+
const canvas = document.createElement("canvas");
|
|
2316
|
+
canvas.width = width * pixelRatio;
|
|
2317
|
+
canvas.height = height * pixelRatio;
|
|
2318
|
+
canvas.style.width = `${width}px`;
|
|
2319
|
+
canvas.style.height = `${height}px`;
|
|
2239
2320
|
const ctx = canvas.getContext("2d");
|
|
2240
2321
|
if (ctx) {
|
|
2322
|
+
ctx.scale(pixelRatio, pixelRatio);
|
|
2241
2323
|
ctx.translate(-left, -top);
|
|
2242
|
-
|
|
2243
|
-
this.fillTo(ctx);
|
|
2244
|
-
} else {
|
|
2245
|
-
this.strokeTo(ctx);
|
|
2246
|
-
}
|
|
2324
|
+
this.drawTo(ctx);
|
|
2247
2325
|
}
|
|
2248
2326
|
return canvas;
|
|
2249
2327
|
}
|
|
@@ -2636,18 +2714,21 @@ function parseStyle(node, style, stylesheets) {
|
|
|
2636
2714
|
function positive(v) {
|
|
2637
2715
|
return Math.max(0, parseFloatWithUnits(v));
|
|
2638
2716
|
}
|
|
2717
|
+
function array(v) {
|
|
2718
|
+
return v.split(" ").filter((v2) => v2 !== "").map((v2) => parseFloatWithUnits(v2));
|
|
2719
|
+
}
|
|
2639
2720
|
addStyle("fill", "fill");
|
|
2640
2721
|
addStyle("fill-opacity", "fillOpacity", clamp);
|
|
2641
2722
|
addStyle("fill-rule", "fillRule");
|
|
2642
2723
|
addStyle("opacity", "opacity", clamp);
|
|
2643
2724
|
addStyle("stroke", "stroke");
|
|
2644
|
-
addStyle("stroke-dashoffset", "strokeDashoffset");
|
|
2645
|
-
addStyle("stroke-dasharray", "strokeDasharray");
|
|
2646
|
-
addStyle("stroke-linecap", "strokeLineCap");
|
|
2647
|
-
addStyle("stroke-linejoin", "strokeLineJoin");
|
|
2648
|
-
addStyle("stroke-miterlimit", "strokeMiterLimit", positive);
|
|
2649
2725
|
addStyle("stroke-opacity", "strokeOpacity", clamp);
|
|
2650
2726
|
addStyle("stroke-width", "strokeWidth", positive);
|
|
2727
|
+
addStyle("stroke-linecap", "strokeLinecap");
|
|
2728
|
+
addStyle("stroke-linejoin", "strokeLinejoin");
|
|
2729
|
+
addStyle("stroke-miterlimit", "strokeMiterlimit", positive);
|
|
2730
|
+
addStyle("stroke-dasharray", "strokeDasharray", array);
|
|
2731
|
+
addStyle("stroke-dashoffset", "strokeDashoffset", parseFloatWithUnits);
|
|
2651
2732
|
addStyle("visibility", "visibility");
|
|
2652
2733
|
return style;
|
|
2653
2734
|
}
|
|
@@ -2722,7 +2803,7 @@ function parseNode(node, style, paths = []) {
|
|
|
2722
2803
|
if (path) {
|
|
2723
2804
|
path.transform(currentTransform);
|
|
2724
2805
|
paths.push(path);
|
|
2725
|
-
path.
|
|
2806
|
+
path.style = style;
|
|
2726
2807
|
}
|
|
2727
2808
|
const childNodes = node.childNodes;
|
|
2728
2809
|
for (let i = 0, len = childNodes.length; i < len; i++) {
|
|
@@ -2766,15 +2847,7 @@ function parseSvgToDom(svg) {
|
|
|
2766
2847
|
}
|
|
2767
2848
|
}
|
|
2768
2849
|
function parseSvg(svg) {
|
|
2769
|
-
return parseNode(parseSvgToDom(svg), {
|
|
2770
|
-
fill: "#000",
|
|
2771
|
-
fillOpacity: 1,
|
|
2772
|
-
strokeOpacity: 1,
|
|
2773
|
-
strokeWidth: 1,
|
|
2774
|
-
strokeLineJoin: "miter",
|
|
2775
|
-
strokeLineCap: "butt",
|
|
2776
|
-
strokeMiterLimit: 4
|
|
2777
|
-
});
|
|
2850
|
+
return parseNode(parseSvgToDom(svg), {});
|
|
2778
2851
|
}
|
|
2779
2852
|
|
|
2780
2853
|
exports.BoundingBox = BoundingBox;
|
|
@@ -2792,5 +2865,11 @@ exports.QuadraticBezierCurve = QuadraticBezierCurve;
|
|
|
2792
2865
|
exports.RectangularCurve = RectangularCurve;
|
|
2793
2866
|
exports.SplineCurve = SplineCurve;
|
|
2794
2867
|
exports.Vector2 = Vector2;
|
|
2868
|
+
exports.addPathCommandsToPath2D = addPathCommandsToPath2D;
|
|
2869
|
+
exports.parseArcCommand = parseArcCommand;
|
|
2870
|
+
exports.parsePathDataArgs = parsePathDataArgs;
|
|
2795
2871
|
exports.parseSvg = parseSvg;
|
|
2796
2872
|
exports.parseSvgToDom = parseSvgToDom;
|
|
2873
|
+
exports.pathCommandsToPathData = pathCommandsToPathData;
|
|
2874
|
+
exports.pathDataToPathCommands = pathDataToPathCommands;
|
|
2875
|
+
exports.setCanvasContext = setCanvasContext;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @link https://developer.mozilla.org/docs/Web/SVG/Attribute/d
|
|
3
|
+
*/
|
|
4
|
+
type PathCommand = {
|
|
5
|
+
type: 'm' | 'M';
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'h' | 'H';
|
|
10
|
+
x: number;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'v' | 'V';
|
|
13
|
+
y: number;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'l' | 'L';
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'c' | 'C';
|
|
20
|
+
x1: number;
|
|
21
|
+
y1: number;
|
|
22
|
+
x2: number;
|
|
23
|
+
y2: number;
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
} | {
|
|
27
|
+
type: 's' | 'S';
|
|
28
|
+
x2: number;
|
|
29
|
+
y2: number;
|
|
30
|
+
x: number;
|
|
31
|
+
y: number;
|
|
32
|
+
} | {
|
|
33
|
+
type: 'q' | 'Q';
|
|
34
|
+
x1: number;
|
|
35
|
+
y1: number;
|
|
36
|
+
x: number;
|
|
37
|
+
y: number;
|
|
38
|
+
} | {
|
|
39
|
+
type: 't' | 'T';
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
} | {
|
|
43
|
+
type: 'a' | 'A';
|
|
44
|
+
rx: number;
|
|
45
|
+
ry: number;
|
|
46
|
+
angle: number;
|
|
47
|
+
largeArcFlag: number;
|
|
48
|
+
sweepFlag: number;
|
|
49
|
+
x: number;
|
|
50
|
+
y: number;
|
|
51
|
+
} | {
|
|
52
|
+
type: 'z' | 'Z';
|
|
53
|
+
};
|
|
54
|
+
type FillRule = 'nonzero' | 'evenodd';
|
|
55
|
+
type StrokeLinecap = 'butt' | 'round' | 'square';
|
|
56
|
+
type StrokeLinejoin = 'arcs' | 'bevel' | 'miter' | 'miter-clip' | 'round';
|
|
57
|
+
interface PathDrawStyle {
|
|
58
|
+
fill: string | CanvasGradient | CanvasPattern;
|
|
59
|
+
stroke: string | CanvasGradient | CanvasPattern;
|
|
60
|
+
shadowColor: string;
|
|
61
|
+
shadowOffsetX: number;
|
|
62
|
+
shadowOffsetY: number;
|
|
63
|
+
shadowBlur: number;
|
|
64
|
+
}
|
|
65
|
+
interface PathStyle extends PathDrawStyle {
|
|
66
|
+
[key: string]: any;
|
|
67
|
+
fillOpacity: number;
|
|
68
|
+
fillRule: FillRule;
|
|
69
|
+
opacity: number;
|
|
70
|
+
strokeOpacity: number;
|
|
71
|
+
strokeWidth: number;
|
|
72
|
+
strokeLinecap: StrokeLinecap;
|
|
73
|
+
strokeLinejoin: StrokeLinejoin;
|
|
74
|
+
strokeMiterlimit: number;
|
|
75
|
+
strokeDasharray: number[];
|
|
76
|
+
strokeDashoffset: number;
|
|
77
|
+
visibility: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
declare function setCanvasContext(ctx: CanvasRenderingContext2D, style: Partial<PathStyle>): void;
|
|
81
|
+
|
|
1
82
|
declare class Matrix3 {
|
|
2
83
|
elements: number[];
|
|
3
84
|
constructor(n11?: number, n12?: number, n13?: number, n21?: number, n22?: number, n23?: number, n31?: number, n32?: number, n33?: number);
|
|
@@ -65,144 +146,6 @@ declare class BoundingBox {
|
|
|
65
146
|
toArray(): [number, number, number, number];
|
|
66
147
|
}
|
|
67
148
|
|
|
68
|
-
declare class CurvePath extends Curve {
|
|
69
|
-
curves: Curve[];
|
|
70
|
-
startPoint?: Vector2;
|
|
71
|
-
currentPoint: Vector2;
|
|
72
|
-
autoClose: boolean;
|
|
73
|
-
protected _cacheLengths: number[];
|
|
74
|
-
constructor(points?: Vector2[]);
|
|
75
|
-
addCurve(curve: Curve): this;
|
|
76
|
-
addPoints(points: Vector2[]): this;
|
|
77
|
-
addCommands(commands: PathCommand[]): this;
|
|
78
|
-
addData(data: string): this;
|
|
79
|
-
getPoint(t: number, output?: Vector2): Vector2;
|
|
80
|
-
getLength(): number;
|
|
81
|
-
updateArcLengths(): void;
|
|
82
|
-
getCurveLengths(): number[];
|
|
83
|
-
getSpacedPoints(divisions?: number): Vector2[];
|
|
84
|
-
getPoints(divisions?: number): Vector2[];
|
|
85
|
-
protected _setCurrentPoint(point: VectorLike): this;
|
|
86
|
-
closePath(): this;
|
|
87
|
-
moveTo(x: number, y: number): this;
|
|
88
|
-
lineTo(x: number, y: number): this;
|
|
89
|
-
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): this;
|
|
90
|
-
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): this;
|
|
91
|
-
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
92
|
-
relativeArc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
93
|
-
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): this;
|
|
94
|
-
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
95
|
-
relativeEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
96
|
-
rect(x: number, y: number, w: number, h: number): this;
|
|
97
|
-
splineThru(points: Vector2[]): this;
|
|
98
|
-
transformPoint(cb: (point: Vector2) => void): this;
|
|
99
|
-
getMinMax(min?: Vector2, max?: Vector2): {
|
|
100
|
-
min: Vector2;
|
|
101
|
-
max: Vector2;
|
|
102
|
-
};
|
|
103
|
-
getBoundingBox(): BoundingBox;
|
|
104
|
-
getCommands(): PathCommand[];
|
|
105
|
-
drawTo(ctx: CanvasRenderingContext2D): this;
|
|
106
|
-
copy(source: CurvePath): this;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Path2D
|
|
111
|
-
*/
|
|
112
|
-
declare class Path2D<T = any> {
|
|
113
|
-
currentPath: CurvePath;
|
|
114
|
-
paths: CurvePath[];
|
|
115
|
-
userData?: T;
|
|
116
|
-
get startPoint(): Vector2 | undefined;
|
|
117
|
-
get currentPoint(): Vector2 | undefined;
|
|
118
|
-
constructor(path?: Path2D | PathCommand[] | string);
|
|
119
|
-
addPath(path: Path2D | CurvePath): this;
|
|
120
|
-
closePath(): this;
|
|
121
|
-
moveTo(x: number, y: number): this;
|
|
122
|
-
lineTo(x: number, y: number): this;
|
|
123
|
-
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): this;
|
|
124
|
-
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): this;
|
|
125
|
-
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
126
|
-
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): this;
|
|
127
|
-
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
128
|
-
rect(x: number, y: number, w: number, h: number): this;
|
|
129
|
-
addCommands(commands: PathCommand[]): this;
|
|
130
|
-
addData(data: string): this;
|
|
131
|
-
splineThru(points: Vector2[]): this;
|
|
132
|
-
forEachCurve(cb: (curve: Curve) => void): this;
|
|
133
|
-
transformPoint(cb: (point: Vector2) => void): this;
|
|
134
|
-
transform(matrix: Matrix3): this;
|
|
135
|
-
getMinMax(min?: Vector2, max?: Vector2): {
|
|
136
|
-
min: Vector2;
|
|
137
|
-
max: Vector2;
|
|
138
|
-
};
|
|
139
|
-
getBoundingBox(): BoundingBox;
|
|
140
|
-
getCommands(): PathCommand[];
|
|
141
|
-
getData(): string;
|
|
142
|
-
getSvgString(): string;
|
|
143
|
-
getSvgDataUri(): string;
|
|
144
|
-
drawTo(ctx: CanvasRenderingContext2D): void;
|
|
145
|
-
strokeTo(ctx: CanvasRenderingContext2D): void;
|
|
146
|
-
fillTo(ctx: CanvasRenderingContext2D): void;
|
|
147
|
-
copy(source: Path2D): this;
|
|
148
|
-
toCanvas(fill?: boolean): HTMLCanvasElement;
|
|
149
|
-
clone(): this;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* @link https://developer.mozilla.org/docs/Web/SVG/Attribute/d
|
|
154
|
-
*/
|
|
155
|
-
type PathCommand = {
|
|
156
|
-
type: 'm' | 'M';
|
|
157
|
-
x: number;
|
|
158
|
-
y: number;
|
|
159
|
-
} | {
|
|
160
|
-
type: 'h' | 'H';
|
|
161
|
-
x: number;
|
|
162
|
-
} | {
|
|
163
|
-
type: 'v' | 'V';
|
|
164
|
-
y: number;
|
|
165
|
-
} | {
|
|
166
|
-
type: 'l' | 'L';
|
|
167
|
-
x: number;
|
|
168
|
-
y: number;
|
|
169
|
-
} | {
|
|
170
|
-
type: 'c' | 'C';
|
|
171
|
-
x1: number;
|
|
172
|
-
y1: number;
|
|
173
|
-
x2: number;
|
|
174
|
-
y2: number;
|
|
175
|
-
x: number;
|
|
176
|
-
y: number;
|
|
177
|
-
} | {
|
|
178
|
-
type: 's' | 'S';
|
|
179
|
-
x2: number;
|
|
180
|
-
y2: number;
|
|
181
|
-
x: number;
|
|
182
|
-
y: number;
|
|
183
|
-
} | {
|
|
184
|
-
type: 'q' | 'Q';
|
|
185
|
-
x1: number;
|
|
186
|
-
y1: number;
|
|
187
|
-
x: number;
|
|
188
|
-
y: number;
|
|
189
|
-
} | {
|
|
190
|
-
type: 't' | 'T';
|
|
191
|
-
x: number;
|
|
192
|
-
y: number;
|
|
193
|
-
} | {
|
|
194
|
-
type: 'a' | 'A';
|
|
195
|
-
rx: number;
|
|
196
|
-
ry: number;
|
|
197
|
-
angle: number;
|
|
198
|
-
largeArcFlag: number;
|
|
199
|
-
sweepFlag: number;
|
|
200
|
-
x: number;
|
|
201
|
-
y: number;
|
|
202
|
-
} | {
|
|
203
|
-
type: 'z' | 'Z';
|
|
204
|
-
};
|
|
205
|
-
|
|
206
149
|
declare abstract class Curve {
|
|
207
150
|
arcLengthDivisions: number;
|
|
208
151
|
protected _cacheArcLengths?: number[];
|
|
@@ -410,7 +353,116 @@ declare class SplineCurve extends Curve {
|
|
|
410
353
|
copy(source: SplineCurve): this;
|
|
411
354
|
}
|
|
412
355
|
|
|
356
|
+
declare class CurvePath extends Curve {
|
|
357
|
+
curves: Curve[];
|
|
358
|
+
startPoint?: Vector2;
|
|
359
|
+
currentPoint: Vector2;
|
|
360
|
+
autoClose: boolean;
|
|
361
|
+
protected _cacheLengths: number[];
|
|
362
|
+
constructor(points?: Vector2[]);
|
|
363
|
+
addCurve(curve: Curve): this;
|
|
364
|
+
addPoints(points: Vector2[]): this;
|
|
365
|
+
addCommands(commands: PathCommand[]): this;
|
|
366
|
+
addData(data: string): this;
|
|
367
|
+
getPoint(t: number, output?: Vector2): Vector2;
|
|
368
|
+
getLength(): number;
|
|
369
|
+
updateArcLengths(): void;
|
|
370
|
+
getCurveLengths(): number[];
|
|
371
|
+
getSpacedPoints(divisions?: number): Vector2[];
|
|
372
|
+
getPoints(divisions?: number): Vector2[];
|
|
373
|
+
protected _setCurrentPoint(point: VectorLike): this;
|
|
374
|
+
closePath(): this;
|
|
375
|
+
moveTo(x: number, y: number): this;
|
|
376
|
+
lineTo(x: number, y: number): this;
|
|
377
|
+
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): this;
|
|
378
|
+
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): this;
|
|
379
|
+
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
380
|
+
relativeArc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
381
|
+
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): this;
|
|
382
|
+
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
383
|
+
relativeEllipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
384
|
+
rect(x: number, y: number, w: number, h: number): this;
|
|
385
|
+
splineThru(points: Vector2[]): this;
|
|
386
|
+
transformPoint(cb: (point: Vector2) => void): this;
|
|
387
|
+
getMinMax(min?: Vector2, max?: Vector2): {
|
|
388
|
+
min: Vector2;
|
|
389
|
+
max: Vector2;
|
|
390
|
+
};
|
|
391
|
+
getBoundingBox(): BoundingBox;
|
|
392
|
+
getCommands(): PathCommand[];
|
|
393
|
+
drawTo(ctx: CanvasRenderingContext2D): this;
|
|
394
|
+
copy(source: CurvePath): this;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Path2D
|
|
399
|
+
*/
|
|
400
|
+
declare class Path2D {
|
|
401
|
+
currentPath: CurvePath;
|
|
402
|
+
paths: CurvePath[];
|
|
403
|
+
style: Partial<PathStyle>;
|
|
404
|
+
get startPoint(): Vector2 | undefined;
|
|
405
|
+
get currentPoint(): Vector2 | undefined;
|
|
406
|
+
get strokeWidth(): number;
|
|
407
|
+
constructor(path?: Path2D | PathCommand[] | string);
|
|
408
|
+
addPath(path: Path2D | CurvePath): this;
|
|
409
|
+
closePath(): this;
|
|
410
|
+
moveTo(x: number, y: number): this;
|
|
411
|
+
lineTo(x: number, y: number): this;
|
|
412
|
+
bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): this;
|
|
413
|
+
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): this;
|
|
414
|
+
arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
415
|
+
arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): this;
|
|
416
|
+
ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
|
|
417
|
+
rect(x: number, y: number, w: number, h: number): this;
|
|
418
|
+
addCommands(commands: PathCommand[]): this;
|
|
419
|
+
addData(data: string): this;
|
|
420
|
+
splineThru(points: Vector2[]): this;
|
|
421
|
+
forEachCurve(cb: (curve: Curve) => void): this;
|
|
422
|
+
transformPoint(cb: (point: Vector2) => void): this;
|
|
423
|
+
transform(matrix: Matrix3): this;
|
|
424
|
+
getMinMax(min?: Vector2, max?: Vector2): {
|
|
425
|
+
min: Vector2;
|
|
426
|
+
max: Vector2;
|
|
427
|
+
};
|
|
428
|
+
getBoundingBox(withStrokeWidth?: boolean): BoundingBox;
|
|
429
|
+
getCommands(): PathCommand[];
|
|
430
|
+
getData(): string;
|
|
431
|
+
getSvgPathXml(): string;
|
|
432
|
+
getSvgXml(): string;
|
|
433
|
+
getSvgDataUri(): string;
|
|
434
|
+
drawTo(ctx: CanvasRenderingContext2D, withStyle?: boolean): void;
|
|
435
|
+
copy(source: Path2D): this;
|
|
436
|
+
toSvg(): SVGElement;
|
|
437
|
+
toCanvas(pixelRatio?: number): HTMLCanvasElement;
|
|
438
|
+
clone(): this;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* @link http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
|
|
443
|
+
*/
|
|
444
|
+
declare function addPathCommandsToPath2D(commands: PathCommand[], path: Path2D | CurvePath): void;
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
|
|
448
|
+
* https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
|
|
449
|
+
* From
|
|
450
|
+
* rx ry x-axis-rotation large-arc-flag sweep-flag x y
|
|
451
|
+
* To
|
|
452
|
+
* aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
|
|
453
|
+
*/
|
|
454
|
+
declare function parseArcCommand(path: Path2D | CurvePath, rx: number, ry: number, xAxisRotation: number, largeArcFlag: number, sweepFlag: number, start: Vector2, end: Vector2): void;
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* from https://github.com/ppvg/svg-numbers (MIT License)
|
|
458
|
+
*/
|
|
459
|
+
declare function parsePathDataArgs(input: string, flags?: number[], stride?: number): number[];
|
|
460
|
+
|
|
461
|
+
declare function pathCommandsToPathData(commands: PathCommand[]): string;
|
|
462
|
+
|
|
463
|
+
declare function pathDataToPathCommands(d: string): PathCommand[];
|
|
464
|
+
|
|
413
465
|
declare function parseSvgToDom(svg: string | SVGElement): SVGElement;
|
|
414
466
|
declare function parseSvg(svg: string | SVGElement): Path2D[];
|
|
415
467
|
|
|
416
|
-
export { BoundingBox, CircleCurve, CubicBezierCurve, Curve, CurvePath, EllipseCurve, HeartCurve, LineCurve, Matrix3, Path2D, PloygonCurve, QuadraticBezierCurve, RectangularCurve, SplineCurve, Vector2, type VectorLike, parseSvg, parseSvgToDom };
|
|
468
|
+
export { BoundingBox, CircleCurve, CubicBezierCurve, Curve, CurvePath, EllipseCurve, type FillRule, HeartCurve, LineCurve, Matrix3, Path2D, type PathCommand, type PathDrawStyle, type PathStyle, PloygonCurve, QuadraticBezierCurve, RectangularCurve, SplineCurve, type StrokeLinecap, type StrokeLinejoin, Vector2, type VectorLike, addPathCommandsToPath2D, parseArcCommand, parsePathDataArgs, parseSvg, parseSvgToDom, pathCommandsToPathData, pathDataToPathCommands, setCanvasContext };
|