svg-path-simplify 0.2.6 → 0.3.0
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 +80 -5
- package/dist/svg-path-simplify.esm.js +465 -392
- package/dist/svg-path-simplify.esm.min.js +6 -6
- package/dist/svg-path-simplify.js +465 -392
- package/dist/svg-path-simplify.min.js +6 -6
- package/dist/svg-path-simplify.pathdata.esm.js +5094 -0
- package/dist/svg-path-simplify.pathdata.esm.min.js +9 -0
- package/dist/svg-path-simplify.poly.cjs +10 -1
- package/dist/svg-path-simplify.worker.js +27 -0
- package/dist/svg-path-simplify.worker.polyfills.js +32 -0
- package/index.html +90 -17
- package/package.json +3 -2
- package/src/index-pathdata.js +6 -0
- package/src/index-poly.js +9 -1
- package/src/index-worker.js +17 -0
- package/src/index.js +4 -1
- package/src/pathData_simplify_cubic_extrapolate.js +3 -2
- package/src/pathSimplify-main.js +54 -72
- package/src/pathSimplify-only-pathdata.js +274 -0
- package/src/svgii/geometry.js +48 -4
- package/src/svgii/pathData_analyze.js +8 -6
- package/src/svgii/pathData_convert.js +1 -1
- package/src/svgii/pathData_fix_directions.js +83 -0
- package/src/svgii/pathData_line_to_cubic.js +21 -0
- package/src/svgii/pathData_parse_els.js +7 -2
- package/src/svgii/pathData_remove_zerolength.js +0 -1
- package/src/svgii/pathData_reverse.js +0 -1
- package/src/svgii/pathData_toPolygon.js +61 -12
- package/src/svgii/rounding.js +2 -0
- package/src/svgii/svg-styles-to-attributes-const.js +2 -0
- package/src/svgii/svg_cleanup.js +92 -15
- package/tests/testSVG2.js +55 -0
- package/dist/svg-path-simplify.min.js.gz +0 -0
package/README.md
CHANGED
|
@@ -32,6 +32,7 @@ Unlike most existing approaches (e.g in graphic applications), it checks where s
|
|
|
32
32
|
* adaptive coordinate rounding: small or large details can be auto-detected to find a suitable floating point accuracy without guessing the decimal value (3 decimals may not be the silver bullet=)
|
|
33
33
|
* split segments at extremes – only useful for manual editing
|
|
34
34
|
* optimize either path data strings or SVG markup code
|
|
35
|
+
* create curves from polylines (curve-fitting)
|
|
35
36
|
|
|
36
37
|
|
|
37
38
|
## TOC
|
|
@@ -41,13 +42,15 @@ Unlike most existing approaches (e.g in graphic applications), it checks where s
|
|
|
41
42
|
- [ESM version](#esm-version)
|
|
42
43
|
+ [node.js](#nodejs)
|
|
43
44
|
- [Example 2: Apply options](#example-2-apply-options)
|
|
44
|
-
+ [
|
|
45
|
+
+ [Web worker](#web-worker)
|
|
46
|
+
* [API](#api)
|
|
45
47
|
+ [Simplification parameters](#simplification-parameters)
|
|
46
48
|
+ [Advanced simplifications](#advanced-simplifications)
|
|
47
49
|
+ [Output options](#output-options)
|
|
48
50
|
+ [SVG scaling](#svg-scaling)
|
|
49
51
|
+ [SVG output optimizations](#svg-output-optimizations)
|
|
50
52
|
+ [SVG input normalization](#svg-input-normalization)
|
|
53
|
+
* [Lite version](#lite-version–only-path-data)
|
|
51
54
|
* [Demos](#demos)
|
|
52
55
|
+ [Web app](#web-app)
|
|
53
56
|
+ [Demo files](#demo-files)
|
|
@@ -117,7 +120,7 @@ npm install svg-path-simplify
|
|
|
117
120
|
|
|
118
121
|
To simplify entire SVG documents in node.js we need to emulate the browsers DOM methods `DOMParser` and `XMLSerializer`. I opted for [linkedom](https://github.com/WebReflection/linkedom). Just make sure to import the `svg-path-simplify/node` module. It will load linkedom's methods `DOMParser` and add a polyfill for `XMLSerializer`
|
|
119
122
|
|
|
120
|
-
```
|
|
123
|
+
```js
|
|
121
124
|
/**
|
|
122
125
|
* load node polyfills for DOM parsing
|
|
123
126
|
* loads linkedom npm module for DOM parsing and emulation
|
|
@@ -131,10 +134,22 @@ let pathDataString = `M 57.13 15.5c 13.28 0 24.53 8.67 28.42 20.65c 0.94 2.91 1
|
|
|
131
134
|
let pathDataOpt = svgPathSimplify(pathDataString);
|
|
132
135
|
```
|
|
133
136
|
|
|
137
|
+
### Web worker
|
|
138
|
+
Similar to node.js usage we need [linkedom](https://github.com/WebReflection/linkedom) to polyfill missing DOMParser functionality in a headless environment.
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
import './svg-path-simplify.worker.polyfills.js';
|
|
142
|
+
import { svgPathSimplify } from './svg-path-simplify.esm.js';
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Import the polyfill module from the dist folder. It imports the worker version of linkedom.
|
|
146
|
+
See [svg-path-simplify.worker.js](https://github.com/herrstrietzel/svg-path-simplify/blob/main/dist/svg-path-simplify.worker.js).
|
|
147
|
+
|
|
148
|
+
|
|
134
149
|
#### Example 2: Apply options
|
|
135
150
|
The following example would return a detailed object containing the stringified "normalized" pathdata (all absolute and "longhand" command notation).
|
|
136
151
|
|
|
137
|
-
```
|
|
152
|
+
```js
|
|
138
153
|
let options = {
|
|
139
154
|
extrapolateDominant: false,
|
|
140
155
|
decimals: 3,
|
|
@@ -200,9 +215,9 @@ Z
|
|
|
200
215
|
|
|
201
216
|
|
|
202
217
|
|
|
203
|
-
|
|
218
|
+
## API
|
|
204
219
|
|
|
205
|
-
```
|
|
220
|
+
```js
|
|
206
221
|
let options = {}
|
|
207
222
|
let output = svgPathSimplify(input, options);
|
|
208
223
|
```
|
|
@@ -238,6 +253,21 @@ These params control shich simplifications are applied. The default settings aim
|
|
|
238
253
|
| simplifyRound | replaces small round segments encloses by linetos – helps to simplify shapes like gears/cogs | Boolean | false |
|
|
239
254
|
|
|
240
255
|
|
|
256
|
+
### Path direction
|
|
257
|
+
| parameter | effect | type | default |
|
|
258
|
+
| -- | -- | -- | -- |
|
|
259
|
+
| fixDirections | alternates sub path directions to fulfill non-zero. Makes fill-rule attribute obsolete and render correct in other environments e.g when converting to fonts | Boolean | false |
|
|
260
|
+
| reversePath | simply reverses drawing direction - sometimes needed for line animations | Boolean | false |
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
### Polygon options
|
|
265
|
+
| parameter | effect | type | default |
|
|
266
|
+
| -- | -- | -- | -- |
|
|
267
|
+
| smoothPoly | Curve-fitting: Converts polylines to cubic beziers | Boolean | false |
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
241
271
|
### Output options
|
|
242
272
|
|
|
243
273
|
| parameter | effect | type | default |
|
|
@@ -254,6 +284,7 @@ These params control shich simplifications are applied. The default settings aim
|
|
|
254
284
|
| toRelative | converts all commands to relative – reduces file size | Boolean | true |
|
|
255
285
|
| toShorthands | converts all commands to shorthand when applicable – reduces file size | Boolean | true |
|
|
256
286
|
|
|
287
|
+
|
|
257
288
|
### SVG scaling
|
|
258
289
|
Scaling only indirectly affects file size analogous to rounding. Use it only for very large or small SVGs. Finding the »sweet spot« where all coordinates can be expressed in integers can reduce file size as no decimal separators are required.
|
|
259
290
|
|
|
@@ -270,6 +301,39 @@ Scaling only indirectly affects file size analogous to rounding. Use it only for
|
|
|
270
301
|
| arcToCubic | converts elliptic arc `A` commands to cubic approximations – not recommended | Boolean | false |
|
|
271
302
|
| removeHidden | removes hidden elements for SVG inputs | Boolean | true |
|
|
272
303
|
| mergePaths | concatenates paths into single one – does not respect individual styles! | Boolean | false |
|
|
304
|
+
| shapesToPaths | converts shapes to paths - usually not recommended as shapes are most often more compact. But useful for path concatenation | Boolean | false |
|
|
305
|
+
| stylesToAttributes | consolidates styles and set them to attributes. Also removes invalid attributes (e.g `font-family` for paths) | Boolean | false |
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
## Lite version – only path data
|
|
309
|
+
Since the library aims at a complete toolset it might be an overkill for your needs.
|
|
310
|
+
For this use case you can opt for the lighter pathdata only version »svg-path-simplify.pathdata.esm.min«. Minified filesize is ~41 KB / 17 KB gzipped (compared to the full library with ~72/27 KB)
|
|
311
|
+
|
|
312
|
+
The API is compatible but simply missing some options such as:
|
|
313
|
+
* polygon smoothing/curve fitting
|
|
314
|
+
* svg processing - only path data is allowed
|
|
315
|
+
* no scaling
|
|
316
|
+
* no path direction fixes
|
|
317
|
+
|
|
318
|
+
```js
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
import { simplifyPathData } from '../dist/svg-path-simplify.pathdata.esm.js'
|
|
322
|
+
|
|
323
|
+
let pathDataString =
|
|
324
|
+
`
|
|
325
|
+
M 57.13 15.5
|
|
326
|
+
c 13.28 0 24.53 8.67 28.42 20.65
|
|
327
|
+
c 0.94 2.91 1.45 6.01 1.45 9.23
|
|
328
|
+
`
|
|
329
|
+
// try to simplify
|
|
330
|
+
let pathDataOpt = simplifyPathData(pathDataString);
|
|
331
|
+
|
|
332
|
+
/*
|
|
333
|
+
or with options let pathDataOpt = simplifyPathData(pathDataString, options);
|
|
334
|
+
*/
|
|
335
|
+
|
|
336
|
+
```
|
|
273
337
|
|
|
274
338
|
|
|
275
339
|
|
|
@@ -279,6 +343,15 @@ You can easily test this library via the [**webapp**](https://herrstrietzel.gith
|
|
|
279
343
|
|
|
280
344
|

|
|
281
345
|
|
|
346
|
+
#### Features
|
|
347
|
+
* test all provided simplification option - export settings as JS object
|
|
348
|
+
* preview different settings
|
|
349
|
+
* accepts SVG files
|
|
350
|
+
* path data strings
|
|
351
|
+
* supports multi file batch processing
|
|
352
|
+
* open results in codepen or svg-path-editor
|
|
353
|
+
* download self contained SVG
|
|
354
|
+
|
|
282
355
|
|
|
283
356
|
### Demo files
|
|
284
357
|
* [simple setup IIFE](./demo/simple-iife.html)
|
|
@@ -338,4 +411,6 @@ You can also post in the [discussions](https://github.com/herrstrietzel/svg-path
|
|
|
338
411
|
* [Vitaly Puzrin](https://github.com/puzrin) for [svgpath library](https://github.com/fontello/svgpath) providing for instance a great and customizable [arc-to-cubic approximation](https://github.com/fontello/svgpath/blob/master/lib/a2c.js) – the base for the more accurate arc-to-cubic approximations
|
|
339
412
|
* [Jarek Foksa](https://github.com/jarek-foksa) for developping the great [getPathData() polyfill](https://github.com/jarek-foksa/path-data-polyfill) – probably the most productive contributor to the ["new" W3C SVGPathData interface draft](https://svgwg.org/specs/paths/#InterfaceSVGPathData)
|
|
340
413
|
* obviously, [Dmitry Baranovskiy](https://github.com/dmitrybaranovskiy) – a lot of these helper functions originate either from Raphaël or snap.svg – or are at least heavily inspired by some helpers from these libraries
|
|
414
|
+
* [Andrea Giammarchi a.k.a WebReflection](https://github.com/WebReflection) for [linkedom](https://github.com/WebReflection/linkedom) which helped to make this lib run also in node or a web worker
|
|
415
|
+
* [Photopea](https://github.com/photopea) for the fast [UZIP](https://github.com/photopea/UZIP.js) which is deployed in the webapp for batch file downloads.
|
|
341
416
|
|