setup()
setup() runs exactly once when the sketch starts. It's where you initialize variables, load resources, and configure the canvas. The createGraphics() function is key here—it creates a layer that persists between frames, making splats permanent.
function setup() {
createCanvas(windowWidth, windowHeight);
cursor(CROSS);
// Canvas for the permanent splats
splatBuffer = createGraphics(windowWidth, windowHeight);
// Load all three images with tracking so we know when they are ready
let url1 = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQqBiqGS05DirD1Ek79Jt29YKY_YvXW7zgJ7g&s";
let url2 = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSjM-gYw5UQrnxOOr-xOFksPpF8bWNc8ea5hA&s";
let url3 = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ6viABUZkfkI_89OhRG1nc7cQ9uXRMoIiZCg&s";
img1 = loadImage(url1, () => img1Loaded = true);
img2 = loadImage(url2, () => img2Loaded = true);
img3 = loadImage(url3, () => img3Loaded = true);
}
Line-by-line explanation (4 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
cursor(CROSS);
Creates a full-screen canvas and changes the cursor to a crosshair for targeting
splatBuffer = createGraphics(windowWidth, windowHeight);
Creates a hidden graphics layer that will store all tomato splats permanently
img1 = loadImage(url1, () => img1Loaded = true);
Loads an image from a URL and sets a flag when it's ready to draw
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window—windowWidth and windowHeight are p5.js variables that automatically detect screen size
cursor(CROSS);- Changes the mouse cursor to a crosshair, giving visual feedback that you're aiming at targets
splatBuffer = createGraphics(windowWidth, windowHeight);- Creates a hidden graphics layer—like a second canvas—that will store permanent splat marks. Drawing to this buffer instead of the main canvas means splats stay visible every frame
img1 = loadImage(url1, () => img1Loaded = true);- Loads an image from a URL and runs a callback function when ready. The callback sets img1Loaded to true so the draw function knows it can display the image