class Shape {
constructor(xRange, yRange, yMargin, minSpeed, speedRange) {
this.x = Math.random() * xRange;
this.y = -yMargin - (Math.random() * (yRange + (2*yMargin)));
this.speed = minSpeed + (Math.random() * speedRange);
let s = Math.floor(Math.random() * shapes.length)
let c = Math.floor(Math.random() * colors.length)
this.shape = shapes[s]
this.color = colors[c]
if (this.shape.type === "path") {
this.rotate = Math.floor(Math.random() * 360)
}
}
fall() {
this.y += this.speed;
}
draw(ctx) {
switch (this.shape.type) {
case "circle":
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.shape.radius, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();
break;
case "rect":
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.shape.w, this.shape.h);
ctx.strokeRect(this.x, this.y, this.shape.w, this.shape.h);
break;
case "path":
let pSrc = new Path2D(this.shape.path);
let mat = new DOMMatrix();
mat.translateSelf(this.x, this.y);
mat.rotateSelf(this.rotate);
let p = new Path2D();
p.addPath(pSrc, mat)
ctx.fillStyle = this.color;
ctx.fill(p);
ctx.stroke(p);
break;
}
}
}