We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to call a p5 method using [instanceobject].method(). I'm receiving an "undefined error. What am I doing wrong (or maybe what I'm trying to do isn't possible??). Error is on last line of code below.
var s = function(sketch){
var gray = 0;
sketch.setup = function() {
sketch.createCanvas(200, 200);
sketch.ellipse(sketch.width/2, sketch.height/2, 100, 100);
};
sketch.draw = function() {
};
};
var p5_0 = new p5(s,"m-canvas-shape");
var p5_1 = new p5(s,"m-canvas-gd");
//this line generates an error:
//"Cannot read property 'clearRect' of undefined"
p5_1.clear();
Comments
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
Thanks for the formatting link
Warning: I know little about P5.js itself...
You mention clearRect in the comment, and I see clear()...
Basically, the error means that p5_1 is not defined, I suppose it is because p5 doesn't find the "m-canvas-gd" id in your HTML file. (I am guessing, actually, what this second parameter is... Actually, I can't find this constructor after a quick search on the site.)
Found that in this link: https://github.com/lmccart/p5.js/wiki/Instantiation-Cases
Unfortunately the order of parameters there is WRONG! @-)
It says
p5(node, s);
. But it's the opposite as we can see from its source below:var p5 = function(sketch, node, sync) {
https://github.com/lmccart/p5.js/blob/master/src/core/core.js#L38
Nevertheless,
var p5_0 = new p5(s,"m-canvas-shape");
should work if "m-canvas-shape" exists.Philho and GoToLoop: Thanks for your feedback.
I finally tried something a little different and so far it appears to be doing what I want it to. I'm not sure if p5 is intended to be used this way, so if somebody has some other ideas, I'm def open to suggestions: (old man trying to learn new technology)
Hmm... There are some points I haven't completely got:
var obj = new complexGraph(sketch);
new
?Take a look at this tweaked version: B-)
GoToLoop, thanks for the input.
re: using objects. I probably didn't provide enough info in my previous post. (Also, I have a c# background and it's probably influencing my approach to js.) What I'm trying to do is:
Yes, I had been using just one canvas and swapping objects in and out for editing, etc. It was working well for the most. But I was having some problems thinking through the idea of saving /restoring images to the canvas. So, thanks for the lead on the graphics. I remember reading it, but it must not have sunk in. (The concrete in my head is getting fairly thick. :) )
s.
orsketch.
.Thanks much. This is very helpful.
Should I mark this discussion as "answered"?? Or does somebody else do that??
In order to mark as "answered", the thread gotta be of type "Question" instead of "Discussion".
Unfortunately after some recent upgrade fixes, that feature seemed to have disappeared! =(
Finally got my head wrapped-around your code example above. I think it's going to work out really well. Opens a lot of new avenues. I'm so excited I'll probably have problems falling asleep tonight!