As an object that moves, it is the initiator of possible collision events, as well as the movement of the mouse. In each frame, using requestAnimationFrame() , the object must check to see if it is facing the current mouse position.
There are no ready-made jQuery solutions that I can recall, but implementation will require only a few lines of code, and trigger a custom jQuery event.
Update. I sketched a simple demo below. Div bounces from left to right inside the box, which tracks the position of the mouse. In the example, only the horizontal position for the collision is checked. Move the mouse pointer somewhere in the path of the jumping square and leave it motionless. The square turns red when the collision is active, and back to gray when it exits.
warnings. In JS, there is no way to get the mouse position without moving the mouse - then it emits an event with coordinates. Therefore, detection will not occur until the mouse is moved at least once after loading the page.
var $cont = $('#container') ,$out = $('#out') ,$el = $('#bouncer') ,mouse = { x: -1, y: -1} ,bouncer = {x: -1, y: -1} ; function updateMousePos(e) { mouse.x = e.offsetX; mouse.y = e.offsetY; collisionTest(); } $cont.on('mousemove', updateMousePos);
#container {position:relative; width:200px; height:50px; border:1px solid #333} #bouncer {position:absolute;top:15px;width:20px;height:20px;background-color:#CCC;} #out { margin-top: 60px; } .collision {background-color:red !important}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"><div id="bouncer"></div></div>
Serge
source share