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

Canvas ';toObject()';fabric.js中的函数,canvas,html5-canvas,fabricjs,Canvas,Html5 Canvas,Fabricjs,我在原始画布对象上设置了一些用户定义的属性,当我使用canvas.getActiveObject.toObject()时,结果无法显示用户定义的属性。我应该怎么做才能让它显示这些属性?或者我设置属性的方法是错误的?谢谢。您应该跳过原型函数toObject()稍微改变一下,像这样: //override prototype.toObject and add your custom properties here fabric.Object.prototype.toObject = (fu

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

您应该跳过原型函数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()
时,您可以提供一组
属性包括
。看

例如:

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'

欢迎来到StackOverflow!请花点时间阅读这篇文章,如果可能的话,请提供一个答案。我想知道为什么有人投我反对票。请告诉我是谁做的谢谢,我已经试过了,它很有效。但是@forrert告诉我API文档也有类似的方法来解决它,无论如何谢谢Shello,当然它100%有效。为了互相帮助,我们花了几个小时在这里阅读、理解和回答。当你投反对票的时候,请确保。就我个人而言,只有当有愚蠢的答案或不相关的答案时,我才会投反对票。对不起,我不明白你为什么认为我投了反对票,我根本没有投你的反对票,也许是其他人投的。我是新来的,我的声誉太低,不能投反对票或赞成票。我只是想感谢你。