In Construct 2 engine there is an automatic letterbox scaling for the game canvas. I've tried to do this in my Javascript game, but i couldn't get the same result. The canvas fits vertically, but sometimes the width is high enough to overflow the screen. How can i make the canvas to fit the screen without overflowing while keeping aspect ratio like in Construct 2?
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
background-color: black
}
</style>
<script src="main.js"></script>
<meta charset="UTF-8">
<title>My Game</title>
</head>
<body id="body">
<canvas id="canvas"></canvas>
</body>
</html>