最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Canvas arc clearing - Stack Overflow

programmeradmin4浏览0评论

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>

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>
Share Improve this question asked May 30, 2011 at 10:20 Alex LatchfordAlex Latchford 6551 gold badge9 silver badges20 bronze badges 3
  • Just out of curiosity, is this faster than using ctx.clear()? – Alexander Tsepkov Commented Jun 11, 2011 at 22:48
  • Not really, I ended up restructuring the format with which I rendered using the standard update/draw cycle. As a part of the draw I used ctx.clear() and that worked much faster. – Alex Latchford Commented Jul 8, 2011 at 14:17
  • ctx.imageSmoothingEnabled= false; – ashleedawg Commented Dec 22, 2021 at 9:29
Add a ment  | 

3 Answers 3

Reset to default 3

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 pletely:

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

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论