setup()
setup() runs once when the sketch starts. It's the perfect place to initialize your canvas, set up color modes, create graphics buffers, and calculate starting values. The off-screen graphics buffer (pathLayer) is key to this sketch—it lets us draw continuously without clearing, creating the accumulating pattern effect.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100); // HSB with alpha 0–100
smooth();
// Layer that accumulates the drawing over time
pathLayer = createGraphics(windowWidth, windowHeight);
pathLayer.colorMode(HSB, 360, 100, 100, 100);
pathLayer.background(0, 0, 0); // black
pathLayer.smooth();
pathLayer.strokeCap(ROUND);
initSpiroParams();
}
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
Creates a full-window canvas and switches to HSB color mode for easier rainbow color transitions
pathLayer = createGraphics(windowWidth, windowHeight);
pathLayer.colorMode(HSB, 360, 100, 100, 100);
pathLayer.background(0, 0, 0);
Creates a persistent graphics layer that accumulates all drawn lines without clearing each frame
initSpiroParams();
Calls the function to calculate all spirograph parameters based on canvas dimensions
Line by Line:
createCanvas(windowWidth, windowHeight)- Creates a canvas that fills the entire browser window, making the sketch responsive to window size
colorMode(HSB, 360, 100, 100, 100)- Switches from RGB to HSB (Hue, Saturation, Brightness) color mode with ranges 0-360 for hue and 0-100 for saturation, brightness, and alpha. This makes rainbow color transitions much easier
smooth()- Enables anti-aliasing for smoother, less pixelated lines and shapes
pathLayer = createGraphics(windowWidth, windowHeight)- Creates an off-screen graphics buffer (invisible drawing surface) that will accumulate all the spirograph lines without being cleared each frame
pathLayer.colorMode(HSB, 360, 100, 100, 100)- Sets the same HSB color mode on the graphics buffer so colors match the main canvas
pathLayer.background(0, 0, 0)- Fills the graphics buffer with black (HSB: hue=0, saturation=0, brightness=0) as the starting background
pathLayer.strokeCap(ROUND)- Makes the ends of drawn lines rounded instead of square, creating a smoother visual appearance
initSpiroParams()- Calls the initialization function to calculate spirograph parameters (R, r, d, k) based on the canvas size