Khairy Abd El-Zaher
Khairy Abd El-Zaher

Reputation: 139

Adding new attribute to a konvajs group object

How can I add a new attribute to an existing Konva.Group() object?

for example:

var myRoom = {
  title: "living room",
  group: new Konva.Group({
    x: 0,
    y: 0,
    draggable: true
  })
};

myRoom.group.setAttr("obj", myRoom);
myRoom.group.on('dblclick', function(evt) {
  console.log('room obj', evt.target.getAttr("obj"));
});

The attribute "obj" is `undefined! So how can I add it to the Group obj and be able to retrieve it properly?

Upvotes: 1

Views: 1976

Answers (1)

lavrton
lavrton

Reputation: 20363

evt.target is reference to a shape that you dbclicked.

To get reference to Group instance you may use this:

myRoom.group.on('dblclick', function(evt) {
  console.log(evt.target);  // circle
  console.log(this);   // group
  console.log('room obj', this.getAttr("obj"));
});

Demo: https://jsfiddle.net/qsyw4bqm/

Note: in new version of Konva (0.11.0) you will be able to use evt.currentTarget as such reference.

Upvotes: 3

Related Questions