繁体   English   中英

fabric.js将自定义属性添加到IText

[英]fabric.js add custom property to IText

我想向fabricjs.IText添加自定义属性,我使用了与fabricjs.Text类相同的脚本:

fabric.CustomIText = fabric.util.createClass(fabric.IText, {
        type        : 'custom-itext',
        initialize  : function(element, options) {
            this.callSuper('initialize', element, options);
            options && this.set('textID', options.textID);
        },
        toObject: function() {
            return fabric.util.object.extend(this.callSuper('toObject'), {textID: this.textID});
        }
    });    
    fabric.CustomIText.fromObject = function(object) {
        return new fabric.CustomIText(object.text, object);
    };
    fabric.CustomIText.async = false;  

当我创建新的自定义itext时,没有问题。

 var text    = new fabric.CustomIText('NewText', { left: 0, top: 0 , fill: color, fillColor:color, textID: "SommeID"});
    canvas.add(text);

但是,当我想从JSON加载新的CustomItext时,我遇到了javascrip错误:

Uncaught TypeError: Cannot read property 'async' of undefined

谢谢

这是一个代码,可在画布上保存任何对象的序列化附加属性。 这可能会解决您的问题,对我有用

// Save additional attributes in Serialization
fabric.Object.prototype.toObject = (function (toObject) {
    return function () {
        return fabric.util.object.extend(toObject.call(this), {
            textID: this.textID
        });
    };
})(fabric.Object.prototype.toObject);

我将其与异步初始化一起使用:

fabric.TextAsset = fabric.util.createClass(fabric.IText, {
    type: 'textAsset',

    initialize: function(element, options) {
        this.callSuper('initialize', element, options);
        this.set('extraProp', options.extraProp);
    },

    toObject: function() {
        return fabric.util.object.extend(this.callSuper('toObject'), {
            extraProp: this.get('extraProp')
        });
    }
});

fabric.TextAsset.fromObject = function (object, callback) {
    callback(new fabric.TextAsset(object.text, object));
};

fabric.TextAsset.async = true;
}

暂无
暂无

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

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