CreateJS / EaselJS

The Easel Javascript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier.
http://createjs.com/
MIT License
8.11k stars 1.97k forks source link

How to trigger mouse event without Mouse? #1005

Open bailiqun opened 5 years ago

bailiqun commented 5 years ago

Hi, I'd like to use createjs for making games, I want to use my data[like (x, y) ] to fire mouse event to trigger 'click' event in animate cc. I don't want use mouse to trigger click event, I need to use my data to fire animate. wait for your help.

lannymcnie commented 5 years ago

This is more of a StackOverflow question vs a bug, but since there is no official way to do this, it might be valuable to add this behaviour to EaselJS to simulate mouse events.

Here is a workaround:

Sample code:

var e = {preventDefault:function(){}} // Fake "mouse event" to prevent errors
stage._handlePointerDown(-1, e, xCoordinate, yCoordinate); 
stage._handlePointerUp(-1);

I made a quick sample that moves a shape every time it is clicked. The demo tests the current stage mouseX and mouseY every 100 milliseconds using the code above, so if you roll your mouse over the square it will trigger click events constantly. https://jsfiddle.net/lannymcnie/uwb9c3L7/

If you just want to force an object to dispatch a mouse event, you can use:

obj.dispatchEvent("click");

I hope that helps. I will leave this issue open to track the feature request to do this officially. Cheers,

DavidHGillen commented 5 years ago

Lanny's information is a great way to get started but here's a heads up though. Games often require extreme speed and the built in mouse interaction code is built for accuracy not speed. Given that we don't know which shortcuts you want to take "axis aligned bounding boxes?" "pure circle collision?" "physics engine?" we don't have a built in fast hit detection (yet?). So you may want to implement something faster yourself using our bounds system if you need extra performance.

bailiqun commented 5 years ago

@lannymcnie really thanks for your help~ it works~ Alt text