setup(), draw(), and mousePressed() are special p5.js lifecycle functions┌────────────────────────────┐ │ Global Scope │ │ │ │ function setup() { } │ │ function draw() { } │ │ │ │ function generateArt() { │ │ // helper code │ │ } │ │ │ │ function mousePressed() { │ │ generateArt(); │ │ } │ └────────────────────────────┘
In p5.js, all event functions must be defined in the global scope:
mousePressed()mouseReleased()mouseMoved()keyPressed()keyReleased()keyTyped()If they are inside another function, they will not run.
p5.js works like this:
“At startup, I scan the global scope to see if you defined any event handlers.
If I don’t see them there, I assume they don’t exist.”
p5.js does not search inside your own functions.