You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Bug]: When activating two or more objects, discardActiveObject or setActiveObject after setting props of active objects cause unexpected movement.
#10193
Open
7 tasks done
huangbuyi opened this issue
Oct 2, 2024
· 3 comments
I have searched and referenced existing issues and discussions
I am filing a BUG report.
I have managed to reproduce the bug after upgrading to the latest version
I have created an accurate and minimal reproduction
Version
6.0.2
In What environments are you experiencing the problem?
Chrome
Node Version (if applicable)
None
Link To Reproduction
See code below.
Steps To Reproduce
Acitve two or more objects manually.
Set props of active object.
Run discardActiveObject or setActiveObject.
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Fabric Test</title><!--<scriptsrc="https://cdn.jsdelivr.net/npm/fabric@latest/dist/index.min.js"></script> --><scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script></head><body><canvasid="canvas"></canvas><buttonid="btn">Reproduce Exception</button><p>When activating two or more objects, discardActiveObject or setActiveObject after setting props of active objects cause unexpected movement. </p><p>Click it before select all objects manually or at least two objects</p><script>
const canvas = new fabric.Canvas('canvas', {width: 800,height: 800});
const rects = [[20, 20], [40, 20], [60, 20], [80, 20]].map(item =>{returnnewfabric.Rect({left: item[0],top: item[1],fill: 'yellow',width: 120,height: 200,strokeWidth: 1,stroke: 'lightgreen',})});
canvas.add(...rects);
const btn = document.getElementById('btn');
btn.addEventListener('click', () =>{for(constrectofrects){rect.set({left: 0,top: 0})}
canvas.renderAll();
canvas.discardActiveObject();
// canvas.setActiveObject(rects[0]);
});</script></body></html>
Expected Behavior
All objects move to right position.
Actual Behavior
Active objects move to wrong position.
Origin positions:
Unexcepted positions:
I found this problems in v5.3.0, then I upgraded to latest version, the problem was still existed. Is this a real problem, or just I did something wrong? To fix this, I have to discardActiveObject before set object's props then setActiveObject same objects back in my project.
Error Message & Stack Trace
No response
The text was updated successfully, but these errors were encountered:
When A and B (multiple elements) are selected, an active group (C) is created internally. Setting the position of A means setting A's position relative to C. The design is such that you need to deselect A before setting its position.
I need to change objects's positions whether they are selected or not. The way, Deselecting -> Setting Position, -> Selecting back, is not good enough for me. Instead, I will check the object before setting and getting, if it is in a group, then make a conversion:
exportfunctionsetObjectProps(object: fabric.Object,props: Partial<IObjectProps>){const{ left, top }=props;if(typeofleft==='number'&&object.group){props.left=left-object.group.left!-object.group.width!/2;}if(typeoftop==='number'&&object.group){props.top=top-object.group.top!-object.group.height!/2;}object.set(props);}exportfunctiongetObjectProp<TextendskeyofIObjectProps>(object: fabric.Object,prop: T): IObjectProps[T]{if(object.group){if(prop==='left'){return(object.left!+object.group.left!+object.group.width!/2)asIObjectProps[T];}if(prop==='top'){return(object.top!+object.group.top!+object.group.height!/2)asIObjectProps[T];}}returnobject.get(prop)asIObjectProps[T];}
The size of the group is determined by the position of its child elements. When you modify the position of the child elements, the size of the group should update. I noticed that the code in your example does not take into account rotated groups; you might want to try the code below.
exportfunctionsetObjectProps(object: FabricObject,props: AnyObject){unwarp(object,()=>object.set(props))}exportfunctiongetObjectProp(object: FabricObject,prop: string){returnunwarp(object,()=>object.get(prop));}functionunwarp(object: FabricObject,fn=()=>{}){const{ group, parent }=object;constisActiveSelection=group&&"multiSelectionStacking"ingroup;// const isActiveSelection = group && group != object.parent; // Equivalent to the line above.if(isActiveSelection)group.remove(object);// const parent = object.group; // Equivalent to `the parent` mentioned above.parent?.remove(object);constv=fn();parent?.add(object);parent?.triggerLayout();if(isActiveSelection){group.add(object);group.triggerLayout();}returnv;}
CheckList
Version
6.0.2
In What environments are you experiencing the problem?
Chrome
Node Version (if applicable)
None
Link To Reproduction
See code below.
Steps To Reproduce
discardActiveObject
orsetActiveObject
.Expected Behavior
All objects move to right position.
Actual Behavior
Active objects move to wrong position.
Origin positions:
Unexcepted positions:
I found this problems in v5.3.0, then I upgraded to latest version, the problem was still existed. Is this a real problem, or just I did something wrong? To fix this, I have to
discardActiveObject
before set object's props thensetActiveObject
same objects back in my project.Error Message & Stack Trace
No response
The text was updated successfully, but these errors were encountered: