1

How do I trigger a mouse move event with custom coordinates in jQuery?

I tried the following:

canvas1.trigger('mousemove',{pageX: window.width/2 , pageY: window.height/2});

and I also tried running this:

canvas1.trigger('mousemove',{pageX: 800 , pageY: 800});

However the pageX and pageY seems to be undefined in the event.

I've called this inside a mouse move event:

console.log("Fake mouse move event called successfully! X:", e.pageX, "& Y:", e.pageY);

And I didn't move my mouse at all so I could only see the fake event.

This is the result I am getting:

Fake mouse move event called successfully! X: undefined & Y: undefined

I also tried this with clientX and clientY, and the problem persists..

Has anyone got an idea to how to fix this problem? Have no idea what is going on here.

Thanks, help much appreciated!

EDIT:

canvas1 = $("#canvas");
David Callanan
  • 5,601
  • 7
  • 63
  • 105

2 Answers2

4

See this answer. Here is how you can create an event and customize its properties in jQuery.

// create a jQuery event
e = $.Event('mousemove');

// set coordinates
e.pageX = 100;
e.pageY = 100;

// trigger event - must trigger on document
$(document).trigger(e);
Community
  • 1
  • 1
  • This doesn't seem to work either... What do you mean by must trigger on document. Do I have to do $(document).trigger(e)? – David Callanan Aug 03 '16 at 20:52
  • you do not need to trigger on the document, unless you want to fire the handler you attached on the document. – tstoev Mar 24 '21 at 10:14
0

Okay this might help then, (editing the answer) How to simulate a click by using x,y coordinates in JavaScript?

Community
  • 1
  • 1
Siddhartha Chowdhury
  • 2,724
  • 1
  • 28
  • 46
  • I am triggering a custom mouse move event and I would like to specify custom coordinates in that event. Sorry if you misunderstood my question. – David Callanan Aug 03 '16 at 14:05
  • Not seeming to work, probably because it clicks the elements. I would also like to trigger the event onto a canvas. When the event is triggered I would like a mouse move event to be called, and e.pageX + e.pageY will be a custom coordinate. Thanks for the help. – David Callanan Aug 03 '16 at 21:01