svg-path-commander 0.1.21 → 0.1.22
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 +72 -12
- package/dist/svg-path-commander.esm.js +647 -190
- package/dist/svg-path-commander.esm.min.js +2 -2
- package/dist/svg-path-commander.js +670 -189
- package/dist/svg-path-commander.min.js +2 -2
- package/package.json +1 -1
- package/src/convert/pathToAbsolute.js +3 -2
- package/src/convert/pathToCurve.js +2 -1
- package/src/convert/pathToRelative.js +2 -1
- package/src/parser/paramsCount.js +1 -0
- package/src/parser/parsePathString.js +4 -3
- package/src/parser/pathParser.js +2 -1
- package/src/parser/scanSegment.js +0 -1
- package/src/process/fixPath.js +1 -1
- package/src/process/lineToCubic.js +4 -5
- package/src/process/normalizePath.js +4 -2
- package/src/util/getClosestPoint.js +12 -0
- package/src/util/getCubicSize.js +41 -39
- package/src/util/getPathArea.js +19 -19
- package/src/util/getPathBBox.js +0 -11
- package/src/util/getPathLength.js +9 -9
- package/src/util/getPointAtLength.js +6 -29
- package/src/util/getPointAtPathLength.js +44 -0
- package/src/util/getPropertiesAtLength.js +62 -0
- package/src/util/getPropertiesAtPoint.js +77 -0
- package/src/util/getSegmentAtLength.js +13 -0
- package/src/util/getSegmentOfPoint.js +14 -0
- package/src/util/getTotalLength.js +15 -0
- package/src/util/isAbsoluteArray.js +2 -1
- package/src/util/isCurveArray.js +2 -1
- package/src/util/isNormalizedArray.js +2 -1
- package/src/util/isPointInStroke.js +13 -0
- package/src/util/isRelativeArray.js +2 -1
- package/src/util/pathLengthFactory.js +99 -0
- package/src/util/segmentArcFactory.js +42 -0
- package/src/util/segmentCubicFactory.js +73 -0
- package/src/util/segmentLineFactory.js +30 -0
- package/src/util/segmentQuadFactory.js +70 -0
- package/src/util/util.js +19 -1
- package/types/index.d.ts +14 -5
- package/types/more/modules.ts +17 -5
- package/types/more/svg.d.ts +21 -0
- package/types/svg-path-commander.d.ts +242 -122
- package/src/util/getPointAtSegLength.js +0 -28
- package/src/util/getSegArcLength.js +0 -27
- package/src/util/getSegCubicLength.js +0 -52
- package/src/util/getSegLineLength.js +0 -14
- package/src/util/getSegQuadLength.js +0 -31
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the {x,y} coordinates of a point at a
|
|
3
|
-
* given length of a cubic-bezier segment.
|
|
4
|
-
*
|
|
5
|
-
* @param {number} p1x the starting point X
|
|
6
|
-
* @param {number} p1y the starting point Y
|
|
7
|
-
* @param {number} c1x the first control point X
|
|
8
|
-
* @param {number} c1y the first control point Y
|
|
9
|
-
* @param {number} c2x the second control point X
|
|
10
|
-
* @param {number} c2y the second control point Y
|
|
11
|
-
* @param {number} p2x the ending point X
|
|
12
|
-
* @param {number} p2y the ending point Y
|
|
13
|
-
* @param {number} t a [0-1] ratio
|
|
14
|
-
* @returns {{x: number, y: number}} the requested {x,y} coordinates
|
|
15
|
-
*/
|
|
16
|
-
export default function getPointAtSegLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
|
|
17
|
-
const t1 = 1 - t;
|
|
18
|
-
return {
|
|
19
|
-
x: (t1 ** 3) * p1x
|
|
20
|
-
+ t1 * t1 * 3 * t * c1x
|
|
21
|
-
+ t1 * 3 * t * t * c2x
|
|
22
|
-
+ (t ** 3) * p2x,
|
|
23
|
-
y: (t1 ** 3) * p1y
|
|
24
|
-
+ t1 * t1 * 3 * t * c1y
|
|
25
|
-
+ t1 * 3 * t * t * c2y
|
|
26
|
-
+ (t ** 3) * p2y,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import getSegCubicLength from './getSegCubicLength';
|
|
2
|
-
import arcToCubic from '../process/arcToCubic';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Returns the A (arc-to) segment length.
|
|
6
|
-
*
|
|
7
|
-
* @param {number[]} args the arc-to coordinates
|
|
8
|
-
* @returns {number} the arc-to segment length
|
|
9
|
-
*/
|
|
10
|
-
export default function getSegArcLength(...args) {
|
|
11
|
-
let [x1, y1] = args.slice(0, 2);
|
|
12
|
-
// @ts-ignore -- this is an `arcSegment`
|
|
13
|
-
const cubicSeg = arcToCubic(...args);
|
|
14
|
-
let cumulatedLength = 0;
|
|
15
|
-
let cubicSubseg = [];
|
|
16
|
-
let argsc = [];
|
|
17
|
-
|
|
18
|
-
for (let i = 0, ii = cubicSeg.length; i < ii; i += 6) {
|
|
19
|
-
cubicSubseg = cubicSeg.slice(i, i + 6);
|
|
20
|
-
argsc = [x1, y1, ...cubicSubseg];
|
|
21
|
-
// @ts-ignore -- this is a `cubicSegment`
|
|
22
|
-
cumulatedLength += getSegCubicLength(...argsc);
|
|
23
|
-
[x1, y1] = cubicSubseg.slice(-2);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return cumulatedLength;
|
|
27
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {number} p1
|
|
3
|
-
* @param {number} p2
|
|
4
|
-
* @param {number} p3
|
|
5
|
-
* @param {number} p4
|
|
6
|
-
* @param {number} t a [0-1] ratio
|
|
7
|
-
* @returns {number}
|
|
8
|
-
*/
|
|
9
|
-
function base3(p1, p2, p3, p4, t) {
|
|
10
|
-
const t1 = -3 * p1 + 9 * p2 - 9 * p3 + 3 * p4;
|
|
11
|
-
const t2 = t * t1 + 6 * p1 - 12 * p2 + 6 * p3;
|
|
12
|
-
return t * t2 - 3 * p1 + 3 * p2;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Returns the C (cubic-bezier) segment length.
|
|
17
|
-
*
|
|
18
|
-
* @param {number} x1 the starting point X
|
|
19
|
-
* @param {number} y1 the starting point Y
|
|
20
|
-
* @param {number} x2 the first control point X
|
|
21
|
-
* @param {number} y2 the first control point Y
|
|
22
|
-
* @param {number} x3 the second control point X
|
|
23
|
-
* @param {number} y3 the second control point Y
|
|
24
|
-
* @param {number} x4 the ending point X
|
|
25
|
-
* @param {number} y4 the ending point Y
|
|
26
|
-
* @param {number} z a [0-1] ratio
|
|
27
|
-
* @returns {number} the cubic-bezier segment length
|
|
28
|
-
*/
|
|
29
|
-
export default function getSegCubicLength(x1, y1, x2, y2, x3, y3, x4, y4, z) {
|
|
30
|
-
let Z = z;
|
|
31
|
-
if (z === null || Number.isNaN(+z)) Z = 1;
|
|
32
|
-
|
|
33
|
-
// Z = Z > 1 ? 1 : Z < 0 ? 0 : Z;
|
|
34
|
-
if (Z > 1) Z = 1;
|
|
35
|
-
if (Z < 0) Z = 0;
|
|
36
|
-
|
|
37
|
-
const z2 = Z / 2; let ct = 0; let xbase = 0; let ybase = 0; let sum = 0;
|
|
38
|
-
const Tvalues = [-0.1252, 0.1252, -0.3678, 0.3678,
|
|
39
|
-
-0.5873, 0.5873, -0.7699, 0.7699,
|
|
40
|
-
-0.9041, 0.9041, -0.9816, 0.9816];
|
|
41
|
-
const Cvalues = [0.2491, 0.2491, 0.2335, 0.2335,
|
|
42
|
-
0.2032, 0.2032, 0.1601, 0.1601,
|
|
43
|
-
0.1069, 0.1069, 0.0472, 0.0472];
|
|
44
|
-
|
|
45
|
-
Tvalues.forEach((T, i) => {
|
|
46
|
-
ct = z2 * T + z2;
|
|
47
|
-
xbase = base3(x1, x2, x3, x4, ct);
|
|
48
|
-
ybase = base3(y1, y2, y3, y4, ct);
|
|
49
|
-
sum += Cvalues[i] * Math.sqrt(xbase * xbase + ybase * ybase);
|
|
50
|
-
});
|
|
51
|
-
return z2 * sum;
|
|
52
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import distanceSquareRoot from '../math/distanceSquareRoot';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Returns the L (line-to) segment length.
|
|
5
|
-
*
|
|
6
|
-
* @param {number} ax the starting point X
|
|
7
|
-
* @param {number} ay the starting point Y
|
|
8
|
-
* @param {number} bx the ending point X
|
|
9
|
-
* @param {number} by the ending point Y
|
|
10
|
-
* @returns {number} the line-to segment length
|
|
11
|
-
*/
|
|
12
|
-
export default function getSegLineLength(ax, ay, bx, by) {
|
|
13
|
-
return distanceSquareRoot([ax, ay], [bx, by]);
|
|
14
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the Q (quadratic-bezier) segment length.
|
|
3
|
-
* https://gist.github.com/tunght13488/6744e77c242cc7a94859#gistcomment-2047251
|
|
4
|
-
*
|
|
5
|
-
* @param {number} x1 the starting point X
|
|
6
|
-
* @param {number} y1 the starting point Y
|
|
7
|
-
* @param {number} qx the control point X
|
|
8
|
-
* @param {number} qy the control point Y
|
|
9
|
-
* @param {number} x2 the ending point X
|
|
10
|
-
* @param {number} y2 the ending point Y
|
|
11
|
-
* @param {number} z a [0-1] ratio
|
|
12
|
-
* @returns {number} the quadratic-bezier segment length
|
|
13
|
-
*/
|
|
14
|
-
export default function getSegQuadLength(x1, y1, qx, qy, x2, y2) {
|
|
15
|
-
const ax = x1 - 2 * qx + x2;
|
|
16
|
-
const ay = y1 - 2 * qy + y2;
|
|
17
|
-
const bx = 2 * qx - 2 * x1;
|
|
18
|
-
const by = 2 * qy - 2 * y1;
|
|
19
|
-
const A = 4 * (ax * ax + ay * ay);
|
|
20
|
-
const B = 4 * (ax * bx + ay * by);
|
|
21
|
-
const C = bx * bx + by * by;
|
|
22
|
-
|
|
23
|
-
const Sabc = 2 * Math.sqrt(A + B + C);
|
|
24
|
-
const A_2 = Math.sqrt(A);
|
|
25
|
-
const A_32 = 2 * A * A_2;
|
|
26
|
-
const C_2 = 2 * Math.sqrt(C);
|
|
27
|
-
const BA = B / A_2;
|
|
28
|
-
|
|
29
|
-
return (A_32 * Sabc + A_2 * B * (Sabc - C_2) + (4 * C * A - B * B)
|
|
30
|
-
* Math.log((2 * A_2 + BA + Sabc) / (BA + C_2))) / (4 * A_32);
|
|
31
|
-
}
|