I am making an online game with p5.js and I would like to manually call setup, and once setup is called I want draw() to run.
For example, if I click a button:
<button id="somebutton" onclick="setup()">CLICK ME!!!</button>
Then the canvas will be created and all of the stuff in setup will be run and draw() will run.
Why do you want to do this?
Processing needs to do a bunch of things related to calling the
setup()
function, so there's almost never a good reason for you to call it manually.Using a Variable
If you want to not start your sketch until you click a button, you should do that separately from the
setup()
function. You could keep track of aboolean
that tells Processing whether to start the sketch, then set thatboolean
when you click the button. Something like this:Then in your html, you'd have:
Using Instance Mode
You could also use instance mode to delay the creation of your sketch. Something like this:
Then in your html, you'd have: