简体   繁体   English

d3 Javascript-绘制多边形

[英]d3 Javascript - Draw polygon

I have an SVG canvas that I want to be able to click points and draw a polygon. 我有一个SVG画布,我希望它能够单击点并绘制多边形。 I have the following code to do that: 我有以下代码可以做到这一点:

var clicks = [];
    function polyClick(){

        var x = event.clientX;
        var y = event.clientY;
        var point = document.getElementById("num").value;

        clicks.push.apply(clicks, [x,y]);
        drawPoly(clicks)
        function drawPoly(params){

        d3.select(".p").remove();
        mySVG.append("svg:polygon")
        .style("fill", "red")
        .style("stroke-width", "1")
        .classed("p", true)
        .attr("points", params + " ");  //This LINE

        }            

    }

The line marked "This LINE" give me the correct x and y coordinates but in the format of "21,50,60,70,90,100". 标有“ This LINE”的行为我提供了正确的x和y坐标,但格式为“ 21,50,60,70,90,100”。 What I need is for the coordinates in the array are added as sets such as "21,50 60,70 90,100". 我需要的是将数组中的坐标添加为集合,例如“ 21,50 60,70 90,100”。 Any idea on how to go about removing the comma in between each coordinate? 关于如何删除每个坐标之间的逗号的任何想法吗?

BONUS: Can anyone tell me exactly what this line does? 奖金:有人能确切告诉我这行是什么吗?

drawPoly(clicks)
function drawPoly(params)

I found it as a suggestion to use array values as a parameter to a function but I don't fully understand how that is working. 我发现建议将数组值用作函数的参数,但我不完全了解它是如何工作的。 I'm using "drawPoly" before the function is even declared. 在函数声明之前,我正在使用“ drawPoly”。

Thanks for any and all help! 感谢您提供的所有帮助!

Function definitions are processed during compilation and expressions (like function call) are processed during execution. 函数定义在编译期间进行处理,而表达式(如函数调用)在执行期间进行处理。 This is why in JavaScript function definition and function call order does not matter. 这就是为什么在JavaScript中函数定义和函数调用顺序无关紧要的原因。

Code Explanation: 代码说明:

function drawPoly(params){
    d3.select(".p").remove(); //'p' is the class name of polygon. This line deletes the polygon if exists.
    mySVG.append("svg:polygon") //Appends a new polygon with the calculated position attributes.
      .style("fill", "red")
      .style("stroke-width", "1")
      .classed("p", true) //Sets classname as p
      .attr("points", params);
}    

The code seems to be working with the current format of coordinates. 该代码似乎适用于当前的坐标格式。 Below is the working code snippet. 以下是工作代码段。

Try out by clicking more than 3 points as the coordinates of polygon. 通过单击3个以上的点作为多边形的坐标来进行尝试。

 var mySVG = d3.select("#container").append("svg").attr({ width: 500, height: 200 }); mySVG.on('click', polyClick); var clicks = []; function polyClick() { var x = event.clientX; var y = event.clientY; clicks.push.apply(clicks, [x, y]); drawPoly(clicks) function drawPoly(params) { d3.select(".p").remove(); mySVG.append("svg:polygon") .style("fill", "red") .style("stroke-width", "1") .classed("p", true) .attr("points", params + " "); } } 
 svg { background-color: black; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <div id="container"> </div> 

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

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