preload()
preload() is a special p5.js function that runs before setup()—it's the only place where you can safely call blocking operations like loadImage() and loadFont(). If you load images inside setup() or draw(), your sketch will stutter.
function preload() {
// Load the first jumpscare image
jumpscareImage = loadImage('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ5y9hqK1_1m7460gHkxhPzZGlqL53u1SiUwA&s');
// Load the second jumpscare image (NEW)
jumpscareImage2 = loadImage('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR8cVYRFVxnhmOEBzEkBjpMrhYG7LuQWj8MVA&s');
// Load a suitable font for the virus text to give it a techy, monospaced look.
// We're using Roboto Mono from the Fontsource CDN, which is reliable and works well with p5.js loadFont().
// This specific URL is for latin characters, 400 weight (normal), normal style, in woff format.
virusFont = loadFont('https://cdn.jsdelivr.net/npm/@fontsource/roboto-mono@latest/files/roboto-mono-latin-400-normal.woff');
}
Line-by-line explanation (3 lines)
jumpscareImage = loadImage('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ5y9hqK1_1m7460gHkxhPzZGlqL53u1SiUwA&s');- Loads the first jumpscare image from a URL and stores it in the jumpscareImage variable—p5.js blocks the rest of the sketch until this finishes.
jumpscareImage2 = loadImage('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR8cVYRFVxnhmOEBzEkBjpMrhYG7LuQWj8MVA&s');- Loads the second jumpscare image from a URL and stores it in jumpscareImage2—having two different images makes the scares feel less predictable.
virusFont = loadFont('https://cdn.jsdelivr.net/npm/@fontsource/roboto-mono@latest/files/roboto-mono-latin-400-normal.woff');- Loads the Roboto Mono font from a CDN so all text in the sketch has a technical, monospaced appearance that fits the virus warning aesthetic.