xxxxxxxxxx
65
let stars = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 100; i++) {
stars.push(new Star(random(width), random(height), random(2, 5)));
}
}
function draw() {
background(0);
for (let star of stars) {
star.update();
star.checkEdges();
star.display();
}
}
class Star {
constructor(x, y, r) {
this.position = createVector(x, y);
this.velocity = createVector(random(-2, 2), random(-2, 2));
this.r = r;
}
update() {
this.position.add(this.velocity);
}
checkEdges() {
if (this.position.x > width - this.r || this.position.x < this.r) {
this.velocity.x *= -1;
}
if (this.position.y > height - this.r || this.position.y < this.r) {
this.velocity.y *= -1;
}
}
display() {
noStroke();
fill(255, 204, 0);
push();
translate(this.position.x, this.position.y);
rotate(frameCount / 100); // Slow rotation for twinkling effect
this.drawStar(0, 0, this.r, this.r / 2, 5); // 5-pointed star
pop();
}
// Function to draw a star shape
drawStar(x, y, radius1, radius2, npoints) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius1;
let sy = y + sin(a) * radius1;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius2;
sy = y + sin(a + halfAngle) * radius2;
vertex(sx, sy);
}
endShape(CLOSE);
}
}