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

Add event listener to dynamically created KonvaJS image shape

我在舞台上每次双击时添加一个 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;

  }

}
});

你可以做到,就像你对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');
})