Canvas arc clearing

北城以北 提交于 2019-12-12 10:44:37

问题


How do I overwrite an HTML5 canvas arc? I presumed this code would work but it leaves a border around it despite the fact that its exactly the same values and just a different colour.. Is there a border property I'm missing??

<!DOCTYPE html>
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
  <canvas id="surface" width="300" height="300"></canvas>

  <script type="text/javascript">
      var canvas = document.getElementById('surface');
      var ctx = canvas.getContext('2d');

      ctx.fillStyle = 'black';
      ctx.beginPath();
      ctx.arc(100, 100, 20, 0, Math.PI * 2, true);
      ctx.fill();

      ctx.fillStyle = 'white';
      ctx.beginPath();
      ctx.arc(100, 100, 20, 0, Math.PI * 2, true);
      ctx.fill();
  </script>
  </body>
</html>

回答1:


This black edge is a side affect of anti-aliasing.

The easiest solution is to increase the radius of the arc slightly.




回答2:


for this situation, it makes more sense to just redraw the the portion of the canvas that contained the arc. You can clear a section of the canvas with

ctx.clearRect(x, y, width, height);

or for simplicity you could just clear the entire canvas and redraw it completely:

ctx.clearRect(0, 0, canvas.width, canvas.height);



回答3:


In case you want something like undo feature you could copy the image data to a swap canvas before the arc drawing. Then copy the image data from swap canvas to the visible one if the undo is required.



来源:https://stackoverflow.com/questions/6175042/canvas-arc-clearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!