xxxxxxxxxx
57
class Star{
constructor(x,y){
this.xPos= x;
this.yPos= y;
this.size = random(1, 3);
this.brightness = random(150, 255);
this.twinkleSpeed = random(0.01, 0.05);
}
display() {
noStroke();
fill(this.brightness);
ellipse(this.xPos, this.yPos, this.size);
}
twinkle() {
this.brightness += this.twinkleSpeed;
if (this.brightness > 255 || this.brightness < 150) {
this.twinkleSpeed *= -1;
}
}
}
let stars = [];
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 100; i++) {
stars.push(new Star(random(width), random(height)));
}
}
function drawConstellations() {
stroke(255, 100);
for (let i = 0; i < stars.length; i++) {
for (let j = i + 1; j < stars.length; j++) {
let d = dist(stars[i].xPos, stars[i].yPos, stars[j].xPos, stars[j].yPos);
if (d < 100) { // Adjust threshold as needed
line(stars[i].xPos, stars[i].yPos, stars[j].xPos, stars[j].yPos);
}
}
}
}
function draw() {
background(0);
drawConstellations();
for (let star of stars) {
star.twinkle();
star.display();
}
}
function mousePressed() {
stars = [];
for (let i = 0; i < 100; i++) {
stars.push(new Star(random(width), random(height)));
}
}