setup()
setup() runs once when the sketch starts. It is the perfect place to initialize variables, create the canvas, and prepare UI elements like buttons. Everything calculated here sets up the initial state that draw() will use.
function setup() {
createCanvas(windowWidth, windowHeight);
// Initialize lavaY here, after createCanvas has set 'height'
lavaY = height; // Initial lava position (off-screen bottom)
// Calculate rock position and size
rockX = width / 2;
rockY = height * 0.7; // Position the rock lower on the screen
rockSize = min(width, height) * 0.2; // Rock size is 20% of the smaller canvas dimension
// Calculate UFO size
ufoSize = min(width, height) * 0.15; // UFO size is 15% of the smaller canvas dimension
// Initialize UFO position off-screen (top-left)
ufoX = -ufoSize;
ufoY = -ufoSize;
eventStartTime = millis(); // Start the timer immediately
console.log(`Waiting for ${EVENT_DELAY_SECONDS} seconds...`);
// Create the reset button
resetBtn = createButton('Reset (Next: Lava Event)'); // Initial text
resetBtn.position(10, 50); // Position below the state/timer display
resetBtn.style('font-size', '16px');
resetBtn.style('padding', '10px 15px');
resetBtn.style('border-radius', '5px');
resetBtn.style('cursor', 'pointer');
resetBtn.style('background-color', '#4CAF50'); // Green
resetBtn.style('color', 'white');
resetBtn.style('border', 'none');
resetBtn.mousePressed(resetSketch);
resetBtn.touchStarted(resetSketch); // For touch devices
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that fills the entire browser window
rockX = width / 2;
rockY = height * 0.7;
rockSize = min(width, height) * 0.2;
Calculates the rock's center position and diameter based on canvas size
ufoSize = min(width, height) * 0.15;
ufoX = -ufoSize;
ufoY = -ufoSize;
Sets UFO size and hides it off-screen (top-left corner)
resetBtn = createButton('Reset (Next: Lava Event)');
Creates a clickable reset button that lets the user restart and toggle events
createCanvas(windowWidth, windowHeight);- Creates a canvas the size of the entire browser window. windowWidth and windowHeight are p5.js variables that update whenever the browser resizes.
lavaY = height;- Initializes the lava position to the bottom of the canvas (off-screen). Must happen after createCanvas so 'height' is defined.
rockX = width / 2;- Places the rock horizontally at the center of the canvas.
rockY = height * 0.7;- Places the rock vertically at 70% down the canvas, keeping it in the lower portion so lava and UFO can approach from outside.
rockSize = min(width, height) * 0.2;- Makes the rock 20% of the smaller canvas dimension. Using min() ensures the rock stays proportional whether the window is wide or tall.
ufoSize = min(width, height) * 0.15;- Sets the UFO to be 15% of the smaller canvas dimension, keeping it slightly smaller than the rock.
ufoX = -ufoSize;- Positions the UFO completely off-screen to the left, so it doesn't appear until the event starts.
eventStartTime = millis();- Records the current time in milliseconds. Later, draw() will compare millis() to this value to trigger the event after a delay.
resetBtn.mousePressed(resetSketch);- Attaches the resetSketch function to the button, so clicking it calls resetSketch().
resetBtn.touchStarted(resetSketch);- Also allows touch devices (phones, tablets) to trigger reset by tapping the button.