forked from w3c/pointerlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1014 lines (1004 loc) · 41.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Pointer Lock 2.0
</title>
<meta charset="utf-8"><!--
=== NOTA BENE ===
For the three scripts below, if your spec resides on dev.w3 you can check them
out in the same tree and use relative links so that they'll work offline,
-->
<script src='https://www.w3.org/Tools/respec/respec-w3c' class=
'remove'></script>
<script class='remove'>
var respecConfig = {
// specification status (e.g. WD, LCWD, NOTE, etc.). If in doubt use ED.
specStatus: "ED",
// the specification's short name, as in http://www.w3.org/TR/short-name/
caniuse: "pointerlock",
// if your specification has a subtitle that goes below the main
// formal title, define it here
// subtitle : "an excellent document",
// if you wish the publication date to be other than today, set this
//publishDate: "2011-01-01",
// if the specification's copyright date is a range of years, specify
// the start date here:
// copyrightStart: "2005"
// if there is a previously published draft, uncomment this and set its YYYY-MM-DD date
// and its maturity status
previousPublishDate: "2019-05-30",
previousMaturity: "FPWD",
testSuiteURI:
"https://github.com/web-platform-tests/wpt/tree/master/pointerlock",
// if this is a LCWD, uncomment and set the end of its review period
// lcEnd: "2009-08-05",
// editors, add as many as you like
// only "name" is required
editors: [
{
name: "Navid Zolghadr",
company: "Google",
companyURL: "http://www.google.com/",
w3cid: "92361",
},
{
name: "Mustaq Ahmed",
company: "Google",
companyURL: "http://www.google.com/",
w3cid: "75206",
},
],
formerEditor: [
{
name: "Vincent Scheib",
url: "http://www.scheib.net",
company: "Google",
companyURL: "http://www.google.com/",
},
],
github: "w3c/pointerlock",
group: "webapps",
xref: "web-platform"
};
</script>
</head>
<body>
<section id='abstract'>
This specification defines an API that provides scripted access to raw
mouse movement data while locking the target of mouse events to a single
element and removing the cursor from view. This is an essential input
mode for certain classes of applications, especially first person
perspective 3D applications and 3D modeling software.
</section>
<section id="sotd">
<!-- Status of This Document -->
<p>
A history of changes to this document can be found at <a href=
"https://github.com/w3c/pointerlock/commits/gh-pages/index.html">http://dvcs.w3.org/hg/pointerlock/log/default/index.html</a>
</p>
<p>
Summary of changes since <a href=
"https://www.w3.org/TR/2016/WD-pointerlock-2-20161122/">W3C
Recommendation 27 October 2016</a>:
</p>
<ul>
<li>
<a href=
"https://github.com/w3c/pointerlock/commit/027f57ae80cf5bdb03379203828aa234f3e7e433">
IDL blocks updated</a> to be machine <a href=
"https://github.com/w3c/pointerlock/issues/13">parsable</a>, no
substantive change.
</li>
</ul>
</section>
<section id='introduction' class='informative'>
<h2>
Introduction
</h2>
<p>
The Pointer Lock API provides applications the ability to directly
interpret mouse movements as an input method, rather than being limited
to only read the position of the mouse cursor. A popular example is
that of first person movement controls in three dimensional graphics
applications such as games: movement of the mouse is interpreted to
control the rotation/direction of the player's camera; no mouse cursor
is displayed, and the movement is not limited to the traditional
boundaries (such as the user agent's window, or the overall screen)
that the mouse cursor is usually subject to, meaning that any mouse
movements can be tracked indefinitely in any direction.
</p>
<p>
Pointer Lock is related to Mouse Capture [[MSDN-SETCAPTURE]]
[[MDN-SETCAPTURE]] (Mouse Capture is unspecified: bug <a href=
"https://www.w3.org/Bugs/Public/show_bug.cgi?id=14600">14600</a>).
Capture provides continued event delivery to a target element while a
mouse is being dragged, but ceases when the mouse button is released.
Pointer Lock differs by being persistent, not limited by screen
boundaries, sending events regardless of mouse button state, hiding the
cursor, and not releasing until an API call or specific <a>default
unlock gesture</a> by the user.
</p>
<p>
Pointer Lock deals with capturing a single resource and relating it to
a single element. This is similar to the Fullscreen API [[FULLSCREEN]],
which promotes a single element to be full screen. The Pointer Lock API
chooses to pattern the resource capture, state change, and release API
as closely as possible after the Fullscreen API.
</p>
<p>
The Pointer Lock interaction mode was previously referred to as mouse
lock. The name was changed as many different controller types besides
mice can manipulate the on screen pointing cursor, and they are all
impacted.
</p>
</section>
<section>
<h2>
Glossary
</h2>
<dl>
<dt>
<dfn>Engagement gesture</dfn>
</dt>
<dd>
An event generated by the user agent as a result of user interaction
processed by the user agent to interact with the page. e.g.
<code>click</code>, but not <code>mousemove</code>. Engagement
gestures are defined by the events in the list of <a>activation
triggering input event</a> with the addition of
<code>keypress</code> and <code>keyup</code> events. Note that
operating system level accessibility and internationalization features
may intercept gestures before the user agent processes them for
interaction with a page. E.g. multiple key press codes used to enter
an extended international character.
</dd>
</dl>
</section>
<section>
<h2>
<code>pointerlockchange</code> and <code>pointerlockerror</code> Events
</h2>
<p>
Two events are used to communicate pointer lock state change or an
error in changing state. They are named <dfn>pointerlockchange</dfn>
and <dfn>pointerlockerror</dfn>. If pointer lock is entered or exited
for any reason a <a>pointerlockchange</a> event must be sent.
</p>
<p>
<a>User agents</a> must deliver these events by <a data-lt=
"queue a task">queuing a task</a> to <a>fire an event</a> of the
appropriate name with its <code>bubbles</code> attribute [[DOM]] set to
false to the pointer lock <a>target</a> element's [=Node/node
document=].
</p>
<p class="note">
Magnification software increases the size of content on the screen. It
uses the mouse to move the magnified point of focus around. When a
pointer lock is initiated, the magnification software needs to switch
to using the keyboard to move the magnified point of focus around
instead. When a pointerlockchange event is fired, web browsers
therefore need to make sure the event is communicated to assistive
technologies like screen magnifiers.
</p>
</section>
<section data-dfn-for="Element" data-link-for="Element">
<h2>
Extensions to the `Element` Interface
</h2>
<p>
The <a>Element</a> interface is extended to provide the ability to
request the the pointer be locked.
</p>
<pre class="idl">
partial interface Element {
undefined requestPointerLock();
};
</pre>
<dl>
<dt>
<dfn>requestPointerLock()</dfn> method
</dt>
<dd>
<p>
Requests that the pointer be locked to a DOM element
<dfn>target</dfn>. The <a>user agent</a> determines if pointer lock
state will be entered and upon lock state change or error must send
either a <a>pointerlockchange</a> or <a>pointerlockerror</a> event
respectively.
</p>
<p>
Pointer lock must not succeed if the document object's
[=Document/active sandboxing flag set=] has the <a>sandboxed
pointer lock browsing context flag</a> set.
</p>
<p>
Pointer lock must not succeed unless the <a>target</a>'s
<a>shadow-including root</a> is the <a>active document</a> of a
[=Document/browsing context=] which is (or has an <a>ancestor
browsing context</a> which is) in focus by a window which is in
focus by the operating system's window manager. The <a>target</a>
element and its [=Document/browsing context=] need not be in focus.
</p>
<p>
If a user has exited pointer lock via the <a>default unlock
gesture</a>, or pointer lock has not previously been entered for
this document, an event generated as a result of an <a>engagement
gesture</a> must be received by the document before
{{requestPointerLock()}} will succeed.
</p>
<aside class="note" data-link-for="Document">
<p>
This ensures a user can leave a document that constantly attempts
to lock the pointer. The document will be blocked from locking
upon initial navigation or re-acquiring lock unless the user
re-engages directly with the document.
</p>
<p>
Conversely, if pointer lock is exited via {{exitPointerLock()}}
no <a>engagement gesture</a> is required to reenter pointer lock.
This enables applications that frequently move between
interaction modes, and ones that may do so based on a timer or
remote network activity.
</p>
<p data-link-for="Element">
Pointer lock is commonly combined with fullscreen [[FULLSCREEN]],
and if an <a>engagement gesture</a> is used to enter fullscreen
it is sufficient for a subsequent {{requestPointerLock()}}.
</p>
</aside>
<p>
If any element (including this one), whose <a>shadow-including
root</a> is same as this element's <a>shadow-including root</a>, is
already locked (or pending lock) the pointer lock <a>target</a>
must be updated to this element and a <a>pointerlockchange</a>
event sent.
</p>
<p>
If any element, whose <a>shadow-including root</a> is a different
document, is already locked the request must fail and a
<a>pointerlockerror</a> event be sent.
</p>
<p>
Once in the locked state the <a>user agent</a> must fire all
relevant user generated {{MouseEvent}} events (for example:
`mousemove`, `mousedown`, `mouseup`, `click`, and `wheel`)
[[ui-events]] to the <a>target</a> of pointer lock, and not fire
mouse events to other elements. Events that require the concept of
a mouse cursor must not be dispatched (for example: `mouseover`,
`mouseout`, `drag`, and `drop`).
</p>
<p>
In the locked state the system mouse cursor must be hidden.
Movement and button presses of the mouse must not cause the window
to lose focus.
</p>
<p>
Synthetic mouse events created by application script act the same
regardless of lock state.
</p>
</dd>
</dl>
</section>
<section data-dfn-for="Document" data-link-for="Document">
<h2>
Extensions to the `Document` Interface
</h2>
<pre class="idl">
partial interface Document {
attribute EventHandler onpointerlockchange;
attribute EventHandler onpointerlockerror;
undefined exitPointerLock();
};
</pre>
<dl>
<dt>
<dfn>onpointerlockchange</dfn> attribute
</dt>
<dd>
<p>
An <a>event handler idl attribute</a> for <a>pointerlockchange</a>
events.
</p>
</dd>
<dt>
<dfn>onpointerlockerror</dfn> attribute
</dt>
<dd>
<p>
An <a>event handler idl attribute</a> for <a>pointerlockerror</a>
events.
</p>
</dd>
<dt>
<dfn>exitPointerLock()</dfn> method
</dt>
<dd>
<p>
Initiates an exit from pointer lock state if currently locked to a
target whose <a>shadow-including root</a> is this document, and
sends a <a>pointerlockchange</a> event when the lock state has been
exited.
</p>
<p>
The system mouse cursor must be displayed again and positioned at
the same location that it was when pointer lock was entered (the
same location that is reported in <code>screenX</code>,
<code>screenY</code>, when the pointer is locked).
</p>
</dd>
</dl>
</section>
<section data-dfn-for="DocumentOrShadowRoot">
<h2>
Extensions to the `DocumentOrShadowRoot` Mixin
</h2>
<pre class="idl">
partial interface mixin DocumentOrShadowRoot {
readonly attribute Element ? pointerLockElement;
};
</pre>
<dl>
<dt>
<dfn>pointerLockElement</dfn>
</dt>
<dd>
<p>
While the pointer is locked, returns the result of
<a>retargeting</a> the element, which is the target for mouse
events, against the <a>context object</a> if the result and the
<a>context object</a> are in the same tree, otherwise returns null.
</p>
<p>
Returns null if lock is pending or if pointer is unlocked.
</p>
<pre class='example' id="example-retarget-pointerlock">
<body>
<div id="host1">
<shadow-root id="root1">
<canvas id="canvas1"></canvas>
</shadow-root>
</div>
<div id="host2">
<shadow-root id="root2">
<canvas id="canvas2"></canvas>
</shadow-root>
</div>
</body>
</pre>
<p>
Note: the example uses fictional <code>shadow-root</code> element
to denote a [=Element/shadow root=] instance.
</p>
<p>
If <code>#canvas1</code> is the target,
<code>document.pointerLockElement</code> returns
<code>#host1</code>, and <code>root1.pointerLockElement</code>
returns <code>#canvas1</code>. The result of <a>retargeting</a>
<code>#canvas1</code> against <code>#root2</code> is
<code>#host1</code>, but as <code>#host1</code> is not in the same
tree as <code>#root2</code>, null will be returned for
<code>root2.pointerLockElement</code>.
</p>
</dd>
</dl>
</section>
<section data-dfn-for="MouseEvent" data-link-for="MouseEvent" data-cite=
"ui-events">
<h2>
Extensions to the `MouseEvent` Interface
</h2>
<pre class="idl">
partial interface MouseEvent {
readonly attribute double movementX;
readonly attribute double movementY;
};
</pre>
<dl>
<dt>
<dfn>movementX</dfn> attribute
</dt>
<dt>
<dfn>movementY</dfn> attribute
</dt>
<dd>
<p>
The attributes {{movementX}} {{movementY}} must provide the change
in position of the pointer, as if the values of
<code>screenX</code>, <code>screenY</code>, were stored between two
subsequent <code>mousemove</code> events <code>eNow</code> and
<code>ePrevious</code> and the difference taken <code>movementX =
eNow.screenX-ePrevious.screenX</code>.
</p>
<p>
{{movementX}}/{{movementY}} must be zero for all mouse events
except <code>mousemove</code>. All motion data must be delivered
via <code>mousemove</code> events such that between any two mouse
events <code>earlierEvent</code> and <code>currentEvent</code> the
value of <code>currentEvent.screenX-earlierEvent.screenX</code> is
equivalent to the sum of all {{movementX}} {{movementY}}/code>
events after <code>earlierEvent</code>, with the exception of when
screenX can not be updated because the pointer is clipped by the
user agent screen boundaries.
</p>
<p>
{{movementX}}/{{movementY}} must be updated regardless of pointer
lock state.
</p>
<p>
When unlocked, the system cursor can exit and re-enter the <a>user
agent</a> window. If it does so and the <a>user agent</a> was not
the target of operating system mouse move events then the most
recent pointer position will be unknown to the <a>user agent</a>
and {{movementX}}/{{movementY}} can not be computed and must be set
to zero.
</p>
<p>
When pointer lock is enabled <code>clientX</code>,
<code>clientY</code>, <code>screenX</code>, and
<code>screenY</code> must hold constant values as if the pointer
did not move at all once pointer lock was entered. But
{{movementX}}/{{movementY}} must continue to provide the change in
position of the pointer as when the pointer is unlocked. There will
be no limit to {{movementX}}/{{movementY}} values if the mouse is
continuously moved in a single direction. The concept of the mouse
cursor will have been removed, and it will not move off the window
or be clamped by a screen edge.
</p>
<p>
The un-initialized value of {{movementX}}/{{movementY}} must be
<code>0</code>.
</p>
<p>
Large movement values must not appear in situations when mouse
input is interupted, such as the mouse cursor leaving the window
and then re-entering at another location. If a <a>User agent</a>
experiences a gap in receiving mouse input data from the operating
system then the next generated <code>mousemove</code> event must
have {{movementX}}/{{movementY}} set to <code>0</code>. These gaps
may appear for example when the <a>User agent</a> receives a mouse
leaving event at the window system API. As an exception mouse
capture may allow the <a>User agent</a> to continue receiving mouse
events when the cursor moves outside the window.
</p>
</dd>
</dl>
</section>
<section data-dfn-for="MouseEventInit" data-link-for="MouseEventInit"
data-cite="ui-events">
<h2>
Extensions to the `MouseEventInit` Dictionary
</h2>
<pre class="idl">
partial dictionary MouseEventInit {
double movementX = 0;
double movementY = 0;
};
</pre>
<dl>
<dt>
<dfn>movementX</dfn> member
</dt>
<dt>
<dfn>movementY</dfn> member
</dt>
<dd>
The members {{movementX}} and {{movementY}} are used to initialize
respective members of {{MouseEvent}}.
</dd>
</dl>
</section>
<section>
<h2>
Requirements
</h2>
<p>
A <dfn>default unlock gesture</dfn> must always be available that will
exit pointer lock.
</p>
<aside class="note">
<p>
The ESC key is the recommended <a>default unlock gesture</a> for
<a>user agents</a> with keyboard input. It is recommended that the
unlock gesture also match any used to exit fullscreen [[FULLSCREEN]].
</p>
</aside>
<p>
Pointer lock must be exited if the <a>target</a> <a data-lt=
"become disconnected">becomes disconnected</a>, or the <a>user
agent</a>, window, or tab loses focus. Moving focus between elements of
<a>active documents</a>, including between [=Document/browsing
contexts=] , does not exit pointer lock. E.g. using the keyboard to
move focus between contents of frames or iframes will not exit.
</p>
<p>
Pointer lock must not be exited when fullscreen [[FULLSCREEN]] is
entered or exited unless the pointer is required to enable interaction
with the <a>user agent</a> graphical user interface, the <a>default
unlock gesture</a> was used to exit both fullscreen and pointer lock,
or window or tab focus was lost.
</p>
</section>
<section class='informative'>
<h2>
Use Cases
</h2>
<section>
<h2>
Relative view-port rotation of free moving virtual actors
</h2>
<p>
A player on a first/third person game will need to control the
view-port orientation. A widely used method is the use of mouse
movements to control the viewing angle. This kind of application can
use the Pointer Lock API to allow a complete freedom of control over
the viewport's yaw and pitch even when the user is not pressing mouse
buttons. Those buttons can be used for other actions while constantly
providing navigation via mouse movement.
</p>
</section>
<section>
<h2>
Free rotation of 3D models or panning of 2D layers
</h2>
<p>
Users of a three dimensional modeling application will need to rotate
models. A application can use the Pointer Lock API to enable the
author to rotate the model freely in a drag operation without
limiting motion. Without pointer lock a drag would stop providing
motion data when the mouse cursor is limited by the edge of the
screen.
</p>
<p>
Similarly, absolute motion panning of a large two dimensional image
could be permitted in a single drag operation without cursor / screen
limits.
</p>
</section>
<section>
<h2>
Relative movement of actors
</h2>
<p>
A player on a fast reflexes game controls a paddle to bounce back a
ball to the opponent, while allowing the same paddle to execute
actions based on different mouse buttons being pressed. The
application can use the Pointer Lock API to allow the player to react
quickly without being concerned about the mouse cursor leaving the
game play area and clicking another system application, thus breaking
the game flow.
</p>
</section>
<section>
<h2>
Jog movement over spinner controls
</h2>
<p>
When modifying numerically magnitudes in applications sometimes the
user will prefer to "drag" a numeric control by its button handles to
increment or decrement the numeric value. E.g. a spinner with a
number entry text box and arrows pointing up and down that can be
clicked or dragged on to change the value. An application could use
the Pointer Lock API to allow modifying the numeric values beyond
what the logical screen bounds allow. The same could apply for a
control that fast forwards or rewinds a video or audio stream like a
"jog".
</p>
</section>
<section>
<h2>
Synthetic cursor interaction with HTML DOM UI
</h2>
<p>
Some games use a classical cursor, however they want it to be limited
or controlled in some manner. E.g. limited to the bounds of the game,
or movable by the game. Locking the pointer enables this if the
application creates their own cursor. However HTML and DOM should
still be available to use for user interface. Synthetic mouse events
should be permitted to allow an application defined cursor to
interact with DOM. E.g. the following code should permit a custom
cursor to send click events while the pointer is locked:
</p>
<pre class='example' id="example-synthetic-cursor">
document.addEventListener("click", function (e) {
if (e._isSynthetic)
return;
// send a synthetic click
var ee = document.createEvent("MouseEvents");
ee._isSynthetic = true;
x = myCursor.x;
y = myCursor.y;
ee.initMouseEvent("click", true, true, null, 1,
x + e.screenX - e.clientX,
y + e.screenY - e.clientY,
x,
y);
var target = document.elementFromPoint(x, y);
if (target)
target.dispatchEvent(ee);
});
</pre>
<p>
Note that synthetic clicks may not be permitted by a <a>user
agent</a> to produce the same default action as a non-synthetic
click. However, application handlers can still take action and
provide user interface with existing HTML & DOM mechanisms.
</p>
</section>
<section>
<h2>
View-port panning by moving a mouse cursor against the bounds of a
view-port.
</h2>
<p>
Real Time Strategy games often use this technique. When the player
moves the pointer to the view-port borders, if they "push" the border
with a mouse movement, the view-port is panned over the game area
according to how much they move the mouse. When moving the mouse
cursor within the bounds of the view port it acts at is typically
would on a system. Applications may choose to implement this using
pointer lock and the previous use case of "Synthetic cursor
interaction with HTML DOM UI" to bring cursor behavior completely
under their control.
</p>
</section>
<section>
<h2>
Game Lobby, timer based pointer lock
</h2>
<p>
Games that use pointer lock may desire a traditional UI and system
cursor while players prepare in a game lobby. Games usually start
after a short timer when all players are ready. Ideally the game
could then switch to pointer lock mode without requiring an
<a>engagement gesture</a>. Players should be able to seamlessly move
from the game lobby into game navigation.
</p>
</section>
<section>
<h2>
Game Portal
</h2>
<p>
Game portals, and other sites such as Facebook and Google Plus, host
games for users to play. These games may be hosted and served from a
different origin from that of the portal site. Embedded games should
be able to lock the pointer, even in non-full screen mode.
</p>
</section>
</section>
<section class='informative'>
<h2>
Security
</h2>
<p>
Security Concerns:
</p>
<ul>
<li>User actions may be misdirected to elements the user did not intend
to interact with.
</li>
<li>Pointer Lock will remove the ability of a user to interact with
user agent and operating system controls
</li>
<li>Pointer Lock can be called repeated by script after user exits
pointer lock, blocking user from meaningful progress.
</li>
<li>Full screen exit instructions are displayed in some <a>user
agent</a>s when the pointer is moved to the top of the screen. During
pointer lock that gesture is not possible.
</li>
</ul>
<p>
Responses:
</p>
<ul>
<li>
<a>User agents</a> may limit what security origins may lock the
pointer.
</li>
<li>
<a>User agents</a> may prompt for confirmation before locking, this
preference may be saved as a content setting.
</li>
<li>Escape will always be provided by a <a>default unlock gesture</a>,
e.g. Esc key.
</li>
<li>Persistent display of escape instructions can be provided.
</li>
<li>Repeated escapes of pointer lock can signal <a>user agent</a> to
not re-lock the pointer without more specific <a>engagement
gesture</a>, e.g. similar to how Chrome suppresses repeated alert()
calls.
</li>
<li>Changing to new tabs, windows, or any other action that causes a
page to lose focus will exit pointer lock.
</li>
<li>Pointer lock can only be engaged when the window is in focus in the
user agent and operating system.
</li>
</ul>
<p>
Recommendations:
</p>
<ul>
<li>Esc key should exit pointer lock.
</li>
<li>Preferences per sub-domain can be used to allow or block pointer
lock, similar to pop-up, geolocation, and fullscreen.
</li>
</ul>
<p>
Pointer lock is a required user interaction mode for certain
application types, but carries a usability concern if maliciously used.
An attacker could remove the ability for a user to control their mouse
cursor on their system. <a>User agents</a> will prevent this by always
providing a mechanism to exit pointer lock, by informing the user of
how, and by limiting how pointer lock can be entered.
</p>
<p>
<a>User agents</a> will determine their own appropriate policies, which
may be specialized per device or differ based on user options.
</p>
</section>
<section class='informative'>
<h2>
Frequently Asked Questions
</h2>
<section>
<h2>
Why not merge with Mouse Capture: setCapture()?
</h2>
<p>
Mouse Capture [[MDN-SETCAPTURE]] handles low security risk mouse
event target lock for the duration of a mouse drag gesture. Pointer
lock removes the concept of the cursor and directs all events to a
given target. They are related, but different.
</p>
<p>
If a browser implemented both, it would be reasonable to support a
combination of traits: The security simplicity of "automatically
release lock when mouse up" and the increased functionality of total
control over mouse input and removal of the system cursor. The
security trait would allow more permissive use of the feature for
applications that only required a short burst of pointer lock during
a drag event.
</p>
<p>
This functionality is omitted from the initial version of this spec
because it helps the minor use cases in windowed mode but we still do
not have an implementation solving the major ones. And, to implement
this a browser must implement both, which none does yet. It is not
clear if this feature should live on .lock or on .setCapture. If both
were implemented, either API could be augmented fairly easily to
offer the hybrid functionality.
</p>
</section>
<section>
<h2>
Why not repurpose MouseEvent's .clientX/Y .screenX/Y?
</h2>
<p>
Even in non locked state, the delta values of mouse movement are
useful. Changing the meaning of .client or .screen based on lock
state would also cause easy errors in code not carefully monitoring
the lock state.
</p>
</section>
<section>
<h2>
Why use .movementX/Y instead of .deltaX/Y?
</h2>
<p>
When the pointer is locked 'wheel' events should be sent to the
pointer lock <a>target</a> element just as 'mousemove' events are.
There is a naming conflict with .deltaX/Y/Z as defined in <a href=
"https://w3c.github.io/uievents/#events-wheelevents">DOM 3 'wheel'
event</a>.
</p>
</section>
<section id="why-bundle">
<h2>
Why bundle all functionality (hiding cursor, providing mouse deltas)
instead of using CSS to hide the cursor, always providing delta
values, and offering an API to restrict the cursor movement to a
portion of the web page?
</h2>
<p>
There are good motivations to provide a more fine grained approach.
E.g. the use case "View-port panning by moving a mouse cursor against
the bounds of a view-port" doesn't require hiding the mouse cursor,
only bounding it and always having delta values available. Also, this
specification defines the movement deltas to be taken from how the
system mouse cursor moves, which incorporates operating system
filtering and acceleration of the mouse movement data. Applications
may desire access to a more raw form of movement data prior to
adjustments appropriate for a mouse cursor. Also, raw data may
provide better than pixel level accuracy for movement, as well as
higher frequency updates. Providing the raw delta movement would also
not require special permission or mode from a user, and for some set
of applications that do not require bounding the cursor may reduce
the security barriers and prompts needed.
</p>
<p>
There are two justifications for postponing this finer grained
approach. The first is a concern of specifying what units mouse
movement data are provided in. This specification defines
.movementX/Y precisely as the same values that could be recorded when
the mouse is not under lock by changes in .screenX/Y. Implementations
across multiple <a>user agents</a> and operating systems will easily
be able to meet that requirement and provide application developers
and users with a consistent experience. Further, users are expected
to have already configured the full system of hardware input and
operating system options resulting in a comfortable control the
system mouse cursor. By specifying .movementX/Y in the same units
mouse lock API applications will be instantly usable to all users
because they have already settled their preferences.
</p>
<p>
Secondly, the implementation of providing movement data and bounding
the mouse cursor is more difficult in the fine grained approach.
Bundling the features together gives implementations freedom to use a
variety of techniques as appropriate on each operating system and is
more practical to implement. Direct APIs do not exist on major
desktop platforms (Windows, Mac OS X, Linux) to bound the cursor to a
specific rectangle, and prototypes have not yet been developed to
demonstrate building that behavior by e.g. <a href=
"https://tronche.com/gui/x/xlib/window/attributes/">invisible
windows</a> with <a href=
"https://en.wikipedia.org/wiki/Xlib">Xlib</a> or manual cursor
movement on Mac. Unaccelerated Delta values have been proposed to be
accessed by reading raw Human Interface Device (HID) data. E.g.
WM_INPUT messages on Windows, and USB device APIs on Mac OS X /
Linux. The challenge here is interpreting and normalizing the units
to some consistent and specifiable scale. Also, most APIs considered
to date are limited to USB devices.
</p>
<p>
It would be reasonable to consider adding these capabilities in the
future, as the currently specified pointer lock API would be easy to
continue to support if the finer grained delta and confinement
features were implemented.
</p>
<p>
The bundled API is selected for implementation practicality, because
the desired use cases are supported, and because it will not conflict
with future improvements as discussed here.
</p>
</section>
<section>
<h2>
High resolution deltas / High frequency updates?
</h2>
<p>
Not yet, for the same reasons in the previous question: <a href=
"#why-bundle">"Why bundle all functionality (hiding cursor, providing
mouse deltas) instead of using CSS to hide the cursor, always
providing delta values, and offering an API to restrict the cursor
movement to a portion of the web page?"</a>.
</p>
</section>
<section>
<h2>
Why modify MouseEvent and reuse existing mouse events instead of
creating a mouse delta event?
</h2>
<p>
When under pointer lock many mouse events remain relevant, e.g.
click, mousedown, etc. These all share the same event data structure
MouseEvent. If movement data were reported via a new data structure
then a new event would be needed for reporting delta movement. The
new data structure would have many parallels to MouseEvent to offer
the same conveniences, e.g. button and modifier key states. When
handling click, down, and up events would the existing mousedown,
mouseup be used? If so, they would provide .clientX/Y and .screenX/Y
with no useful data, but would lack the convenience of containing the
current movement data. Or, new events would also be required for when
the mouse is locked.
</p>
<p>
Also, movementX/Y are convenient even when the mouse is not locked.
This spec requires movement members to always be valid, even when the
mouse cursor exists. This reduces code required to track the last
cursor state and mouseover/mouseout transitions if applications wish
to make use of delta motion of the mouse.
</p>
<p>
The only negative of adding movementX/Y to MouseEvent appears to be
the unused values in clientX/Y and screenX/Y when under pointer lock.
This does not seem to be a significant problem.
</p>
<p>
Therefore the minimal change to add movementX/Y to MouseEvent is
selected to reduce API and implementation complexity.
</p>
</section>
<section>
<h2>
Why separate targets for mouse events under pointer lock and keyboard
input focus?
</h2>
<p>
Consider a game with a 3D view controlled by moving the mouse cursor,
while the user may still chat with other users via a text console. It
is reasonable for the application to accept text input to an element
that is different than where mouse events are being dispatched. This
is similar to pre-existing behavior of receiving mousemove events
over any element while typing into a form on a page.
</p>
</section>
</section>
<section id='conformance'>
<p>
This specification defines conformance criteria that apply to a single
product: the <dfn>user agent</dfn> that implements the interfaces that
it contains.
</p>
</section>
<section class='appendix informative'>
<h2>
Acknowledgments
</h2>
<p>
Many thanks to lots of people who made contributions to the discussions
of this specification:
</p>
<ul>
<li>Adam Barth
</li>
<li>Alexey Proskuryakov
</li>
<li>Aryeh Gregor
</li>
<li>Boris Zbarsky
</li>
<li>Brandon Andrews
</li>
<li>Darin Fisher
</li>
<li>Dimitri Glazkov
</li>
<li>Glenn Maynard
</li>
<li>Gregg Tavares
</li>
<li>John Villar
</li>
<li>Jonas Sicking
</li>
<li>Klaas Heidstra
</li>
<li>Léonie Watson
</li>
<li>Marcos Cáceres
</li>
<li>Ms2ger
</li>
<li>Navid Zolghadr
</li>
<li>Olli Pettay
</li>
<li>Patrick H. Lauke
</li>
<li>Philip Jägenstedt
</li>
<li>Philippe Le Hegaret
</li>
<li>Robert O'Callahan
</li>
<li>Tab Atkins Jr.
</li>