# Wait until Canvas gets created in DOM

**URL:** <https://discourse.processing.org/t/wait-until-canvas-gets-created-in-dom/5602>\
**Category:** Coding Questions\
**Created:** [November 16, 2018, 3:19pm UTC](https://discourse.processing.org/t/wait-until-canvas-gets-created-in-dom/5602 "2018-11-16T15:19:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FedFod](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedfod/32/2312_2.png) [@FedFod](https://discourse.processing.org/u/FedFod)\
**Post date:** [November 16, 2018, 3:19pm UTC](https://discourse.processing.org/t/wait-until-canvas-gets-created-in-dom/5602/1 "2018-11-16T15:19:08Z")

</div>

Hi all,  
I have this code:

```auto
    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:

```auto
  glCanvas.disable(glCanvas.DEPTH_TEST);

```

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 16, 2018, 3:41pm UTC](https://discourse.processing.org/t/wait-until-canvas-gets-created-in-dom/5602/2 "2018-11-16T15:41:11Z")

</div>

Aren’t you doing that stuff inside callback **setup()**? 😶  
[p5js.org/reference/#/p5/setup](http://p5js.org/reference/#/p5/setup)

---

<div class="post-metadata">

**Author:** ![FedFod](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedfod/32/2312_2.png) [@FedFod](https://discourse.processing.org/u/FedFod)\
**Post date:** [November 16, 2018, 3:42pm UTC](https://discourse.processing.org/t/wait-until-canvas-gets-created-in-dom/5602/3 "2018-11-16T15:42:13Z")

</div>

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