setup()
setup() runs once when the sketch starts. WEBGL mode is crucial here—it's what enables smooth color interpolation across triangles and fast 3D transforms. HSB color mode makes cycling through rainbows mathematically simple: you just increment hue and it wraps around naturally.
function setup() {
// We MUST use WEBGL mode to get smooth per-vertex color interpolation
createCanvas(windowWidth, windowHeight, WEBGL);
// HSB makes it much easier to cycle through rainbow gradients
colorMode(HSB, 360, 100, 100, 100);
// Make the grid 1.5x larger than screen so edges don't show when rotated
let w = windowWidth * 1.5;
let h = windowHeight * 1.5;
cols = floor(w / scl);
rows = floor(h / scl);
// Calculate exact pixel dimensions of the generated grid
realW = cols * scl;
realH = rows * scl;
noStroke(); // Hide wireframe grid lines for a smooth gradient surface
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight, WEBGL);
Creates a 3D canvas that supports vertex-by-vertex color interpolation and smooth transformations
colorMode(HSB, 360, 100, 100, 100);
Switches from RGB to HSB (hue, saturation, brightness) so rainbow color cycles are easier to calculate
cols = floor(w / scl);
rows = floor(h / scl);
realW = cols * scl;
realH = rows * scl;
Calculates how many vertices fit in the grid and the exact pixel size for centering the mesh
createCanvas(windowWidth, windowHeight, WEBGL);- Creates a full-window canvas in WEBGL mode, which is required for smooth 3D rendering and per-vertex color blending
colorMode(HSB, 360, 100, 100, 100);- Switches to HSB color mode (Hue 0-360, Saturation and Brightness 0-100) so you can cycle through rainbow gradients smoothly
let w = windowWidth * 1.5; let h = windowHeight * 1.5;- Makes the mesh 1.5 times larger than the visible screen so rotations don't expose empty edges
cols = floor(w / scl); rows = floor(h / scl);- Divides the width and height by the scale value to determine how many grid points fit horizontally and vertically
realW = cols * scl; realH = rows * scl;- Calculates the exact final mesh dimensions so it can be centered at (0, 0) for proper rotation
noStroke();- Disables drawing of triangle edges, leaving only smooth color gradients without visible grid lines