Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simulate a mouse down and up on a canvas html5 page

How can I simulate a mouse down and then followed by a mouse up event on a canvas hmtl5 section of an aspx page?

I searched on the web and found this... but i cannot correlate this to my canvas html5 element, Can anyone help ?

dispatchMouseEvent(element, 'mouseover', true, true);
dispatchMouseEvent(element, 'mousedown', true, true);
dispatchMouseEvent(element, 'click', true, true);
dispatchMouseEvent(element, 'mouseup', true, true);
like image 884
Philo Avatar asked Aug 21 '13 18:08

Philo


People also ask

How to simulate good animation over a HTML5 canvas using JavaScript?

We can take Javascript help to simulate good animation over a HTML5 canvas. Following are the two important Javascript methods which would be used to animate an image on a canvas − This method repeatedly executes the supplied code after a given time milliseconds. This method executes the supplied code only once after a given time milliseconds.

How do I detect mouse events in HTML5?

Mouse Events and the HTML5 Canvas You can also detect a mouse click on your canvas. Again, this is done with addEventListener. There are quite a few mouse events you can detect: mousedown, mouseup, mousemove, mouseout and mouseover.

How do I detect a mouse click on my Canvas?

You can also detect a mouse click on your canvas. Again, this is done with addEventListener. There are quite a few mouse events you can detect: mousedown, mouseup, mousemove, mouseout and mouseover. As an example, here's some code that detects the mousedown event: The BODY tag calls an onLoad function. Inside of this function we have this:

What is HTML5 canvas?

HTML5 Canvas - Animations. HTML5 canvas provides necessary methods to draw an image and erase it completely. We can take Javascript help to simulate good animation over a HTML5 canvas.


1 Answers

"Simulating" events is very easy, in fact, you can simply trigger them. Using jQuery, this becomes child's play (see this jsfiddle for working example):

$('#canvas_element').on("mousedown mouseup", function(e) {
console.log(e.type + " event fired at coords: " + e.pageX + ", " + e.pageY);
});

x_coord = 1;
y_coord = 1;

var e = jQuery.Event( "mousedown", { pageX: x_coord, pageY: y_coord } );
$('#canvas_element').trigger(e);

// execute more code
x_coord = 255;
y_coord = 255;

var e = jQuery.Event( "mouseup", { pageX: x_coord, pageY: y_coord } );
$('#canvas_element').trigger(e);

See this old SO question for a pure javascript solution.

like image 188
user2701675 Avatar answered Oct 12 '22 04:10

user2701675