preload()
preload() is a special p5.js function that runs once before setup(). Use it to load images, sounds, or other files your sketch needs. This ensures everything is ready before your code tries to use it.
function preload() {
// === IMPORTANT: Replace 'your_plant_image.png' with the actual URL of your PNG image. ===
// Make sure your PNG has a transparent background for the best effect.
// The base of the plant should be at the bottom of the image for natural rotation.
// Replaced unreachable Wikimedia Commons URL with a working Openclipart SVG
plantImage = loadImage('seaweed.png');
}
Line-by-line explanation (1 lines)
🔧 Subcomponents:
plantImage = loadImage('seaweed.png');
Fetches the seaweed image file and stores it in a variable so it can be drawn later
plantImage = loadImage('seaweed.png');- loadImage() fetches the PNG file from your project folder and stores it in the plantImage variable. This must happen in preload() before setup() runs, ensuring the image is ready to use when draw() begins