preload()
preload() is a special p5.js function that runs once before setup(). Use it to load images, sounds, and text files—p5.js will wait for them to finish loading before moving on, preventing crashes from missing data.
function preload() {
// Load the poem as an array of lines
// https://p5js.org/reference/#/p5/loadStrings
poemLines = loadStrings('poem.txt');
}
Line-by-line explanation (1 lines)
poemLines = loadStrings('poem.txt');- Fetches the poem.txt file from your project folder and stores each line as a separate element in the poemLines array. This happens BEFORE setup() runs, which guarantees the file is ready to use.