setup()
setup() runs once when the sketch starts. Here we create the canvas, configure p5.js color mode, disable automatic looping so we save CPU, and set up the auto-refresh timer. The noLoop()/redraw() pattern is useful for simulations that should only update on demand.
function setup() {
createCanvas(windowWidth, windowHeight);
colorMode(HSB, 360, 100, 100, 100);
textFont('monospace');
textSize(13);
generateWeather();
noLoop(); // draw only when we explicitly ask for it
// Set up automatic radar refresh
autoRefreshTimer = setInterval(() => {
generateWeather();
redraw(); // re-render once
}, autoRefreshMs);
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
colorMode(HSB, 360, 100, 100, 100);
Switches from RGB to HSB so we can easily map intensity values to hue (green→red spectrum)
noLoop(); // draw only when we explicitly ask for it
Stops p5.js from calling draw() every frame—we use redraw() only when weather changes
autoRefreshTimer = setInterval(() => {
generateWeather();
redraw(); // re-render once
}, autoRefreshMs);
Sets up a JavaScript interval that regenerates the weather every autoRefreshMs milliseconds
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window so the radar takes up the full screen
colorMode(HSB, 360, 100, 100, 100);- Switches color mode to HSB (Hue, Saturation, Brightness) with ranges 0–360, 0–100, 0–100, 0–100, making it easy to create the green-to-red weather color ramp
textFont('monospace');- Sets text to monospace font, giving the radar a technical, broadcast TV appearance
textSize(13);- Sets label text size to 13 pixels so city names and tornado labels are readable
generateWeather();- Calls the weather generation function once on startup to populate the initial echoes and tornadoes arrays
noLoop();- Disables the default 60-FPS draw loop so the sketch only redraws when we explicitly call redraw()
autoRefreshTimer = setInterval(() => {- Creates a JavaScript timer that runs a function every autoRefreshMs milliseconds (3000 ms = 3 seconds by default)