-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[p5.js 2.0 RFC Proposal]: New third party library authoring API #7015
Comments
@limzykenneth I like the idea but it's a bit unclear from your example code how this would achieve the goals of esm support for example. Could you elaborate on that and show an example? Also I don't understand the purpose of using an anonymous function, the first and last lines in your example. p5.js users might be loading multiple libraries and the current implementation of I'm also confused how library creators could access a p5 instance. |
Hey everyone, I got asked by @davepagurek to add context to this, since I am currently working on an addon library for p5.js myself and ran into some minor issues where it would be great if a fix is provided for the upcoming 2.0 update. The addon library I am working on renders the p5 canvas as ASCII art, for which the user can upload their own font to be used by using Doing that didn't work immediately and I figured that I have to manually call The relevant code from my work-in-progress addon library looks like this:
Cheers! |
Thanks @humanbydefinition! For the 2.0 API, this could mean changing when the beforePreload and afterPreload hooks run so that an addon's |
Also to shed more clarity on how @limzykenneth do you have thoughts on whether or not the |
@humanbydefinition For the proposed async/await implementation with the new library API and your use case you can detect if Essentially how the lifecycles defined by libraries work is that everytime let fontLoaded = false;
p5.registerAddon((p5, fn, lifecycles) => {
// `fn` being the prototype
fn.loadAsciiFont = async function(font){
// Load user defined font
fontLoaded = true;
};
fn.loadDefaultAsciiFont = async function(){
// Load default font
};
lifecycles.postsetup = async function(){
// Run actions after `setup()` runs
if(fontLoaded === false){
await fn.loadDefaultAsciiFont();
// or maybe `await this.loadDefaultAsciiFont();` still need to figure out `this` binding
}
};
}); Somewhat like the example code above, but probably with a different state management arrangement depending on how you'd design your API. @davepagurek My current plan is to not retain the I will be testing this part the the feature/changes with most if not all external libraries and provide fixes where needed. Mostly libraries that attaches to prototype will likely still work (bar changes we made elsewhere in the core) but those that uses preload will likely need updating. |
Increasing access
Addon libraries have always been an important part of the p5.js ecosystem, expanding its capabilities with community contributed features that solve problems that p5.js itself may not necessarily address. Providing a flexible and easy to use interface to author addon libraries will further this effort.
Which types of changes would be made?
Most appropriate sub-area of p5.js?
What's the problem?
Currently to author an addon library to work with p5.js, the library author will often need to attach methods directly to
p5.prototype
which by itself is not a bad idea but addon libraries often need to do more than that.While async/await setup() proposed in #6767 provides a potentially new way of handling async functions, current libraries that need to hook into
preload()
require the use of internal functions.The lifecycle hooks feature of the current addon library API is also not entire consistent with room for improvement. Finally with a new syntax, there is room for even more customizability, combined with #7014, one can even add or overwrite renderers available to p5.js.
What's the solution?
Existing libraries should have a level of compatibility or require minimal updates to work with p5.js 2.0. This means if existing libraries rely on attaching methods to
p5.prototype
it will likely still work.A new method of authoring libraries will be introduced that is more ergonomic. This will be through a factory function that exposes reasonable interfaces for completing the following tasks as necessary:
As reference, Day.js provide plugin interface in the following way:
And is used with:
While jQuery provides the following interface:
fn
above is just an alias toprototype
which in essense makes jQuery's plugin system identical to what p5.js does.p5.js plugins have some properties that are not present in the Day.js or jQuery use case. With Day.js, plugins are expected to be explicitly provided through
dayjs.extend()
while p5.js addons should have the expectations of being available immediately upon inclusion/load. jQuery plugin don't need to content with lifecycle hooks or other non-class instance related features. A p5.js addon should also have the flexibility of being imported as a ES module or included through a script tag, ie. there should be a ES module version and a UMD version ideally.The proposed interface that a p5.js 2.0 plugin can have is as the following:
Pros (updated based on community comments)
Cons (updated based on community comments)
Proposal status
Under review
The text was updated successfully, but these errors were encountered: