How can I switch canvas ?

I would like to have two canvasses and draw on them without prefixing every command with a canvas variable. I guess there is a global variable in p5, but I could not find it.

What command should be used instead of SELECT below?

c1 = createCanvas(100,100);
c2 = createCanvas(100,100);
SELECT(c1);
background(255);    // NOT c1.background(255);
SELECT(c2);
background(127);

Answers

  • edited March 2017

    p5.js, just like all the other flavors of Processing, can only have 1 global main "canvas"! :-B

    However, if you don't mind to "cheat" or "hack" the library, just know that the current "canvas" is stored in the "hidden" global system variable _renderer. ;)

    Not tested it yet, but I believe you can "select" your desired "canvas" by assigning it to _renderer. :ar!

    _renderer = c1; or _renderer = c2; :>

  • Nice hack if it works! Could you just use two Pgraphics ?

    Kf

  • changing _renderer does not work. maybe p5._renderer.canvas or something similar could work. But I did not succeed. :-)

Sign In or Register to comment.