setup()
setup() runs once when the sketch starts, and is the right place to build your canvas, off-screen buffers, initial object positions, and any HTML UI elements you need.
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1); // make per-pixel math predictable
createFieldGraphics();
initSources();
setupUI();
timeSec = 0;
lastMillis = millis();
}
Line-by-line explanation (7 lines)
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window.
pixelDensity(1); // make per-pixel math predictable- Forces exactly 1 pixel of canvas data per screen pixel, so the pixel-array math in renderField() lines up correctly even on high-DPI (Retina) screens.
createFieldGraphics();- Builds the smaller off-screen buffer used to compute the wave pattern efficiently.
initSources();- Places the two wave source objects (A and B) at their starting positions.
setupUI();- Builds the HTML slider and button controls inside the #controls panel.
timeSec = 0;- Resets the simulation clock to zero at the start.
lastMillis = millis();- Records the current real-world time so updateTime() can measure elapsed time correctly on the first frame.