need help!!! i am following this new javascript 2D gaming video and i am currently stuck on 2:32:29 where we are making an explosion effect on canvas where the mouse clicked. https://www.youtube.com/watch?v=GFO_txvwK_c&t=9365s.
problem: whenever i click on the canvas, no sqaure shapes shows up, in the tutorial it mentioned you will have to offset the e.x and e.y to make it relative the canvas. but i realized whenever i click the top left corner of the screen. sometimes an elongated rectangular shape does appear but when i offset my e.x and e.y. i can not get the correct offset to make it work. could anyone help explain why i am getting this weird offset effect on my event.x and event.y values and how to fix it? thanks a lot!!! you can see the thrid picture attached for my code adding the offset suggested by video and it still does not work.
const canvas=document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = 500;
canvas,height = 700;
const explosion =[];
class Explosion {
constructor(x,y){
this.x = x;
this.y = y;
this.spriteWidth = 200;
this.spriteHeight = 179;
this.width = this.spriteWidth/2;
this.height = this.spriteHeight/2;
this.image = new this.image();
this.image.src = "boom.png";
this.frame=0;
}
update(){
this.frame++;
}
draw(){
ctx.drawImage(this.image,this.spriteWidth*this.frame,0,this.spriteWidth,this.spriteHeight,this.x,this.y,this.width, this.height);
}
}
window.addEventListener('click', function(e){
ctx.fillStyle= 'white';
ctx.fillRect(e.x,e.y,50,50);
})