p5(sketch => {
let snowflakes = [];
sketch.setup = function() {
sketch.createCanvas(400, 600);
sketch.fill(240);
sketch.noStroke();
}
sketch.draw = function() {
sketch.background('brown');
let t = sketch.frameCount / 60;
for (let i = 0; i < sketch.random(5); i++) {
snowflakes.push(new snowflake());
}
for (let flake of snowflakes) {
flake.update(t);
flake.display();
}
}
function snowflake() {
this.posX = 0;
this.posY = sketch.random(-50, 0);
this.initialangle = sketch.random(0, 2 * sketch.PI);
this.size = sketch.random(2, 5);
this.radius = sketch.sqrt(sketch.random(sketch.pow(width / 2, 2)));
this.update = function(time) {
let w = 0.6;
let angle = w * time + this.initialangle;
this.posX = width / 2 + this.radius * sketch.sin(angle);
this.posY += sketch.pow(this.size, 0.5);
if (this.posY > sketch.height) {
let index = snowflakes.indexOf(this);
snowflakes.splice(index, 1);
}
};
this.display = function() {
sketch.ellipse(this.posX, this.posY, this.size);
};
}
})