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

时间:2015-12-18 01:47:29

标签: canvas html5-canvas fabricjs

我在原始画布对象上设置了一些用户定义的属性,当我使用canvas.getActiveObject.toObject()时,结果无法显示用户定义的属性。我该怎么做才能让它显示出来?或者也许我设置属性的方法是错误的?谢谢。

2 个答案:

答案 0 :(得分:1)

调用propertiesToInclude时,您可以提供Object.toObject()数组。请参阅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'

答案 1 :(得分:0)

你应该覆盖原型函数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()获取对象时,您将获得上面输入的属性。

希望能帮助你好运。