Convert SVG polygon to path

后端 未结 4 1072
春和景丽
春和景丽 2020-12-04 13:34

I have a fairly large SVG file of administrative subdivisions that I need to work with in Raphael.JS (it has 600 polygons and weights 1.2 Mb).

Now, I need to convert

4条回答
  •  独厮守ぢ
    2020-12-04 14:00

    Little fix for polygon id, fill and stroke attributes save

    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('d',pathdata);
    
      poly.parentNode.replaceChild(path,poly);
    }
    

提交回复
热议问题