1

I cannot make this ray to follow my mouse cursor.

It's always inverse on X axis.

I think that the problem is with my atan function, but since my trigonometry is fairly low, I have no idea how to fix this one.

This is how I calculate radians:

 var dx = target.x - center.x;
 var dy = target.y - center.y;

 var rad = Math.atan2(dx, dy);
4

1 回答 1

1
var dx = center.x - target.x;

Fiddle


The fiddle above doesn't work in Firefox as FF doesn't natively support event.offsetX/Y for mouse events, I assume your normalization code was cut off when making the minimalistic example but I'll leave my normalization code here for future reference:

function normOffset(event) {
    if (typeof event.offsetX === 'undefined' || typeof event.offsetY === 'undefined') {
        var targetOffset = $(event.target).offset();
        event.offsetX = event.pageX - targetOffset.left;
        event.offsetY = event.pageY - targetOffset.top;
     }
}

Then just pass the jQuery event object to it.

Fiddle

于 2012-12-18T23:17:26.633 回答