Is there a way of clicking an specific point/element in a canvas (with javascript, jquery or selenium) Is there a way of clicking an specific point/element in a canvas (with javascript, jquery or selenium) selenium selenium

Is there a way of clicking an specific point/element in a canvas (with javascript, jquery or selenium)


How about trying

canvas.on('click', function(e) {    // calculate x y coordinates on canvas    var x = e.pageX - $(this).offset().left,    y = e.pageY - $(this).offset().top;    // implement collision detection via the coordinates the element you want to click (assuming you know where it is)    if (y > <elementY> && y < <elementY> + <elementHeight>        && x > <elementX> && x < <elementX> + <elementWidth>) {        alert('found it');    }});

I have no time testing that snippet. If you know the x and y coordinates of your elements within your canvas, that code might just do the trick.

What I tried to do with that snippet is to simulate a bounding box via elmentY and elementY + elementHeight as well as elementX and elementX + elementWidth. If you know all those values, you can simply check whether the x and y values of your click event fall in that said box.

Edit

If you want to trigger a click event at a certain position, define the clickEvent beforehand and set the pageX and pageY attributes.

var e = new jQuery.Event("click");e.pageX = 10;e.pageY = 10;$(<canvas>).trigger(e);

You might have to include the offset of your canvas in the calculations as well.