setup()
setup() runs once when the sketch loads. Here we initialize the canvas size, color mode, and pre-compute expensive graphics like the gradient background so they don't need to be recalculated every frame.
function setup() {
createCanvas(windowWidth, windowHeight);
// Use HSB so we can easily cycle through hues
colorMode(HSB, 360, 100, 100, 100);
// Create a graphics buffer for the background gradient
bgGradient = createGraphics(width, height);
drawBgGradient(); // Draw the gradient once
background(0, 0, 100, 100); // Start with a solid white background (initial clear)
}
Line-by-line explanation (5 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window, making the experience immersive
colorMode(HSB, 360, 100, 100, 100);
Switches to Hue-Saturation-Brightness mode with hue 0–360, making color cycling smooth and intuitive
bgGradient = createGraphics(width, height);
Creates an off-screen graphics object where the background gradient is drawn once and reused, improving performance
createCanvas(windowWidth, windowHeight);- Creates a canvas the size of the browser window so the kaleidoscope fills your entire screen
colorMode(HSB, 360, 100, 100, 100);- Switches color mode to HSB so hues cycle smoothly from 0–360; saturation and brightness range 0–100 each
bgGradient = createGraphics(width, height);- Creates an off-screen graphics buffer the same size as the canvas where we will draw the background gradient
drawBgGradient(); // Draw the gradient once- Calls the function that draws concentric circles to create the radial gradient, done once at startup for efficiency
background(0, 0, 100, 100); // Start with a solid white background (initial clear)- Fills the canvas with white (hue 0, sat 0, bright 100) to start with a clean slate