KineticJS icon indicating copy to clipboard operation
KineticJS copied to clipboard

The new extends throws RangeError

Open mreq opened this issue 14 years ago • 4 comments

Old version (working with v3.10.5):

Kinetic.scaledImage = Kinetic.Image.extend({
  init: function(config) {
    this._super(config);
    return this.setScale(scale);
  }
});

New version (trying to make it work with v4.0.1):

Kinetic.scaledImage = function(config) {
  return this._initImage(config);
};
Kinetic.scaledImage.prototype = {
  _initImage: function(config) {
    this.shapeType = 'scaledImage';
    Kinetic.Image.call(this, config);
    return this.setScale(scale);
  }
};
Kinetic.Global.extend(Kinetic.scaledImage, Kinetic.Image);

Where it just throws a RangeError

Kinetic.Image.call(this, config);
// throws Uncaught RangeError: Maximum call stack size exceeded

I have quite a hierarchy for my canvas game, say:

Coin << Kinetic.droppableImage << Kinetic.scaledImage << Kinetic.Image

it's essential for me to get this to work.

mreq avatar Sep 21 '12 08:09 mreq

I'll put together a tutorial that shows how to extend KineticJS objects and make sure that it's working as expected. stay tuned!

ericdrowell avatar Sep 22 '12 21:09 ericdrowell

The problem of the suggested code is that the _initImage must be renamed to _initScaledImage (or similar). In the example you will have a recursive loop since the image constructor will call back on this._initImage.

davonium avatar Sep 22 '12 21:09 davonium

+1 that's absolutely correct

ericdrowell avatar Sep 22 '12 23:09 ericdrowell

Oh, I see. Thanks!

mreq avatar Sep 23 '12 08:09 mreq