Mouse Capture for Canvas

The main idea is here in bugzilla:
http://www.w3.org/Bugs/Public/show_bug.cgi?id=9557#c4

I'll post it here though since Doug said this might get more people to see it.

The idea would be to allow the browser to capture, as in lock the mouse to a 
certain DOM region such as a canvas tag. Thinking back on this I realized a few 
things were missing from the initial idea so I'll explain the whole concept in 
more detail.

The MouseEvent would need two new members:
deltaX
deltaY
Which in the mousemove event would be set to the change in the mouse position.
So if you move your mouse left 5 pixels deltaX would be set to -5 and if you 
moved the mouse up 5 pixels deltaY would be set to -5.

To keep this as simple as possible a developer would create a <canvas> tag on 
their web page then add a mousecapture, mousemove, mouseup, and mousedown event 
to it. The new mousecapture event would be fired when a user clicked on the 
element. Unless anyone can think of a reason I don't see why any other tag 
should be able to use that event. mousecapture would run before click:
http://www.w3.org/TR/2010/WD-DOM-Level-3-Events-20100907/#events-mouseevent-event-order

In the mousecapture event a user-agent can do whatever it wants to help the 
user. It could make a small yellow bar like in chrome or firefox and say "This 
website is requesting to capture your mouse. [Allow] [Deny]". If denied the 
event would never be called. If allowed the event wouldn't be called until the 
user clicked a second time. The user-agent could handle this how they wanted.

A user-agent would also define a key to release mouse capture. (Probably Esc, 
but there's no need to define it in the standard except as a suggestion). 
Implementers would be encouraged to display something over the page that says 
"Press <key> to release mouse capture".

With the mousecapture is a mouserelease event which when registered on the same 
canvas tag responds when the user presses the release key. The event actually 
does the unregistering. The mouserelease would need to be callable to allow a 
web page to release control when it wanted, but the mousecapture can't be 
callable since that would allow someone to capture a mouse without a click.

A situation that needs to be handled is alt-tabbing or general loss of focus 
from a tab or the browser. During these situations the simplest thing to do is 
to call the mouserelease event. The user would need to click to regain mouse 
capture.

That sums up everything. Obviously it could be implemented differently, but I 
think I outlined what's needed.

Received on Tuesday, 8 February 2011 19:01:12 UTC