Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

angularjs - How to set Bounding box rotation and dimension of fabric group same as it's objects

FabricJs Rotated Group

Version fabricjs 4.2.0

As You can see in picture I have Text and Rectangle in Group. Both text and Rectangle are rotated. So I Believe the Bounding Box of group should be rotated. But it isn't. Instead bounding box not rotated at all.

Note: I have added already Rotated text and rect to group. I tried using addWithUpdate() but doesn't work. I also tried giving angle to Group but its rotate child object again. What I want is bounding box of group should be same size of child object(Rectangle which acting as Border) and with same rotation angle of object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem here is basically that when multiple objects are selected in the canvas, Fabric dynamically creates a group for them. Because of this, it assumes the rotation of the bounding box for the objects should be 0, because that's a sensible default. Therefore, if we want to get around this, we need to create our own group before we add the items to the canvas so that Fabric knows to treat them as part of the same thing.

In order to do that, we create a fabric.Group first, add the objects we want to add to the canvas to that, and then add that group to the canvas. For example,

const text = ...;
const rectangle = ...;

const group = new fabric.Group();
group.initialize([text, rectangle]);
canvas.add(group);

Note that this creates a static group - the objects added to the group cannot be separated from each other by user input. Unless there is other code to explicitly break up the group, fabric will continue to treat them as one entity.

JSFiddle: http://jsfiddle.net/35wgmhca/1/

The linked JSFiddle has an orange square grouped with a blue square to demonstrate how that bounding box correctly sticks to the group.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...