This is the JavaScript for the canvas element. There must be something wring with the code that fills and draw the rectangles:
function initCanvas() {
var ctx = document.getElementById("my_canvas").getContext("2d");
ctx.canvas.addEventListener("mousemove", function(event) {
var mouseX = event.clientX - ctx.canvas.offsetLeft;
var mouseY = event.clientY - ctx.canvas.offsetTop;
var status = document.getElementById("status");
status.innerHTML = mouseX + " | " + mouseY;
});
ctx.canvas.addEventListener("click", function(event) {
var mouseX = event.clientX - ctx.canvas.offsetLeft;
var mouseY = event.clientY - ctx.canvas.offsetTop;
//alert(mouseX + " | " + mouseY);
ctx.fillStyle = "orange";
ctx.fillRect(mouseX, mouseY, 30, 30);
});
}
window.addEventListener("load", function(event) {
initCanvas();
});
This is the HTML with the canvas element
<body>
<canvas id="my_canvas"></canvas>
<h2 id="status">0 | 0</h2>
</body>
This is the CSS giving the canvas a height and width
canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100vh;
}
h2 {
color: black;
font-family: Varela Round;
}