setup()
setup() runs once when the sketch loads. It establishes the canvas size, color space, and default styles for all subsequent frames. WEBGL mode is required for 3D shapes like sphere() and torus().
function setup() {
// WEBGL mode: https://p5js.org/reference/#/p5/createCanvas
createCanvas(windowWidth, windowHeight, WEBGL);
pixelDensity(1); // mobile-friendly: keeps GPU/CPU load saner
colorMode(HSB, 360, 100, 100, 100);
// Use browser default sans-serif font for HUD.
// We do NOT call loadFont(), to avoid OpenType parsing errors.
textFont("sans-serif");
textAlign(CENTER, CENTER);
textSize(16);
noStroke();
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a full-screen canvas in 3D WEBGL mode with dynamic window sizing
colorMode(HSB, 360, 100, 100, 100);
Switches to Hue-Saturation-Brightness color space, making hue-based animations intuitive
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a full-screen 3D canvas. The third argument WEBGL enables 3D rendering instead of 2D.
pixelDensity(1);- Sets pixel density to 1 for mobile performance—high-DPI devices won't render at 2x resolution, saving GPU power
colorMode(HSB, 360, 100, 100, 100);- Switches to HSB (Hue, Saturation, Brightness) where colors are defined by hue (0-360 degrees), saturation (0-100%), and brightness (0-100%). This makes rotating through colors feel more natural than RGB.
textFont("sans-serif");- Sets the font for text rendered in WEBGL to the browser's default sans-serif—avoiding font loading errors
textAlign(CENTER, CENTER);- Centers text both horizontally and vertically around its position—useful for overlay instructions
textSize(16);- Sets the default text size to 16 pixels
noStroke();- Disables outlines on all 3D shapes—only fills will be visible