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

javascript - Weird HTML 5 Canvas Antialiasing - Stack Overflow

programmeradmin5浏览0评论

I've been playing with canvas element and discovered that when I attempt to draw NxN uniform solid-colored cells next to each other, in some width/height configurations, there are blurry white-ish lines between them.

For instance, this canvas is supposed to look black but contains some sort of grid which I conjecture to be a result of faulty antialiasing in the browser.

Suffice to say, this bug appears only in some configurations but I would like to get rid of it for good. Is there any way to circumvent this? Have you ever had problems with antialiasing in canvas?

I have made this fiddle which demonstrates the issue and allows you to play with the dimensions of the canvas and number of cells. It also contains the code I use to draw the cells, so that you can inspect it and tell me if I'm doing anything wrong.

var ctx = canvas.getContext('2d');  
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
for (var i = 0; i < numberOfCells; ++i) {
    for (var j = 0; j < numberOfCells; ++j) {
    ctx.fillStyle = '#000';
    ctx.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);
  }
}

Thanks in advance,

Petr.

I've been playing with canvas element and discovered that when I attempt to draw NxN uniform solid-colored cells next to each other, in some width/height configurations, there are blurry white-ish lines between them.

For instance, this canvas is supposed to look black but contains some sort of grid which I conjecture to be a result of faulty antialiasing in the browser.

Suffice to say, this bug appears only in some configurations but I would like to get rid of it for good. Is there any way to circumvent this? Have you ever had problems with antialiasing in canvas?

I have made this fiddle which demonstrates the issue and allows you to play with the dimensions of the canvas and number of cells. It also contains the code I use to draw the cells, so that you can inspect it and tell me if I'm doing anything wrong.

var ctx = canvas.getContext('2d');  
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
for (var i = 0; i < numberOfCells; ++i) {
    for (var j = 0; j < numberOfCells; ++j) {
    ctx.fillStyle = '#000';
    ctx.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);
  }
}

Thanks in advance,

Petr.

Share edited Dec 17, 2015 at 11:52 Petr Mánek asked Dec 17, 2015 at 11:48 Petr MánekPetr Mánek 1,0661 gold badge10 silver badges25 bronze badges 7
  • Hello Petr, it would seem you forgot to provide your fiddle link, Could you please update – Canvas Commented Dec 17, 2015 at 11:51
  • Just noticed it. Sorry, here you go! :) – Petr Mánek Commented Dec 17, 2015 at 11:53
  • A mon trick is to transform the entire canvas by one pixel at the start to dodge this, I believe. – Evan Knowles Commented Dec 17, 2015 at 11:54
  • 1 It does make sense, 300 / 70 = 4.285... which will make any edge anti-alias, and because of that, a line will appear. It seems to do what is expected, because of rounding... – somethinghere Commented Dec 17, 2015 at 11:57
  • You can find a solution here Filling the gaps – Blindman67 Commented Dec 17, 2015 at 12:02
 |  Show 2 more ments

3 Answers 3

Reset to default 4

jsFiddle : https://jsfiddle/ngxjnywz/2/

snippet of javascript

var cellWidth = Math.ceil(canvasWidth / numberOfCells);
var cellHeight = Math.ceil(canvasHeight / numberOfCells);

Depending on the width, height and your numberOfCells you are sometimes getting a... lets say 4.2 which is 4, however this would be displayed wrong and will allow a 1 pixel blank line to appear. So all you need to do is use the Math.ceil function and this will cause your cellWidth and cellHeight to always be the higher number and you won't get blank lines anymore

The best solution is to add a 0.5 pixel wide stroke around all the fills, using the same style as the fill and offsetting all drawing so that you render at the center of pixels rather than the top left.

If you add scaling or translation you will have to adjust the coordinates so that you still give the centers for your drawing coordinates.

In the end you can only reduce the artifacts but for many situations you will not be able to pletely remove them.

This answer shows you how to remove the artifacts for an untransformed canvas. How to fill the gaps

After reading through and trying several approaches, I've decided to e up with my own. I've created another (virtual) canvas which had integer dimensions corresponding to the number of cells in the grid.

After drawing all the cells in there, I call context.drawImage() on the main canvas and pass the virtual canvas as an argument along with offset and scale parameters to make it fit rest of my drawing. Assuming that the browser would scale the virtual canvas's image as a whole (and not as individual cells), I was hoping to get rid of the unwanted separator lines.

In spite of my efforts, the lines are still there. Any suggestions?

Here's the fiddle demonstrating my technique: https://jsfiddle/ngxjnywz/5/

发布评论

评论列表(0)

  1. 暂无评论