Skip to content

Instantly share code, notes, and snippets.

@d0ky
Last active March 15, 2016 02:19
Show Gist options
  • Save d0ky/f246a7745da5d5cea9ba to your computer and use it in GitHub Desktop.
Save d0ky/f246a7745da5d5cea9ba to your computer and use it in GitHub Desktop.
convertPolyToPath.js
var polys = document.querySelectorAll('polygon,polyline');
[].forEach.call(polys,convertPolyToPath);
function convertPolyToPath(poly){
var svgNS = poly.ownerSVGElement.namespaceURI;
var path = document.createElementNS(svgNS,'path');
var points = poly.getAttribute('points').split(/\s+|,/);
var x0=points.shift(), y0=points.shift();
var pathdata = 'M'+x0+','+y0+'L'+points.join(' ');
if (poly.tagName=='polygon') pathdata+='z';
path.setAttribute('id',poly.getAttribute('id'));
path.setAttribute('fill',poly.getAttribute('fill'));
path.setAttribute('stroke',poly.getAttribute('stroke'));
path.setAttribute('stroke-width',poly.getAttribute('stroke-width'));
path.setAttribute('d',pathdata);
poly.parentNode.replaceChild(path,poly);
}
@d0ky
Copy link
Author

d0ky commented Mar 15, 2016

Convert polyline to path in svg format

@d0ky
Copy link
Author

d0ky commented Mar 15, 2016

else on AI the trick to obtain path in the svg export is to turn the path in to a Compound Path (CMD + 8). That makes the SVG output an actual path rather than a primitive type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment