-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.html
1225 lines (1215 loc) · 49.8 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: "Mustaq Ahmed",
company: "Google",
companyURL: "http://www.google.com/",
w3cid: "75206",
},
{
name: "Vincent Scheib",
url: "http://www.scheib.net",
company: "Google",
companyURL: "http://www.google.com/",
w3cid: "49724",
},
],
formerEditors: [
{
name: "Navid Zolghadr",
company: "Google",
companyURL: "http://www.google.com/",
w3cid: "92361",
},
],
github: "w3c/pointerlock",
group: "webapps",
xref: {
specs: ["uievents"],
profile: "web-platform",
},
};
</script>
</head>
<body data-cite="uievents">
<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">https://github.com/w3c/pointerlock/commits/gh-pages/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 [[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>
Pointer Lock and Interfaces
</h2>
<section>
<h3>
The <dfn>pointer lock state</dfn> definition
</h3>
<p>
The [=pointer lock state=] is the state where a single DOM element,
which we will call the <dfn>pointer-lock target</dfn>, receives all
mouse events and the cursor is hidden.
</p>
<p>
Once in the [=pointer lock state=] the [=user agent=] has a
[=pointer-lock target=], a <dfn>pointer-lock options</dfn>, which is
a {{PointerLockOptions}} and a <dfn>cursor position</dfn> which is a
pair of numbers representing the location of the system mouse cursor
when the Pointer Lock State was entered (the same location that is
reported in `screenX`, `screenY`). The [=pointer-lock target=]
receives all relevant user generated {{MouseEvent}} events: namely,
all user-generated `mousemove`, `mousedown`, `mouseup`, `click`,
`dblclick`, `auxclick`, and `wheel` [[ui-events]]. No other elements
receive these events while in [=pointer lock state=]. There will be
no dispatching of events that require the concept of a mouse cursor:
namely, `mouseenter`, `mouseleave`, `mouseover`, `mouseout`, `drag`,
and `drop`.
<aside class="issue" data-number="97"></aside>
</p>
<p>
While in the [=pointer lock state=] if the [=pointer-lock options=]'
{{PointerLockOptions/unadjustedMovement}} member is `true`, the event
coordinates will not be affected by the underlying platform behaviors
such as mouse acceleration. In other words, the user agent uses the
APIs provided by the underlying platform to guarantee getting the raw
events. If the {{PointerLockOptions}}'
{{PointerLockOptions/unadjustedMovement}} member is `false`, the user
agent relies on the default behavior of the underlying platform
regarding the mouse acceleration.
</p>
<p>
In the [=pointer lock state=], the system mouse cursor is hidden and
the window is prevented from losing focus, regardless of mouse
movement or button presses. This is directly or indirectly achieved
utilizing the underlying operating system API.
</p>
<p class="note">
Synthetic mouse events created by application script act the same
regardless of lock state.
</p>
</section>
<section data-dfn-for="PointerLockOptions">
<h3>
`PointerLockOptions` dictionary
</h3>
<pre class="idl">
dictionary PointerLockOptions {
boolean unadjustedMovement = false;
};
</pre>
<dl>
<dt>
<dfn>PointerLockOptions</dfn> dictionary
</dt>
<dd>
<p>
The options dictionary to customize how the pointer behaves in
the locked mode.
</p>
</dd>
<dt>
<dfn>unadjustedMovement</dfn> member
</dt>
<dd>
<p>
If this value is set to `true`, then the pointer movements will
not be affected by the underlying platform modifications such as
mouse accelaration.
</p>
</dd>
</dl>
</section>
<section>
<h3>
`pointerlockchange` and `pointerlockerror` Events
</h3>
<p>
Two events are used to communicate pointer lock state change or an
error in changing state. They are named <dfn class=
"event">pointerlockchange</dfn> and <dfn class=
"event">pointerlockerror</dfn>. Refer to algorithm of
[[[#requestPointerLock]]] for detail.
</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>
<h3>
<dfn>Exit Pointer Lock</dfn>
</h3>
<p>
The process of exiting pointer lock, given an |element:Element|, is
as follows:
</p>
<ol class="algorithm">
<li>The system mouse cursor must be displayed again and positioned at
[=cursor position=].
</li>
<li>[=Queue an element task=] on the [=user interaction task source=]
to [=fire an event=] named {{pointerlockchange}} at the given
|element:Element|'s [=Node/node document=].
</li>
<li>Exit the [=pointer lock state=] by setting [=pointer-lock
target=], [=pointer-lock options=], and [=cursor position=] to null.
</li>
</ol>
</section>
</section>
<section data-dfn-for="Element" data-link-for="Element" id=
"requestPointerLock">
<h2>
Extensions to the `Element` Interface
</h2>
<p>
The {{Element}} interface is extended to provide the ability to request
the pointer be locked.
</p>
<pre class="idl">
partial interface Element {
Promise<undefined> requestPointerLock(optional PointerLockOptions options = {});
};
</pre>
<pre class='example'>
const lock_element = document.getElementById("lock_element");
const lock_button = document.getElementById("lock");
lock_button.addEventListener("click", async (event) => {
try {
await lock_element.requestPointerLock({ unadjustedMovement: true });
console.log("successfully locked!");
} catch (e) {
console.log("lock failed with error: ", e);
}
});
</pre>
<dl>
<dt>
requestPointerLock({{PointerLockOptions}} options) method
</dt>
<dd>
<p>
A [=parallel queue=] named as <dfn>lock requests queue</dfn> is
used for queuing all requests. When <dfn>requestPointerLock()</dfn>
is invoked, perform the following steps:
</p>
<ol class="algorithm">
<li>Let |promise:Promise| be [=a new promise=].
</li>
<li>When a {{Window/window}} is in [=Window/focus=], if the
[=this=]'s [=shadow-including root=] is the [=navigable/active
document=] of a [=Document/browsing context=] (or has an
[=tree/ancestor=] [=browsing context=]) that is not in focus:
<ol>
<li>[=Queue an element task=] on the [=user interaction task
source=], given [=this=], to perform the following steps:
<ol>
<li>[=Fire an event=] named {{pointerlockerror}} at
[=this=]'s [=Node/node document=].
</li>
<li>[=Reject=] |promise| with a {{"WrongDocumentError"}}
{{DOMException}}.
</li>
</ol>
</li>
<li>Return <var>promise</var>.
</li>
</ol>
<aside class="issue" data-number="93"></aside>
</li>
<li>If the 'relevant global object does not have [=transient
activation=] and the {{Document}} has not previously released a
successful pointer lock with {{Document/exitPointerLock()}}:
<ol>
<li>[=Queue an element task=] on the [=user interaction task
source=], given [=this=], to perform the following steps:
<ol>
<li>[=Fire an event=] named {{pointerlockerror}} at
[=this=]'s [=Node/node document=].
</li>
<li>[=Reject=] <var>promise</var> with a
"{{NotAllowedError}}" {{DOMException}}.
</li>
</ol>
</li>
<li>Return <var>promise</var>.
</li>
</ol>
<aside class="note">
When a single <a>user activation</a> initiates both pointer
lock and fullscreen [[FULLSCREEN]], the
{{requestPointerLock()}} call succeeds only when it is made
before a fullscreen request because fullscreen is a
<a>transient activation-consuming API</a>.
</aside>
</li>
<li>If [=this=]'s [=Node/node document=]'s [=Document/active
sandboxing flag set=] has the [=sandboxed pointer lock browsing
context flag=] set:
<ol>
<li>[=Queue an element task=] on the [=user interaction task
source=], given [=this=], to perform the following steps:
<ol>
<li>[=Fire an event=] named {{pointerlockerror}} at
[=this=]'s [=Node/node document=].
</li>
<li>[=Reject=] <var>promise</var> with a
"{{SecurityError}}" {{DOMException}}.
</li>
</ol>
</li>
<li>Return <var>promise</var>.
</li>
</ol>
</li>
<li>If options["{{PointerLockOptions/unadjustedMovement}}"] is true
and the platform does not support
{{PointerLockOptions/unadjustedMovement}}:
<ol>
<li>[=Queue an element task=] on the [=user interaction task
source=], given [=this=], to perform the following steps:
<ol>
<li>[=Fire an event=] named {{pointerlockerror}} at
[=this=]'s [=Node/node document=].
</li>
<li>[=Reject=] <var>promise</var> with a
"{{NotSupportedError}}" {{DOMException}}.
</li>
</ol>
</li>
<li>Return <var>promise</var>.
</li>
</ol>
<aside class="issue" data-number="90"></aside>
</li>
<li>Enqueue the following steps to the [=lock requests queue=]:
<ol>
<li>If the user agent's [=pointer-lock target=] is an element
whose [=shadow-including root=] is not equal to [=this=]'s
[=shadow-including root=], then:
<ol>
<li>[=Queue an element task=] on the [=user interaction
task source=], given [=this=], to perform the following
steps:
<ol>
<li>[=Fire an event=] named {{pointerlockerror}} at
[=this=]'s [=Node/node document=].
</li>
<li>[=Reject=] <var>promise</var> with a
"{{InvalidStateError}}" {{DOMException}}.
</li>
</ol>
</li>
</ol>
</li>
<li>If the user agent's [=pointer-lock target=]'s
[=shadow-including root=] is equal to [=this=]'s
[=shadow-including root=] and options are equivalent to the
current [=pointer-lock options=], then:
<ol>
<li>Set [=pointer-lock target=] to [=this=].
</li>
<li>[=Queue an element task=] on the [=user interaction
task source=], given [=this=], to perform the following
steps:
<ol>
<li>[=Fire an event=] named {{pointerlockchange}} at
[=this=]'s [=Node/node document=].
</li>
<li>[=Resolve=] the <var>promise</var>.
</li>
</ol>
</li>
</ol>
</li>
<li>If [=pointer-lock target=] is null, or options is not
equivalent to the current [=pointer-lock options=], process the
lock request and handle response upon fulfillment:
<ol>
<li>If the lock request failed:
<ol>
<li>[=Queue an element task=] on the [=user interaction
task source=], given [=this=], to perform the following
steps:
<ol>
<li>[=Fire an event=] named {{pointerlockerror}} at
[=this=]'s [=Node/node document=].
</li>
<li>[=Reject=] <var>promise</var> with a
"{{NotSupportedError}}" {{DOMException}}.
</li>
</ol>
</li>
</ol>
<aside class="issue" data-number="91"></aside>
</li>
<li>If the lock request succeed:
<ol>
<li>Enter [=pointer lock state=] by performing the
following:
<ol>
<li>Set the user agent's [=pointer-lock target=] to
[=this=].
</li>
<li>Set the user agent's [=cursor position=] to the
current system mouse location.
</li>
<li>Set the [=pointer-lock options=] to options.
</li>
</ol>
</li>
<li>[=Queue an element task=] on the [=user interaction
task source=], given [=this=], to perform the following
steps:
<ol>
<li>[=Fire an event=] named {{pointerlockchange}}
at [=this=]'s [=Node/node document=].
</li>
<li>[=Resolve=] the <var>promise</var>.
</li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
<aside class="issue" data-number="96"></aside>
</li>
<li>Return <var>promise</var>.
</li>
</ol>
<aside class="note">
This algorithm is under active development and may be modified in
subsequent updates.
</aside>
</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 [=event handler idl attribute=] for
{{Document/pointerlockchange}} events.
</p>
</dd>
<dt>
<dfn>onpointerlockerror</dfn> attribute
</dt>
<dd>
<p>
An [=event handler idl attribute=] for
{{Document/pointerlockerror}} events.
</p>
</dd>
<dt>
<dfn>exitPointerLock()</dfn> method
</dt>
<dd>
<ol>
<li>If the user agent's pointer-lock target is null, then return.
</li>
<li>If [=pointer-lock target=]'s [=shadow-including root=]is not
equal to [=this=]'s [=Node/node document=], return.
</li>
<li>[=Exit pointer lock=] with the user agent's [=pointer-lock
target=].
</li>
</ol>
</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 [=retargeting=]
the element, which is the target for mouse events, against [=this=]
element if the result and [=this=] element 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 class="note">
The example uses fictional `shadow-root` element to denote a
[=Element/shadow root=] instance.
</p>
<p>
If `#canvas1` is the target, `document.pointerLockElement` returns
`#host1`, and `root1.pointerLockElement` returns `#canvas1`. The
result of [=retargeting=] `#canvas1` against `#root2` is `#host1`,
but as `#host1` is not in the same tree as `#root2`, null will be
returned for `root2.pointerLockElement`.
</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}} and {{movementY}} must provide the
change in position of the pointer, as if the values of `screenX`,
`screenY`, were stored between two subsequent `mousemove` events
`eNow` and `ePrevious` and the difference taken `movementX =
eNow.screenX - ePrevious.screenX`.
</p>
<p>
{{movementX}} and {{movementY}} must be zero for all mouse events
except `mousemove`. All motion data must be delivered via
`mousemove` events such that between any two mouse events
`earlierEvent` and `currentEvent` the value of
`currentEvent.screenX - earlierEvent.screenX` is equivalent to the
sum of all {{movementX}} in the events after `earlierEvent`, with
the exception of when screenX can not be updated because the
pointer is clipped by the [=user agent=] screen boundaries.
</p>
<p>
{{movementX}} and {{movementY}} must be updated regardless of
pointer lock state.
</p>
<p>
When unlocked, the system cursor can exit and re-enter the [=user
agent=] window. If it does so and the [=user agent=] was not the
target of operating system mouse move events then the most recent
pointer position will be unknown to the [=user agent=] and
{{movementX}} / {{movementY}} can not be computed and must be set
to zero.
</p>
<p>
When pointer lock is enabled `clientX`, `clientY`, `screenX`, and
`screenY` must hold constant values as if the pointer did not move
at all once pointer lock was entered. But {{movementX}} and
{{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}} and {{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}} and {{movementY}} must be
`0`.
</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 [=user agent=]
experiences a gap in receiving mouse input data from the operating
system then the next generated `mousemove` event must have
{{movementX}} and {{movementY}} set to `0`. These gaps may appear
for example when the [=user agent=] receives a mouse leaving event
at the window system API. As an exception mouse capture may allow
the [=user agent=] 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=] with the [=user agent=]'s [=pointer-lock
target=].
</p>
<aside class="note">
<p>
The ESC key is the recommended [=default unlock gesture=] for [=user
agents=] 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 [=pointer-lock target=] <a data-lt=
"become disconnected">becomes disconnected</a>, or the [=user agent=],
window, or tab loses focus. Moving focus between elements of
[=navigable/active document=], 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 [=user agent=] graphical user interface, the [=default unlock
gesture=] 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'>
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 [=user agent=]
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 a [=user activation=].
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 [=user agent=]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>The [=user agents=] may limit what security origins may lock the
pointer.
</li>
<li>The [=user agents=] may prompt for confirmation before locking,
this preference may be saved as a content setting.
</li>
<li>Escape will always be provided by a [=default unlock gesture=],
e.g. Esc key.
</li>
<li>Persistent display of escape instructions can be provided.
</li>
<li>Repeated escapes of pointer lock can signal [=user agent=] to not
re-lock the pointer without more specific user action, 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=] with the [=user agent=]'s
[=pointer-lock target=].
</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=] with the user agent's
[=pointer-lock target=].
</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. [=user agents=] 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>
[=user agents=] 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>