简体   繁体   中英

Antialiasing when drawing lines in Canvas in Firefox

I'm trying to draw a diagram with canvas and want to get crisp lines, not anti-aliased. I know about the 0.5 offset you need to use to make lines fall exactly on screen pixels, but even with that I get anti-aliased lines in Firefox, while both Chrome and IE render it fine.

Here's some example code:

JS:

var canvas = document.getElementsByTagName('canvas')[0];
var ctx = canvas.getContext('2d');
canvas.width = 100;
canvas.height = 100;

ctx.translate(-0.5, -0.5); //To get crisp lines
ctx.lineWidth = 1;
ctx.strokeStyle = 'black';
for (var x = 20; x < 100; x += 20){
    ctx.moveTo(x, 20);
    ctx.lineTo(x,100);
    ctx.stroke();
}

See JsFiddle: http://jsfiddle.net/einaregilsson/9yrF6/8/

This is what it looks like in Chrome and IE:

Chrome渲染线

This is what it looks like in Firefox:

Firefox渲染线

This is Firefox 26 on Windows 7. I've tried turning off hardware acceleration, which someone suggested but that makes no difference. Any ideas how I can get crisp lines on Firefox?

Also, is there anyone on Firefox that doesn't get anti-aliased lines when they look at the Fiddle? I'm wondering if this is a general Firefox issue, or particular to my setup.

It looks like you're slightly zoomed-in on Firefox (notice how the lines are spaced slightly further apart)

Hit Ctrl + 0 to reset the zoom level. This should fix your problem.

在Firefox中也应该很清晰,您可能已经放大了。重置缩放(CTRL + 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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