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

问题描述 投票:1回答:1

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

  }

}
});
javascript addeventlistener konvajs
1个回答
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');
})

© www.soinside.com 2019 - 2024. All rights reserved.