I'm fairly new with OOP and classes but was experimenting with it, while trying to create a method that would randomly create triangles on my HTML5 canvas.
The problem is that I can only see the last rectangle produced on the loop. All the other rectangles are not shown on the screen. Here is my code.
class Animations{
constructor(){}
//Background animation
backgroundAnimation(x,y,height,width){
this._x = x;
this._y = y;
this._height = height;
this._width = width;
let canvas = document.querySelector('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let c = canvas.getContext('2d');
c.beginPath;
c.rect(this._x,this._y,this._width,this._height);
c.fillStyle = 'blue';
c.fill()
c.stroke();
}
}
var animations = new Animations();
window.addEventListener("load", ()=>{
for(let i=0;i<10;i++){
let x = Math.floor(Math.random()*100+50);
let y = Math.floor(Math.random()*100+50);
animations.backgroundAnimation(x,y,20,20);
}
});
<canvas></canvas>
Any help would be greatly appreciated. Thanks in advance :)