我想将自定义的Fabric.Image类保存为JSON,然后再使用loadFromJSON将其还原。
我查询了以下问题 Fabric.js - how to save canvas on server with custom attributes 但是看来“ fromObject”的部分进展不顺利。
当您在下面的WEB页面上按下“单击”按钮时,就会发生这种情况。 https://onoderayuuki.github.io/tana_motif/index.html
[子类]
fabric.customMotif = fabric.util.createClass(fabric.Image, {
type: 'customMotif',
borderColor: '#19310B',
cornerColor: '#19310B',
cornerSize: 20,
initialize: function(src, options) {
this.callSuper('initialize', options);
options && this.set('name', options.name);
this.image = new Image();
this.top = options.top;
this.left = options.left;
this.image.src = src;
this.image.onload = (function() {
this.width = this.image.width;
this.height = this.image.height;
this.loaded = true;
this.setCoords();
this.fire('image:loaded');
}).bind(this);
},
toObject: function(options) {
return fabric.util.object.extend(this.callSuper('toObject'), {
});
},
_render: function(ctx) {
if (this.loaded) {
ctx.drawImage(this.image, -this.width / 2, -this.height / 2);
}
}
});
fabric.customMotif.fromObject = function(object, callback) {
fabric.util.loadImage(object.src, function(img) {
callback && callback(new fabric.customMotif(img, object));
});
};
[JSON]
canvasState = JSON.stringify(canvas);
canvas.loadFromJSON(canvasState);
谢谢。
答案 0 :(得分:0)
如果选中此功能,则该类的第一个字母为大写。
getKlass: function(type, namespace) {
// capitalize first letter only
type = fabric.util.string.camelize(type.charAt(0).toUpperCase() + type.slice(1));
return fabric.util.resolveNamespace(namespace)[type];
},
将类从fabric.customMotif
重命名为fabric.CustomMotif