If I have a canvas element that is a certain width and height, e.g.
<canvas id="display" width="900" height="650" tabindex=0></canvas>
and I want to scale up the width and height of the canvas to as large as possible while being contained within the viewport without changing the aspect ratio or increasing the overall area for objects to be displayed within the canvas.
This means that if a window is width 2200
and height 1300
, both the display width and height of the canvas will be doubled (and no larger because otherwise the height could not be contained) and the canvas will have the new dimensions 1800
and 1300
.
However, I am scaling up the CSS display height of the canvas element, not the canvas itself, which means that objects inside the canvas should also have their dimensions increased.
canvas {
width: ...
height: ...
}
It would be even better if the canvas can rescale itself when the window is resized, perhaps with an event listener.
Is there any way to do this in JS?