Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

382
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!