【问题标题】:Konva get image ID from click eventKonva 从点击事件中获取图像 ID
【发布时间】:2015-10-05 14:48:04
【问题描述】:

我正在尝试使用以下代码获取我单击的图像的 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

【问题讨论】:

  • this.id 仍然产生与 OP 中相同的结果。
  • 是的,this.id() 确实获得了图像 ID。请将其作为官方答案,以便我给予您适当的信任。
  • 欢迎您,添加答案。

标签: javascript jquery html canvas konvajs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多