Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
How to autofit canvas in parent div?

I have a canvas inside div:

<div class="preview"><canvas id="cropped" width="2480" height="2003"></canvas></div>

Div block is less then canvas side. How to autofit canvas by two cases:

  1. canvas width autofit proportional of parent width div
  2. canvas height autofit proportional of parent height div

I tried this way:

public autofit = (orientation: RegistryOrientation) => {
    let previews = document.getElementsByClassName('preview');

    if (previews.length > 0) {
        let preview = previews[0];
        let width = preview.clientWidth;
        let height = preview.clientHeight;

        if (orientation === RegistryOrientation.album) {
            this.imgCanvas.style.width = width + 'px';
            this.imgCanvas.style.height = 'auto';
        } else if (orientation === RegistryOrientation.book) {
            this.imgCanvas.style.height = height + 'px';
            this.imgCanvas.style.width = 'auto';
        }
    }
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can simply use the '%' symbol to denote proportional height of the parent. For example (in your CSS):

#cropped {
  width: 100%;
  height: 80%;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Pretty sure this is what you want:

div {
  border: 1px solid red;
  width: 200px;
  height: 200px;
  resize: both;
  overflow: hidden;
}

canvas {
  border: 1px solid blue;
  aspect-ratio: 1 / 1;
  box-sizing: border-box;
  max-width: 100%;
  max-height: 100%;
  margin: auto;
}
<div><canvas width="1000" height="1000"></canvas></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda