For horizontal and vertical lines, using a translation of 0.5 for odd stroke widths results in crisper and sharper lines. But what about diagonal lines?
<!DOCTYPE html>
<html lang="en">
<body style="background: black">
<button id="btn">Draw Next Line</button>
<br>
<canvas style="border: 2px solid red" id="cnv"></canvas>
<script>
const ctx = document.getElementById("cnv").getContext("2d");
debugger;
const delta = 25;
const color = 'white';
const W = window.innerWidth - 80;
const H = window.innerHeight - 100;
ctx.canvas.width = W;
ctx.canvas.height = H;
ctx.lineWidth = 1;
ctx.strokeStyle = color;
// diagonal line.
ctx.moveTo(0.5, 0);
ctx.lineTo(W, H);
ctx.stroke();
// vertical lines
let i = 0.5;
document.getElementById("btn").onclick = () => {
ctx.moveTo(i * delta, 0);
ctx.lineTo(i * delta, H);
ctx.stroke();
i++;
}
</script>
</body>
</html>
Observing the demo reveals that adding another line after the diagonal lines causes them to become bolder or thicker. How can we achieve consistent thickness and sharpness regardless of whether the diagonal line is drawn first or last?