svg-path-commander 0.1.25 → 0.2.0-alpha3
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 +5 -3
- package/dist/svg-path-commander.es5.js +3948 -0
- package/dist/svg-path-commander.es5.min.js +2 -0
- package/dist/svg-path-commander.esm.js +425 -404
- package/dist/svg-path-commander.esm.min.js +2 -2
- package/dist/svg-path-commander.js +1432 -1513
- package/dist/svg-path-commander.min.js +2 -2
- package/package.json +28 -12
- package/src/convert/pathToAbsolute.js +4 -4
- package/src/convert/pathToCurve.js +2 -2
- package/src/convert/pathToRelative.js +4 -4
- package/src/convert/pathToString.js +2 -2
- package/src/math/polygonLength.js +1 -2
- package/src/options/options.js +1 -1
- package/src/parser/error.js +2 -0
- package/src/parser/finalizeSegment.js +1 -1
- package/src/parser/paramsParser.js +1 -1
- package/src/parser/parsePathString.js +6 -11
- package/src/parser/pathParser.js +1 -1
- package/src/parser/scanFlag.js +3 -3
- package/src/parser/scanParam.js +7 -6
- package/src/parser/scanSegment.js +3 -2
- package/src/parser/skipSpaces.js +1 -1
- package/src/process/clonePath.js +1 -1
- package/src/process/fixArc.js +1 -1
- package/src/process/fixPath.js +2 -2
- package/src/process/getSVGMatrix.js +3 -2
- package/src/process/normalizePath.js +3 -3
- package/src/process/normalizeSegment.js +2 -2
- package/src/process/optimizePath.js +2 -2
- package/src/process/projection2d.js +2 -2
- package/src/process/reverseCurve.js +3 -3
- package/src/process/reversePath.js +4 -5
- package/src/process/roundPath.js +7 -6
- package/src/process/segmentToCubic.js +3 -3
- package/src/process/shortenSegment.js +3 -3
- package/src/process/splitCubic.js +1 -1
- package/src/process/splitPath.js +1 -1
- package/src/process/transformPath.js +8 -7
- package/src/svg-path-commander.js +60 -20
- package/src/util/getClosestPoint.js +1 -1
- package/src/util/getCubicSize.js +1 -1
- package/src/util/getDrawDirection.js +1 -1
- package/src/util/getPathArea.js +1 -1
- package/src/util/getPathBBox.js +2 -2
- package/src/util/getPathLength.js +1 -1
- package/src/util/getPointAtLength.js +1 -1
- package/src/util/getPropertiesAtLength.js +3 -3
- package/src/util/getPropertiesAtPoint.js +2 -2
- package/src/util/getSegmentAtLength.js +2 -2
- package/src/util/getSegmentOfPoint.js +2 -2
- package/src/util/getTotalLength.js +1 -1
- package/src/util/isAbsoluteArray.js +1 -1
- package/src/util/isCurveArray.js +1 -1
- package/src/util/isNormalizedArray.js +1 -1
- package/src/util/isPathArray.js +1 -1
- package/src/util/isPointInStroke.js +2 -2
- package/src/util/isRelativeArray.js +1 -1
- package/src/util/pathLengthFactory.js +3 -2
- package/src/util/segmentArcFactory.js +1 -1
- package/src/util/segmentCubicFactory.js +1 -1
- package/src/util/segmentLineFactory.js +1 -1
- package/src/util/segmentQuadFactory.js +1 -1
- package/src/util/shapeToPath.js +13 -13
- package/src/util/util.js +0 -2
- package/types/index.d.ts +1 -3
- package/types/more/modules.ts +0 -3
- package/types/more/svg.d.ts +11 -10
- package/types/svg-path-commander.d.ts +214 -226
- package/src/util/createPath.js +0 -17
- package/src/util/getPointAtPathLength.js +0 -15
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* Details =>
|
|
8
8
|
* https://stackoverflow.com/questions/23792505/predicted-rendering-of-css-3d-transformed-pixel
|
|
9
9
|
*
|
|
10
|
-
* @param {
|
|
10
|
+
* @param {SVGPath.CSSMatrix} m the transformation matrix
|
|
11
11
|
* @param {[number, number]} point2D the initial [x,y] coordinates
|
|
12
|
-
* @param {number[]} origin the
|
|
12
|
+
* @param {number[]} origin the [x,y,z] transform origin
|
|
13
13
|
* @returns {[number, number]} the projected [x,y] coordinates
|
|
14
14
|
*/
|
|
15
15
|
export default function projection2d(m, point2D, origin) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Reverses all segments
|
|
2
|
+
* Reverses all segments of a `pathArray`
|
|
3
3
|
* which consists of only C (cubic-bezier) path commands.
|
|
4
4
|
*
|
|
5
|
-
* @param {
|
|
6
|
-
* @returns {
|
|
5
|
+
* @param {SVGPath.curveArray} path the source `pathArray`
|
|
6
|
+
* @returns {SVGPath.curveArray} the reversed `pathArray`
|
|
7
7
|
*/
|
|
8
8
|
export default function reverseCurve(path) {
|
|
9
9
|
const rotatedCurve = path.slice(1)
|
|
@@ -2,11 +2,10 @@ import pathToAbsolute from '../convert/pathToAbsolute';
|
|
|
2
2
|
import normalizePath from './normalizePath';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Reverses all segments
|
|
6
|
-
* and returns a new instance.
|
|
5
|
+
* Reverses all segments of a `pathArray` and returns a new `pathArray` instance.
|
|
7
6
|
*
|
|
8
|
-
* @param {
|
|
9
|
-
* @returns {
|
|
7
|
+
* @param {SVGPath.pathArray} pathInput the source `pathArray`
|
|
8
|
+
* @returns {SVGPath.pathArray} the reversed `pathArray`
|
|
10
9
|
*/
|
|
11
10
|
export default function reversePath(pathInput) {
|
|
12
11
|
const absolutePath = pathToAbsolute(pathInput);
|
|
@@ -31,7 +30,7 @@ export default function reversePath(pathInput) {
|
|
|
31
30
|
/** @type {number} */
|
|
32
31
|
const x = i ? path[i - 1].x : path[pLen - 1].x;
|
|
33
32
|
const y = i ? path[i - 1].y : path[pLen - 1].y;
|
|
34
|
-
/** @type {
|
|
33
|
+
/** @type {SVGPath.pathSegment} */
|
|
35
34
|
// @ts-ignore
|
|
36
35
|
let result = [];
|
|
37
36
|
|
package/src/process/roundPath.js
CHANGED
|
@@ -4,23 +4,24 @@ import clonePath from './clonePath';
|
|
|
4
4
|
* Rounds the values of a `pathArray` instance to
|
|
5
5
|
* a specified amount of decimals and returns it.
|
|
6
6
|
*
|
|
7
|
-
* @param {
|
|
8
|
-
* @param {number |
|
|
9
|
-
* @returns {
|
|
7
|
+
* @param {SVGPath.pathArray} path the source `pathArray`
|
|
8
|
+
* @param {number | false} roundOption the amount of decimals to round numbers to
|
|
9
|
+
* @returns {SVGPath.pathArray} the resulted `pathArray` with rounded values
|
|
10
10
|
*/
|
|
11
11
|
export default function roundPath(path, roundOption) {
|
|
12
12
|
let { round } = defaultOptions;
|
|
13
13
|
if (roundOption === false || round === false) return clonePath(path);
|
|
14
|
-
round = roundOption >= 1 ? roundOption : round;
|
|
14
|
+
// round = roundOption >= 1 ? roundOption : round;
|
|
15
|
+
// allow for ZERO decimals
|
|
16
|
+
round = roundOption >= 0 ? roundOption : round;
|
|
15
17
|
// to round values to the power
|
|
16
18
|
// the `round` value must be integer
|
|
17
|
-
// @ts-ignore
|
|
18
19
|
const pow = round >= 1 ? (10 ** round) : 1;
|
|
19
20
|
|
|
20
21
|
// @ts-ignore -- `pathSegment[]` is `pathArray`
|
|
21
22
|
return path.map((pi) => {
|
|
22
23
|
const values = pi.slice(1).map(Number)
|
|
23
|
-
.map((n) => (
|
|
24
|
+
.map((n) => (round ? (Math.round(n * pow) / pow) : Math.round(n)));
|
|
24
25
|
return [pi[0], ...values];
|
|
25
26
|
});
|
|
26
27
|
}
|
|
@@ -5,9 +5,9 @@ import lineToCubic from './lineToCubic';
|
|
|
5
5
|
/**
|
|
6
6
|
* Converts any segment to C (cubic-bezier).
|
|
7
7
|
*
|
|
8
|
-
* @param {
|
|
9
|
-
* @param {
|
|
10
|
-
* @returns {
|
|
8
|
+
* @param {SVGPath.pathSegment} segment the source segment
|
|
9
|
+
* @param {SVGPath.parserParams} params the source segment parameters
|
|
10
|
+
* @returns {SVGPath.cubicSegment | SVGPath.MSegment} the cubic-bezier segment
|
|
11
11
|
*/
|
|
12
12
|
export default function segmentToCubic(segment, params) {
|
|
13
13
|
const [pathCommand] = segment;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shorten a single segment of a `pathArray` object.
|
|
3
3
|
*
|
|
4
|
-
* @param {
|
|
5
|
-
* @param {
|
|
4
|
+
* @param {SVGPath.absoluteSegment} segment the `absoluteSegment` object
|
|
5
|
+
* @param {SVGPath.normalSegment} normalSegment the `normalSegment` object
|
|
6
6
|
* @param {any} params the coordinates of the previous segment
|
|
7
7
|
* @param {string} prevCommand the path command of the previous segment
|
|
8
|
-
* @returns {
|
|
8
|
+
* @returns {SVGPath.shortSegment | SVGPath.pathSegment} the shortened segment
|
|
9
9
|
*/
|
|
10
10
|
export default function shortenSegment(segment, normalSegment, params, prevCommand) {
|
|
11
11
|
const [pathCommand] = segment;
|
|
@@ -4,7 +4,7 @@ import midPoint from '../math/midPoint';
|
|
|
4
4
|
* Split a cubic-bezier segment into two.
|
|
5
5
|
*
|
|
6
6
|
* @param {number[]} pts the cubic-bezier parameters
|
|
7
|
-
* @return {
|
|
7
|
+
* @return {SVGPath.cubicSegment[]} two new cubic-bezier segments
|
|
8
8
|
*/
|
|
9
9
|
export default function splitCubic(pts/* , ratio */) {
|
|
10
10
|
const t = /* ratio || */ 0.5;
|
package/src/process/splitPath.js
CHANGED
|
@@ -7,7 +7,7 @@ import pathToAbsolute from '../convert/pathToAbsolute';
|
|
|
7
7
|
* In the process, values are converted to absolute
|
|
8
8
|
* for visual consistency.
|
|
9
9
|
*
|
|
10
|
-
* @param {
|
|
10
|
+
* @param {SVGPath.pathArray | string} pathInput the source `pathArray`
|
|
11
11
|
* @return {string[]} an array with all sub-path strings
|
|
12
12
|
*/
|
|
13
13
|
export default function splitPath(pathInput) {
|
|
@@ -14,9 +14,9 @@ import paramsParser from '../parser/paramsParser';
|
|
|
14
14
|
* Since *SVGElement* doesn't support 3D transformation, this function
|
|
15
15
|
* creates a 2D projection of the <path> element.
|
|
16
16
|
*
|
|
17
|
-
* @param {
|
|
18
|
-
* @param {
|
|
19
|
-
* @returns {
|
|
17
|
+
* @param {SVGPath.pathArray} path the `pathArray` to apply transformation
|
|
18
|
+
* @param {SVGPath.transformObject} transform the transform functions `Object`
|
|
19
|
+
* @returns {SVGPath.pathArray} the resulted `pathArray`
|
|
20
20
|
*/
|
|
21
21
|
export default function transformPath(path, transform) {
|
|
22
22
|
let x = 0; let y = 0; let i; let j; let ii; let jj; let lx; let ly; let te;
|
|
@@ -31,12 +31,12 @@ export default function transformPath(path, transform) {
|
|
|
31
31
|
const matrix2d = [a, b, c, d, e, f];
|
|
32
32
|
const params = { ...paramsParser };
|
|
33
33
|
/** @ts-ignore */
|
|
34
|
-
/** @type {
|
|
34
|
+
/** @type {SVGPath.pathSegment} */
|
|
35
35
|
// @ts-ignore
|
|
36
36
|
let segment = [];
|
|
37
37
|
let seglen = 0;
|
|
38
38
|
let pathCommand = '';
|
|
39
|
-
/** @type {
|
|
39
|
+
/** @type {SVGPath.pathTransformList[]} */
|
|
40
40
|
let transformedPath = [];
|
|
41
41
|
const allPathCommands = []; // needed for arc to curve transformation
|
|
42
42
|
|
|
@@ -75,7 +75,7 @@ export default function transformPath(path, transform) {
|
|
|
75
75
|
params.x2 = +(segment[seglen - 4]) || params.x1;
|
|
76
76
|
params.y2 = +(segment[seglen - 3]) || params.y1;
|
|
77
77
|
|
|
78
|
-
/** @type {
|
|
78
|
+
/** @type {SVGPath.pathTransformList} */
|
|
79
79
|
const result = {
|
|
80
80
|
s: absolutePath[i], c: absolutePath[i][0], x: params.x1, y: params.y1,
|
|
81
81
|
};
|
|
@@ -112,6 +112,7 @@ export default function transformPath(path, transform) {
|
|
|
112
112
|
case 'L':
|
|
113
113
|
case 'H':
|
|
114
114
|
case 'V':
|
|
115
|
+
// @ts-ignore
|
|
115
116
|
[lx, ly] = projection2d(matrixInstance, [seg.x, seg.y], origin);
|
|
116
117
|
|
|
117
118
|
if (x !== lx && y !== ly) {
|
|
@@ -128,7 +129,7 @@ export default function transformPath(path, transform) {
|
|
|
128
129
|
default:
|
|
129
130
|
|
|
130
131
|
for (j = 1, jj = segment.length; j < jj; j += 2) {
|
|
131
|
-
// compute line coordinates without altering previous coordinates
|
|
132
|
+
// @ts-ignore compute line coordinates without altering previous coordinates
|
|
132
133
|
[x, y] = projection2d(matrixInstance, [+segment[j], +segment[j + 1]], origin);
|
|
133
134
|
segment[j] = x;
|
|
134
135
|
segment[j + 1] = y;
|
|
@@ -14,7 +14,11 @@ import optimizePath from './process/optimizePath';
|
|
|
14
14
|
import normalizePath from './process/normalizePath';
|
|
15
15
|
import transformPath from './process/transformPath';
|
|
16
16
|
|
|
17
|
+
import error from './parser/error';
|
|
18
|
+
|
|
17
19
|
import getPathBBox from './util/getPathBBox';
|
|
20
|
+
import getTotalLength from './util/getTotalLength';
|
|
21
|
+
import getPointAtLength from './util/getPointAtLength';
|
|
18
22
|
|
|
19
23
|
/**
|
|
20
24
|
* Creates a new SVGPathCommander instance with the following properties:
|
|
@@ -35,18 +39,25 @@ class SVGPathCommander {
|
|
|
35
39
|
constructor(pathValue, config) {
|
|
36
40
|
const instanceOptions = config || {};
|
|
37
41
|
|
|
42
|
+
const undefPath = typeof pathValue === 'undefined';
|
|
43
|
+
|
|
44
|
+
if (undefPath || !pathValue.length) {
|
|
45
|
+
throw TypeError(`${error}: "pathValue" is ${undefPath ? 'undefined' : 'empty'}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const segments = parsePathString(pathValue);
|
|
49
|
+
if (typeof segments === 'string') {
|
|
50
|
+
throw TypeError(segments);
|
|
51
|
+
}
|
|
52
|
+
|
|
38
53
|
/**
|
|
39
|
-
* @type {
|
|
54
|
+
* @type {SVGPath.pathArray}
|
|
40
55
|
*/
|
|
41
|
-
this.segments =
|
|
42
|
-
|
|
56
|
+
this.segments = segments;
|
|
57
|
+
|
|
43
58
|
const {
|
|
44
|
-
width,
|
|
45
|
-
|
|
46
|
-
cx,
|
|
47
|
-
cy,
|
|
48
|
-
cz,
|
|
49
|
-
} = BBox;
|
|
59
|
+
width, height, cx, cy, cz,
|
|
60
|
+
} = this.getBBox();
|
|
50
61
|
|
|
51
62
|
// set instance options.round
|
|
52
63
|
let { round, origin } = defaultOptions;
|
|
@@ -55,7 +66,7 @@ class SVGPathCommander {
|
|
|
55
66
|
if (roundOption === 'auto') {
|
|
56
67
|
const pathScale = (`${Math.floor(Math.max(width, height))}`).length;
|
|
57
68
|
round = pathScale >= 4 ? 0 : 4 - pathScale;
|
|
58
|
-
} else if (
|
|
69
|
+
} else if (Number.isInteger(roundOption) || roundOption === false) {
|
|
59
70
|
round = roundOption;
|
|
60
71
|
}
|
|
61
72
|
|
|
@@ -65,15 +76,14 @@ class SVGPathCommander {
|
|
|
65
76
|
origin = [
|
|
66
77
|
!Number.isNaN(originX) ? originX : cx,
|
|
67
78
|
!Number.isNaN(originY) ? originY : cy,
|
|
68
|
-
originZ
|
|
79
|
+
!Number.isNaN(originZ) ? originZ : cz,
|
|
69
80
|
];
|
|
70
81
|
} else {
|
|
71
82
|
origin = [cx, cy, cz];
|
|
72
83
|
}
|
|
73
84
|
|
|
74
85
|
/**
|
|
75
|
-
* @type {number |
|
|
76
|
-
* @default 4
|
|
86
|
+
* @type {number | false}
|
|
77
87
|
*/
|
|
78
88
|
this.round = round;
|
|
79
89
|
this.origin = origin;
|
|
@@ -81,6 +91,36 @@ class SVGPathCommander {
|
|
|
81
91
|
return this;
|
|
82
92
|
}
|
|
83
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Returns the path bounding box, equivalent to native `path.getBBox()`.
|
|
96
|
+
* @public
|
|
97
|
+
* @returns {SVGPath.pathBBox}
|
|
98
|
+
*/
|
|
99
|
+
getBBox() {
|
|
100
|
+
return getPathBBox(this.segments);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns the total path length, equivalent to native `path.getTotalLength()`.
|
|
105
|
+
* @public
|
|
106
|
+
* @returns {number}
|
|
107
|
+
*/
|
|
108
|
+
getTotalLength() {
|
|
109
|
+
return getTotalLength(this.segments);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns an `{x,y}` point in the path stroke at a given length,
|
|
114
|
+
* equivalent to the native `path.getPointAtLength()`.
|
|
115
|
+
*
|
|
116
|
+
* @public
|
|
117
|
+
* @param {number} length the length
|
|
118
|
+
* @returns {{x: number, y:number}} the requested point
|
|
119
|
+
*/
|
|
120
|
+
getPointAtLength(length) {
|
|
121
|
+
return getPointAtLength(this.segments, length);
|
|
122
|
+
}
|
|
123
|
+
|
|
84
124
|
/**
|
|
85
125
|
* Convert path to absolute values
|
|
86
126
|
* @public
|
|
@@ -172,16 +212,16 @@ class SVGPathCommander {
|
|
|
172
212
|
|
|
173
213
|
/**
|
|
174
214
|
* Transform path using values from an `Object` defined as `transformObject`.
|
|
175
|
-
* @see
|
|
215
|
+
* @see SVGPath.transformObject for a quick refference
|
|
176
216
|
*
|
|
177
|
-
* @param {
|
|
217
|
+
* @param {SVGPath.transformObject} source a `transformObject`as described above
|
|
178
218
|
* @public
|
|
179
219
|
*/
|
|
180
220
|
transform(source) {
|
|
181
221
|
if (!source || typeof source !== 'object' || (typeof source === 'object'
|
|
182
222
|
&& !['translate', 'rotate', 'skew', 'scale'].some((x) => x in source))) return this;
|
|
183
223
|
|
|
184
|
-
/** @type {
|
|
224
|
+
/** @type {SVGPath.transformObject} */
|
|
185
225
|
const transform = {};
|
|
186
226
|
Object.keys(source).forEach((fn) => {
|
|
187
227
|
// @ts-ignore
|
|
@@ -191,18 +231,18 @@ class SVGPathCommander {
|
|
|
191
231
|
|
|
192
232
|
// if origin is not specified
|
|
193
233
|
// it's important that we have one
|
|
234
|
+
const [cx, cy, cz] = this.origin;
|
|
194
235
|
const { origin } = transform;
|
|
195
|
-
|
|
236
|
+
|
|
237
|
+
if (Array.isArray(origin) && origin.length >= 2) {
|
|
196
238
|
const [originX, originY, originZ] = origin.map(Number);
|
|
197
|
-
const [cx, cy, cz] = this.origin;
|
|
198
239
|
transform.origin = [
|
|
199
240
|
!Number.isNaN(originX) ? originX : cx,
|
|
200
241
|
!Number.isNaN(originY) ? originY : cy,
|
|
201
242
|
originZ || cz,
|
|
202
243
|
];
|
|
203
244
|
} else {
|
|
204
|
-
|
|
205
|
-
transform.origin = { ...this.origin };
|
|
245
|
+
transform.origin = [cx, cy, cz];
|
|
206
246
|
}
|
|
207
247
|
|
|
208
248
|
this.segments = transformPath(segments, transform);
|
|
@@ -3,7 +3,7 @@ import getPropertiesAtPoint from './getPropertiesAtPoint';
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns the point in path closest to a given point.
|
|
5
5
|
*
|
|
6
|
-
* @param {string |
|
|
6
|
+
* @param {string | SVGPath.pathArray} pathInput target `pathArray`
|
|
7
7
|
* @param {{x: number, y: number}} point the given point
|
|
8
8
|
* @returns {{x: number, y: number}} the best match
|
|
9
9
|
*/
|
package/src/util/getCubicSize.js
CHANGED
|
@@ -11,7 +11,7 @@ import segmentCubicFactory from './segmentCubicFactory';
|
|
|
11
11
|
* @param {number} c2y the second control point Y
|
|
12
12
|
* @param {number} x2 the ending point X
|
|
13
13
|
* @param {number} y2 the ending point Y
|
|
14
|
-
* @returns {
|
|
14
|
+
* @returns {SVGPath.segmentLimits} the bounding box of the cubic-bezier segment
|
|
15
15
|
*/
|
|
16
16
|
export default function getCubicSize(x1, y1, c1x, c1y, c2x, c2y, x2, y2) {
|
|
17
17
|
let a = (c2x - 2 * c1x + x1) - (x2 - 2 * c2x + c1x);
|
|
@@ -5,7 +5,7 @@ import pathToCurve from '../convert/pathToCurve';
|
|
|
5
5
|
* Check if a path is drawn clockwise and returns true if so,
|
|
6
6
|
* false otherwise.
|
|
7
7
|
*
|
|
8
|
-
* @param {
|
|
8
|
+
* @param {SVGPath.pathArray} path the path string or `pathArray`
|
|
9
9
|
* @returns {boolean} true when clockwise or false if not
|
|
10
10
|
*/
|
|
11
11
|
export default function getDrawDirection(path) {
|
package/src/util/getPathArea.js
CHANGED
|
@@ -27,7 +27,7 @@ function getCubicSegArea(x1, y1, c1x, c1y, c2x, c2y, x2, y2) {
|
|
|
27
27
|
*
|
|
28
28
|
* @see https://github.com/paperjs/paper.js/blob/develop/src/path/Path.js
|
|
29
29
|
*
|
|
30
|
-
* @param {
|
|
30
|
+
* @param {SVGPath.pathArray} path the shape `pathArray`
|
|
31
31
|
* @returns {number} the length of the cubic-bezier segment
|
|
32
32
|
*/
|
|
33
33
|
export default function getPathArea(path) {
|
package/src/util/getPathBBox.js
CHANGED
|
@@ -4,8 +4,8 @@ import pathToCurve from '../convert/pathToCurve';
|
|
|
4
4
|
/**
|
|
5
5
|
* Returns the bounding box of a shape.
|
|
6
6
|
*
|
|
7
|
-
* @param {
|
|
8
|
-
* @returns {
|
|
7
|
+
* @param {SVGPath.pathArray} path the shape `pathArray`
|
|
8
|
+
* @returns {SVGPath.pathBBox} the length of the cubic-bezier segment
|
|
9
9
|
*/
|
|
10
10
|
export default function getPathBBox(path) {
|
|
11
11
|
if (!path) {
|
|
@@ -7,7 +7,7 @@ import pathToCurve from '../convert/pathToCurve';
|
|
|
7
7
|
* This is the `pathToCurve` version which is faster and more efficient for
|
|
8
8
|
* paths that are `curveArray`.
|
|
9
9
|
*
|
|
10
|
-
* @param {string |
|
|
10
|
+
* @param {string | SVGPath.curveArray} path the target `pathArray`
|
|
11
11
|
* @returns {number} the `curveArray` total length
|
|
12
12
|
*/
|
|
13
13
|
export default function getPathLength(path) {
|
|
@@ -3,7 +3,7 @@ import pathLengthFactory from './pathLengthFactory';
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns [x,y] coordinates of a point at a given length of a shape.
|
|
5
5
|
*
|
|
6
|
-
* @param {string |
|
|
6
|
+
* @param {string | SVGPath.pathArray} pathInput the `pathArray` to look into
|
|
7
7
|
* @param {number} distance the length of the shape to look at
|
|
8
8
|
* @returns {{x: number, y: number}} the requested {x, y} point coordinates
|
|
9
9
|
*/
|
|
@@ -5,9 +5,9 @@ import getTotalLength from './getTotalLength';
|
|
|
5
5
|
* Returns the segment, its index and length as well as
|
|
6
6
|
* the length to that segment at a given length in a path.
|
|
7
7
|
*
|
|
8
|
-
* @param {string |
|
|
8
|
+
* @param {string | SVGPath.pathArray} pathInput target `pathArray`
|
|
9
9
|
* @param {number=} distance the given length
|
|
10
|
-
* @returns {
|
|
10
|
+
* @returns {SVGPath.segmentProperties=} the requested properties
|
|
11
11
|
*/
|
|
12
12
|
export default function getPropertiesAtLength(pathInput, distance) {
|
|
13
13
|
const pathArray = parsePathString(pathInput);
|
|
@@ -19,7 +19,7 @@ export default function getPropertiesAtLength(pathInput, distance) {
|
|
|
19
19
|
let index = pathTemp.length - 1;
|
|
20
20
|
let lengthAtSegment = 0;
|
|
21
21
|
let length = 0;
|
|
22
|
-
/** @type {
|
|
22
|
+
/** @type {SVGPath.pathSegment} */
|
|
23
23
|
let segment = pathArray[0];
|
|
24
24
|
const [x, y] = segment.slice(-2);
|
|
25
25
|
const point = { x, y };
|
|
@@ -10,9 +10,9 @@ import normalizePath from '../process/normalizePath';
|
|
|
10
10
|
* the distance to the path stroke.
|
|
11
11
|
* @see https://bl.ocks.org/mbostock/8027637
|
|
12
12
|
*
|
|
13
|
-
* @param {string |
|
|
13
|
+
* @param {string | SVGPath.pathArray} pathInput target `pathArray`
|
|
14
14
|
* @param {{x: number, y: number}} point the given point
|
|
15
|
-
* @returns {
|
|
15
|
+
* @returns {SVGPath.pointProperties} the requested properties
|
|
16
16
|
*/
|
|
17
17
|
export default function getPropertiesAtPoint(pathInput, point) {
|
|
18
18
|
const path = fixPath(parsePathString(pathInput));
|
|
@@ -2,9 +2,9 @@ import getPropertiesAtLength from './getPropertiesAtLength';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns the segment at a given length.
|
|
5
|
-
* @param {string |
|
|
5
|
+
* @param {string | SVGPath.pathArray} pathInput the target `pathArray`
|
|
6
6
|
* @param {number} distance the distance in path to look at
|
|
7
|
-
* @returns {
|
|
7
|
+
* @returns {SVGPath.pathSegment?} the requested segment
|
|
8
8
|
*/
|
|
9
9
|
export default function getSegmentAtLength(pathInput, distance) {
|
|
10
10
|
const props = getPropertiesAtLength(pathInput, distance);
|
|
@@ -3,9 +3,9 @@ import getPropertiesAtPoint from './getPropertiesAtPoint';
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns the path segment which contains a given point.
|
|
5
5
|
*
|
|
6
|
-
* @param {string |
|
|
6
|
+
* @param {string | SVGPath.pathArray} path the `pathArray` to look into
|
|
7
7
|
* @param {{x: number, y: number}} point the point of the shape to look for
|
|
8
|
-
* @returns {
|
|
8
|
+
* @returns {SVGPath.pathSegment?} the requested segment
|
|
9
9
|
*/
|
|
10
10
|
export default function getSegmentOfPoint(path, point) {
|
|
11
11
|
const props = getPropertiesAtPoint(path, point);
|
|
@@ -6,7 +6,7 @@ import pathLengthFactory from './pathLengthFactory';
|
|
|
6
6
|
* The `normalizePath` version is lighter, faster, more efficient and more accurate
|
|
7
7
|
* with paths that are not `curveArray`.
|
|
8
8
|
*
|
|
9
|
-
* @param {string |
|
|
9
|
+
* @param {string | SVGPath.pathArray} pathInput the target `pathArray`
|
|
10
10
|
* @returns {number} the shape total length
|
|
11
11
|
*/
|
|
12
12
|
export default function getTotalLength(pathInput) {
|
|
@@ -4,7 +4,7 @@ import isPathArray from './isPathArray';
|
|
|
4
4
|
* Iterates an array to check if it's a `pathArray`
|
|
5
5
|
* with all absolute values.
|
|
6
6
|
*
|
|
7
|
-
* @param {string |
|
|
7
|
+
* @param {string | SVGPath.pathArray} path the `pathArray` to be checked
|
|
8
8
|
* @returns {boolean} iteration result
|
|
9
9
|
*/
|
|
10
10
|
export default function isAbsoluteArray(path) {
|
package/src/util/isCurveArray.js
CHANGED
|
@@ -4,7 +4,7 @@ import isPathArray from './isPathArray';
|
|
|
4
4
|
* Iterates an array to check if it's a `pathArray`
|
|
5
5
|
* with all C (cubic bezier) segments.
|
|
6
6
|
*
|
|
7
|
-
* @param {string |
|
|
7
|
+
* @param {string | SVGPath.pathArray} path the `Array` to be checked
|
|
8
8
|
* @returns {boolean} iteration result
|
|
9
9
|
*/
|
|
10
10
|
export default function isCurveArray(path) {
|
|
@@ -5,7 +5,7 @@ import isAbsoluteArray from './isAbsoluteArray';
|
|
|
5
5
|
* with all segments are in non-shorthand notation
|
|
6
6
|
* with absolute values.
|
|
7
7
|
*
|
|
8
|
-
* @param {string |
|
|
8
|
+
* @param {string | SVGPath.pathArray} path the `pathArray` to be checked
|
|
9
9
|
* @returns {boolean} iteration result
|
|
10
10
|
*/
|
|
11
11
|
export default function isNormalizedArray(path) {
|
package/src/util/isPathArray.js
CHANGED
|
@@ -3,7 +3,7 @@ import paramsCount from '../parser/paramsCount';
|
|
|
3
3
|
/**
|
|
4
4
|
* Iterates an array to check if it's an actual `pathArray`.
|
|
5
5
|
*
|
|
6
|
-
* @param {string |
|
|
6
|
+
* @param {string | SVGPath.pathArray} path the `pathArray` to be checked
|
|
7
7
|
* @returns {boolean} iteration result
|
|
8
8
|
*/
|
|
9
9
|
export default function isPathArray(path) {
|
|
@@ -3,8 +3,8 @@ import getPropertiesAtPoint from './getPropertiesAtPoint';
|
|
|
3
3
|
/**
|
|
4
4
|
* Checks if a given point is in the stroke of a path.
|
|
5
5
|
*
|
|
6
|
-
* @param {string |
|
|
7
|
-
* @param {{x: number, y: number}} point
|
|
6
|
+
* @param {string | SVGPath.pathArray} pathInput target path
|
|
7
|
+
* @param {{x: number, y: number}} point the given `{x,y}` point
|
|
8
8
|
* @returns {boolean} the query result
|
|
9
9
|
*/
|
|
10
10
|
export default function isPointInStroke(pathInput, point) {
|
|
@@ -4,7 +4,7 @@ import isPathArray from './isPathArray';
|
|
|
4
4
|
* Iterates an array to check if it's a `pathArray`
|
|
5
5
|
* with relative values.
|
|
6
6
|
*
|
|
7
|
-
* @param {string |
|
|
7
|
+
* @param {string | SVGPath.pathArray} path the `pathArray` to be checked
|
|
8
8
|
* @returns {boolean} iteration result
|
|
9
9
|
*/
|
|
10
10
|
export default function isRelativeArray(path) {
|
|
@@ -7,9 +7,10 @@ import segmentCubicFactory from './segmentCubicFactory';
|
|
|
7
7
|
import segmentQuadFactory from './segmentQuadFactory';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* Returns a {x,y} point at a given length
|
|
10
|
+
* Returns a {x,y} point at a given length
|
|
11
|
+
* of a shape or the shape total length.
|
|
11
12
|
*
|
|
12
|
-
* @param {string |
|
|
13
|
+
* @param {string | SVGPath.pathArray} pathInput the `pathArray` to look into
|
|
13
14
|
* @param {number=} distance the length of the shape to look at
|
|
14
15
|
* @returns {{x: number, y: number} | number} the total length or point
|
|
15
16
|
*/
|
|
@@ -2,7 +2,7 @@ import segmentCubicFactory from './segmentCubicFactory';
|
|
|
2
2
|
import arcToCubic from '../process/arcToCubic';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Returns the length of
|
|
5
|
+
* Returns the length of an A (arc-to) segment
|
|
6
6
|
* or an {x,y} point at a given length.
|
|
7
7
|
*
|
|
8
8
|
* @param {number} X1 the starting x position
|
|
@@ -29,7 +29,7 @@ function getPointAtCubicSegmentLength(x1, y1, c1x, c1y, c2x, c2y, x2, y2, t) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* Returns the length of a C (cubic-bezier) segment
|
|
32
|
+
* Returns the length of a C (cubic-bezier) segment
|
|
33
33
|
* or an {x,y} point at a given length.
|
|
34
34
|
*
|
|
35
35
|
* @param {number} x1 the starting point X
|
|
@@ -2,7 +2,7 @@ import midPoint from '../math/midPoint';
|
|
|
2
2
|
import distanceSquareRoot from '../math/distanceSquareRoot';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Returns the length of a line (L,V,H,Z) segment
|
|
5
|
+
* Returns the length of a line (L,V,H,Z) segment
|
|
6
6
|
* or a point at a given length.
|
|
7
7
|
*
|
|
8
8
|
* @param {number} x1 the starting point X
|
|
@@ -28,7 +28,7 @@ function getPointAtQuadSegmentLength(x1, y1, cx, cy, x2, y2, t) {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Returns the Q (quadratic-bezier) segment length
|
|
31
|
+
* Returns the Q (quadratic-bezier) segment length
|
|
32
32
|
* or an {x,y} point at a given length.
|
|
33
33
|
*
|
|
34
34
|
* @param {number} x1 the starting point X
|