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.
Files changed (115) hide show
  1. package/README.md +59 -15
  2. package/dist/svg-path-commander.esm.js +679 -638
  3. package/dist/svg-path-commander.esm.min.js +2 -2
  4. package/dist/svg-path-commander.js +701 -656
  5. package/dist/svg-path-commander.min.js +2 -2
  6. package/package.json +3 -2
  7. package/src/convert/pathToAbsolute.js +43 -59
  8. package/src/convert/pathToCurve.js +16 -11
  9. package/src/convert/pathToRelative.js +51 -57
  10. package/src/convert/pathToString.js +1 -1
  11. package/src/math/distanceSquareRoot.js +3 -3
  12. package/src/math/polygonLength.js +1 -0
  13. package/src/options/options.js +5 -6
  14. package/src/parser/finalizeSegment.js +4 -0
  15. package/src/parser/isPathCommand.js +1 -1
  16. package/src/parser/paramsCount.js +1 -1
  17. package/src/parser/paramsParser.js +8 -0
  18. package/src/parser/parsePathString.js +5 -2
  19. package/src/parser/pathParser.js +1 -0
  20. package/src/parser/scanFlag.js +1 -1
  21. package/src/parser/scanSegment.js +1 -0
  22. package/src/process/arcToCubic.js +0 -2
  23. package/src/process/clonePath.js +2 -7
  24. package/src/process/fixArc.js +2 -3
  25. package/src/process/fixPath.js +7 -6
  26. package/src/process/getSVGMatrix.js +12 -12
  27. package/src/process/normalizePath.js +9 -11
  28. package/src/process/normalizeSegment.js +10 -6
  29. package/src/process/optimizePath.js +51 -18
  30. package/src/process/reverseCurve.js +5 -5
  31. package/src/process/reversePath.js +17 -13
  32. package/src/process/roundPath.js +16 -32
  33. package/src/process/segmentToCubic.js +19 -17
  34. package/src/process/shortenSegment.js +36 -28
  35. package/src/process/splitCubic.js +5 -1
  36. package/src/process/transformPath.js +12 -7
  37. package/src/svg-path-commander.js +65 -38
  38. package/src/util/createPath.js +4 -4
  39. package/src/util/getDrawDirection.js +1 -1
  40. package/src/util/getPathArea.js +3 -8
  41. package/src/util/getPathBBox.js +4 -4
  42. package/src/util/getPathLength.js +1 -2
  43. package/src/util/getPointAtLength.js +1 -3
  44. package/src/util/getSegArcLength.js +4 -5
  45. package/src/util/isAbsoluteArray.js +2 -2
  46. package/src/util/isCurveArray.js +2 -3
  47. package/src/util/isNormalizedArray.js +3 -7
  48. package/src/util/isPathArray.js +1 -1
  49. package/src/util/isRelativeArray.js +2 -2
  50. package/src/util/shapeToPath.js +11 -11
  51. package/src/util/util.js +5 -4
  52. package/types/index.d.ts +98 -112
  53. package/types/more/modules.ts +75 -0
  54. package/types/more/svg.d.ts +183 -0
  55. package/types/svg-path-commander.d.ts +1076 -17
  56. package/types/convert/pathToAbsolute.d.ts +0 -1
  57. package/types/convert/pathToCurve.d.ts +0 -1
  58. package/types/convert/pathToRelative.d.ts +0 -1
  59. package/types/convert/pathToString.d.ts +0 -1
  60. package/types/math/epsilon.d.ts +0 -2
  61. package/types/math/midPoint.d.ts +0 -1
  62. package/types/math/rotateVector.d.ts +0 -4
  63. package/types/options/options.d.ts +0 -6
  64. package/types/parser/finalizeSegment.d.ts +0 -1
  65. package/types/parser/invalidPathValue.d.ts +0 -2
  66. package/types/parser/isArcCommand.d.ts +0 -1
  67. package/types/parser/isDigit.d.ts +0 -1
  68. package/types/parser/isDigitStart.d.ts +0 -1
  69. package/types/parser/isPathCommand.d.ts +0 -1
  70. package/types/parser/isSpace.d.ts +0 -1
  71. package/types/parser/paramsCount.d.ts +0 -4
  72. package/types/parser/parsePathString.d.ts +0 -1
  73. package/types/parser/pathParser.d.ts +0 -12
  74. package/types/parser/scanFlag.d.ts +0 -1
  75. package/types/parser/scanParam.d.ts +0 -1
  76. package/types/parser/scanSegment.d.ts +0 -1
  77. package/types/parser/skipSpaces.d.ts +0 -1
  78. package/types/process/arcToCubic.d.ts +0 -1
  79. package/types/process/clonePath.d.ts +0 -1
  80. package/types/process/fixArc.d.ts +0 -1
  81. package/types/process/fixPath.d.ts +0 -1
  82. package/types/process/getSVGMatrix.d.ts +0 -2
  83. package/types/process/lineToCubic.d.ts +0 -1
  84. package/types/process/normalizePath.d.ts +0 -1
  85. package/types/process/normalizeSegment.d.ts +0 -1
  86. package/types/process/optimizePath.d.ts +0 -1
  87. package/types/process/projection2d.d.ts +0 -1
  88. package/types/process/quadToCubic.d.ts +0 -1
  89. package/types/process/reverseCurve.d.ts +0 -1
  90. package/types/process/reversePath.d.ts +0 -1
  91. package/types/process/roundPath.d.ts +0 -1
  92. package/types/process/segmentToCubic.d.ts +0 -1
  93. package/types/process/shortenSegment.d.ts +0 -1
  94. package/types/process/shorthandToCubic.d.ts +0 -4
  95. package/types/process/shorthandToQuad.d.ts +0 -4
  96. package/types/process/splitPath.d.ts +0 -1
  97. package/types/process/transformEllipse.d.ts +0 -5
  98. package/types/process/transformPath.d.ts +0 -1
  99. package/types/util/getCubicSize.d.ts +0 -1
  100. package/types/util/getDrawDirection.d.ts +0 -1
  101. package/types/util/getPathArea.d.ts +0 -1
  102. package/types/util/getPathBBox.d.ts +0 -1
  103. package/types/util/getPathLength.d.ts +0 -1
  104. package/types/util/getPointAtLength.d.ts +0 -1
  105. package/types/util/getPointAtSegLength.d.ts +0 -4
  106. package/types/util/getSegCubicLength.d.ts +0 -1
  107. package/types/util/isAbsoluteArray.d.ts +0 -1
  108. package/types/util/isCurveArray.d.ts +0 -1
  109. package/types/util/isNormalizedArray.d.ts +0 -1
  110. package/types/util/isPathArray.d.ts +0 -1
  111. package/types/util/isRelativeArray.d.ts +0 -1
  112. package/types/util/isValidPath.d.ts +0 -1
  113. package/types/util/shapeToPath.d.ts +0 -6
  114. package/types/util/util.d.ts +0 -63
  115. package/types/util/version.d.ts +0 -2
