Set p5 canvas to maximum width of parent div

Viewed 521

How can I get the width of my parent div?

I need the canvas to bethe maximum width it can be. But the div should still be inside the boundaries set by my CSS-file.

In other words: How do i get the maximum width possible whilst still being inside the divs boundaries?

So:

function setup() {
    //somehow get the width of the div and set the canvas to that
    const myCanvas = createCanvas(400, 400);
    myCanvas.parent('canvasDiv');
}

function draw() {
    background(0);
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Default Page Title</title>
    <link rel="stylesheet" href="blogPost.css">
    <script type="text/javascript" src="../p5.js"></script>
    <script type="text/javascript" src="sketch.js"></script>
</head>
<body>


<div id="containerHeader">
    <h1>Default Page header</h1>
</div>
<div id="canvasDiv"></div>
<div id="container">
    <p>default description</p>
</div>
</body>
</html>

And lastly my CSS:

#canvasDiv {
    margin: 0;
    padding: 0;
}
1 Answers

To get the width of my parent div, you can add CSS height and width for canvasDiv. And after if you want that container take height and width from parent, you can write height: inherit; and width: inherit;

Related