简体   繁体   English

Fabric.js:如何观察对象:缩放事件并动态更新属性?

[英]Fabric.js: How to observe object:scaling event and update properties dynamically?

I'm trying to get the shape properties to dynamically update when the shape is being scaled.我试图让形状属性在缩放形状时动态更新。

Here's a fiddle: http://jsfiddle.net/anirudhrantsandraves/DAjrp/这是一个小提琴: http : //jsfiddle.net/anirudhrantsandraves/DAjrp/

The console log commands:控制台日志命令:

console.log('Width =  '+scaledObject.currentWidth);
console.log('Height = '+scaledObject.currentHeight);

are supposed to dynamically display the height and width of the shape as it is being scaled.应该在缩放时动态显示形状的高度和宽度。

The properties remain the same in the console when the shape gets scaled.当形状被缩放时,控制台中的属性保持不变。 However, when I select the object again and scale it, the previous values of the properties are displayed.但是,当我再次选择对象并对其进行缩放时,会显示属性的先前值。

Is there a way to make this dynamic?有没有办法让这个动态?

getWidth() and getHeight() are used to get the current width and height in Fabric.js. getWidth()getHeight()用于获取 Fabric.js 中的当前宽度和高度。

So in "object:scaling" event:所以在“对象:缩放”事件中:

canvas.on('object:scaling', onObjectScaled);

function onObjectScaled(e)
{
    var scaledObject = e.target;
    console.log('Width =  '+scaledObject.getWidth());
    console.log('Height = '+scaledObject.getHeight());
}

will serve your purpose.将服务于您的目的。

Updated fiddle — http://jsfiddle.net/DAjrp/2/更新小提琴 - http://jsfiddle.net/DAjrp/2/

Just in case, if you wanted to get the scaled height以防万一,如果您想获得缩放高度

canvas.on("object:scaling", (e) => {
    canvas.getActiveObjects().forEach((o) => {
        // if it's scaled then just multiple the height by scaler
        const objHeight = o.scaleY ? o.height * o.scaleY : o.height;
        // ...
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM