Issue with the dimensions and proportions of the canvas

After creating a canvas with HTML and setting the ratio to 16/9 in the CSS, I specified a width of 100vw and added a border. However, I noticed that the canvas appeared larger than expected on the screen.

#canvas{
width: 100vw;
aspect-ratio: 16/9;
border: 5px solid red;

}

To address this issue, I had to adjust the size and padding accordingly.


*{

margin: 5px;

margin-left: 3%

}

.canvas{

width: 80vw;

aspect-ratio: 16/9

border: 3px solid red;

}

Although this solution worked, I was hoping for a fullscreen display.

Screenshots:

Answer №1

To ensure your canvas adjusts dynamically to window size, use this JavaScript snippet:

const myCanvas = document.querySelector("canvas");
myCanvas.width = window.innerWidth - myCanvas.offsetLeft;
myCanvas.height = window.innerHeight - myCanvas.offsetTop;

This code will set the canvas dimensions to match the window size and adjust for any other HTML elements present. Don't forget to add an event listener to update the canvas size when the window is resized:

addEventListener("resize", function(){
    myCanvas.width = innerWidth;
    myCanvas.height = innerHeight;
    //INCLUDE FUNCTIONS TO CALL HERE!!
})

Remember to call any necessary functions that need to be reset, such as those involved in object generation.

Answer №2

To adjust the width of your canvas, you need to account for the border by subtracting it from the total width:

body {
  margin: 0;
  overflow: hidden;
}

#canvas{
  width: calc(100vw - 10px);
  aspect-ratio: 16/9;
  border: 5px solid red;

}

Check it out here: https://codepen.io/kikosoft/pen/ZEmjRxW

Keep in mind that I've set the body's margin to zero. If yours is different, make sure to adjust accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensuring divs are centered when resizing the page

Is there a way to center an unordered list of tables while ensuring that it remains centered even when the window is resized? The list should move each table to a new line if they can no longer fit. Check out the code snippet below for reference: <d ...

After applying the rotateY function, the z-index appears to be malfunctioning

My current project involves creating a page flip effect, which requires two overlapping div elements. Initially, both divs are side by side with the second div having a -webkit-translateY(180deg) property applied to it. The first div has a z-index of 2, wh ...

Issues with button hover causing background transition difficulties

I've been trying to achieve a smooth background image transition upon hovering over my buttons. Despite searching for solutions in various posts, I haven't been successful in applying any of them to my code. I realize that J Query is the way to ...

Creating a JavaScript function to automatically hide a dropdown menu after a specific duration

I'm currently working on a side menu that drops down when hovering over Section One within the <a> tag. I need some guidance on how to make the JavaScript code continuously check the state of the list after a set amount of time in order to autom ...

Changing the appearance of a radio button dynamically upon clicking

I am currently working on a dynamic pickup date form that utilizes radio buttons. My goal is to change the style of the selected value when a user clicks on it. Below is the code I have tried, but it has not been successful: foreach ($period as $day){ ech ...

The height attribute in HTML tables fails to restrict height in Firefox

Is there a way to restrict the height of a table while still being able to scroll through the hidden elements? I've tried a few methods but none seem to work as intended: http://www.example.com/code/example table.ex1 { table-layout: auto; h ...

Previewing multiple images with multiple file inputs

I am trying to find a way to preview multiple images uploaded from different inputs. When the button is pressed, the content from a textarea containing <img src="$img" id="img1"> is written inside a div called seeimg. The IDs for these images range f ...

Is it possible for ng-model to verify its own value?

Recently, I've been experimenting with Angular and found myself facing a dilemma. Is it possible to apply a different CSS style based on whether the value of ng-model is greater than 0? `<span ng-model="users2" ng-hide="!myVar" ng-class="{'te ...

Tips for containing `overflow` within a flexbox container

I've organized my body space into three sections: header, content, and footer. To achieve a sticky footer effect at the bottom, I used the flex property in my layout. However, I'm struggling to add a scrollbar to my main container box. I've ...

How to perfectly center the Bootstrap Modal using CSS styling

Looking for a way to vertically align the Bootstrap Modal? I've attempted using CSS with no success. I tried this: .modal-dialog { display: inline-block; text-align: left; vertical-align: middle; } However, the modal still displays in t ...

Unusual CSS behavior discovered while implementing horizontal scrolling navbar with overflow-x

I've been working on a navbar that will scroll horizontally when it exceeds the width of its parent container. It features a bottom border with a different color for the active link. Using a negative margin to overlap them works well, but as soon as ...

Utilizing Regular Expressions to remove specific CSS attributes from an HTML content

I'm facing a challenge with my telerik RadEditor where users can input HTML that gets saved to my database. While this usually works well, I encounter issues when the CSS attributes position: absolute; or z-index: 100; (any # for z-index) are present ...

Infinite scrolling with a dynamic background

Hi there, I am working on my website and trying to create a smooth transition between sections similar to the one demonstrated here:. The challenge I'm facing is that the backgrounds of my sections cannot be fixed; they need to have background-attachm ...

Preserve the authentic picture along with a blur mask that can be dragged and applied to it

Is there a way to preserve the original image while having a draggable blur mask over it? If you want to see an example of a draggable blur mask over an image, you can check out this link: https://codepen.io/netsi1964/pen/AXRabW $(function() { $("#ma ...

Is it possible to use window.print() to print the entire contents of a DIV with both horizontal and vertical scroll bars?

In my code, I have a div that contains both horizontal and vertical scrollers. Whenever I try to print the content inside this div using the window.print() method, it only prints the visible portion of the div. Here is the code snippet that I have attempte ...

Efforts to seamlessly combine two divisions of a business card through the use of CSS and HTML

Currently, I am working on designing a business card that includes both front and back sections. Below is the code snippet that I have been using: .business-card:hover .front{ transform: perspective(700px) rotateX(180deg); } .business-card .back{ tr ...

What is the best way to combine flex-direction and flex-wrap in one row?

I'm working with a React component that looks like this: <Card className='form-margin width card' zDepth='3'> <CardText> <strong>Test</strong><br /> This is some text </Card ...

The images on my website are not displaying properly on different devices

My attempt at creating a website for fun and testing purposes is facing issues when viewed on mobile devices. The images and specially positioned elements are not displaying correctly, extending beyond the page boundaries. I have tried adding the viewport ...

How to temporarily disable CSS hover effects

I have a menu with some links <ul id="nav"> <li> <a href="#">Project</a> <div class="subs"> <div> <ul> <li> <h3>Save</h3> <ul> <li> <a i ...

Creating a Unique CSS Grid Border with Custom Images

I have a client project using nextjs and the design calls for a component with a custom border on a responsive CSS grid. I've successfully created the CSS grid with all the necessary content, but I'm struggling to add the required border as per t ...