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

384
Vistas
Offset issues images when inside group (scaleToWidth)

I'm experiencing an issue when scaling an image inside a group the offset of the image changes. The group consists of a background (Rect) and an image. When scaling the group the offset if the image becomes different. Expected is that the offset remains the same as the background.

I'm using Fabric 4.0.0-beta.6

enter image description here

const canvas = new fabric.Canvas('canvas');
canvas.setWidth(document.body.clientWidth);
canvas.setHeight(document.body.clientHeight);

const bg = new fabric.Rect({
  width: 100,
  height: 100,
  fill: 'red',
});

fabric.Image.fromURL("https://dummyimage.com/300x150/000/fff", function(img) {

  img.scaleToWidth(bg.width)
  const post = new fabric.Group([bg, img], {
    left: 100,
    top: 100,
  });

  post.scaleToWidth(400);
  canvas.add(post);
  canvas.renderAll();
});

https://jsbin.com/migogekoxu/1/edit?html,css,js,output

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Setting the strokeWidth property of the background element to 0 fixed the offset for me.

const canvas = new fabric.Canvas('canvas');
canvas.setWidth(document.body.clientWidth);
canvas.setHeight(document.body.clientHeight);

const bg = new fabric.Rect({
  width: 100,
  height: 100,
  fill: 'red',
  strokeWidth: 0, //<---
});

fabric.Image.fromURL("https://dummyimage.com/300x150/000/fff", function(img) {

  img.scaleToWidth(bg.width)
  const post = new fabric.Group([bg, img], {
    left: 100,
    top: 100,
  });

  post.scaleToWidth(400);
  canvas.add(post);
  canvas.renderAll();
});
<canvas id="canvas"></canvas>
<script src="https://cdn.jsdelivr.net/npm/fabric@4.0.0-beta.6-browser/dist/fabric.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Obviously that's a bug with fabricJS. If you don't want to fix it on your own in it's source code you can workaround it however.

You can manually correct the position by adding an offset of 0.5 to the image's top and left properties.

For example:

const canvas = new fabric.Canvas('canvas');

const bg = new fabric.Rect({
  width: 100,
  height: 100,
  fill: 'red'
});

fabric.Image.fromURL("https://dummyimage.com/300x150/000/fff", function(img) {
  img.scaleToWidth(bg.width);
  img.left += 0.5;
  img.top += 0.5;
  const post = new fabric.Group([bg, img], {
    left: 100,
    top: 100
  });

  post.scaleToWidth(400);
  canvas.add(post);
  canvas.renderAll();
});
<script src="https://cdn.jsdelivr.net/npm/fabric@4.0.0-beta.6-browser/dist/fabric.min.js"></script>
<canvas id="canvas" width="600" height="400"></canvas>

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