I try to overlay a canvas
exactly on top of a 256px by 256px image (of 8x8 grids) and draw an 8x8 black square --
var canvas = document.querySelector("canvas");
canvas.width = 1024;
canvas.height = 1024;
var c = canvas.getContext("2d");
c.fillRect(0, 0, 8, 8);
#wrapper {
margin: 0 auto;
width: 256px;
}
#img {
position: relative;
top: 0;
left: 0;
}
#canvas {
position: relative;
margin-top: 0;
margin-left: 0;
top: -256px;
left: 0;
z-index: 1;
}
<html>
<body>
<div id="wrapper">
<img id="img" src="https://i.imgur.com/0qjIa89.png" />
<canvas id="canvas"></canvas>
</div>
</body>
</html>
However, I don't understand why the black square drawn is slightly offset down relative to the grid image --
complete jsfiddle https://jsfiddle.net/ze8ufqcv/7/
How to position the canvas drawing exactly on top of the image?