I have two HTML canvas elements, and I've drawn some lines on them.
The two canvases are basically identical except that in one I have specified the width and height using tag attributes (canvas-1), and in the other one I have specified the width and height in CSS (canvas-2).
I've found that when I log width and height (I do this in drawDividers
below), the height for canvas-2 is 150 instead of 300 as I would have expected. I find it surprising that even with height
very wrong, the lines were drawn basically where I would have expected them, except blurry. What is going on here? Is there a simple mental model I could adopt of HTML/CSS/canvas that could explain this?
I am on Chrome in Windows in case it is relevant.
Demo here: codepen link
HTML
<canvas id="canvas-1" class="outlined" width=300 height=300></canvas>
<span> __ </span>
<canvas id="canvas-2" class="outlined"></canvas>
CSS
#canvas-1 {
outline-style: solid;
}
#canvas-2 {
width: 300px;
height: 300px;
outline-style: solid;
}
Javascript
function drawDividers(eid) {
var canvas = document.getElementById(eid);
var width = canvas.width;
var height = canvas.height;
console.log(eid, width, height);
var ctx = canvas.getContext('2d');
ctx.lineWidth = 1;
for (var i = 1; i <= 2; i++) {
ctx.moveTo(i*width/3, 0.1*height);
ctx.lineTo(i*width/3, 0.9*height);
ctx.stroke();
ctx.moveTo(0.1*width, i*height/3);
ctx.lineTo(0.9*width, i*height/3);
ctx.stroke();
}
}
drawDividers('canvas-1');
drawDividers('canvas-2');