xxxxxxxxxx
61
let leaves = [];
const numLeaves = 500;
let windDir;
function setup() {
createCanvas(400, 400);
windDir = random(-1, 1);
for(let i = 0; i < numLeaves; i++) {
leaves.push(randomLeaf());
}
noStroke();
fill('#dd8800');
}
function draw() {
background(220);
leaves = leaves.map(leaf => {
leaf.update();
leaf.draw();
return leaf.y < height ? leaf : randomLeaf();
});
}
function randomLeaf() {
return new Leaf(random(-50, width + 50), random(-10, -50), windDir, 2);
}
class Leaf {
constructor(x, y, vx, vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.a = random(TAU);
this.r = random(10, 20);
}
update() {
this.x += this.vx;
this.y += this.vy;
this.a += 0.05;
}
draw() {
push();
translate(this.x, this.y);
rotate(this.a);
translate(this.r, 0);
circle(0, 0, 5);
pop();
}
}