The code I am using is supposed to make an image, square.png, move to the cursor. However, the code cannot run.
I've tried to look for typos and errors in my code.
var canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
document.body.appendChild(canvas);
document.addEventListener('mousemove',function(event){
var similarX = event.clientX;
var similarY = event.clientY;
document.getElementById('body').innerHTML = "x:" + similarX + ", y:" + similarY;
})
var square = new Image();
square.src = 'square.png';
window.addEventListener('load' , start);
var c = canvas.getContext('2d');
function start() {
c.fillStyle = 'green';
c.fillRect = 10, 10, 30, 30;
c.drawImage(square,similarX , similarY);
window.requestAnimationFrame(start)
}
document.body.appendChild(canvas);
this is from between the script tags in an html document.
The square is supposed to be drawn at where the mouse pointer is, but according to Chrome dev tools, the variables similarX and similarY are not recognized in the function "load".