user992364
user992364

Reputation: 452

JS - Need to get raw mouse input (or close to it)

I am writing an HTML5 game, and I want users to be able to control their character's direction by moving the mouse left/right.

I can get mouse movement with onmousemove and pageX and pageY, but once the mouse hits the edge of the screen... too bad for you. Is there any cross-browser way around this (perhaps getting actual mouse movement instead of cursor position)?

If not, can I set the mouse cursor to be in the middle of an element if it reaches the edge, and just allow them to move their mouse out of the element vertically?

Upvotes: 1

Views: 1583

Answers (3)

Alex Mund
Alex Mund

Reputation: 431

Use the Pointer Lock API.

http://www.html5rocks.com/en/tutorials/pointerlock/intro/

https://developer.mozilla.org/en-US/docs/WebAPI/Pointer_Lock

It's "experimental".

The browser will ask the user for permission to lock the cursor.

Upvotes: 5

Shawn Khameneh
Shawn Khameneh

Reputation: 472

This is not possible unless you contain the page within a native app for your target platform(s) or you request the page fullscreen for the game, which modern browsers support.

https://developer.mozilla.org/en/DOM/Using_full-screen_mode

http://hacks.mozilla.org/2012/01/using-the-fullscreen-api-in-web-browsers/

Here's things to keep in mind:

  • For security a web page cannot detect the cursor outside of the browser viewport.
  • System level devices cannot be controlled via a web page (eg: Can't control mouse)

*This may be possible with flash though, not sure.

Upvotes: 0

Felix Glas
Felix Glas

Reputation: 15534

No there isn't any way to do this that i know of, but there might be a way to get the same effect.

When the cursor starts moving left (or right) you record it. As the cursor hits the edge of the screen it will likely start to move up or down (because nobody moves their hand steady enough to follow a 1 pixel straight line). You interpret this up and down movement as movement in the same direction (left in this case) until movement in the right direction is detected.

If the cursor is moved right at the beginning you record up and down as right movement until left direction is detected.

Upvotes: 1

Related Questions