preload()
preload() is a special p5.js function that runs before setup(). It's the standard place to load images, fonts, JSON, or text files so your sketch never tries to use data that hasn't arrived yet.
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');- p5.js automatically waits for this file to finish loading before running setup(), so poemLines is guaranteed to be filled with an array of text lines by the time it's needed.