【问题标题】:the 'toObject()' function in fabric.jsfabric.js 中的“toObject()”函数
【发布时间】:2016-03-24 16:10:21
【问题描述】:

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

【问题讨论】:

标签: canvas html5-canvas fabricjs


【解决方案1】:

您可以在调用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'

【讨论】:

    【解决方案2】:

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

    希望能带来好运。

    【讨论】:

    • 我想知道为什么有人给了我反对票。请启发我这样做的人
    • 非常感谢,我已经尝试过了,它确实有效。但是@forrert 告诉我 API DOC 也有类似的方法来解决它,无论如何谢谢
    • 你好,它当然可以 100% 工作。我们在这里花几个小时阅读、理解和回答,以便互相帮助。当你投反对票时,请确保。就我个人而言,只有当有愚蠢的答案或不相关的答案时,我才会投反对票。
    • 对不起,我不明白你为什么认为我投反对票,我根本没有对你投反对票,也许其他人这样做了。我是新来的,我的声誉票数太低,无法投反对票或赞成票。我只想谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2016-05-18
    相关资源
    最近更新 更多