xxxxxxxxxx
63
let stars = [];
function setup() {
createCanvas(windowWidth, windowHeight);
}
function draw() {
background(0);
for (let i = 0; i < stars.length; i++) {
stars[i].fall();
stars[i].display();
}
if (random(1) < 0.05) {
for (let i = 0; i < 5; i++) {
stars.push(new Star());
}
}
for (let i = stars.length - 1; i >= 0; i--) {
if (stars[i].y > height) {
stars.splice(i, 1);
}
}
}
class Star {
constructor() {
this.x = random(width);
this.y = 0;
this.speed = random(1, 3);
this.color = color(random(200, 255), random(200, 255), random(200, 255), 100);
this.size = random(10, 20);
this.rotation = random(-PI/4, PI/4);
}
fall() {
this.y += this.speed;
this.x += random(-1, 1);
}
display() {
noStroke();
fill(this.color);
push();
translate(this.x, this.y);
rotate(this.rotation);
beginShape();
vertex(0, -this.size/2);
vertex(this.size/6, -this.size/6);
vertex(this.size/2, -this.size/6);
vertex(this.size/3, this.size/6);
vertex(this.size/2, this.size/2);
vertex(0, this.size/3);
vertex(-this.size/2, this.size/2);
vertex(-this.size/3, this.size/6);
vertex(-this.size/2, -this.size/6);
vertex(-this.size/6, -this.size/6);
endShape(CLOSE);
pop();
}
}