xxxxxxxxxx
148
let table;
let sun;
let planets = [];
let angles = []; // Array to store current angles for each planet
let stars = [];
let shootingStars = []; // Array to store multiple shooting stars
let backgroundColor; // Variable to store the current background color
let lerpAmount = 0; // This controls the change between black and dark blue
function preload() {
// Load the CSV file containing planet data
table = loadTable("solar_system_planet_data.csv", "csv", "header");
}
function setup() {
createCanvas(600, 600);
// stars for a galaxy like background
for (let i = 0; i < 500; i++) {
stars.push({
x: random(width),
y: random(height),
size: random(1, 3),
});
}
// Initialize the sun
sun = {
x: width / 2,
y: height / 2,
diameter: 100,
color: color(255, 204, 0),
};
// Load planet data from the CSV file
for (let i = 0; i < table.getRowCount(); i++) {
let planet = {
name: table.getString(i, "Planet"),
distance: table.getNum(i, "Distance"),
diameter: table.getNum(i, "Diameter"),
speed: table.getNum(i, "Speed"),
color: table.getString(i, "Color"),
};
planets.push(planet);
angles.push(0); // Start all planets' angles at 0
}
// 3 shooting stars
for (let i = 0; i < 3; i++) {
shootingStars.push({
x: -50,
y: random(height / 2),
speedX: random(5, 10),
speedY: random(-2, 2),
visible: false,
timer: 0,
});
}
// main background color (black)
backgroundColor = color(0, 0, 0);
}
function draw() {
// Gradually change between black and dark blue for the background color
backgroundColor = lerpColor(color(0, 0, 0), color(0, 0, 60), lerpAmount);
// Only increase lerpAmount until it reaches 1 (fully dark blue)
if (lerpAmount < 1) {
lerpAmount += 0.001; // Adjust the rate of transition
}
background(backgroundColor); // Apply the background color
//stars in the background
for (let i = 0; i < stars.length; i++) {
fill(255);
noStroke();
ellipse(stars[i].x, stars[i].y, stars[i].size, stars[i].size);
}
// sun
fill(sun.color);
noStroke();
ellipse(sun.x, sun.y, sun.diameter);
// Draw and animate planets orbiting the sun
for (let i = 0; i < planets.length; i++) {
let planet = planets[i];
// planet position based on orbit
angles[i] += planet.speed;
let x = sun.x + cos(angles[i]) * planet.distance;
let y = sun.y + sin(angles[i]) * planet.distance;
// Draw the orbit path
stroke(255, 255, 255, 50);
noFill();
ellipse(sun.x, sun.y, planet.distance * 2);
// Draw the planet
noStroke();
fill(planet.color);
ellipse(x, y, planet.diameter);
// Display planet name
fill(255);
textSize(12);
text(planet.name, x + planet.diameter / 2 + 5, y);
}
// Animate multiple shooting stars
for (let i = 0; i < shootingStars.length; i++) {
let star = shootingStars[i];
if (star.visible) {
fill(255);
noStroke();
ellipse(star.x, star.y, 8, 8);
// Draw the trail of the shooting star
stroke(255);
line(star.x, star.y, star.x - star.speedX * 3, star.y - star.speedY * 3);
// Move the shooting star
star.x += star.speedX;
star.y += star.speedY;
// Reset the shooting star if it goes off the screen
if (star.x > width || star.y < 0 || star.y > height) {
star.visible = false;
}
} else {
// new shooting star every 10 frames
star.timer++;
if (star.timer > 10) {
star.x = -50;
star.y = random(height / 2);
star.speedX = random(5, 10);
star.speedY = random(-2, 2);
star.visible = true;
star.timer = 0;
}
}
}
}