Wait until Canvas gets created in DOM

Hi all,
I have this code:

    canvas2 = mCreateCanvas(m4.windowWidth, m4.windowHeight, m4.WEBGL);
    m4.setAttributes('antialias', true);
    glCanvas = document.getElementById('defaultCanvas0').getContext('webgl');

  glCanvas.disable(glCanvas.DEPTH_TEST);

which sometimes fails at that line:

  glCanvas.disable(glCanvas.DEPTH_TEST);

because glCanvas is not defined. How can I wait until Canvas2 is defined in the DOM?

Aren’t you doing that stuff inside callback setup()? :no_mouth:
p5js.org/reference/#/p5/setup

Sure it’s in the setup(). Anyway your solution to my other post pretty solves the issue