setup()
setup() runs once when the sketch starts. It's the right place to configure global settings like canvas size and color mode before anything gets drawn or calculated.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
maxAngle = radians(MAX_ANGLE_DEG);
initPendulums();
}
Line-by-line explanation (4 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, so the sketch always uses all available screen space.
colorMode(HSB, 360, 100, 100, 100);- Switches p5's color system to Hue-Saturation-Brightness with ranges 0-360 for hue and 0-100 for the rest, which makes generating a rainbow of colors as simple as changing one number (hue).
maxAngle = radians(MAX_ANGLE_DEG);- Converts the MAX_ANGLE_DEG constant (30 degrees) into radians, since p5's trig functions like cos() and sin() expect radians, not degrees.
initPendulums();- Calls the helper function that actually builds the array of Pendulum objects, keeping setup() short and readable.