┌────────────────────────────┐ │ Global Scope │ │ │ │ function setup() { } │ │ function draw() { } │ │ │ │ function generateArt() { │ │ // helper code │ │ } │ │ │ │ function mousePressed() { │ │ generateArt(); │ │ } │ └────────────────────────────┘

✅ The rule

In p5.js, all event functions must be defined in the global scope:

If they are inside another function, they will not run.


🧠 Why this is true (conceptually)

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.