简体   繁体   English

在HTML5画布中绘制任意形状

[英]Arbitrary shape getting drawn in HTML5 canvas

I am trying to draw a graph using HTML5 canvas element. 我正在尝试使用HTML5 canvas元素绘制图形。 The graph consists of vertices and edges connecting these vertices. 该图由顶点和连接这些顶点的边组成。 I'm representing the vertices by small circles and edges by lines connecting the circles. 我用小圆圈表示顶点,用连​​接圆圈的线表示边缘。

In my project, I want to change the position of vertices, when a 'Next' button is pressed. 在我的项目中,我想在按下“下一步”按钮时更改顶点的位置。 Shown below is a state of the canvas when the page is first loaded: 下面显示的是页面首次加载时画布的状态:

在此处输入图片说明

When the 'Next' button is clicked the graph is redrawn, based on the new coordinates of the vertices. 单击“下一步”按钮后,将根据顶点的新坐标重新绘制图形。 However, there seems to be a error with the rendering as shown below: 但是,渲染似乎出现错误,如下所示:

在此处输入图片说明

Strangely, I am not using the fill() functions anywhere, so I am not able to understand why the filled portion is coming. 奇怪的是,我没有在任何地方使用fill()函数,所以我无法理解为什么填充部分会来。 Following is the code for draw() function that is used for drawing the graph: 以下是用于绘制图形的draw()函数的代码:

function draw(position)
{
    var canvas_elem = document.getElementById('drawing');

    // Check the element is in the DOM and the browser supports canvas
    if(canvas_elem.getContext)
    {
        // Initaliase a 2-dimensional drawing context
        var canvas = canvas_elem.getContext('2d');

        // Clear canvas
        canvas.clearRect(0, 0, canvas_elem.width, canvas_elem.height);

        var data = data_array[num_vertices+1+position].split(",");

        // Draw all vertices
        for( var i=0; i<num_vertices; i++)
        {
            canvas.arc(data[2*i], data[2*i+1], radius, 0, 2 * Math.PI, false);
            canvas.fillStyle = 'black';
            canvas.fill();
        }

        // Connect vertices by lines according to adjacency matrix
        for(var i=0;i<num_vertices; i++)
        {
            for(var j=i; j<num_vertices; j++)
            {
                if(adjacency[i][j]==1)
                {
                    canvas.beginPath();
                    canvas.moveTo(data[2*i],data[2*i+1]);
                    canvas.lineTo(data[2*j],data[2*j+1]);
                    canvas.stroke();
                }
            }
        }
    }
    else
    {
        alert("Please use an HTML5 compatible browser.");
    }
}

Any leads on what might be happening here? 有什么线索可能会发生在这里吗?


Take following values of these variables for debugging: 取这些变量的以下值进行调试:

data_array=[
"10",
"0,1,1,0,0,0,0,0,0,0",
"1,0,0,1,0,0,0,0,0,1",
"1,0,0,0,1,0,0,0,0,1",
"0,1,0,0,0,1,0,0,1,1",
"0,0,1,0,0,0,1,0,1,1",
"0,0,0,1,0,0,0,1,1,0",
"0,0,0,0,1,0,0,1,1,0",
"0,0,0,0,0,1,1,0,0,0",
"0,0,0,1,1,1,1,0,0,0",
"0,1,1,1,1,0,0,0,0,0",
"10,2,35.4,23.3,88.9,100,210,350,70,500,412,208,336,112,45,32,89,92,102,23",
"30,4,19.4,35.3,80.9,90,230,310,120,440,400,220,330,105,40,29,80,89,90,18"
]

num_vertices=10

The .arc() method adds points to the current path, and the .fill() method fills in the current path. .arc()方法将点添加到当前路径,而.fill()方法将填充当前路径。 If you keep calling .arc() , you are re-using the same path, so it eventually gets filled. 如果继续调用.arc() ,那么您将重新使用同一路径,因此最终将被填充。 Start a new path with .beginPath() : 使用.beginPath()开始新路径:

// Draw all vertices
for( var i=0; i<num_vertices; i++)
{
    canvas.beginPath();
    canvas.arc(data[2*i], data[2*i+1], radius, 0, 2 * Math.PI, false);
    canvas.fill();
}

http://jsfiddle.net/PAPr5/2/ http://jsfiddle.net/PAPr5/2/

Or, start a new sub-path using .moveTo() . 或者,使用.moveTo()开始一个新的子路径。 This let's you paint all the points at once with a single call to .fill() . 这样,您只需一次调用.fill()即可一次绘制所有点。

// Draw all vertices
for (var i = 0; i < num_vertices; i++) {
    var x = data[2 * i];
    var y = data[2 * i + 1]
    canvas.moveTo(x, y);
    canvas.arc(x, y, radius, 0, 2 * Math.PI, false);
}
canvas.fill();

http://jsfiddle.net/PAPr5/5/ http://jsfiddle.net/PAPr5/5/

According to the spec , .arc() should not create a new path or sub-path. 根据规范.arc()不应创建新路径或子路径。 The implementation of .arc() in Google Chrome does not conform to the specification. Google Chrome浏览器中.arc()的实现不符合规范。 In Chrome, each call to .arc() results in a new sub-path being created. 在Chrome中,每次调用.arc()创建一个新的子路径。 This behavior is non-standard. 此行为是非标准的。

Okay, a silly mistake. 好的,这是一个愚蠢的错误。 I forgot to call closePath() after drawing each line. 我忘了在绘制每条线后调用closePath()。

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

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