setup()
setup() runs once when the sketch starts. It is the ideal place to load images, create DOM elements, and set initial values. Notice how we use .hide() on both images at the start—the draw() function will show them conditionally on each frame.
🔬 This code positions the timer in the top-right corner. What happens if you change 'right' to 'left' on the last line? Where does the timer move?
timerDiv.style('position', 'absolute');
timerDiv.style('top', '15px');
timerDiv.style('right', '20px');
function setup() {
createCanvas(windowWidth, windowHeight);
const url1 = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgOLhscWLJ0cu5ZvaiLGuM5FnpRgfqHSki-w&s';
const url2 = 'https://m.media-amazon.com/images/I/61PsAlzDFZL.jpg';
img1 = createImg(url1, "first image");
img2 = createImg(url2, "second image");
// Style the images to act like a flat fullscreen presentation
// Added z-index: 1 to ensure they stay behind our new timer
let baseStyle = 'position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100vw; height: 100vh; object-fit: contain; pointer-events: none; z-index: 1;';
img1.style(baseStyle);
img2.style(baseStyle);
// Hide them initially
img1.hide();
img2.hide();
// Create a DOM element for the timer so it sits completely on top of the images
timerDiv = createDiv('');
timerDiv.style('position', 'absolute');
timerDiv.style('top', '15px');
timerDiv.style('right', '20px');
timerDiv.style('color', 'rgba(255, 255, 255, 0.5)'); // Semi-transparent white
timerDiv.style('font-family', 'monospace');
timerDiv.style('font-size', '20px');
timerDiv.style('z-index', '10'); // Ensures it stays above everything
// Start the timer
startTime = millis();
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
img1 = createImg(url1, "first image");
Loads the first image from a URL and stores it as a p5.js image object that can be shown/hidden
img1.style(baseStyle);
Applies CSS styles to center the image and make it fill the screen responsively
timerDiv = createDiv('');
Creates an HTML div element that will display the countdown timer above the images
createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas that fills the entire browser window using dynamic width and height values
const url1 = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgOLhscWLJ0cu5ZvaiLGuM5FnpRgfqHSki-w&s';- Stores the URL of the first image as a string constant
img1 = createImg(url1, "first image");- createImg() loads an image from a URL and returns a p5.Renderer object; the second argument is alt text for accessibility
let baseStyle = 'position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100vw; height: 100vh; object-fit: contain; pointer-events: none; z-index: 1;';- Defines a CSS style string that centers the image on screen, makes it fill the viewport, and prevents it from blocking mouse clicks
img1.style(baseStyle);- The .style() method applies the CSS string to img1, positioning and sizing it like a fullscreen presentation
img1.hide();- The .hide() method hides img1 by setting its display to 'none'; it will be shown later by the draw loop
timerDiv = createDiv('');- Creates an empty HTML div element that will hold the countdown timer text
timerDiv.style('z-index', '10');- Sets z-index to 10, making the timer appear above the images (which have z-index 1)
startTime = millis();- Records the current time in milliseconds when setup() runs; this becomes the reference point for calculating elapsed time