Search code examples
fabricjs

How to update size of group in fabricjs


In the fabricjs library, the class fabric.Group offers a .addWithUpdate(...) method which Adds an object to a group; Then recalculates group's dimension, position.

Is there a way to force the update?

I am creating a group using fabric.util.groupSVGElements( array_of_fabric_elements ), which does not update its size.

I have tried fabric.Group.setObjectsCoords(), but it throws a "not a function" error.


Solution

  • Which fabric.js version do you use? fabric.util.groupSVGElements didn't create fabric.Group – it created fabric.PathGroup.

    If you want to create a fabric.Group please use:

    var group = new fabric.Group([obj1, obj2, ...]);
    canvas.add(group);
    

    If you want to add/remove object from your group you can do this with:

    group.addWidthUpdate(object)/group.removeWidthUpdate(object)
    

    Both functions recalculate the group dimensions.