setup()
setup() runs exactly once when the sketch starts. By generating the landscape here instead of in draw(), you ensure mountains and clouds don't change randomly each frame, keeping the scene stable and the code efficient.
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
// Generate static landscape objects once to keep performance high
generateLandscape();
}
Line-by-line explanation (3 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the scene full-screen immersive
textAlign(CENTER, CENTER);- Centers all text drawing both horizontally and vertically—text coordinates now point to the center of the text, not the top-left
generateLandscape();- Calls a helper function once to create the mountains and clouds arrays—this runs only at startup, not every frame, saving performance