Skip to content

Instantly share code, notes, and snippets.

@ericjames
Forked from ricardoriogo/convertPolyToPath.js
Last active August 29, 2015 14:16
Show Gist options
  • Save ericjames/fb214db9ef4da6c48cb0 to your computer and use it in GitHub Desktop.
Save ericjames/fb214db9ef4da6c48cb0 to your computer and use it in GitHub Desktop.
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('d',pathdata);
poly.parentNode.replaceChild(path,poly);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment