admin管理员组文章数量:1293149
I have a fabricImage and I want to allow resizing while keeping the aspect ratio intact.
By default, Fabric.js allows resizing from:
- Corners, which scales both x and y proportionally (this works as expected).
- Edges (left, right, top, bottom), which scales only x or y, breaking the aspect ratio.
I tried setting lockScalingX = true and lockScalingY = true, but this completely prevents scaling, including corner scaling. Desired Behavior:
- Allow resizing from corners while maintaining the aspect ratio.
- Allow resizing from edges, but force uniform scaling so that dragging an edge scales both x and y equally. (or disable border scaling, both ok)
- Prevent aspect ratio from being broken when resizing from edges.
What I’ve tried:
- Setting lockScalingX = true and lockScalingY = true (prevents all scaling).
- Searching the documentation but haven't found a clear way to enforce aspect ratio for all resize handles.
How can I achieve this? Any help would be greatly appreciated!
I have a fabricImage and I want to allow resizing while keeping the aspect ratio intact.
By default, Fabric.js allows resizing from:
- Corners, which scales both x and y proportionally (this works as expected).
- Edges (left, right, top, bottom), which scales only x or y, breaking the aspect ratio.
I tried setting lockScalingX = true and lockScalingY = true, but this completely prevents scaling, including corner scaling. Desired Behavior:
- Allow resizing from corners while maintaining the aspect ratio.
- Allow resizing from edges, but force uniform scaling so that dragging an edge scales both x and y equally. (or disable border scaling, both ok)
- Prevent aspect ratio from being broken when resizing from edges.
What I’ve tried:
- Setting lockScalingX = true and lockScalingY = true (prevents all scaling).
- Searching the documentation but haven't found a clear way to enforce aspect ratio for all resize handles.
How can I achieve this? Any help would be greatly appreciated!
Share asked Feb 12 at 22:03 AdamAdam 29.1k27 gold badges183 silver badges272 bronze badges 02 Answers
Reset to default 0Set the centeredScaling
property of the fabric.Object.prototype
to true
.
fabric.Object.prototype.centeredScaling = true;
This will ensure that all objects created using Fabric.js will have centered scaling enabled by default.
You can check Vue FabricJs Starter kit for basic structure initialization.
Create custom controls with controlled scaling :
fabric.FabricObject.prototype.controls = customControls();
Then make each controls individualy:
Action handler scalingEqually
doc
const scaleEquallyControl = {
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
actionHandler: fabric.controlsUtils.scalingEqually,
actionName: "scaling",
};
Define defaultControl()
with scalingEqually
action handler demo | doc
export const customControls = () => ({
ml: new fabric.Control({
x: -0.5,
y: 0,
...scaleEquallyControl,
}),
mr: new fabric.Control({
x: 0.5,
y: 0,
...scaleEquallyControl,
}),
mb: new fabric.Control({
x: 0,
y: 0.5,
...scaleEquallyControl,
}),
mt: new fabric.Control({
x: 0,
y: -0.5,
...scaleEquallyControl,
}),
...yourOtherControls
})
本文标签: javascriptHow to allow resizing while keeping aspect ratio in all casesStack Overflow
版权声明:本文标题:javascript - How to allow resizing while keeping aspect ratio in all cases? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741573990a2386174.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论