I am a beginner to JavaScript, I already built a simply pygame with oo design and is translating it to JavaScript.
So in the main function, I am initializing a canvas in main,
let canvas = new Canvas();
canvas.render();
inside Canvas.js it is like this
class Canvas{
constructor() {
this.canvas = document.getElementById("main");
this.ctx = this.canvas.getContext('2d');
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
window.addEventListener('resize', function(){
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
});
this.img = new Image();
this.img.src = './image/background.png';
}
render() {
requestAnimationFrame(this.paint);
}
paint = () => {
this.img.onload = () => {
this.ctx.drawImage(this.img,this.canvas.width/2 - this.img.width/2 ,this.canvas.height/2 - this.img.height/2);
}
}
}
export {Canvas};
The resize function inside constructor is not working. I have searched but still can't find what's my problem. Thanks!