-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone method does not work on subclassed objects. #1278
Comments
Do not know if this is a bug or not. http://jsfiddle.net/Tfb2d/66/ updated with fabric 1.5.0 The problem is that the function _render defined by your subclassing was not getting cloned in the object when calling .clone() @kangax, is the toObject method responsible for cloning also the functions? |
http://jsfiddle.net/asturur/Tfb2d/67/ after a similar stack overflow question i realized that the extended class was just declared wrong. var LabeledRectangle = fabric.util.createClass(fabric.Rect, {
type: 'labeledRect',
initialize: function (options) {
options || (options = {});
this.callSuper('initialize', options);
this.set('objectType', options.objectType || '');
},
toObject: function () {
return fabric.util.object.extend(this.callSuper('toObject'), {
objectType: this.get("objectType"),
});
},
_render: function (ctx) {
this.callSuper('_render', ctx);
ctx.font = '12px verdana';
ctx.fillStyle = '#333';
switch (this.objectType) {
case "text":
ctx.fillText("TEXT", -this.width / 2 + (this.width * 0.33), -this.height / 2 + (this.height * 0.53));
break;
default:
ctx.fillText("", -this.width / 2, -this.height / 2);
break;
}
}
});
LabeledRectangle.fromObject = function(options) {
return new LabeledRectangle(options);
} |
@asturur it looks like the code for declaring the extended class was modeled on the Subclassing tutorial on fabricjs.com. I ran into the same problem (render function didn't get cloned on .clone()) and finally came across this solution, adding those last three lines of your answer. I don't understand why the last 3 lines (.fromObject) are necessary (new to Fabric) but maybe this is something to try to bring into the tutorial + documentation? What's the best way to go about that? |
@asturur I extended the functionality of the fiddle you uploaded to include the copy and paste example in the fabricjs demos. I'm not able to copy the new Labeledrectangle object. I'm not able to clone this object created by subclassing. Here is the fiddle http://jsfiddle.net/7w3m4pLL/3/ I'm fabricjs version Im using is 2.0.0.beta7 as the demos are based on that version, if I'm not wrong. Can you help me with this, I want to be able to copy and paste new objects that I implement myself. |
So am I. When i try to undo the action that move more than 3 objects...
|
http://jsfiddle.net/Tfb2d/
subclassing objects are not transform to group object. (v1.4.4)
run the jsfiddle.
#1 rectangle - grouped. success.
Remove Prototype.js as a dependency. #2 subclassing object - fail.
error messages are below.
Uncaught TypeError: undefined is not a function fabric-1.4.4.js:10702
fabric.Object.fabric.util.createClass.renderfabric-1.4.4.js:10702
fabric.Group.fabric.util.createClass._renderObjectfabric-1.4.4.js:15504
fabric.Group.fabric.util.createClass.renderfabric-1.4.4.js:15477
fabric.StaticCanvas.fabric.util.createClass._drawfabric-1.4.4.js:5751
fabric.StaticCanvas.fabric.util.createClass._renderObjectsfabric-1.4.4.js:5879
fabric.StaticCanvas.fabric.util.createClass.renderAllfabric-1.4.4.js:5850
fabric.Collection.addfabric-1.4.4.js:181
(anonymous function)GroupSubclassing.html:71
x.event.dispatchjquery-2.0.3.js:4676
y.handle
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: