开发者

What is a good technique for detecting mouse movement distance in Javascript?

I'm designing user controls and I'm trying to code the controls for the mouse. This is what I came up with to get user input.

var mouseInput = new GLGE.MouseInput(window);
window.onmousemove = function(ev){
        var dx = window.mouseX - prevMousePos.x;
        var dy = window.mouseY - prevMousePos.y;
        prevMousePos ={
        x:window.mouseX,
        y:window.mouseY
     };
    // I do movement calculations with dx and dy here 
}

However what I came up with above isn't perfect because if the mouse reaches the end of the window it would not detect movement.

Is there a better way of detecting mouse movement? I'd rather not calculate 开发者_如何转开发it using its co-ordinates because using that method I'm unable to calculate distance moved when the mouse is at the edge of the screen.

PS: If anyone was wondering, what I'm designing is sorta like Google Streetview or a first person shooter. I just want the user to be able to move the mouse in one direction infinitely.


I mean, you're already using an onmuseover event handler (the most efficient way because Javascript is async). So you just compute distances from he previous, only when the user moves the mouse. If he doesn't further move the muse, the player just proceeds in the same direction prviously computed.


No, there is no method to handle mouse movements outside of the browser window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