简体   繁体   English

从javascript中的点数组绘制SVG多边形

[英]Draw SVG Polygon from array of points in javascript

if there is an array like this,(arr includes points) 如果有这样的数组,(arr包括点)

arr = [ [ 0,0 ], 
             [ 50,50 ],
             [ 25,25 ], ];

i want to draw SVG polygon using this array. 我想使用此数组绘制SVG多边形。

At first, i think this code will be okay, but it isn't. 起初,我认为这段代码可以,但是事实并非如此。

<polygon points="arr[0][0],arr[0][1] arr[1][0],arr[1][1] arr[2][0],arr[2][1]" style = "fill:lime; stroke:purple; stroke-width:3;/">

Please, let me know how to make polygon using array of points. 请让我知道如何使用点数组制作多边形。 Make some example code, please. 请编写一些示例代码。

You can use the SVG DOM to do this although a polygon where the points form a straight line doesn't display unless you set a stroke. 您可以使用SVG DOM进行此操作,尽管除非您设置笔触,否则不会显示点形成直线的多边形。

 var svg = document.getElementById("svg"); var polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon"); svg.appendChild(polygon); var array = arr = [ [ 0,0 ], [ 50,50 ], [ 25,25 ], ]; for (value of array) { var point = svg.createSVGPoint(); point.x = value[0]; point.y = value[1]; polygon.points.appendItem(point); } 
 polygon { stroke: black; } 
 <svg id="svg"> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM