Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
Automatic letterbox scaling HTML5 Canvas

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>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would try to set the <canvas> element max-width: 100% or max-width: 100vw.

about 4 years ago · Juan Pablo Isaza Denunciar

0

After trying so hard, i've finally got an answer for my question. I created a flex container with align-items as stretch (so the canvas inside it can be automatically resized), and the width and height of this container is handled by javascript, so when a resize event is triggered, the container is automatically resized to fit in screen keeping aspect ratio.

<!DOCTYPE html>
<html>
    <head>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                width: 100vw;
                height: 100vh;
                background-color: black;
            }
            #container {
                display: flex;
                justify-content: center;
                align-items: stretch;
            }
        </style>
        <script src="main.js"></script>
        <meta charset="UTF-8">
        <title>Game</title>
    </head>

    <body>
        <div id="container">
            <canvas id="canvas"></canvas>
        </div>
    </body>
</html>

window.onload = function() {
    var container = document.getElementById("container")
    var canvas = document.getElementById("canvas")
    var ctx = canvas.getContext("2d")

    canvas.width = 640
    canvas.height = 1200
}
window.addEventListener("resize", function() {
    let width_scale = window.innerWidth / canvas.width
    let height_scale = window.innerHeight / canvas.height
    let scale = Math.min(width_scale, height_scale)
    container.style.width = `${canvas.width * scale}px`
    container.style.height = `${canvas.height * scale}px`
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda