We are about to switch to a new forum software. Until then we have removed the registration on this forum.
If I have this right, it's possible (customary) to use e.g. jQuery in the global JS interpreter namespace as $
. How would one use p5 similarly? I've tried p5
, but that doesn't seem to work.
Answers
function
that we pass anotherfunction
as its running "sketch".We might give a better answer if you clarify why you want to have a global reference to p5; but I've certainly found it a useful strategy.
If you look at the Instantiation/namespace example you'll find one way of setting a 'global' reference: in this case you should be able to get at p5 properties and methods from the
myp5
variable.I've been using a slightly different pattern:
This avoids having to pass a reference to p5 around as a parameter in function calls...
If you wan to get overly technical, pretty much everything in JavaScript is an Object; even functions. So essentially both jQuery and p5 are Objects with their own set of properties and methods. jQuery is helpful enough to attach itself to a single global variable automatically; because it's meant to be used globally and you only ever want a single instance (seriously!).
I'd assume that since P5 is meant for a specific application the authors leave it to you to apply it where appropriate; and you can create multiple instances: e.g. in order to have multiple sketches running on the same page.
Ah, I don't know how I missed the namespace example. I knew about "everything's an Object" in JS; that's partly why I figured this should be possible.
What I'm ultimately trying to do is a horrible Frankenstein's Monster involving PyPy.js and p5, e.g. write Python Processing sketches which run in the browser as "first-class citizens".
This gist works.
O_O
There's a definite need for a load screen, as it takes a while to download everything/spin up.
This
"import js\n" + "import time\n" + "p = js.globals['p5js']\n" + "p.textSize(32)\n" + "p.text('word', 10, 30)\n" + "p.fill(0, 102, 153)"
(which I think is down to my lack of experience with JS) is truly unfortunate.
function setup() {}
would be enough to pull the global trick out! *-:)function
from within setup():I think I understand what you're saying, but I'm not sure how it will work with PyPy.js, which requires something like
for more-or-less the same reason; we have to wait for the PyPy.js vm to fully load and instantiate before we can use it.
There are various strategies you can use to ensure resources are ready. The modern approach is to use a library like requireJS. In this case that's probably overkill. A standard direct approach is to use setInterval, check that all resources are ready (you might have to set a global Boolean from p5 setup()), and then launch a callback function once they are.
I might get a chance to post some example code - and properly test this with p5 - later...
This appears to be working; though I haven't actually tried doing anything with pypy and haven't tried it on a remote server...
You'll notice pypy blocks fairly heavily: in the console you'll see the 'checking' msg output from setInterval pauses for a moment - one reason for not relying on it for accurate time-keeping!.
Snippet of content in index.html:
sketch.js: