HTML5 Canvas translate(0.5,0.5) not fixing line blurryness
Reading related articles, I've tried forcing the canvas size in pixels, specifying half pixels for the paths and also context.translate(0.5) but my canvas lines are still blurry.
See Codepen.
const ctx = canvas.getContext("2d");
ctx.translate(0.5, 0.5);
/* Function to draw HTML5 canvas line */
const drawPath = (startX, startY, endX, endY) => {
ctx.beginPath();
ctx.lineWidth = "1";
ctx.strokeStyle = "red";
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.stroke();
};
Where am I going wrong and how can I make my lines crisp like the border around the boxes in the demo?
How it appears for me:
Retina & HiDPI devices
Looking at the image you supplied. The canvas is half the resolution of the display. Zoom in and look at the pixels on the "X", The DOM line is 1px yet still twice a wide as the pixels on that letter. That means the canvas is being stretched and the blur is due to the bilinear filtering. Either you are zoomed out on the tab or you have a retina or HiDPI display. Set the canvas.width
& canvas.height
to twice what it is, set the canvas.style.width
& canvas.style.height
to DOM pixels. Remove the ctx.translate
and add ctx.scale(2,2)
and all things will be clear.
A zoom in on the image