setup()
setup() runs once when the sketch starts. It is the ideal place to define your canvas size, set global colors, and initialize your scene by creating data structures like arrays of objects.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(RGB);
// Define colors once
topSkyColor = color(16, 10, 38); // deep purple-blue
horizonSkyColor = color(8, 40, 20); // dark green near horizon
groundColor = color(2, 18, 6); // very dark green ground
farGrassColor = color(8, 50, 18, 220);
midGrassColor = color(16, 90, 30, 240);
nearGrassColor = color(24, 130, 46, 255);
treeColor = color(2, 20, 10);
fireflyInnerColor = color(210, 255, 170); // bright core
fireflyOuterColor = color(190, 255, 150); // soft outer glow
initScene();
}
Line-by-line explanation (7 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the scene immersive and responsive
colorMode(RGB);- Sets the color mode to RGB so all color() calls use Red, Green, Blue values (0–255)
topSkyColor = color(16, 10, 38);- Defines a deep purple-blue color for the top of the gradient and stores it in a variable so it can be reused without recalculating
horizonSkyColor = color(8, 40, 20);- Defines a dark green color for the horizon where the sky meets the landscape
farGrassColor = color(8, 50, 18, 220);- Defines a muted green with 220 alpha (slightly transparent) for distant grass blades that appear farther away
fireflyInnerColor = color(210, 255, 170);- Bright yellowish-green for the firefly's core, which glows inside the softer outer glow
initScene();- Calls the initScene() function to populate the scene with stars, trees, grass, and fireflies after colors are defined