-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Document/unit test ability for clients to initialize global mode explicitly #1570
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
Comments
I've consolidated parts of @toolness's notes above, made up the name
"However, this might be inconvenient if you are mixing with other JS libraries (synchronously or asynchronously) or writing long programs of your own. We currently support two ways around this problem: instance mode and asynchronous global mode. In instance mode, all p5 functions are bound up in a single variable instead of polluting your global namespace... Another option is asynchronous global mode. To initialize asynchronous global mode at any point in a web page's lifetime, simply call Along these lines, asynchronous global mode also allows p5 to be instantiated before page load, so folks who really want to work around the assigning variables using p5 functions and variables before setup limitation can write the following sorts of sketches..." [code block from @toolness notes above]
"Well, technically, you can by using [after last para] We mentioned Seems like there could also be room in asynchronous-calls-and-file-loading for this? Thoughts? I'm happy to make changes, write more/less, etc. |
$0.02, maybe "on-demand global mode?" As a JS developer async makes me think of the callbacks I register to react to some change, like user input or an AJAX request; and putting on a newbie hat I'm not sure how I'd interpret "asynchronous" in this context. |
@kadamwhite thanks for the feedback. I think that'd be a great change. |
@kaganjd this looks great, thanks for taking a crack at this. I like @kadamwhite's suggestion of "on demand". feel free to go ahead and make the proposed changes to the overview wiki page. the only other point I'll mention is that this should also theoretically work without the p5();
var boop = random(100);
function setup() {
createCanvas(100, 100);
}
function draw() {
background(255, 0, boop);
} I'm not sure if this looks or feels better? or if it adds confusion with instance mode? I don't think you need it there either btw, but we decided at some point that it felt like it made sense because it aligned with the pattern of constructor functions. |
Ohhh, interesting! Hmm... I think the |
Sounds good. I think that's similar reasoning to why we included it for instance mode examples. Let's leave as is for now then at least. |
Hmm, does just calling Something I'm noticing now is that on-demand mode actually seems to make the friendly error system feature that warns when the user overwrites p5 globals, introduced in #1318, think that all the globals are being redefined. It's possible this is because p5 is actually initializing itself twice in this case (once on-demand, again on page load). We're seeing the same kind of behavior in the web IDE in processing/p5.js-web-editor#7 (comment) so I'll need to look into that... Anyhow, another option: I wrote a mini library a few years ago called TinyTurtle and it had an on-demand mode which was invoked via the following: TinyTurtle.apply(window); This is basically equivalent to Analogously, Anyways, I agree with @lmccart and @kaganjd that just leaving it as |
oops, @toolness I just tested and you're right it does require the |
The TL;DR here is that it'd be nice to be able to initialize p5 global mode explicitly at any point in a web page's lifetime.
Currently there is no documented way to do this, but there is an undocumented way to do it: simply call
new p5()
, without any arguments.So this issue isn't actually about adding any new functionality--it's just about documenting and/or adding unit tests so that tools or sketches that depend on it don't break in the future.
Use case for the p5 widget
In the p5 widget, we're currently trying to support scenarios where widget embedders may want to specify p5 addon libraries to load.
Currently, the p5 widget's preview frame works like this:
<script>
element to the page containing the user's sketch code (which expects to be run in global mode).<script>
element with itssrc
attribute pointing at a version of p5.As soon as p5.js loads, it sees the
setup
/draw
functions defined by the user's sketch and initializes global mode.However, because the p5 widget loads p5 asynchronously, adding support for p5 addon libraries poses a conundrum:
p5.prototype.registerPreloadMethod()
to function properly.setup
/draw
exists in the global namespace and will start the sketch immediately, before we have a chance to asynchronously load any addon libraries.While there are some ways the p5 widget could change its architecture to work around this, the easiest solution would be the following:
<script>
element to the page containing the user's p5 global mode sketch.Step 3 is where we need ongoing support from p5. While it's possible right now, it'd be nice to have the functionality documented and/or unit tested, so that the widget doesn't break when used with newer versions of p5.
I've written a working proof-of-concept of this approach at toolness/p5.js-widget#63.
Other benefits
Aside from making the p5 widget's implementation easier, I can think of a few other benefits to being able to explicitly start p5 global mode:
It actually allows p5 global mode to be instantiated before page load, which means that folks who really want to work around the assigning variables using p5 functions and variables before
setup
limitation can write the following sorts of sketches:Kind of weird, but it's nice to have it available as an option.
One might argue that the ability to control the activation of p5 global mode isn't a feature "advanced" JS developers would need, because such users should just use instance mode. However, the value of the p5 global mode's API to advanced developers shouldn't be underestimated. There's lots of reasons they might still prefer to use global mode:
Advanced users and tools (like p5 widget, IDEs) can potentially improve page load times by asynchronously loading multiple p5 add-ons simultaneously. I don't think this is possible without those users/tools having the ability to explicitly control when p5 global mode is initialized, but I could be wrong.
The text was updated successfully, but these errors were encountered: