setup()
setup() runs once when the sketch starts. Here it's used to configure the 3D rendering mode and precompute expensive data (the star field) so draw() only has to render, not recalculate, every frame.
function setup() {
// Create a WebGL canvas that fills the window
createCanvas(windowWidth, windowHeight, WEBGL);
// Disable drawing outlines for stars and glow
noStroke();
// Enable anti-aliasing for smoother edges (optional, but recommended)
smooth();
// Define the purple and blue colors for the gradient
purpleColor = color(80, 0, 120);
blueColor = color(0, 50, 150);
// Define the warm white/yellow color for the glowing center
glowCenterColor = color(255, 240, 220);
// Generate all the star positions and colors
generateStars();
}
Line-by-line explanation (7 lines)
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a canvas that fills the browser window and enables the WEBGL renderer, which is required for 3D drawing like sphere() and vertex() with a z coordinate.
noStroke();- Turns off outlines so points and spheres are drawn as solid filled shapes without borders.
smooth();- Enables anti-aliasing so edges of 3D shapes look less jagged.
purpleColor = color(80, 0, 120);- Defines the deep purple color used near the galaxy's core in the color gradient.
blueColor = color(0, 50, 150);- Defines the blue color used towards the outer edge of the spiral arms.
glowCenterColor = color(255, 240, 220);- Defines a warm white/yellow color used for both the core stars and the glowing sphere layers.
generateStars();- Calls the function that computes and stores all 20,000 star positions and colors just once, since the galaxy's shape doesn't need to be recalculated every frame.