I am facing an issue with my responsive canvas that should resize to fit the screen window within a div. The canvas is intended to be 1100x1100 but needs to scale based on the screen size while maintaining a square aspect ratio.
The problem arises when the canvas expands below the screen rather than filling the div size, possibly due to the presence of a top menu navbar and its dynamic behavior. In some cases, the navmenu can collapse or be hidden, making it challenging to adjust the canvas position based on pixels.
How can I resolve this issue to make sure the canvas fits the screen without any scrolling required? Should I look for a CSS solution or turn to Javascript for assistance?
If you try setting the CSS width/height to 100%, you will notice that the square aspect is lost, and the canvas still extends beyond the height viewport. My goal is for the canvas to always remain a square shape. (Please click Full page to see the demo)
// Code snippets here...
// Additional CSS code snippets...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav class="navbar navbar-expand-md navbar-light mb-1" style="background-color: #f2e9e4">
<!-- Navbar content -->
</nav>
<div class="container-fluid">
<canvas id="myCanvas" style="position: absolute; z-index: 1">Your browser does not support HTML5.
</canvas>
</div>