In order to get fabricjs canvas image by name, I need to set unique id or name to this iamge. I've created a new class fabric.NamedImage
That's how I did it.
fabric.NamedImage = fabric.util.createClass(fabric.Image, {
type: 'nameimage',
initialize: function (element, options) {
this.callSuper('initialize', element, options);
options && this.set('name', options.name);
},
toObject: function () {
return fabric.util.object.extend(this.callSuper('toObject'), { name: this.name });
},
_render: function (ctx) {
this.callSuper('_render', ctx);
}});
And fromObject
fabric.NamedImage.fromObject = function (object, callback) {
fabric.util.loadImage(object.src, function (img) {
var instance = new fabric.NamedImage(img, object);
callback && callback(instance);
});
};
fabric.NamedImage.async = true;
But when I'm trying to load canvas loadFromJSON
for some reasons I keep getting error
cannot read property 'async' of undefined
Here is code where I'm trying to load JSON
for (i = 0; i <= canvas.length; i++) {
JSON.parse(imageQuery[i]);
canvas[i].loadFromJSON(imageQuery[i]);
canvas[i].renderAll();
console.log(' this is a callback. invoked when canvas is loaded!xxx ');
}
I've already read cannot read property 'async' of undifined and save canvas to server with custom attribute
Is there some way to get object by name?