Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incorrect display lineWidth=1 at html5 canvas

I have example: https://developer.mozilla.org/samples/canvas-tutorial/4_5_canvas_linewidth.html But first line is not equals 1 pixel: enter image description here How can i fix this? (browser Google Chrome)

like image 435
Glebka Avatar asked Sep 30 '11 07:09

Glebka


2 Answers

Always add 0.5 pixel to the position of your line to prevent the anti-aliasing.

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Applying_styles_and_colors#A_lineWidth_example

like image 111
chris_b Avatar answered Sep 22 '22 16:09

chris_b


To make life easier you can move the whole canvas by 0.5px:

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.translate(0.5, 0.5); // Move the canvas by 0.5px to fix blurring

It prevents anti-aliasing on all graphics, except images, so you'll have to use +0.5px only for images.

like image 27
Yakovenko Max Avatar answered Sep 22 '22 16:09

Yakovenko Max