将事件侦听器添加到动态创建的KonvaJS图像形状

时间:2019-03-18 12:14:56

标签: javascript addeventlistener konvajs

我在每次双击舞台时都添加一个konvajs图像对象,如下所示。如何向以这种方式创建的konvajs图像对象添加事件侦听器,konvajs中是否有与标准javascript addEventListener等效的对象?

stage.on('dblclick', function(e) {


  //getString tell what shape to draw.
  if (getString == "real-input") {
    var imageObj = new Image();
    imageObj.onload = function() {

      var yoda = new Konva.Image({
        x: Number(stage.getPointerPosition().x),
        y: Number(stage.getPointerPosition().y),
        image: imageObj,
        width: this.width,
        height: this.height,
        name: "image",
        draggable: true
      });

      // add the shape to the layer
      layer.add(yoda).draw();

      // add the layer to the stage

    };
    imageObj.src = document.getElementById("customImage").src;

  }

}
});

1 个答案:

答案 0 :(得分:2)

您可以完全按照对stage节点的操作来做到这一点:

var yoda = new Konva.Image({
        x: Number(stage.getPointerPosition().x),
        y: Number(stage.getPointerPosition().y),
        image: imageObj,
        width: this.width,
        height: this.height,
        name: "image",
        draggable: true
});

yoda.on('click', () => {
   console.log('clicked');
})