setup()
setup() runs once when the sketch starts. Here we initialize the canvas, the off-screen graphics buffer where splats accumulate, and load the three target images. The boolean flags (img1Loaded, etc.) let us know when images are ready to display, since network loading takes time.
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:ANd9GcQYep0cyzg9ywfQILSBMn4-Fp7NMaeO7M7lyQ&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);
Creates a fullscreen canvas that matches the browser window size
splatBuffer = createGraphics(windowWidth, windowHeight);
Creates a hidden drawing surface where splats accumulate permanently each frame
img1 = loadImage(url1, () => img1Loaded = true);
Loads images asynchronously from URLs and sets boolean flags when each finishes loading
createCanvas(windowWidth, windowHeight);- Creates the main p5.js canvas the full width and height of the browser window
cursor(CROSS);- Changes the mouse cursor to a crosshair, giving the aiming-based game visual feedback
splatBuffer = createGraphics(windowWidth, windowHeight);- Creates an off-screen graphics buffer (like a transparent layer) where splats are drawn and persist between frames
img1 = loadImage(url1, () => img1Loaded = true);- Loads an image from a URL asynchronously; the arrow function callback sets img1Loaded to true when the download finishes