问题
I am trying to subclass fabric.Group and load a JSON string into it. The problem I'm having is that the color of the fabric.Rect in the subclassed group is getting changed to black. When I do it directly in fabric.Group it works great. I've been playing with variations on fromObject but I can't seem to figure out what I'm doing wrong. The change seems to happen when I "enliven" the objects in the group.
Here is a fiddle demonstrating my problem https://jsfiddle.net/adgu/49feomm7/
Click on the "Load Group" button to see the red rect (which is correct) and the "Load Custom Group" button to see the black rect.
The JSON is identical with the exception of the type being 'group' in one case and 'customGroup' in the other.
fabric.CustomGroup = fabric.util.createClass(fabric.Group, {
type: 'customGroup',
initialize: function(objects, options) {
options || (options = { });
//console.log('initialize objects', objects);
//console.log('initialize options', options);
this.callSuper('initialize', objects, options);
},
toObject: function() {
return fabric.util.object.extend(this.callSuper('toObject'), {
});
},
_render: function(ctx) {
this.callSuper('_render', ctx);
}
});
fabric.CustomGroup.fromObject = function(object, callback) {
//console.log('fromObject', object);
var _enlivenedObjects;
fabric.util.enlivenObjects(object.objects, function (enlivenedObjects) {
//console.log('enlivenObjects object', object);
//console.log('enlivenObjects object.objects', object.objects);
//console.log('enlivenObjects enlivenedObjects', enlivenedObjects);
delete object.objects;
_enlivenedObjects = enlivenedObjects;
});
return new fabric.CustomGroup(_enlivenedObjects, object);
};
回答1:
I found a closed bug report that addressed my problem: https://github.com/kangax/fabric.js/issues/3156
By adding true to the end of the initialize callSuper it started working. Like this:
this.callSuper('initialize', objects, options, true);
来源:https://stackoverflow.com/questions/40391668/fabric-js-colors-are-changing-to-black-in-subclassed-fabric-group