preload()
preload() is a special p5.js function that runs before setup(). Use it to load files, images, or sounds that your sketch needs. Without preload(), the sketch might try to use data before it's finished loading.
function preload() {
// Load the poem as an array of lines
// https://p5js.org/reference/#/p5/loadStrings
poemLines = loadStrings('poem.txt');
}
đź”§ Subcomponents:
poemLines = loadStrings('poem.txt');
Reads the poem.txt file and stores each line as an element in the poemLines array
Line by Line:
poemLines = loadStrings('poem.txt');- loadStrings() is a p5.js function that reads a text file and returns an array where each element is one line from the file. This must happen in preload() so the file is loaded before setup() runs.