简体   繁体   English

canvas html5的问题无法关闭绘图

[英]Problems with canvas html5 can't close a draw

I'm developing a website where you can insert data of person, each person is represented by a image (a circle), the images are in a html table(the rows are the years and the columns are the months), depends of birth date, they will be in one position or other position. 我正在开发一个网站,你可以插入人的数据,每个人用图像(圆圈)表示,图像在html表中(行是年份,列是月份),取决于出生约会,他们将处于一个位置或其他位置。 Well the thing is that I have to link the images with canvas to make a figure, a close figure. 嗯,我必须将图像与画布链接起来制作一个数字,一个近似的数字。 And by now I only managed to join it with lines, I know that if you use the property ".fill()" the figure will be closed, but it doesn't work and I don't know why. 到目前为止,我只是设法用线条加入它,我知道如果你使用属性".fill()"这个数字将被关闭,但它不起作用,我不知道为什么。 What I'm doing wrong? 我做错了什么?

Here is my js code: (I call the function "person" from the event onClick() in a button of the html, every time you insert a person) 这是我的js代码:(每次插入一个人时,我在html的按钮中调用onClick()事件中的函数“person”)

    function position(year, mon) //this function puts the images
    {
        $('#' + year + ' .' + mon).prepend('<img class="black_point" src="./images/circle.png"/>');    
    }
    function person () {
        var date;
        date=prompt('Birthday date','01/01/1935');
        var elem = date.split('/'); 
        var month= elem[1]; //we take the month
        var year=elem[2]; //we take the year
        var mon= num2mon(month);
        var rw=document.getElementById("table").rows.length;
        var cols = $("#table").find('tr')[0].cells.length;
        position(year,mon);
     draw();
    }


    function draw() { //this function draw the lines
        var table = document.getElementById("table");
        var images = table.getElementsByTagName("img");  
        var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext("2d");
        var x,y;

        canvas.width  = table.offsetWidth;
        canvas.height = table.offsetHeight;

          function connect(image) { //this function link the images
            var tabBcr = table.getBoundingClientRect();
            var imgBcr = image.getBoundingClientRect();
            ctx.beginPath();
            ctx.moveTo(x, y);

            x = imgBcr.left + (imgBcr.width / 2) - tabBcr.left;
            y = imgBcr.top + (imgBcr.height / 2) - tabBcr.top;

            ctx.lineTo(x, y);
            ctx.stroke();

            //ctx.fill(); //that's no work :S
            //ctx.closePath();

          }
        for(var i=0; i<images.length; i++){ 
          connect( images[i]);
        }
    }

Thank you! 谢谢!

There are two kind of paths in canvas, the main path and then sub-paths. 画布中有两种路径,主路径和子路径。

For a close to work you need to only have max one sub-path. 要近距离工作,您只需要最多一个子路径。 Every time moveTo() is used a new sub-path will be created so when a line is made like this: 每次使用moveTo()时都会创建一个新的子路径,所以当一行像这样:

moveTo(x1, y1);
lineTo(x2, y2);
moveTo(x2, y2);
lineTo(x3, y3);

You have two sub-paths that are not connected. 您有两个未连接的子路径。 What you want is to continue adding lines to the existing sub-paths like this: 你想要的是继续添加到现有子路径的行,如下所示:

moveTo(x1, y1);
lineTo(x2, y2);
lineTo(x3, y3);

Now it is possible to close the shape connecting x3->x1 and y3->y1. 现在可以关闭连接x3-> x1和y3-> y1的形状。

Using ctx.beginPath() in this case makes it worst. 在这种情况下使用ctx.beginPath()会使其最糟糕。 It will clear all sub-paths added to the main path. 它将清除添加到主路径的所有子路径。

What you need to do is to globally (or at some higher level) create a new path using beginPath() (every time you need to redraw the content for example). 您需要做的是全局(或在更高级别)使用beginPath()创建新路径(例如,每次需要重绘内容时)。

Then the first line needs to be set using moveTo() . 然后需要使用moveTo()设置第一行。 Then every other lines using lineTo() . 然后使用lineTo()每隔一行。

Finally you can use closePath() and render it using stroke() or fill() (closePath() is not needed with fill, with stroke it must be called before stroke). 最后你可以使用closePath()并使用stroke()fill()渲染它stroke() fill()不需要closePath(),必须描边之前调用它)。

For example (untested, adopt as needed): 例如 (未经测试,根据需要采用):

function draw() { //this function draw the lines
    var table = document.getElementById("table");
    var images = table.getElementsByTagName("img");  
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    var x,y;

    canvas.width  = table.offsetWidth;
    canvas.height = table.offsetHeight;

    function connect(image, index) { //this function link the images
        var tabBcr = table.getBoundingClientRect();
        var imgBcr = image.getBoundingClientRect();

        x = imgBcr.left + (imgBcr.width / 2) - tabBcr.left;
        y = imgBcr.top + (imgBcr.height / 2) - tabBcr.top;

        index === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
    }

    // new path here
    ctx.beginPath();

    for(var i=0; i<images.length; i++){ 
      connect( images[i], i);  // provide index so we can sep. move/line
    }

    // then at the end:
    ctx.fill();
}

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

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