setup()
setup() runs once when the sketch starts. It initializes the canvas, defines colors, generates stars, and creates UI elements. This is where you set up everything the sketch needs before animation begins.
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
colorMode(RGB);
gradientColors = [
color(0, 0, 20),
color(0, 0, 40),
color(0, 0, 60),
color(0, 0, 80)
];
generateStars();
clearButton = createButton('Clear Constellations');
clearButton.position(20, 20);
clearButton.mousePressed(clearConnections);
clearButton.addClass('p5-button');
infoDiv = createDiv(`Stars: ${starCount} | Lines: ${lineCount}`);
infoDiv.position(20, 60);
infoDiv.addClass('p5-info');
}
๐ง Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window
gradientColors = [color(0, 0, 20), color(0, 0, 40), color(0, 0, 60), color(0, 0, 80)];
Defines four shades of dark blue for the night sky gradient background
clearButton = createButton(...); infoDiv = createDiv(...);
Creates interactive button to clear connections and info display showing star/line counts
Line by Line:
createCanvas(windowWidth, windowHeight);- Creates a canvas that matches the full browser window size, allowing the sketch to fill the entire screen
noStroke();- Disables outlines on all shapes drawn after this point, so stars and shapes will have no borders
colorMode(RGB);- Sets color mode to RGB (Red, Green, Blue), which is the default and what we use throughout the sketch
gradientColors = [color(0, 0, 20), color(0, 0, 40), color(0, 0, 60), color(0, 0, 80)];- Creates an array of four dark blue colors, each progressively lighter, used to create a smooth gradient background
generateStars();- Calls the function that randomly generates 50-80 stars and populates the stars array
clearButton = createButton('Clear Constellations');- Creates a clickable button with the label 'Clear Constellations' and stores it in the clearButton variable
clearButton.position(20, 20);- Positions the button 20 pixels from the left and 20 pixels from the top of the window
clearButton.mousePressed(clearConnections);- Attaches the clearConnections function to the button, so clicking it calls that function
infoDiv = createDiv(`Stars: ${starCount} | Lines: ${lineCount}`);- Creates a text display showing the current number of stars and constellation lines
infoDiv.position(20, 60);- Positions the info display 20 pixels from the left and 60 pixels from the top (below the button)