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

124
Visualizações
centralize background image inside canvas based on zoom factor

I need to centralize background image inside canvas based on zoom factor while retaining aspect ratio of image and without being cropped. I tried in this way but either it gets cropped(cannot make whole image viewable). This is how I have done and this is how it looks

import { fabric } from "fabric";
import "./styles.css";

document.getElementById("app").innerHTML = `
<div>
  <canvas class="canvas" id="conCanvas"></canvas>
</div>
`;

let canvas = new fabric.Canvas("conCanvas");
let zoomedScaleFromServer = 1.5;
canvas.setHeight(window.innerHeight);
canvas.setWidth(window.innerWidth);
// let imgWidth, imgHeight;

fabric.Image.fromURL(
  "https://media.istockphoto.com/photos/modern-interior-design-floor-plan-3d-render-picture-id1210637949?b=1&k=20&m=1210637949&s=170667a&w=0&h=_AYB0vxwWWVFSUjaNpVb3X88ybQLLXtdNJUW8IllR1E=",
  function (img) {
    canvas.setBackgroundImage(
      img,
      () => {
        canvas.renderAll();
        // imgWidth = img.width * img.scaleX;
        // imgHeight = img.height * img.scaleY;
      }
      // { FIX : Aspect ratio
      //   scaleX: canvas.width / img.width,
      //   scaleY: canvas.height / img.height
      // }
    );
    // canvas.centerObject(img);
    canvas.setWidth(img.width * zoomedScaleFromServer);
    canvas.setHeight(img.height * zoomedScaleFromServer);
    canvas.zoomToPoint(
      new fabric.Point(canvas.width / 2, canvas.height / 2),
      zoomedScaleFromServer
    );
  }
);

enter image description here

Here is the sandbox as well https://codesandbox.io/s/festive-water-izl19?file=/src/index.js

about 4 years ago · Juan Pablo Isaza
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