We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I can't get more than one sketch to run on a webpage. Each works fine when it's on its own. I have in the sketches
var c1 = createCanvas(200, 200);
c1.parent('firstcanvas');
and
var c2 = createCanvas(300, 300);
c2.parent('secondcanvas');
and, in the html
<div id = "firstcanvas"></div>
<script src="pathto/firstscript.js" type="text/javascript"></script>
<div id = "secondcanvas"></div>
<script src="pathto/secondscript.js" type="text/javascript"></script>
Inspect shows
<div id="firstcanvas"></div>
<script src="pathto/firstscript.js" type="text/javascript"></script>
<canvas id="defaultCanvas0" width="200" height="200" style="width: 200px; height: 200px;"></canvas>
<div id = "secondcanvas"></div>
<script src="pathto/secondscript.js" type="text/javascript"></script>
and the webpage just shows the first sketch. I've tried various combos of div and canvas, with no luck.
<div id="firstcanvas">
<script src="pathto/firstscript.js" type="text/javascript"></script>
<canvas id="firstcanvas" width="200" height="200" style="width: 200px; height: 200px;"></canvas>
</div>
and
<canvas id="firstcanvas">
<script src="pathto/firstscript.js" type="text/javascript"></script>
</canvas>
with no luck. If I put ordinary javascript animations on the page, as long as I target the right canvas id, they work fine together. Any ideas?
Answers
Are you using instance mode?
Ha! I nearly wrote at the end of my question that I've looked an instance mode and don't understand it.
The example is
Am I right in inferring:
so functions inside 'classes' would look like this?
I don't get what 'node' is, and don't see if I have to reference the html parent element for my canvas.
Could you expand, please?
That's a tricky question and demands a huge answer! #:-S
var gray = 0;
if we wish could be written instead:p.gray = 0;
.gray = (gray + 16) % 256;
would bep.gray = (p.gray + 16) % 256;
p.
works. But it's bad practice. :-@ClassName.prototype.myFunction = function () {}
class
:https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
Thx, gotoloop [insert overwhelmed emoticon here ;) ].
All I would like to do is to run more than one P5 sketch on a webpage, and I have achieved that by putting the scripts on separate pages and using iframes to show them. This may be an inelegant solution but as I don't have to go through (theoretically) loads of codes inserting "p." before every function, it'll do for me.
For completeness, though, by class I meant a class - in my normal Processing code this is usually a Ball class - not a pde tab. So question 4 still stands:
Actually that's the best solution for performance reasons. B/c each
<iframe>
is a separate thread: \m/http://StackOverflow.com/questions/11329487/iframe-cost-of-implementing-an-iframe-in-a-web-application
Those are answered in 2) & 4). Gonna try to elaborate more... >-)
p.
.class
in order to implement your classes in JS:https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
@allonestring: I've been inspired to write a tutorial post on instance mode which will hopefully explain what it does...
this
to almost all 3rd-party libraries:new Minim(this);
.new ClassName(p);
.Thank you both, but although
allonestring != anIdiot;
I'm afraid thatallonestring != anExpert;
and, more unfortunately,allonestring.age > theAgeAtWhichThingsAreEasytoLearn;
so I'm still struggling to understand.Below are two very simple sketches that I have in P5. Would you mind making just the changes necessary to make them work in instance mode? I know, of course, that the code itself could be improved, but it's my understanding of instance mode that needs improving here, please.
sketch 1:
and sketch 2:
Thx!
allonestring.feeling = stupid;
@allonestring - did you check my instance mode example with two sketches? Today I also added examples of using instance mode with external classes.
I'd suggest looking at the latter example first and trying to convert one of your sketches to instance mode. If that doesn't work post your attempt here. If you do get that working get the second sketch working separately in instance mode and then try getting them both running alongside each other...
@allonestring, I wonder if you had already read:
https://GitHub.com/processing/p5.js/wiki/Processing-transition
Lotsa conversion tips from Java Mode to p5.js above. O:-)
Anyways, I've converted your 1st ES5 JS sketch to ES6 using
class
structure. :ar!Watch it online here: http://p5ide.HerokuApp.com/editor#?sketch=577310949e3f9603000ab5bc
Now the same sketch above converted from global to instance mode. See it online below: :-bd
http://CodePen.io/anon/pen/RRpJGm?editors=0010
Recall that class p5, those that need prefixing w/
p.
, can be looked up at p5.js' reference: L-)http://p5js.org/reference/
Thank you blindfish and gotoloop for your patience. I hope, for your sakes, that you never start to lose your marbles.
for(i = today; i < endOfLife; i += day) allonestring.marbles--;
No worries, as long as you're making progress! :))
Nonetheless, I did your 2nd "Orbits" sketch too.
Watch it online here: http://p5ide.HerokuApp.com/editor#?sketch=577369489e3f9603000ab5c6
"instance.html":
"Body.js":
"orbits.js":
"instance.html":
"Ball.js":
"bouncing.js":