Konva从点击事件中获取图片ID

时间:2015-10-05 14:48:04

标签: javascript jquery html5 canvas konvajs

我正在尝试使用以下代码获取我点击的图像的ID:

theImg.on('click', function() {
    alert($(this).attr('id')); //Should show 'IDofImg'
});

Konva代码是:

var theImg = new Konva.Image({
    image: imageObj,
    x: stage.getWidth() / 2 - 200 / 2,
    y: stage.getHeight() / 2 - 137 / 2,
    opacity: 0.8,
    shadowColor: 'black',
    shadowBlur: 5,
    id: 'IDofImg',
    shadowOffset: {
        x: 0,
        y: 0
    },
    startScale: 1,
    shadowOpacity: 0.6,
    draggable: true
});

如您所见,我在图片制作过程中有 id:'IDofImg',,但似乎没有输出所需的ID。

点击时目前输出:

function() {
   // setting
   if (arguments.length) {
       this[setter](arguments[0]);
       return this;
   }
   // getting
   else {
       return this[getter]();
   }
}

我错过了什么?

Fiddle here

1 个答案:

答案 0 :(得分:2)

您应该使用this.id(),因为它是Konva Image对象,而不是html/javascript对象。

另请参阅文档:http://konvajs.github.io/api/Konva.Node.html#id

相关问题