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.
Files changed (48) hide show
  1. package/README.md +72 -12
  2. package/dist/svg-path-commander.esm.js +647 -190
  3. package/dist/svg-path-commander.esm.min.js +2 -2
  4. package/dist/svg-path-commander.js +670 -189
  5. package/dist/svg-path-commander.min.js +2 -2
  6. package/package.json +1 -1
  7. package/src/convert/pathToAbsolute.js +3 -2
  8. package/src/convert/pathToCurve.js +2 -1
  9. package/src/convert/pathToRelative.js +2 -1
  10. package/src/parser/paramsCount.js +1 -0
  11. package/src/parser/parsePathString.js +4 -3
  12. package/src/parser/pathParser.js +2 -1
  13. package/src/parser/scanSegment.js +0 -1
  14. package/src/process/fixPath.js +1 -1
  15. package/src/process/lineToCubic.js +4 -5
  16. package/src/process/normalizePath.js +4 -2
  17. package/src/util/getClosestPoint.js +12 -0
  18. package/src/util/getCubicSize.js +41 -39
  19. package/src/util/getPathArea.js +19 -19
  20. package/src/util/getPathBBox.js +0 -11
  21. package/src/util/getPathLength.js +9 -9
  22. package/src/util/getPointAtLength.js +6 -29
  23. package/src/util/getPointAtPathLength.js +44 -0
  24. package/src/util/getPropertiesAtLength.js +62 -0
  25. package/src/util/getPropertiesAtPoint.js +77 -0
  26. package/src/util/getSegmentAtLength.js +13 -0
  27. package/src/util/getSegmentOfPoint.js +14 -0
  28. package/src/util/getTotalLength.js +15 -0
  29. package/src/util/isAbsoluteArray.js +2 -1
  30. package/src/util/isCurveArray.js +2 -1
  31. package/src/util/isNormalizedArray.js +2 -1
  32. package/src/util/isPointInStroke.js +13 -0
  33. package/src/util/isRelativeArray.js +2 -1
  34. package/src/util/pathLengthFactory.js +99 -0
  35. package/src/util/segmentArcFactory.js +42 -0
  36. package/src/util/segmentCubicFactory.js +73 -0
  37. package/src/util/segmentLineFactory.js +30 -0
  38. package/src/util/segmentQuadFactory.js +70 -0
  39. package/src/util/util.js +19 -1
  40. package/types/index.d.ts +14 -5
  41. package/types/more/modules.ts +17 -5
  42. package/types/more/svg.d.ts +21 -0
  43. package/types/svg-path-commander.d.ts +242 -122
  44. package/src/util/getPointAtSegLength.js +0 -28
  45. package/src/util/getSegArcLength.js +0 -27
  46. package/src/util/getSegCubicLength.js +0 -52
  47. package/src/util/getSegLineLength.js +0 -14
  48. 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
- // apply a 2D transformation
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
- // 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();
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
- // convert a shape to `<path>` and transfer all non-specific attributes
76
- const circle = document.getElementById('myCircle');
77
- SVGPathCommander.shapeToPath(circle, true);
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
- // alternatively you can create <path> from specific attributes
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 non-shorthand commands to provide a solid foundation for the main processing tools of our library.
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) and [bezierjs](https://github.com/Pomax/bezierjs)
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).