setup()
setup() runs exactly once when the sketch starts. It initializes the canvas size, sets up the coordinate system (radians), and pre-generates all the static data structures (buildings and stars) that draw() will use every frame.
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(RADIANS);
createCityscape();
createStars();
}
Line-by-line explanation (4 lines)
createCanvas(windowWidth, windowHeight);- Creates a full-screen canvas that matches the browser window dimensions
angleMode(RADIANS);- Tells p5.js to use radians (0 to TWO_PI) instead of degrees—required for Math.sin() to work intuitively with animation
createCityscape();- Calls the helper function to generate an array of random buildings with varying heights, window counts, and colors
createStars();- Calls the helper function to populate the stars array with random positions and twinkling offsets