svg-path-commander 0.1.11-alpha4 → 0.1.16
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 +59 -15
- package/dist/svg-path-commander.esm.js +679 -638
- package/dist/svg-path-commander.esm.min.js +2 -2
- package/dist/svg-path-commander.js +701 -656
- package/dist/svg-path-commander.min.js +2 -2
- package/package.json +3 -2
- package/src/convert/pathToAbsolute.js +43 -59
- package/src/convert/pathToCurve.js +16 -11
- package/src/convert/pathToRelative.js +51 -57
- package/src/convert/pathToString.js +1 -1
- package/src/math/distanceSquareRoot.js +3 -3
- package/src/math/polygonLength.js +1 -0
- package/src/options/options.js +5 -6
- package/src/parser/finalizeSegment.js +4 -0
- package/src/parser/isPathCommand.js +1 -1
- package/src/parser/paramsCount.js +1 -1
- package/src/parser/paramsParser.js +8 -0
- package/src/parser/parsePathString.js +5 -2
- package/src/parser/pathParser.js +1 -0
- package/src/parser/scanFlag.js +1 -1
- package/src/parser/scanSegment.js +1 -0
- package/src/process/arcToCubic.js +0 -2
- package/src/process/clonePath.js +2 -7
- package/src/process/fixArc.js +2 -3
- package/src/process/fixPath.js +7 -6
- package/src/process/getSVGMatrix.js +12 -12
- package/src/process/normalizePath.js +9 -11
- package/src/process/normalizeSegment.js +10 -6
- package/src/process/optimizePath.js +51 -18
- package/src/process/reverseCurve.js +5 -5
- package/src/process/reversePath.js +17 -13
- package/src/process/roundPath.js +16 -32
- package/src/process/segmentToCubic.js +19 -17
- package/src/process/shortenSegment.js +36 -28
- package/src/process/splitCubic.js +5 -1
- package/src/process/transformPath.js +12 -7
- package/src/svg-path-commander.js +65 -38
- package/src/util/createPath.js +4 -4
- package/src/util/getDrawDirection.js +1 -1
- package/src/util/getPathArea.js +3 -8
- package/src/util/getPathBBox.js +4 -4
- package/src/util/getPathLength.js +1 -2
- package/src/util/getPointAtLength.js +1 -3
- package/src/util/getSegArcLength.js +4 -5
- package/src/util/isAbsoluteArray.js +2 -2
- package/src/util/isCurveArray.js +2 -3
- package/src/util/isNormalizedArray.js +3 -7
- package/src/util/isPathArray.js +1 -1
- package/src/util/isRelativeArray.js +2 -2
- package/src/util/shapeToPath.js +11 -11
- package/src/util/util.js +5 -4
- package/types/index.d.ts +98 -112
- package/types/more/modules.ts +75 -0
- package/types/more/svg.d.ts +183 -0
- package/types/svg-path-commander.d.ts +1076 -17
- package/types/convert/pathToAbsolute.d.ts +0 -1
- package/types/convert/pathToCurve.d.ts +0 -1
- package/types/convert/pathToRelative.d.ts +0 -1
- package/types/convert/pathToString.d.ts +0 -1
- package/types/math/epsilon.d.ts +0 -2
- package/types/math/midPoint.d.ts +0 -1
- package/types/math/rotateVector.d.ts +0 -4
- package/types/options/options.d.ts +0 -6
- package/types/parser/finalizeSegment.d.ts +0 -1
- package/types/parser/invalidPathValue.d.ts +0 -2
- package/types/parser/isArcCommand.d.ts +0 -1
- package/types/parser/isDigit.d.ts +0 -1
- package/types/parser/isDigitStart.d.ts +0 -1
- package/types/parser/isPathCommand.d.ts +0 -1
- package/types/parser/isSpace.d.ts +0 -1
- package/types/parser/paramsCount.d.ts +0 -4
- package/types/parser/parsePathString.d.ts +0 -1
- package/types/parser/pathParser.d.ts +0 -12
- package/types/parser/scanFlag.d.ts +0 -1
- package/types/parser/scanParam.d.ts +0 -1
- package/types/parser/scanSegment.d.ts +0 -1
- package/types/parser/skipSpaces.d.ts +0 -1
- package/types/process/arcToCubic.d.ts +0 -1
- package/types/process/clonePath.d.ts +0 -1
- package/types/process/fixArc.d.ts +0 -1
- package/types/process/fixPath.d.ts +0 -1
- package/types/process/getSVGMatrix.d.ts +0 -2
- package/types/process/lineToCubic.d.ts +0 -1
- package/types/process/normalizePath.d.ts +0 -1
- package/types/process/normalizeSegment.d.ts +0 -1
- package/types/process/optimizePath.d.ts +0 -1
- package/types/process/projection2d.d.ts +0 -1
- package/types/process/quadToCubic.d.ts +0 -1
- package/types/process/reverseCurve.d.ts +0 -1
- package/types/process/reversePath.d.ts +0 -1
- package/types/process/roundPath.d.ts +0 -1
- package/types/process/segmentToCubic.d.ts +0 -1
- package/types/process/shortenSegment.d.ts +0 -1
- package/types/process/shorthandToCubic.d.ts +0 -4
- package/types/process/shorthandToQuad.d.ts +0 -4
- package/types/process/splitPath.d.ts +0 -1
- package/types/process/transformEllipse.d.ts +0 -5
- package/types/process/transformPath.d.ts +0 -1
- package/types/util/getCubicSize.d.ts +0 -1
- package/types/util/getDrawDirection.d.ts +0 -1
- package/types/util/getPathArea.d.ts +0 -1
- package/types/util/getPathBBox.d.ts +0 -1
- package/types/util/getPathLength.d.ts +0 -1
- package/types/util/getPointAtLength.d.ts +0 -1
- package/types/util/getPointAtSegLength.d.ts +0 -4
- package/types/util/getSegCubicLength.d.ts +0 -1
- package/types/util/isAbsoluteArray.d.ts +0 -1
- package/types/util/isCurveArray.d.ts +0 -1
- package/types/util/isNormalizedArray.d.ts +0 -1
- package/types/util/isPathArray.d.ts +0 -1
- package/types/util/isRelativeArray.d.ts +0 -1
- package/types/util/isValidPath.d.ts +0 -1
- package/types/util/shapeToPath.d.ts +0 -6
- package/types/util/util.d.ts +0 -63
- package/types/util/version.d.ts +0 -2
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
2
|
|
|
3
|
-
A modern set of ES6+ JavaScript tools for manipulating the `d` (description) attribute for *SVGPathElement* items
|
|
3
|
+
A modern set of ES6+ JavaScript tools for manipulating the `d` (description) attribute for *SVGPathElement* items. The library is implementing modern JavaScript API to produce reusable path strings with lossless quality. In addition, you also have a powerful tool to convert other SVG shapes like `<circle>` or `<rect>` to `<path>`.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/svg-path-commander)
|
|
6
|
+
[](http://npm-stat.com/charts.html?svg-path-commander)
|
|
7
|
+
[](https://www.jsdelivr.com/package/npm/svg-path-commander)
|
|
4
8
|
|
|
5
9
|
While you may find familiar tools inside, this library brings ***new additions***:
|
|
6
10
|
* a tool that can *reverse path draw direction* without altering path commands, even with specific shorthand path commands;
|
|
@@ -9,9 +13,10 @@ While you may find familiar tools inside, this library brings ***new additions**
|
|
|
9
13
|
|
|
10
14
|
**The key differences with other libraries**:
|
|
11
15
|
* ES6+ sourced with modernized codebase and build tools; all inherited codebase has been modernized as well;
|
|
12
|
-
*
|
|
16
|
+
* along with the modern codebase, the library also comes with strong TypeScript definitions;
|
|
17
|
+
* you can use this library in both web apps and Node.js, you are not restricted to a single environment;
|
|
13
18
|
* path command transformations are all consistent with the SVG coordinates system, where others compute transform origin only for rotation transformation;
|
|
14
|
-
*
|
|
19
|
+
* this library can create 3D to 2D projections, making your SVGs look like 3D but in the SVG coordinate system.
|
|
15
20
|
|
|
16
21
|
**SVGPathCommander** implements the [DOMMatrix API](https://developer.mozilla.org/en-US/docs/Web/API/DOMMatrix) for *SVGPathElement* path command transformation and falls back to a modernized [CSSMatrix shim](https://github.com/thednp/DOMMatrix) on older browsers as well as Node.js.
|
|
17
22
|
There are a couple of good reasons for this implementation:
|
|
@@ -21,10 +26,6 @@ There are a couple of good reasons for this implementation:
|
|
|
21
26
|
|
|
22
27
|
This library is available on [CDN](https://www.jsdelivr.com/package/npm/svg-path-commander) and [npm](https://www.npmjs.com/package/svg-path-commander).
|
|
23
28
|
|
|
24
|
-
[](https://www.npmjs.com/package/svg-path-commander)
|
|
25
|
-
[](http://npm-stat.com/charts.html?svg-path-commander)
|
|
26
|
-
[](https://www.jsdelivr.com/package/npm/svg-path-commander)
|
|
27
|
-
|
|
28
29
|
|
|
29
30
|
# Install
|
|
30
31
|
|
|
@@ -44,7 +45,49 @@ import SVGPathCommander from 'svg-path-commander';
|
|
|
44
45
|
|
|
45
46
|
const path = 'M0 0 L50 100';
|
|
46
47
|
|
|
47
|
-
|
|
48
|
+
// flip a path on X axis
|
|
49
|
+
const flippedPathString = new SVGPathCommander(path).flipX().toString();
|
|
50
|
+
|
|
51
|
+
// apply a 2D transformation
|
|
52
|
+
const transform = {
|
|
53
|
+
translate: 15, // X axis translation
|
|
54
|
+
rotate: 15, // Z axis rotation
|
|
55
|
+
scale: 0.75, // uniform scale on X, Y, Z axis
|
|
56
|
+
skew: 15, // skew 15deg on the X axis
|
|
57
|
+
origin: [15, 0] // if not specified, it will calculate a bounding box to determine a proper `transform-origin`
|
|
58
|
+
}
|
|
59
|
+
const transformed2DPathString = new SVGPathCommander(path).transform(transform).toString();
|
|
60
|
+
|
|
61
|
+
// apply a 3D transformation
|
|
62
|
+
const transform = {
|
|
63
|
+
translate: [15, 15, 15], // `[15, 15]` would apply a 2D translation, and only `15` for X axis translation
|
|
64
|
+
rotate: [15, 15, 15], // or only "15" for 2D rotation on Z axis
|
|
65
|
+
scale: [0.7, 0.75, 0.8], // or only "0.7" for 2D scale on all X, Y, Z axis
|
|
66
|
+
skew: [15, 15], // or only "15" for the X axis
|
|
67
|
+
origin: [15, 15, 15] // full `transform-origin` for a typical 3D transformation
|
|
68
|
+
}
|
|
69
|
+
const transformed3DPathString = new SVGPathCommander(path).transform(transform).toString();
|
|
70
|
+
|
|
71
|
+
// optimize a path string for best outcome by using the `round: 'auto'` option
|
|
72
|
+
// which will determine the amount of decimals based on the shape's bounding box
|
|
73
|
+
const optimizedPathString = new SVGPathCommander(path, {round: 'auto'}).optimize().toString();
|
|
74
|
+
|
|
75
|
+
// convert a shape to `<path>` and transfer all non-specific attributes
|
|
76
|
+
const circle = document.getElementById('myCircle');
|
|
77
|
+
SVGPathCommander.shapeToPath(circle, true);
|
|
78
|
+
|
|
79
|
+
// alternatively you can create <path> from specific attributes
|
|
80
|
+
const myRectAttr = {
|
|
81
|
+
type: 'rect',
|
|
82
|
+
x: 25,
|
|
83
|
+
y: 25,
|
|
84
|
+
width: 50,
|
|
85
|
+
height: 50,
|
|
86
|
+
rx: 5
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const myRectPath = SVGPathCommander.shapeToPath(myRectAttr);
|
|
90
|
+
document.getElementById('mySVG').append(myRectPath);
|
|
48
91
|
```
|
|
49
92
|
|
|
50
93
|
# WIKI
|
|
@@ -55,17 +98,18 @@ For developer guidelines, head over to the [wiki pages](https://github.com/thedn
|
|
|
55
98
|
|
|
56
99
|
* converting and optimizing *SVGPathElement* for use in third party application; our [KUTE.js](https://github.com/thednp/kute.js) animation engine is using it to process *SVGPathElement* coordinates for [SVG morphing](https://thednp.github.io/kute.js/svgMorph.html) and [SVG cubic morphing](https://thednp.github.io/kute.js/svgCubicMorph.html);
|
|
57
100
|
* animators that work with SVGs and need tools for performing specific path command processing;
|
|
58
|
-
*
|
|
101
|
+
* front-end developers looking to spice up the content by combining, splitting or transforming paths;
|
|
102
|
+
* font-icon creators can use it in both Node.js and web applications to process, optimize and test their creations.
|
|
59
103
|
|
|
60
104
|
|
|
61
105
|
# Technical Considerations
|
|
62
106
|
|
|
63
|
-
* the `optimize()` instance method will not merge path segments (for instance
|
|
64
|
-
* all tools processing path segments will
|
|
107
|
+
* the `optimize()` instance method will not merge path segments (for instance two or more cubic-bezier segments into one or more arc segments); however, the script will try to provide shorthand notations where possible, pick the shortest string for each segment, and generally try to deliver the best possible outcome;
|
|
108
|
+
* all tools processing path segments will never round float values, however `pathToString`, `optimizePath` and especially `roundPath` will always round values to the default of 4 decimals; EG: 0.56676 => 0.567, 0.50 => 0.5; you can change the default option with `SVGPathCommander.options.round = 2` or remove the value rounding all together with `SVGPathCommander.options.round = false`; you can also control this feature via instance options;
|
|
65
109
|
* the `getSVGMatrix` utility we developed will always compute the matrix by applying the transform functions in the following order: `translate`, `rotate`, `skew` and `scale`, which is the default composition/recomposition order specified in the W3C draft;
|
|
66
|
-
* all 3d transformations as well as skews will convert `A` (arc) path commands to `C` (cubic bezier) due to the lack of resources
|
|
67
|
-
* most tools included with **SVGPathCommander** should work in your Node.js apps, but
|
|
68
|
-
* other path commands like `R` (catmulRomBezier), `O`, `U` (ellipse and shorthand ellipse) are not present in the current draft and
|
|
110
|
+
* all 3d transformations as well as skews will convert `A` (arc) path commands to `C` (cubic bezier) due to the lack of resources;
|
|
111
|
+
* most tools included with **SVGPathCommander** should work in your Node.js apps, but feel free to report any issue;
|
|
112
|
+
* other path commands like `R` (catmulRomBezier), `O`, `U` (ellipse and shorthand ellipse) are not present in the current draft and are not supported;
|
|
69
113
|
* normalization can mean many things to many people and our library is developed to convert path command values to absolute and shorthand to non-shorthand commands to provide a solid foundation for the main processing tools of our library.
|
|
70
114
|
|
|
71
115
|
|