HTML5 Canvas – Add image on mouse click, at mouse coordinates

canvashtml

Alright guys, I have been trying to get this for hours now, I cannot find anything.

Can someone type up a quick script, or point me in the direction of a tutorial for this..

I want to have an image (imgObj) come up on the canvas when the user clicks on the canvas. I want the image to come up at the coordinates that the mouse is at when the user clicks.

Any ideas?

Thanks a lot!

Best Answer

You could start here: http://www.html5canvastutorials.com/

This is a tutorial on the HTML5 Canvas.

KineticJS is a library that makes this really easy: http://www.kineticjs.com/

essentially with KineticJS you can use the following methods to achieve your goal:

container.on("mousedown", function(){
      image.show();
 });

container.on("dragmove", function(){
     var mousePos = container.getMousePosition();
     var x = mousePos.x;
     var y = mousePos.y;
     image.move( x, y )
});

container.on("dragend"), function(){
     image.hide();
});