fabric.js 中的 'toObject()' 函数

the 'toObject()' function in fabric.js

我在原来的canvas对象上设置了一些用户自定义的属性,当我使用canvas.getActiveObject.toObject()时,结果不能显示用户自定义的attributes.What,我应该这样做吗可以让它显示吗?或者我设置属性的方法不对?谢谢。

你应该覆盖原型函数 toObject() 并稍微改变它,像这样:

 //override prototype.toObject and add your custom properties here
    fabric.Object.prototype.toObject = (function(toObject) {
        return function() {
            return fabric.util.object.extend(toObject.call(this), {
                objectId: this.objectId,//my custom property
                _controlsVisibility:this._getControlsVisibility(),//i want to get the controllsVisibility
                typeTable:this.typeTable,//custom property
                txtType:this.txtType,//custom property
                customOptions: this.customOptions,//custom property
                emptySeats: this.emptySeats,//custom property
            });
        };
    })(fabric.Object.prototype.toObject);

之后,当您使用 toObject() 获取对象时,您将获得上面输入的属性。

希望有助于好运。

调用Object.toObject()时可以提供propertiesToInclude的数组。见 API Doc here.

示例:

var rect = new fabric.Rect({
    foo: 'bar', // property that should be included
    width: 50,
    height: 50,
    left: 50,
    top: 50
});
var asObject = rect.toObject(['foo']);
console.log(object.foo); // 'bar'