I have the following JavaScript mousedown
function, which I'm using to detect when a 'mousedown' has been performed on one of the images I have displayed on an HTML5 canvas:
_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);
}
As you can see, whenever a 'mousedown' is performed on a part of the canvas which has an object drawn to it, this function calls the function displayTip
.
The displayTip
function currently looks like this:
function displayTip(index){
document.getElementById('tipsParagraph').innerHTML = tips[index];
console.log("displayTip being called");
console.log("canvasImage id = " + canvasImage.id);
}
Now, the mousedown
function is partially working correctly, in that when it calls displayTip
, this function then updates the contents of the paragraph tipsParagraph
to whatever is stored in the tips
array at the position index
.However, as you can see, at the end of the displayTip
function, I have the line
`console.log("canvasImage id = " + canvasImage.id);
and at the end of the mousedown
function, I have the line
console.log(obj);
It is my understanding, that whatever object is detected at the point of the click on the canvas, will be stored in the variable obj
, as determined by the line
var obj = this.getIntersection(this.getUserPosition());
But in the console, the log at the end of my displayTip()
function logs that canvasImage.id
is undefined, and the log at the end of my mousedown
function logs that the value of the variable obj
is [object Object]
I'm not sure why my canvasImage.id
is undefined... it should display the ID attribute of the image that the mousedown
has been detected on... any ideas why this isn't happening?
Presumably, the console log value of variable obj:[object Object]
means that the mousedown has 'got hold' of the image at the position of the mousedown- so it should display the id of that object shouldn't it?
Edit 01/03/2013 @ 14:35
The code for canvas image is as follows:
It is first defined as a global variable at the start of the file, along with all of my other global variables, using the line:
var canvasImage;
The next use comes within my 'drawImage()' function, where I have used it as follows:
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
});
I'm not sure if this is the correct way of doing it, but what I want to do, is use the mousedown
function to 'get hold' of whatever image the mousedown function is called on, and store that particular image in a variable temporarily, along with all of its attributes (id, width, height, x, y), so that I can access those attributes for use by other bits of code.