# Access properties of a sketch's container in instance mode

**URL:** https://discourse.processing.org/t/access-properties-of-a-sketchs-container-in-instance-mode/10088
**Category:** Coding Questions
**Created:** [April 8, 2019, 12:47pm UTC](https://discourse.processing.org/t/access-properties-of-a-sketchs-container-in-instance-mode/10088 "2019-04-08T12:47:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![surmvoise](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/surmvoise/32/4495_2.png) [@surmvoise](https://discourse.processing.org/u/surmvoise)
#### Post date: [April 8, 2019, 12:47pm UTC](https://discourse.processing.org/t/access-properties-of-a-sketchs-container-in-instance-mode/10088/1 "2019-04-08T12:47:47Z")

</div>

How do I access properties of a sketch’s container in instance mode?

I’d like to run multiple sketches in all the div’s with a certain class on a page. And I’d like to make the width of these sketches the same as the with of their div. I do specify the container element when I append the div as a second argument into the p5() constructor. But how can I now access the container element and its properties like width, etc. from within a sketch?

Can someone help me?

Here’s some example code of my situation:

```auto
window.onload = function() {

function runAllSketches() {
    let sketches = [];
    let sketchDivs = document.getElementsByClassName("sketchDiv");
    for (var i = 0; i < sketchDivs.length; i++) {
      sketches[i] = new p5(sketch, sketchDivs[i]);
    }

var sketch = function(p) {
    var sketchHeight = 15;

    p.setup = function() {
        p.createCanvas(<<<here I need to get the container's width>>>, sketchHeight);
    }
    p.draw = function(){
        ...
    }
    runAllSketches();
}

```

---

<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: [April 8, 2019, 4:17pm UTC](https://discourse.processing.org/t/access-properties-of-a-sketchs-container-in-instance-mode/10088/2 "2019-04-08T16:17:11Z")

</div>

Let’s say you’ve got some “style.css” file similar to this 1:

```auto
.sketchDiv {
  width: 300px; height: 200px;
}

```

You can access the sketch’s main canvas via its hidden variable: _canvas_:

> <https://github.com/processing/p5.js/blob/0.7.3/src/core/p5.Renderer.js#L24-L34>

From there, you can access its most immediate parent container via: _parentElement_:

> **[Node.parentElement](https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement)**
>
> The Node.parentElement read-only property returns the DOM node's parent Element, or null if the node either has no parent, or its parent isn't a DOM Element.

So far, those are translated as: `const parentDiv = p.canvas.parentElement;`

Now we can access _parentDiv_’s dimensions via its _offsetWidth_ & _offsetHeight_ properties:

> **[HTMLElement.offsetWidth](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth)**
>
> The HTMLElement.offsetWidth read-only property returns the layout width of an element as an integer.

  

> **[HTMLElement.offsetHeight](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight)**
>
> The HTMLElement.offsetHeight read-only property returns the height of an element, including vertical padding and borders, as an integer.

Now you can call p5::**createCanvas()** using those 2 dimensions:

```javascript
p.setup = function () {
  const parentDiv = p.canvas.parentElement;
  p.createCanvas(parentDiv.offsetWidth, parentDiv.offsetHeight);

  // ...
};

```

---

<div class="post-metadata">

### Author: ![surmvoise](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/surmvoise/32/4495_2.png) [@surmvoise](https://discourse.processing.org/u/surmvoise)
#### Post date: [April 8, 2019, 4:44pm UTC](https://discourse.processing.org/t/access-properties-of-a-sketchs-container-in-instance-mode/10088/3 "2019-04-08T16:44:51Z")

</div>

Wow, thanks so much @GoToLoop! That worked for me. I would never have found that out by myself.

One question though, why do you use **const** and not **var** or **let**? I tried it with **var** and it worked just the same.

---

<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: [April 8, 2019, 4:57pm UTC](https://discourse.processing.org/t/access-properties-of-a-sketchs-container-in-instance-mode/10088/4 "2019-04-08T16:57:17Z")

</div>

There are 5 keywords that can be used to declare a variable in JS: 🔑

1. `var`
2. `let`
3. `const`
4. `function`
5. `class`

Here’s the reference for `const` btW: 😇

- [Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const](http://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 10, 2019, 11:59pm UTC](https://discourse.processing.org/t/access-properties-of-a-sketchs-container-in-instance-mode/10088/5 "2019-04-10T23:59:31Z")

</div>

Here is a breakdown of `var` vs `let` vs `const` and their scopes:

[http://www.constletvar.com/](http://www.constletvar.com/)

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/18cee3f48c16d6000d26766d4466293d28abdb28.png)

Const also cannot be reassigned.

By contrast, in Processing(Java), you create pseudo-global scope with top-level variables in the sketch – and limit scope further by declaring variables inside functions, classes, methods, within loops etc. To create something that cannot be reassigned (like const), you may use `final`.

The habit of declaring variables as constant can be a form of defensive programming – it helps catch errors if you later accidentally attempt to change a value which you have declared should not change. In some languages constants may also sometimes also have memory management or performance benefits.
