我想为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;
当我创建新的自定义迭代时,没有问题。
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
谢谢
答案 0 :(得分:4)
这是为画布上的任何对象在序列化中保存其他属性的代码。这可能会解决您的问题,它对我有用
// 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);
答案 1 :(得分:0)
我让它使用异步初始化:
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;
}