Mario
Mario

Reputation: 1253

Get the width of a rect shape

I'm trying to get the width of a node shape in kineticjs. I'm doing this:

      var node = left.find('Rect');
      var width = node.width()
      console.log(width);

and left is a group that has a Rect.

  var shape = new Kinetic.Rect({
    x: 0,
    y: 0,
    width: C_EC_WIDTH,
    height: C_EC_WIDTH,
    fill: viewcolor,
    stroke: "black",
    strokeWidth: 1
  });

  left.add(shape);

But I always get this result:

enter image description here

I have tried getWidth(), setWidth(100), width() and width(100). The width(100) and setWidth(100) worked properly. But the others not! I can not have access to the width of the shape, I always get the object!

What am I doing wrong?

Edit:

Tried this:

var width = node.width().width().width();

and have the same result. It returns the kinetic object! I have no idea why! It is not making any sense for me.

Upvotes: 0

Views: 279

Answers (1)

lavrton
lavrton

Reputation: 20308

find() function returns collection of objects. You may have many rectangles in container. So should do this:

  var node = left.find('Rect')[0];
  var width = node.width()
  console.log(width);

Upvotes: 1

Related Questions