Skip to content

[p5.js 2.0] Unnecessary arguments supplied to addon function by p5.registerAddon #7744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
quinton-ashley opened this issue Apr 16, 2025 · 2 comments

Comments

@quinton-ashley
Copy link
Contributor

quinton-ashley commented Apr 16, 2025

Topic

I'd like to suggest a change to p5.registerAddon before the release of p5.js 2.0.

Currently it passes: (p5, fn, lifecycles)

But it's only necessary to pass lifecycles to addon functions.

Users already need access to p5 to call p5.registerAddon, so it's confusing to provide them something they already have, even when using the same name for it.

The fn name could make sense to people familiar with jQuery's addon system but that's a stretch. Otherwise "fn" is already common shorthand for "function" and addon devs may want to use that variable name for other purposes such as iterating through functions. It's best to leave the naming of a shortcut for p5.prototype up to addon devs because it's not helpful to give it a generic name that obfuscates what it is imo.

Ideally addon registration examples could look simpler with shorter function signatures like this:

p5.registerAddon((lifecycles) => { 

  const proto = p5.prototype; // user defined shortcut, if desired

  // ...addon code

});
@limzykenneth
Copy link
Member

The addon is

function addon(p5, fn, lifecycles){
}

not

p5.registerAddon(function(p5, fn, lifecycles){
});

The existance of p5 when the addon is defined is not guaranteed. Yes it would be the case in script tag usage where addons are encouraged to have the second version above, but the addon can also export the addon function (first one above) to be imported by a sketch using ESM:

// addon.js
export function(p5, fn, lifecycles){
  // Define addon code here
}
// ESM p5 sketch
import p5 from "p5";
import addon from "./addon.js";

p5.registerAddon(addon);

const sketch = function(p){ /* ... */ };

new p5(sketch);

@quinton-ashley
Copy link
Contributor Author

@limzykenneth Ah okay, that makes sense. It still would've been nice to make lifecyles the first param though, as it's not always necessary to provide p5.

@github-project-automation github-project-automation bot moved this from Open for Discussion to Completed in p5.js 2.x 🌱🌳 Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Completed
Development

No branches or pull requests

2 participants