# Using the canvas as the parent of an element makes it disappear

**URL:** https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937
**Category:** p5.js
**Created:** [July 20, 2018, 3:32pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937 "2018-07-20T15:32:15Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Albizia](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Albizia](https://discourse.processing.org/u/Albizia)
#### Post date: [July 20, 2018, 3:32pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/1 "2018-07-20T15:32:15Z")

</div>

Hello,  
Why does a button disappear if I set canvas as his parent ?

```
canvas = createCanvas(800, 600);
button = createButton("huhu");
button.parent(canvas);
```

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 20, 2018, 4:22pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/2 "2018-07-20T16:22:53Z")

</div>

Check this answer [https://stackoverflow.com/questions/4797748/can-i-put-an-html-button-inside-the-canvas](https://stackoverflow.com/questions/4797748/can-i-put-an-html-button-inside-the-canvas)

I don’t think a canvas could contain a button. It could hover over the canvas. You just need to place it relative to the canvas’ parent and change its z-index.

Kf

---

<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: [July 20, 2018, 6:40pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/3 "2018-07-20T18:40:13Z")

</div>

- [https://Forum.Processing.org/two/discussion/28080/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear](https://Forum.Processing.org/two/discussion/28080/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear)

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 20, 2018, 6:59pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/4 "2018-07-20T18:59:41Z")

</div>

So I guess he is missing p5.dom? A canvas element can contain other html elements? That is not what I got from the stackoverflow question, @GoToLoop

Kf

---

<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: [July 20, 2018, 7:20pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/5 "2018-07-20T19:20:27Z")

</div>

> [@kfrajer](#):
>
> A `<canvas>` element can contain other HTML elements?

I didn’t state anything other than reporting the sketch worked alright! 😑

That is, the `<button>` SHOWS UP, regardless we use **parent()** or not to force it to be a child of the sketch’s `<canvas>`. 🤑

If we hit F12 and inspect the “Elements” tab, we’ll see the `<button>` is OUTSIDE the `<canvas>` tag: 🤪

```auto
<body>
  ...
  <canvas id="defaultCanvas0" class="p5Canvas" width="800" ...></canvas></canvas>
  <button>huhu</button>
</body>

```

---

<div class="post-metadata">

### Author: ![Albizia](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Albizia](https://discourse.processing.org/u/Albizia)
#### Post date: [July 20, 2018, 9:30pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/6 "2018-07-20T21:30:17Z")

</div>

So that you can see what i’m seeing. [http://gosuness.free.fr/button/](http://gosuness.free.fr/button/)  
Do you see that button disappear after 4 seconds ?

why is that button disappearing, does it go somewhere else ?

as you can see, I did include p5.dom

```auto
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport", charset="UTF-8", width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0>
        <title>nouvelle page</title>
        <style> body {padding: 0; margin: 0;} </style>
        <script src="libraries/p5.min.js"></script>
        <script src="libraries/addons/p5.dom.min.js"></script>
        <script src="libraries/addons/p5.sound.min.js"></script>
        <script src="sketch.js"></script>
    </head>
    <body>
    </body>
</html>

```

```auto
function setup() {
    canvas = createCanvas(800,600);
    background(100);
    button = createButton("huhu");
    button.position(400,300);
    setTimeout(function() {
        button.parent(canvas);
    }, 4000);
}

```

---

<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: [July 20, 2018, 9:41pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/7 "2018-07-20T21:41:01Z")

</div>

> [@Albizia](#):
>
> Do you see that button disappear after 4 seconds?

Nope, it stayed there! Have you tried your sketch on [My Sketch - OpenProcessing](https://OpenProcessing.org/sketch/create) already? 😳

---

<div class="post-metadata">

### Author: ![Albizia](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Albizia](https://discourse.processing.org/u/Albizia)
#### Post date: [July 20, 2018, 9:43pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/8 "2018-07-20T21:43:51Z")

</div>

I did try that code on [https://OpenProcessing.org/sketch/create](https://OpenProcessing.org/sketch/create),  
and there the button doesn’t disappear.  
But if I try it on [http://gosuness.free.fr/button/](http://gosuness.free.fr/button/) the button DOES disappear.  
Why on earth is it doing that ?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [July 20, 2018, 10:02pm UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/9 "2018-07-20T22:02:49Z")

</div>

You need to open the developer console and see if there is any error. Based on this discussion, if you don’t have p5.dom on the path you are specifying, then this things will happen. A simple test would be to use an online repo instead of local storage for your supporting script files.

Kf

---

<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: [July 22, 2018, 7:39am UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/10 "2018-07-22T07:39:04Z")

</div>

> [@Albizia](#):
>
> … and there the button doesn’t disappear.

Sketches running under [OpenProcessing.org](http://OpenProcessing.org) are inside an `<iframe>` element. 😯

And when we invoke p5.Element::**parent()** to force a `<button>` to become a child of `<canvas>`, it seems like the browser just ignores that command! 😮

> [@Albizia](#):
>
> … the button DOES disappear.

On [Gosuness.free.fr/button/](http://Gosuness.free.fr/button/), the sketch isn’t inside an `<iframe>` element. 😺

And when method p5.Element::**parent()** is issued, the `<button>` is indeed transferred inside the sketch’s `<canvas>`! 🤣

However, anything inside a `<canvas>` isn’t displayed at all, unless a browser doesn’t have or can’t create the `<canvas>`. 👾

---

<div class="post-metadata">

### Author: ![Albizia](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Albizia](https://discourse.processing.org/u/Albizia)
#### Post date: [July 22, 2018, 8:41am UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/11 "2018-07-22T08:41:05Z")

</div>

Thanks for the enligthment.  
using the canvas as parent seems like just a bad idea :),  
The idea was to have a button on top on the canvas, using a position relative to the canvas instead of an absolute position.  
But I couldn’t find a way to do relative position to another object anyway, I just gave up and made my own button inside the canvas using just p5.

---

<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: [July 22, 2018, 9:02am UTC](https://discourse.processing.org/t/using-the-canvas-as-the-parent-of-an-element-makes-it-disappear/1937/12 "2018-07-22T09:02:10Z")

</div>

> [@Albizia](#):
>
> … using a position relative to the canvas instead of an absolute position.

https://codepen.io/GoSubRoutine/embed/preview/KNbwLW?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=KNbwLW
