Convert Polygon to Path Data

I've had to do this a few times recently so I thought I'd save the method. StackOverflow has a method that works great:


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';


Michael Schofield made a Pen to do it quick:

See the Pen Convert SVG Polygon to Path by Michael Schofield (@michaelschofield) on CodePen.

You put your own Polygon in the SVG above and then it gets replaced by a path in the DOM. You can DevTools inspect to get out the path data.

The purpose is, for example, you're trying to animate from a shape with straight lines to a shape with curved lines. SVG software tools will output the former as a polygon, which is a different type of data that can't animate natively to the path syntax.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.