xxxxxxxxxx
52
// Reference: https://p5js.org/zh-Hans/examples/simulate-snowflakes.html
let snowflakes = [];
function setup() {
createCanvas(500, 500);
}
function draw() {
background('blue');
let t = frameCount / 60;
for (let i = 0; i < random(5); i++) {
snowflakes.push(new Snowflake());
}
for (let flake of snowflakes) {
flake.update(t);
flake.display();
}
// console.log(snowflakes.length)
}
class Snowflake {
constructor() {
this.pos = createVector(random(width), random(-50, 0));
this.vel= createVector();
this.initAngle = random(0, 2 * PI);
this.size = random(2,5);
this.radius = sqrt(random(pow(width / 2, 2)));
}
update(time) {
let w = 0.6;
let angle = w * time + this.initAngle;
this.pos.x = width / 2 + this.radius * sin(angle);
this.pos.y += pow(this.size, 0.5);
if (this.pos.y > height) {
let index = snowflakes.indexOf(this);
snowflakes.splice(index, 1);
}
}
display() {
noStroke();
fill(255, 255, 255, 100);
ellipse(this.pos.x, this.pos.y, this.size);
}
}