views:

642

answers:

3

I am experimenting with html5 and I have a little image dropdown, the user selects and image and it draws it to the canvas using drawImage();

I can't seem to figure out how to add an event listener to the newly drawn image on the canvas.

I have tried putting it in a variable like so:

var newImg = ctx.drawImage(myImage, 200, 200);

and then adding an eventlistener to that, but it doesn't seem to work.

newImg.addEventListener('mousedown', onImgClick, false);

What is the correct way to do this.

+1  A: 

Canvas is not a retained-mode drawing surface. It's just a flat image; there is no object inside it to bind events to, and drawImage returns nothing.

You will have to detect clicks on the <canvas> and check if they are inside the [200, 200, 200+w, 200+h] rectangle manually. Or, if you want retained-mode drawing, consider using SVG instead of a canvas.

bobince
I am confused by this, because it seems like there are many tutorials on how to manipulate objects drawn on the canvas. There is even a javascript game library that uses canvas. http://www.kesiev.com/akihabara/So it does seem like you can manipulate those images you put on canvas.
pfunc
You can paint a circle on a real canvas, but afterwards there is only paint. You couldn't pick up that circle and take it back off the canvas; you couldn't move it on top of another part of the painting; *it's just paint*. You can apply objects like images, paths and texts to a `<canvas>`. But once you have, those objects don't have any identity or existance; *it's just pixels*. Game libraries typically keep their own list of higher-level objects to draw, and draw them back onto the canvas anew on every new frame.
bobince
+2  A: 

If you're looking for this sort of interactivity, <canvas> is probably not what you want. You're looking for SVG. There is a fantastic library called Raphaël that makes working with SVG a piece of cake on all browsers, even on IE6. It's also completely compatible with jQuery, so you can do:

var paper = Raphael(10, 50, 320, 200);
var img = paper.image("/path/to/img.png", 10, 10, 80, 80);
$(img).click(onImgClick);

I'm pretty certain this will treat you better and be easier to use than <canvas>.

Note: Raphaël does come with some helpers for basic events like "click" and "mousedown", just do img.click(onImgClick), but if you're already using a library like jQuery, which you probably are, I'd recommend being consistent and using the library's event handling.

bcherry
ahh, good to know. I thought that might be the case, but I also thought since I could see the img added in the source I'd be able to manipulate it.
pfunc
the difference is that `<canvas>` just adds pixels when you draw an image. SVG is markup-based, like HTML, so there is an actual chunk of markup added that is essentially like any HTML DOM Element, so you can interact with it, clone it, move it, transform it, and listen to events on it. There is sometimes a performance overhead from this, as compared to `<canvas>`, though, but it should work great for the situation you described.
bcherry
cool. Already started working with it and it's pretty simple. And I take that back, I can't see the source img in the canvas element, I had some leftover code in there from a previous test. THank you.
pfunc
A: 

even i wanted to do this using javascript is it possible? i am trying but unsuccessful

Nitesh