setup()
setup() runs once when the sketch loads. It initializes the canvas size, places it in the DOM, and creates the two main objects that power the entire sketch. The responsive sizing pattern using min() and windowWidth makes this sketch adapt to different screen sizes.
function setup() {
let canvas = createCanvas(min(windowWidth - 40, 1200), 400);
canvas.parent('canvas-container');
station = new WeatherStation();
gui = new GUI();
// textFont('Roboto'); // Roboto is loaded via CSS, this should work fine for 2D canvas.
}
Line-by-line explanation (4 lines)
let canvas = createCanvas(min(windowWidth - 40, 1200), 400);- Creates a canvas that is either 40 pixels narrower than the window or 1200 pixels wide (whichever is smaller), with a fixed height of 400 pixels - this responsive sizing ensures the sketch looks good on phones and large monitors
canvas.parent('canvas-container');- Tells p5.js to place the canvas inside the HTML div with id 'canvas-container' instead of at the top of the page
station = new WeatherStation();- Creates the main sensor data manager object that will store all readings, history, and statistics
gui = new GUI();- Creates the interactive interface object that builds buttons, dropdowns, sensor cards, and the statistics panel