javascript - How to fit HTML canvas to screen while maintaining aspect ratio and not cutting off the canvas? - Stack Overflow

I'm making a game using HTML canvas and javascript. My canvas is 1280 x 720 px (16:9 ratio). I wan

I'm making a game using HTML canvas and javascript. My canvas is 1280 x 720 px (16:9 ratio). I want my game to be displayed at full screen, showing black bars if the screen ratio is not 16:9.

If I do this:

canvas {
    position: absolute;
    width: 100%;
    height: 100%;
}

Then my game is played at fullscreen, but stretched to fill screen if the screen ratio is not 16:9. I don't want it to be stretched, I want to show black bars if needed.

If I do this:

canvas {
    position: relative;
    width: 100%;
    height: 100%;
}

Then the canvas keeps the right aspect ratio, but it will be cut off if the screen is wider than 16:9, and I don't want that either.

How can I achieve this? Other answers I found revolve around updating canvas.width and canvas.height on window resize, but I'd rather not resize canvas.width or canvas.height since the game is designed to work under 1280 x 720. Sure I could scale the drawing according to canvas.width and canvas.height, but I wonder if this can be achieved without doing that.

Thank you

I'm making a game using HTML canvas and javascript. My canvas is 1280 x 720 px (16:9 ratio). I want my game to be displayed at full screen, showing black bars if the screen ratio is not 16:9.

If I do this:

canvas {
    position: absolute;
    width: 100%;
    height: 100%;
}

Then my game is played at fullscreen, but stretched to fill screen if the screen ratio is not 16:9. I don't want it to be stretched, I want to show black bars if needed.

If I do this:

canvas {
    position: relative;
    width: 100%;
    height: 100%;
}

Then the canvas keeps the right aspect ratio, but it will be cut off if the screen is wider than 16:9, and I don't want that either.

How can I achieve this? Other answers I found revolve around updating canvas.width and canvas.height on window resize, but I'd rather not resize canvas.width or canvas.height since the game is designed to work under 1280 x 720. Sure I could scale the drawing according to canvas.width and canvas.height, but I wonder if this can be achieved without doing that.

Thank you

Share Improve this question asked May 30, 2021 at 6:43 David M. CasasDavid M. Casas 591 silver badge9 bronze badges 1
  • 1 canvas is a replaced-level element. As such object-fit can be applied to it. Particular you looking into object-fit: contain; – tacoshy Commented May 30, 2021 at 6:51
Add a ment  | 

1 Answer 1

Reset to default 6

First as stated in the ments already, canvas is a replaced-level element. As such object-fit can be applied to it just like with images that are used for this demonstration.

The solution starts with a "wrapper" (div.fullscreen). This element is positioned to fill the entire viewport by using: position: fixed; inset: 0; (inset: 0; = top, right, bottom, left: 0 not fully supported yet: https://developer.mozilla/en-US/docs/Web/CSS/inset).

Next we address the image within this wrapper (or the canvas in your case) and setting the max-height and max-width to 100%. Then we use object-fit: contain; to let the canvas maintain its aspect-ratio but filling the wrapper without getting cut off.

Last but not least we use display: flex; justify-content: center; align-items: center; to display the canvas in the vertical and horizontal center.

body {
  margin: 0;
}

.fullscreen {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: black;
  display: flex;
  justify-content: center;
  align-items: center;
}

.fullscreen img {
  object-fit: contain;
  max-height: 100%;
  max-width: 100%;
}
<div class="fullscreen">
  <img src="https://via.placeholder./1280x720.jpg">
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342562a4623394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信