setup()
setup() runs once when the sketch starts, making it the right place to configure drawing settings and prepare data (like splitting a string) before the draw loop begins animating.
function setup() {
createCanvas(windowWidth, windowHeight);
textSize(32);
textAlign(CENTER, CENTER);
noStroke();
fill(0);
// Split phrase into individual characters (including spaces and punctuation)
chars = phrase.split("");
generatePositions();
}
Line-by-line explanation (7 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window using p5's built-in windowWidth and windowHeight variables.
textSize(32);- Sets the font size for all text drawn afterward to 32 pixels.
textAlign(CENTER, CENTER);- Makes text() draw each character centered on its x/y coordinate both horizontally and vertically, so the position you give is the character's true center point.
noStroke();- Turns off outlines so shapes and text don't get an unwanted border.
fill(0);- Sets the fill color to black (0 = black in grayscale mode), which is the color the text will be drawn in.
chars = phrase.split("");- Splits the phrase string into an array where every single character (letters, spaces, and punctuation) becomes its own array element.
generatePositions();- Calls the helper function that assigns a random position to each character, filling the positions array.