preload()
preload() is a special p5.js function that runs before setup(). Use it to load images, sounds, or data files so they are ready when your sketch needs them. Without preload(), images might not be loaded yet when draw() tries to display them.
function preload() {
img = loadImage('thumbnail.png');
}
Line-by-line explanation (1 lines)
img = loadImage('thumbnail.png');- loadImage() fetches the image file from your project folder and stores it in the img variable. preload() ensures this completes before setup() runs, preventing 'undefined image' errors