调整 fabricjs Rects 的宽度/高度属性

resizing width / height properties of fabricjs Rects

我正在使用 fabric.js 来定义可以调整大小的矩形区域。我想知道是否有任何方法可以设置 fabric 以便它可以通过更改宽度/高度属性而不是 scaleX 和 scaleY 来调整元素的大小?

您可以使用 object:scaling 事件。

canvas.on("object:scaling", function(e) {
    var target = e.target;
    if (!target || target.type !== 'rect') {
        return;
    }
    var sX = target.scaleX;
    var sY = target.scaleY;
    target.width *= sX;
    target.height *= sY;
    target.scaleX = 1;
    target.scaleY = 1;
});

你应该设置脏标志,这对我来说解决了大部分问题:

canvas.on("object:scaling", function (e) {
            var target = e.target;
            if (!target || target.type !== 'rect') {
                return;
            }
            var sX = target.scaleX;
            var sY = target.scaleY;
            target.width *= sX;
            target.height *= sY;
            target.scaleX = 1;
            target.scaleY = 1;
            target.dirty = true;
        });