我有以下 JavaScriptmousedown
函数,我用它来检测何时对我在 HTML5 画布上显示的图像之一执行了“mousedown”:
_mousedown: function(evt) {
this._setUserPosition(evt);
var obj = this.getIntersection(this.getUserPosition());
if(obj && obj.shape) {
var shape = obj.shape;
this.clickStart = true;
shape._handleEvent('mousedown', evt);
displayTip(obj.shape.index);
console.log(obj);
}
如您所见,每当在画布上绘制对象的部分执行“mousedown”时,此函数都会调用函数displayTip
。
该displayTip
函数当前如下所示:
function displayTip(index){
document.getElementById('tipsParagraph').innerHTML = tips[index];
console.log("displayTip being called");
console.log("canvasImage id = " + canvasImage.id);
}
现在,该mousedown
函数部分正常工作,因为当它调用时displayTip
,该函数会将段落的内容更新tipsParagraph
为存储在tips
数组中位置的任何内容。index
但是,如您所见,在displayTip
函数的末尾,我有线
`console.log("canvasImage id = " + canvasImage.id);
在mousedown
函数的最后,我有一行
console.log(obj);
我的理解是,在画布上单击时检测到的任何对象都将存储在变量obj
中,由行确定
var obj = this.getIntersection(this.getUserPosition());
但是在控制台中,我的displayTip()
函数末尾的日志canvasImage.id
未定义,而我的mousedown
函数末尾的日志记录了变量的值obj
是[object Object]
我不确定为什么 mycanvasImage.id
未定义...它应该显示mousedown
已检测到的图像的 ID 属性...任何想法为什么这没有发生?
据推测,控制台日志value of variable obj:[object Object]
意味着 mousedown 已经在 mousedown 的位置“获取”了图像 - 所以它应该显示该对象的 id 不是吗?
编辑 01/03/2013 @ 14:35
画布图像的代码如下:
它首先被定义为文件开头的全局变量,以及我的所有其他全局变量,使用以下行:
var canvasImage;
下一个用途是在我的“drawImage()”函数中,我使用它如下:
var canvasImage = new Kinetic.Image({
image: imageObj,
width: 50,
height: 50,
// puts the image in teh middle of the canvas
x: randomPosition(0, 950), //imageX, //stage.getWidth() / 2 - 50 / 2,
y: randomPosition(30, 350), //imageY, //stage.getHeight() / 2 - 50 / 2,
draggable: true
});
我不确定这是否是正确的做法,但我想做的是使用该mousedown
函数“获取”调用 mousedown 函数的任何图像,并将该特定图像临时存储在变量中,以及它的所有属性(id、width、height、x、y),以便我可以访问这些属性以供其他代码位使用。