{"action":"opened","issue":{"url":"https://api.github.com/repos/processing/p5.js/issues/1570","repository_url":"https://api.github.com/repos/processing/p5.js","labels_url":"https://api.github.com/repos/processing/p5.js/issues/1570/labels{/name}","comments_url":"https://api.github.com/repos/processing/p5.js/issues/1570/comments","events_url":"https://api.github.com/repos/processing/p5.js/issues/1570/events","html_url":"https://github.com/processing/p5.js/issues/1570","id":173643896,"number":1570,"title":"Document/unit test ability for clients to initialize global mode explicitly","user":{"login":"toolness","id":124687,"avatar_url":"https://avatars.githubusercontent.com/u/124687?v=3","gravatar_id":"","url":"https://api.github.com/users/toolness","html_url":"https://github.com/toolness","followers_url":"https://api.github.com/users/toolness/followers","following_url":"https://api.github.com/users/toolness/following{/other_user}","gists_url":"https://api.github.com/users/toolness/gists{/gist_id}","starred_url":"https://api.github.com/users/toolness/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/toolness/subscriptions","organizations_url":"https://api.github.com/users/toolness/orgs","repos_url":"https://api.github.com/users/toolness/repos","events_url":"https://api.github.com/users/toolness/events{/privacy}","received_events_url":"https://api.github.com/users/toolness/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-08-28T12:16:06Z","updated_at":"2016-08-28T12:16:06Z","closed_at":null,"body":"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.\r\n\r\nCurrently there is no *documented* way to do this, but there is an *undocumented* way to do it: simply call `new p5()`, without any arguments.\r\n\r\nSo 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.\r\n\r\n## Use case for the p5 widget\r\n\r\nIn the [p5 widget](https://github.com/toolness/p5.js-widget), we're currently trying to support scenarios where widget embedders may want to [specify p5 addon libraries to load](https://github.com/toolness/p5.js-widget/issues/53).\r\n\r\nCurrently, the p5 widget's preview frame works like this:\r\n\r\n1. Dynamically add a `<script>` element to the page containing the user's sketch code (which expects to be run in global mode).\r\n2. Dynamically add a `<script>` element with its `src` attribute pointing at a version of p5.\r\n\r\nAs soon as p5.js loads, it sees the `setup`/`draw` functions defined by the user's sketch and initializes global mode.\r\n\r\nHowever, because the p5 widget loads p5 asynchronously, adding support for p5 addon libraries poses a conundrum:\r\n\r\n* We can't load p5 addon libraries *before* loading p5 because they depend on e.g. [`p5.prototype.registerPreloadMethod()`](https://github.com/processing/p5.js/wiki/Libraries#creating-a-new-library) to function properly.\r\n\r\n* It's difficult to load p5 addon libraries *after* loading p5 because p5 will have noticed that `setup`/`draw` exists in the global namespace and will start the sketch immediately, before we have a chance to asynchronously load any addon libraries.\r\n\r\nWhile there are some ways the p5 widget could [change its architecture](https://github.com/toolness/p5.js-widget/issues/53#issuecomment-240387563) to work around this, the easiest solution would be the following:\r\n\r\n1. Asynchronously load p5.js, followed by any add-on libraries.\r\n2. Add a `<script>` element to the page containing the user's p5 global mode sketch.\r\n3. Activate global mode to start the sketch.\r\n\r\nStep 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.\r\n\r\nI've written a working proof-of-concept of this approach at https://github.com/toolness/p5.js-widget/pull/63.\r\n\r\n## Other benefits\r\n\r\nAside 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:\r\n\r\n* 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`](https://github.com/processing/p5.js/wiki/Frequently-Asked-Questions#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup) limitation can write the following sorts of sketches:\r\n\r\n    ```js\r\n    new p5();\r\n\r\n    var boop = random(100);\r\n\r\n    function setup() {\r\n        createCanvas(100, 100);\r\n    }\r\n\r\n    function draw() {\r\n        background(255, 0, boop);\r\n    }\r\n    ```\r\n\r\n    Kind of weird, but it's nice to have it available as an option.\r\n\r\n* 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:\r\n  * It's very expressive, while having to prefix one's sketches with the sketch instance suddenly makes one's code very verbose and harder to read.\r\n  * It's still possible to use lots of third-party libraries without namespace collisions if one uses a module loader like requireJS or browserify.\r\n  * One might start writing their sketch in global mode because it's easier, and needing to convert one's code over to instance mode just to have a bit more control over the way their sketch is loaded feels like an unnecessary trade-off.\r\n\r\n* 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.\r\n"}}
{"actor":{"display_login":"toolness"}}