this.output is undefined after calling .remove() on an instance.

Thanks in advance!

I'm running the following snippet:

    var sketch;

    $('#title').click(function(){

    if (typeof(sketch) == 'undefined' ){                       
                sketch = new p5(fft, "sketchDiv");
            } else {
                sketch.remove();
                console.log("step1");
                sketch = new p5(fft, "sketchDiv");
                console.log("step2");
            };
      });

When I click the title div once, it works...

When I click it a second time, it also works (removes and replaces).

However, when I click it a third time, the console reports the error:

" this.output is undefined"

  • If I click it a fourth time everything runs again as planned *

Not sure why the error isn't getting called after the first "remove()" call.

Here is the sketch: (in instance mode).

      var fft = function (p){

       p.mic, p.fft;

        p.setup = function() {
 p.createCanvas(p.windowWidth,400);
 p.noFill();

 p.mic = new p5.AudioIn();
 p.mic.start();
 p.fft = new p5.FFT();
 p.fft.setInput(p.mic);
   }

     p.draw = function() {

      p.clear();  // using this instead of redrawing background

       var spectrum = p.fft.analyze();

 p.beginShape();

 for (i = 0; i<spectrum.length; i++) {
  p.vertex(i, p.map(spectrum[i], 0, 250, p.height, 0) );
 }
 p.endShape();

} }

Answers

  • First suggestion. Add this in your line 4: console.log("step0: "+typeof(sketch)); I am curious to see what line of the code causes the error, is it within the if caluse or the else?

    Could you provide more details about why you want to do this? Couldn't you just modified the current state of your sketch, or re-init your sketch instead of creating a new object?

    Kf

  • It logs as "step0 = object".

  • edited May 2017

    re-init sounds interesting... how do you mean? I am basically trying to avoid having sketches stacked on sketches in my div.

  • I am basically trying to avoid having sketches stacked on sketches in my div.

    What do you mean? Could you provide your html file? Do you define two div objects in your body?

    Kf

Sign In or Register to comment.