javascript - Fabricjs width and getWidth() - Stack Overflow

I have an algorithm which is rendering an object with width and height. But when I use setWidth() that

I have an algorithm which is rendering an object with width and height. But when I use setWidth() that object doesn't actually get that width, it somehow gets something like x2 of the width I gave it. Check the code below:

    var width = canvas.getWidth();
    obj.setWidth(width);
    obj.setCoords();
    console.log(obj.getWidth());
    console.log(obj.width);

First log has wrong width "x2" and the second log has the correct width.

I have an algorithm which is rendering an object with width and height. But when I use setWidth() that object doesn't actually get that width, it somehow gets something like x2 of the width I gave it. Check the code below:

    var width = canvas.getWidth();
    obj.setWidth(width);
    obj.setCoords();
    console.log(obj.getWidth());
    console.log(obj.width);

First log has wrong width "x2" and the second log has the correct width.

Share Improve this question edited Aug 8, 2017 at 9:35 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Nov 19, 2015 at 19:28 Honchar DenysHonchar Denys 1,5184 gold badges29 silver badges58 bronze badges 2
  • Is the object a circle? You could be passing it a radius. – Jack Guy Commented Nov 19, 2015 at 19:31
  • object is group actually. – Honchar Denys Commented Nov 19, 2015 at 19:50
Add a ment  | 

1 Answer 1

Reset to default 4

I've struggled with similar stuff, and so I've tested (with fabricjs 1.6.2) and found out that for fabric.Rect rect.width is not equal to rect.getWidth()

rect.width gives the width not considering the rect's strokeWidth

rect.getWidth() considers the rect's strokeWidth

Also remember that the stroke is painted by default half inside the object's edge and half outside of it, thus for example strokeWidth of 10 enlarges the rect's perceived size by 5 + 5 = 10 pixels.

Hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745188465a4615748.html

相关推荐

  • javascript - Fabricjs width and getWidth() - Stack Overflow

    I have an algorithm which is rendering an object with width and height. But when I use setWidth() that

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信