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
package/README.md
CHANGED
|
@@ -40,15 +40,25 @@ npm install svg-path-commander
|
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
# Quick Guide
|
|
43
|
+
|
|
44
|
+
Flip a path on the X axis:
|
|
43
45
|
```js
|
|
44
46
|
import SVGPathCommander from 'svg-path-commander';
|
|
45
47
|
|
|
46
48
|
const path = 'M0 0 L50 100';
|
|
47
49
|
|
|
48
|
-
// flip a path on X axis
|
|
49
50
|
const flippedPathString = new SVGPathCommander(path).flipX().toString();
|
|
51
|
+
// result => 'M0 100L50 0'
|
|
52
|
+
```
|
|
50
53
|
|
|
51
|
-
|
|
54
|
+
Optimize a path string for best outcome by using the `round: 'auto'` option which will determine the amount of decimals based on the shape's bounding box:
|
|
55
|
+
```js
|
|
56
|
+
const optimizedPathString = new SVGPathCommander(path, {round: 'auto'}).optimize().toString();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Or why not apply a **2D transformation** and even a **3D transformation**:
|
|
60
|
+
```js
|
|
61
|
+
// a transform object
|
|
52
62
|
const transform = {
|
|
53
63
|
translate: 15, // X axis translation
|
|
54
64
|
rotate: 15, // Z axis rotation
|
|
@@ -67,16 +77,45 @@ const transform = {
|
|
|
67
77
|
origin: [15, 15, 15] // full `transform-origin` for a typical 3D transformation
|
|
68
78
|
}
|
|
69
79
|
const transformed3DPathString = new SVGPathCommander(path).transform(transform).toString();
|
|
80
|
+
```
|
|
70
81
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
82
|
+
SVGPathCommander comes with a full range of additional static methods, here's how to normalize a path:
|
|
83
|
+
```js
|
|
84
|
+
const path = 'M0 0 H50';
|
|
85
|
+
|
|
86
|
+
const normalizedPath = SVGPathCommander.normalizePath(path);
|
|
87
|
+
// result => [['M', 0, 0], ['L', 50, 0]]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Reverse a path:
|
|
91
|
+
```js
|
|
92
|
+
const path = 'M0 0 H50';
|
|
74
93
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
94
|
+
const reversedPath = SVGPathCommander.reversePath(path);
|
|
95
|
+
// result => [['M', 50, 0], ['H', 0]]
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Export to string:
|
|
99
|
+
```js
|
|
100
|
+
const myPathString = SVGPathCommander.pathToString([['M', 0, 0], ['L', 50, 0]]);
|
|
101
|
+
// result => 'M0 0 L50 0'
|
|
102
|
+
```
|
|
78
103
|
|
|
79
|
-
|
|
104
|
+
|
|
105
|
+
Check a path string validity:
|
|
106
|
+
```js
|
|
107
|
+
SVGPathCommander.isValidPath(path);
|
|
108
|
+
// result => boolean
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Convert a shape to `<path>` and transfer all non-specific attributes
|
|
112
|
+
```js
|
|
113
|
+
const myCircle = document.getElementById('myCircle');
|
|
114
|
+
SVGPathCommander.shapeToPath(myCircle, true);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Alternatively you can create `<path>` from specific attributes:
|
|
118
|
+
```js
|
|
80
119
|
const myRectAttr = {
|
|
81
120
|
type: 'rect',
|
|
82
121
|
x: 25,
|
|
@@ -90,8 +129,26 @@ const myRectPath = SVGPathCommander.shapeToPath(myRectAttr);
|
|
|
90
129
|
document.getElementById('mySVG').append(myRectPath);
|
|
91
130
|
```
|
|
92
131
|
|
|
132
|
+
Get the path length:
|
|
133
|
+
```js
|
|
134
|
+
const myPathLength = SVGPathCommander.getTotalLength('M0 0L50 0L25 50z');
|
|
135
|
+
// result => 161.80339887498948
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Get a point along the path:
|
|
139
|
+
```js
|
|
140
|
+
const myPoint = SVGPathCommander.getPointAtLength('M0 0L50 0L25 50z', 85);
|
|
141
|
+
// result => {x: 34.34752415750147, y: 31.304951684997057}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Get the path bounding box:
|
|
145
|
+
```js
|
|
146
|
+
const myPathBBox = SVGPathCommander.getPathBBox('M0 0L50 0L25 50z');
|
|
147
|
+
// result => {width: 50, height: 50, x: 0, y: 0, x2: 50, y2: 50, cx: 25, cy: 25, cz: 75}
|
|
148
|
+
```
|
|
149
|
+
|
|
93
150
|
# WIKI
|
|
94
|
-
For developer guidelines, head over to the [wiki pages](https://github.com/thednp/svg-path-commander/wiki).
|
|
151
|
+
For developer guidelines, and a complete list of static methods, head over to the [wiki pages](https://github.com/thednp/svg-path-commander/wiki).
|
|
95
152
|
|
|
96
153
|
|
|
97
154
|
# What Is It For?
|
|
@@ -110,7 +167,8 @@ For developer guidelines, head over to the [wiki pages](https://github.com/thedn
|
|
|
110
167
|
* all 3d transformations as well as skews will convert `A` (arc) path commands to `C` (cubic bezier) due to the lack of resources;
|
|
111
168
|
* most tools included with **SVGPathCommander** should work in your Node.js apps, but feel free to report any issue;
|
|
112
169
|
* other path commands like `R` (catmulRomBezier), `O`, `U` (ellipse and shorthand ellipse) are not present in the current draft and are not supported;
|
|
113
|
-
* normalization can mean many things to many people and our library is developed to convert path command values to absolute and shorthand to
|
|
170
|
+
* normalization can mean many things to many people and our library is developed to convert path command values to absolute and shorthand to longshorthand commands to provide a solid foundation for the main processing tools of our library;
|
|
171
|
+
* when compared to the native methods like `SVGPathElement.getTotalLength()` or `SVGPathElement.getPointAtLength()`, the output of our static methods is within a [0.002 - 0.05] margin delta, but from our experience it's proven to be a more consistent outcome.
|
|
114
172
|
|
|
115
173
|
|
|
116
174
|
# Special Thanks
|
|
@@ -119,8 +177,10 @@ For developer guidelines, head over to the [wiki pages](https://github.com/thedn
|
|
|
119
177
|
* Vitaly Puzrin & Alex Kocharin for their [SvgPath](https://github.com/fontello/svgpath)
|
|
120
178
|
* Jürg Lehni & Jonathan Puckey for their [Paper.js](https://github.com/paperjs/paper.js/)
|
|
121
179
|
* Andrew Willems for his [awesome guide](https://stackoverflow.com/users/5218951/andrew-willems)
|
|
122
|
-
* Mike 'Pomax' Kamermans for his awesome [svg-path-reverse](https://github.com/Pomax/svg-path-reverse)
|
|
180
|
+
* Mike 'Pomax' Kamermans for his awesome [svg-path-reverse](https://github.com/Pomax/svg-path-reverse), [bezierjs](https://github.com/Pomax/bezierjs) and [bezierinfo](https://pomax.github.io/bezierinfo/)
|
|
123
181
|
* Nicolas Debeissat for the inspiration on [svg3d](https://github.com/ndebeiss/svg3d)
|
|
182
|
+
* Mike Bostock for his awesome [closestPoint](https://bl.ocks.org/mbostock/8027637)
|
|
183
|
+
* James Halliday for his excelent [point-at-length](https://github.com/substack/point-at-length)
|
|
124
184
|
|
|
125
185
|
# License
|
|
126
186
|
**SVGPathCommander** is released under [MIT Licence](https://github.com/thednp/svg-path-commander/blob/master/LICENSE).
|