The error "Unable to read the property" async "from undefined" occurs because "klass" was not found - https://github.com/kangax/fabric.js/blob/master/src/util/misc.js#L214-215 .
You must assign your custom object to the fabric object - otherwise canvas.loadFromJSON() does not work.
var fabric.CustomGroup = fabric.util.createClass(fabric.Group, { type : 'customGroup', initialize : function(objects, options) { options || ( options = { }); this.callSuper('initialize', objects, options); this.set('customAttribute', options.customAttribute || 'undefinedCustomAttribute'); }, toObject : function() { return fabric.util.object.extend(this.callSuper('toObject'), { customAttribute : this.get('customAttribute') }); }, _render : function(ctx) { this.callSuper('_render', ctx); } });
In addition, you must declare a fromObject method - this is necessary for loadFromJSON . In this case, your object is synchronous with the load.
fabric.CustomGroup.fromObject = function (object, callback) { var _enlivenedObjects; fabric.util.enlivenObjects(object.objects, function (enlivenedObjects) { delete object.objects; _enlivenedObjects = enlivenedObjects; }); return new fabric.CustomGroup(_enlivenedObjects, object); };
If your custom object is loaded async, you should do this:
fabric.CustomGroup.fromObject = function (object, callback) { fabric.util.enlivenObjects(object.objects, function (enlivenedObjects) { delete object.objects; callback && callback(new fabric.CustomGroup(enlivenedObjects, object)); }); }; fabric.CustomGroup.async = true;
I made a small jsfiddle test file: http://jsfiddle.net/Kienz/qPLY6/
Kienz source share