1
我需要知道svg路径是否可以转换为svg多段线。
我发现javascript code which convert a path to a polygon:svg路径到svg多段线
function pathToPolygon(path,samples){
if (!samples) samples = 0;
var doc = path.ownerDocument;
var poly = doc.createElementNS('http://www.w3.org/2000/svg','polygon');
// Put all path segments in a queue
for (var segs=[],s=path.pathSegList,i=s.numberOfItems-1;i>=0;--i) segs[i] = s.getItem(i);
var segments = segs.concat();
var seg,lastSeg,points=[],x,y;
var addSegmentPoint = function(s){
if (s.pathSegType == SVGPathSeg.PATHSEG_CLOSEPATH){
}else{
if (s.pathSegType%2==1 && s.pathSegType>1){
// All odd-numbered path types are relative, except PATHSEG_CLOSEPATH (1)
x+=s.x; y+=s.y;
}else{
x=s.x; y=s.y;
}
var lastPoint = points[points.length-1];
if (!lastPoint || x!=lastPoint[0] || y!=lastPoint[1]) points.push([x,y]);
}
};
for (var d=0,len=path.getTotalLength(),step=len/samples;d<=len;d+=step){
var seg = segments[path.getPathSegAtLength(d)];
var pt = path.getPointAtLength(d);
if (seg != lastSeg){
lastSeg = seg;
while (segs.length && segs[0]!=seg) addSegmentPoint(segs.shift());
}
var lastPoint = points[points.length-1];
if (!lastPoint || pt.x!=lastPoint[0] || pt.y!=lastPoint[1]) points.push([pt.x,pt.y]);
}
for (var i=0,len=segs.length;i<len;++i) addSegmentPoint(segs[i]);
for (var i=0,len=points.length;i<len;++i) points[i] = points[i].join(',');
poly.setAttribute('points',points.join(' '));
return poly;
}
读音字在JS一个初学者,不明白这行: VAR SEG,lastSeg,点= [],X,Y;
它工作的很好,但它在路径线周围绘制了一个框架。 但我不想要一个框架,我想将路径转换为点集,折线。 有没有可能 谢谢
我正在寻找直接使用路径,现在不是很重要。谢谢 – merveotesi
我已经了解到,一条路径不能被转换成多段线,因为路径并没有像我以前想的那样提供一条线。 svg文档中的路径描述了一个“区域”。所以它只能被转换成多边形。 – merveotesi