我正在尝试子类 Fabric.Group 并将 JSON 字符串加载到其中。
我遇到的问题是子类组中的 Fabric.Rect 的颜色正在更改为黑色。
当我直接在 Fabric.Group 中执行此操作时,效果很好。
我一直在玩 fromObject 的变体,但我似乎无法弄清楚我做错了什么。
当我“激活”组中的物体时,这种变化似乎就发生了。
这是一个小提琴,展示了我的问题https://jsfiddle.net/adgu/49feomm7/ https://jsfiddle.net/adgu/49feomm7/
单击“加载组”按钮可以看到红色矩形(这是正确的),单击“加载自定义组”按钮可以看到黑色矩形。
JSON 是相同的,只是类型在一种情况下为“group”而在另一种情况下为“customGroup”。
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);
};