├── LICENSE ├── readme.md ├── src └── svgpathnormalizer.js └── test └── test.html /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Tadahisa Motooka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # SVGPathNormalizer # 2 | 3 | Most modern web browsers do not implement [SVGAnimatedPathData.normalizedPathSegList](http://www.w3.org/TR/SVG/paths.html#InterfaceSVGAnimatedPathData) probably because of its severe specification : for example, it is too difficult to express elliptical arcs by cubic bezier curves. 4 | 5 | This normalizer expresses paths using the following commands only : 6 | 7 | - SVG_PATHSEG_MOVETO_ABS (M), 8 | - SVG_PATHSEG_LINETO_ABS (L), 9 | - SVG_PATHSEG_CURVETO_CUBIC_ABS (C), 10 | - SVG_PATHSEG_CURVETO_QUADRATIC_ABS (Q), 11 | - SVG_PATHSEG_ARC_ABS (A) and 12 | - SVG_PATHSEG_CLOSEPATH (z). 13 | 14 | ## License ## 15 | MIT License. See `LICENSE` file for more details. 16 | 17 | ## Usage ## 18 | 1. Import the file `svgpathnormalizer.js` in your HTML or SVG document 19 | 1. Get reference to `` element. For example, use `document.getElementById(idString)` 20 | 1. Get an instance of `SVGPathSegList` via `pathSegList` property of the `` element 21 | 1. Call `SVGPathNormalizer.normalize` function with just one argument : the instance of `SVGPathSegList` 22 | 1. You will get another instance of `SVGPathSegList` which is normalized. 23 | 24 | Please note that the original `` will NOT be modified even if you modify the returned instance of `SVGPathSegList`. 25 | 26 | ## Requirement ## 27 | Your runtime environment MUST support SVG 1.1. Most modern web browsers - including Internet Explorer 11 - match the requirement. 28 | 29 | Chrome 48 and Firefox 43 have removed `SVGPathSegList` implementation. So you will also require [SVGPathSeg polyfill](https://github.com/progers/pathseg). 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/svgpathnormalizer.js: -------------------------------------------------------------------------------- 1 | /** 2 | * SVGPathNormalizer 3 | * https://github.com/motooka/SVGPathNormalizer 4 | * 5 | * Copyright (c) Tadahisa Motooka 6 | * Licensed under the MIT license 7 | * See the file "LICENSE" for more detail. 8 | * 9 | */ 10 | var SVGPathNormalizer = {}; 11 | 12 | SVGPathNormalizer.normalize = function(pathSegList) { 13 | var path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); 14 | var newPathSegList = path.pathSegList; 15 | var seg = null; 16 | var newSeg = null; 17 | var curX = 0; 18 | var curY = 0; 19 | var newX = 0; 20 | var newY = 0; 21 | var startX = null; 22 | var startY = null; 23 | var lastCommandIsCubicBezier = false; 24 | var lastCommandIsQuadraticBezier = false; 25 | var lastControlX = null; 26 | var lastControlY = null; 27 | var thisCommandIsCubicBezier = false; 28 | var thisCommandIsQuadraticBezier = false; 29 | 30 | 31 | for(var i=0; i 2 | 3 | 14 | 15 | 16 | SVGPathNormalizer Test Page 17 | 18 | 19 | 44 | 50 | 51 | 52 |

SVGPathNormalizer Test Page

53 |

54 | The red line is the original line. By clicking the button below, a black line will appear. If it does not differ from the red one, we can say OK.
55 | 56 |

57 |
58 | 59 | 95 | 96 | 97 | 98 | 99 | --------------------------------------------------------------------------------