clone bug
Hi,
I've just updated KineticJS from 4.0.1 to 4.0.4. In this version i've been trying to use clone() function to duplicate a shape but somehow it returns an error.
Cannot read property offset of undefined.
Tried downgrading to KineticJS 4.0.2 but the same issue occurs.
Here's how i use clone:
var highlight = new Kinetic.Rect( {x:0,y:0, width:100, height:100, stroke: 'red']);
var newHighlight = highlight.clone({stroke: 'green'});
I'm not sure whether i'm using it the wrong way or it's a bug in the framework?
interesting, you're right this looks like a bug. After looking through the clone unit tests, I noticed that the tests are instantiating objects that use a shadow object, which has an offset property, so my guess is that if you include an empty shadow object like this:
var rect = new Rect({shadow:{}});
that this may provide a work around for now. I'll have this fixed in the next release, v4.0.5
Thanks!!!
I've tried including shadow to the rect object and it didn't solve the problem.
Here's what i did with the rect with more attributes:
BLOCK_WIDTH = 50; BLOCK_HEIGHT = 50
highlight = new Kinetic.Rect( { x:0, y:0, width: BLOCK_WIDTH -2, height: BLOCK_HEIGHT -2, stroke: 'red', strokeWidth: 2, opacity:0, cornerRadius: 15, offset: [(BLOCK_WIDTH -2)/2, (BLOCK_HEIGHT -2)/2], shadow:{} });
then this is what i do with my clone:
var newHighlight = highlight.clone({stroke: 'green', shadow:{}});
I was thinking to force the clone to not have a shadow but it's not working as i intended. Hope it can be fixed soon
Thanks
Hi again.. Not sure if this is a bug or not. Since I stick with kinetic 4.0.1, I've been trying to clone a sprite object as well. But the result: cloned object seems to be a shape rather than a sprite. Is it suppose to be like that?
Thanks
I don't think I have unit tests that clone sprites - ill look into that. Thanks!!
Sent from my iPhone
On Oct 30, 2012, at 9:50 AM, Wazabii [email protected] wrote:
Hi again.. Not sure if this is a bug or not. Since I stick with kinetic 4.0.1, I've been trying to clone a sprite object as well. But the result: cloned object seems to be a shape rather than a sprite. Is it suppose to be like that?
Thanks
— Reply to this email directly or view it on GitHub.
I've noticed this issue as well. The easiest fix is simply to modify your existing KineticJS file to include the following:
Line 4144 -
change: if(config.offset !== undefined) { to: if(config && config.offset !== undefined) {
and line 4164 - change: if(fill.offset !== undefined) { to: if(fill && fill.offset !== undefined) {
These changes should be included in the next release as a fix for this (please, Eric).
PS: Line numbers correspond to version 4.0.3. Do a search to locate them in 4.0.4 if you've updated.
sorry for the pain guys! Yes, it will be fixed by v4.0.5
This issue was causing a bit of trouble for me, lazerblade's fix seemed to fix it though.
@ericdrowell, is there a better way to store states than cloning the entire object? It seems like this ends up being a bit heavy what with dom injection and subsequent dom removal...
Check out my stackoverflow question, the clone issue was resolved after finding this issue, but with it working, it seems quite heavy. any ideas?
@lazerblade01, interestingly, I'm getting shadows applied to every child of the layers I'm cloning.
Same problem here. Cloning really doesn't work the way one might think. It almost seems to work better to get the object you wish to clone and simply use its more common properties, or the ones you really want, to sort of "duplicate" it. Transparent .png's are horrible cloned - they turn all black! I think at this point perhaps clone needs to be revisited and there needs to be a separation of user-defined values and default / auto values, so when you DO clone, you're basically just copying previously-set values.
I created a jsfiddle that creates a rectangle and clones it without any issues (not using shadows or offsets). Check it out:
http://jsfiddle.net/hz77b/
Am I missing something? If so, could you modify the jsfiddle to break the clone method?
P.S. you're right about Sprite shapes (unrelated to the first bug you mentioned), the Sprites class wasn't defining the shapeType which meant that the clone method couldn't figure out that it was a Sprite. this issue will be fixed and added to the upcoming release - thanks!!
@ericdrowell I think the bigger issue with cloning is the way it clones png images with transparency. I have a glass image with highlights around the edges but most of the image is almost transparent (approx. 10% transparency with a white base). The clone looks like it's basically black, though the pure white highlights where there's no transparency do show up.
I've found a different, but maybe related, bug in the clone() method. I don't know if I should write this here or file a new issue. If this is not the place, please excuse me. Just tell me and I'll create a new issue for it.
I tried to clone a group where one of the Rects had a custom drawFunc declared. When I cloned the group, the custom drawFunc was lost even from the source object!
After pulling my hair for some time with different tests, by tracing the clone process with Firebug, I think I've found the problem to rely on line 4481 of the 4.0.5 source (in the case of Rects):
config.drawFunc = this.drawFunc;
The _initRect function receives the config data from the source, and then overwrites the original config.drawFunc with the default prototype drawFunc, which overwrites the reference to the custom function. My solution has been quite simple, sure there's a better or more elegant way:
if (!config.drawFunc) {
config.drawFunc = this.drawFunc;
}
The same should be done for every Shape derivatives prototype, so they can be cloned along with their custom function.
Being quite a newbie in Javascript, I maybe totally wrong. But I did not find another way to use clone() for my purposes. Hope it helps.
@macl this sounds like the expected behavior. The whole point of Shape classes was to provide draw functions for you. Doesn't seem to make sense to overwrite them. If there are particular methods in a Shape class that you'd like to reuse, you could create a new class and reference those methods. A good example is the TextPath shape which references methods from Text and Path.
follow up with this thread - Sprite cloning has been fixed as of v4.0.5
okay guys, there's a lot going on with this ticket, and since a lot of these issues have been resolved, I'm closing this one for now. If you're still having problems with clone(), please open up a new ticket so it that can be tracked, and also provide a jsfiddle demonstrating the problem. I've been unable to reproduce the error mentioned in the first comment, or a black transparent image. I provided a jsfiddle in one of my earlier comments that demonstrates cloning a node without a shadow or offset.
Thanks!
@ericdrowell thanks for your answer. In my case, I am creating somewhat complex objects based on numeric parameters, one of which should have a hole in the middle that should allow to see the background through it. It's quite useful for me to use the default rectangular method, but I need to be change the context composition mode to xor beforehand. That's what my custom draw function does. As you can see, there are several scenarios where cloning this function would be useful for the developer.
I can understand this to be a design criteria. Anyway, I don't get to see how cloning a shape should overwrite the source function. I suppose you agree with me on that one.
@macl sure, but the drawFunc attr for Shapes was never intended to be editable (if this were Java, it would have been a private attribute)
Funny thing is, the Rect shape actually doesn't even have any methods that you could reuse:
https://github.com/ericdrowell/KineticJS/blob/master/src/shapes/Rect.js
It really would make more sense to create a custom shape for what you're doing. Also, if you want to make a hole inside of something, you should look into leveraging the winding rule, rather than xor.
I'll probably go ahead and add logic to each Shape class to make sure it doesn't overwrite a user defined drawFunc though, because several other people have had similar problems.
Cheers!