xxxxxxxxxx
114
let stars = [];
let connections = [];
let bg;
function preload() {
bg = loadImage("sky_bg.jpg");
}
class Star {
constructor(x, y) {
this.xPos = x;
this.yPos = y;
this.baseSize = random(2, 4);
this.brightness = random(150, 255);
this.twinkleSpeed = random(0.02, 0.07);
this.alpha = random(120, 180);
this.angle = random(TWO_PI); // Random start angle
}
display() {
noStroke();
fill(this.brightness, this.alpha);
// Pulsate size smoothly
let pulsate = sin(this.angle) * 1.5;
let size = this.baseSize + pulsate;
ellipse(this.xPos, this.yPos, size);
}
twinkle() {
this.angle += this.twinkleSpeed;
this.brightness = 150 + sin(this.angle) * 105; // Creates more noticeable twinkle
}
setBrightness(brightness) {
this.brightness = brightness;
}
setSizeAndAlpha(d) {
this.baseSize = map(d, 0, 50, 4, 2); // Closer stars are bigger
this.alpha = map(d, 0, 50, 255, 100); // More transparent as they move away
}
}
function createConstellation(x, y) {
let constellationStars = [];
let numStarsInConstellation = int(random(4, 8));
for (let star of stars) {
let d = dist(x, y, star.xPos, star.yPos);
if (d < 100) {
constellationStars.push(star);
// Update size and alpha based on distance from the mouse click
if (d < 50) {
star.setSizeAndAlpha(d);
}
}
}
connections = [];
for (let i = 0; i < constellationStars.length; i++) {
for (let j = i + 1; j < constellationStars.length; j++) {
let d = dist(
constellationStars[i].xPos,
constellationStars[i].yPos,
constellationStars[j].xPos,
constellationStars[j].yPos
);
if (d < 70) {
connections.push([constellationStars[i], constellationStars[j]]);
}
}
}
for (let star of constellationStars) {
star.setBrightness(255);
}
}
function drawConstellations() {
stroke(255, 150);
for (let conn of connections) {
line(conn[0].xPos, conn[0].yPos, conn[1].xPos, conn[1].yPos);
}
}
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 150; i++) {
stars.push(new Star(random(width), random(height)));
}
}
function draw() {
image(bg, 0, 0, width, height);
for (let star of stars) {
star.twinkle();
star.display();
}
drawConstellations();
}no
function mousePressed() {
createConstellation(mouseX, mouseY);
}
function keyPressed() {
createConstellation(mouseX, mouseY);
}