package/README.md CHANGED
@@ -1,6 +1,10 @@
1
- # SVG Path Commander
1
+ ![image](./assets/SVGPathCommander.svg)
2
2
 
3
- A modern set of ES6+ JavaScript tools for manipulating the `d` (description) attribute for *SVGPathElement* items, and is implementing modern JavaScript API to produce reusable path strings with lossless quality.
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
+ [![NPM Version](https://img.shields.io/npm/v/svg-path-commander.svg?style=flat-square)](https://www.npmjs.com/package/svg-path-commander)
6
+ [![NPM Downloads](https://img.shields.io/npm/dm/svg-path-commander.svg?style=flat-square)](http://npm-stat.com/charts.html?svg-path-commander)
7
+ [![jsDeliver](https://data.jsdelivr.com/v1/package/npm/svg-path-commander/badge)](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
- * you can use this library in both web apps and Node.js, where others are restricted to a single environment;
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
- * as you will see below, our library can create 3D to 2D projections, making your SVGs look like 3D but in the SVG coordinate system.
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
- [![NPM Version](https://img.shields.io/npm/v/svg-path-commander.svg?style=flat-square)](https://www.npmjs.com/package/svg-path-commander)
25
- [![NPM Downloads](https://img.shields.io/npm/dm/svg-path-commander.svg?style=flat-square)](http://npm-stat.com/charts.html?svg-path-commander)
26
- [![jsDeliver](https://data.jsdelivr.com/v1/package/npm/svg-path-commander/badge)](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
- const flippedPath = new SVGPathCommander(path).flipX().toString();
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
- * font-icon creators can use it in both Node.js and web applications to process and test their creations.
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 2 or more cubic-bezier segments into one or more arc segments); however, it will try to provide shorthand notations where possible, pick the shortest string for each segment; while computing path command values, the script will try to deliver the best possible outcome;
64
- * all tools processing path segments will always round float values to 3 decimals; EG: 0.5666 => 0.566, 0.50 => 0.5; you can change the default option with `SVGPathCommander.options.decimals = 2` or remove the value rounding all together with `SVGPathCommander.options.round = 0`; you can also control this feature via instance options;
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 on 3D to 2D projection;
67
- * most tools included with **SVGPathCommander** should work in your Node.js apps, but if you choose to use other complimentary 3rd party libraries, make sure you have the proper tools for them;
68
- * other path commands like `R` (catmulRomBezier), `O`, `U` (ellipse and shorthand ellipse) are not present in the current draft and they are not supported;
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