What exactly is a canvas path, and what is the use of ctx.closePath()?

后端 未结 2 672
悲哀的现实
悲哀的现实 2020-12-04 18:17

I\'m working on an HTML5 game. I need to draw tail lines in the canvas and check for intersections in the game, which is a Tron-style game.

I\'m actually using the d

2条回答
  •  一个人的身影
    2020-12-04 18:48

    This is the basic representation of closed path:

    ctx.beginPath();
    ctx.moveTo(0,0);
    ctx.lineTo(100,0);
    ctx.lineTo(100,100);
    ctx.lineTo(0,100);    
    ctx.closePath(); // <--the image right side has this line
    ctx.stroke();
    

    The result of closePath() is that the start and the end point will be bounded.

    closed path

提交回复
热议问题