-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPointerLockAndFullscreen.html
309 lines (294 loc) · 12 KB
/
PointerLockAndFullscreen.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
<!DOCTYPE html>
<html><head>
<style type="text/css">
body {
background: grey;
-webkit-user-select: none;
}
#container {
border: solid black 1px;
background: white;
position: absolute;
top: 10px;
bottom: 10px;
left: 10px;
right: 10px;
display: flex;
flex-direction: column;
margin: 0;
}
#container > iframe {
margin: 10px;
flex-grow: 1
}
#clicktarget {
border: solid black 1px;
background: yellowgreen;
}
#HTMLCursor {
border: solid black 1px;
background: yellow;
display: inline;
position: fixed;
pointer-events: none;
z-index: 1;
}
#resetPos {
border: solid black 1px;
background: pink;
position: relative;
}
.locktarget {
border: solid black 1px;
background: lightgrey;
}
#locktarget1 {
border: solid red 1px;
}
#locktarget2 {
border: solid green 1px;
}
#logoutput {
height: 150px;
overflow: auto;
}
</style>
<script type="text/javascript">
documentMouseMoveCallCounter = 0;
function RequestFullscreen(element) {
console.log("requestFullscreen()")
var requestFullscreen = element.requestFullscreen ||
element.webkitRequestFullscreen ||
element.mozRequestFullScreen ||
element.msRequestFullscreen;
requestFullscreen.call(element);
}
function ExitFullscreen() {
console.log("exitFullscreen()")
var exitfulLscreen = document.exitfulLscreen ||
document.webkitExitFullscreen ||
document.mozCancelFullScreen ||
document.msExitFullscreen;
exitfulLscreen.call(document);
}
function ToggleFullscreen(element) {
var currentFSElement = document.fullscreenElement ||
document.webkitFullscreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement;
if (currentFSElement === element)
ExitFullscreen();
else
RequestFullscreen(element);
}
function DelayedFullscreen() {
log("Delaying a fullscreen...");
window.setTimeout("ToggleFullscreen(document.getElementById('container'))", 1010); // just over a second causes this to not be detected as a user gesture in WebKit.
}
function LockMouse(element) {
log("Trying a lock to " + element.id + "...");
element.requestPointerLock();
}
function ToggleMouseLock(element) {
if (document.pointerLockElement) {
document.exitPointerLock();
} else {
LockMouse(element)
}
}
function DelayedLockMouse1() {
log("Delaying a lock...");
window.setTimeout("LockMouse(locktarget1)", 1010); // just over a second causes this to not be detected as a user gesture in WebKit.
}
function SpamLockMouse2() {
log("Spamming lock requests for target2...");
window.setInterval("LockMouse(locktarget2)", 111);
}
function log(x) {
logoutput.insertAdjacentHTML("beforeEnd", "<div>" + x + "</div>");
logoutput.scrollTop = logoutput.scrollHeight - logoutput.clientHeight;
}
function init() {
document.onpointerlockchange = function () {
log("document.onpointerlockchange to " +
(document.pointerLockElement ? "locked" : "unlocked") +
((document.pointerLockElement == locktarget1) ? " to target 1" : "") +
((document.pointerLockElement == locktarget2) ? " to target 2" : ""));
}
document.onpointerlockerror = function () {
log("document.onpointerlockerror. Current element: " +
(document.pointerLockElement ? "locked" : "unlocked") +
((document.pointerLockElement == locktarget1) ? " to target 1" : "") +
((document.pointerLockElement == locktarget2) ? " to target 2" : ""));
}
function updateIsLocked () {
var locked = document.pointerLockElement;
if (document.getElementById("isLocked").lastLocked != locked) {
document.getElementById("isLocked").innerHTML = "document.pointerLockElement " +
(document.pointerLockElement ? "locked" : "unlocked") +
(document.pointerLockElement == locktarget1 ? " to target 1" : "") +
(document.pointerLockElement == locktarget2 ? " to target 2" : "");
document.getElementById("isLocked").lastLocked = locked;
}
requestAnimationFrame(updateIsLocked);
};
updateIsLocked();
var HTMLCursor = document.getElementById("HTMLCursor");
var resetPos = document.getElementById("resetPos");
function moveHTMLCursorTo(x, y) {
HTMLCursor.style.left = x + "px";
HTMLCursor.style.top = y + "px";
}
function moveHTMLCursorToCenter() {
moveHTMLCursorTo(window.innerWidth / 2, window.innerHeight / 2);
}
moveHTMLCursorToCenter();
function moveHTMLCursorBy(x, y) {
let zoom = 1;//window.devicePixelRatio;
moveHTMLCursorTo(
HTMLCursor.getBoundingClientRect().left + parseInt(x) / zoom,
HTMLCursor.getBoundingClientRect().top + parseInt(y) / zoom);
}
document.addEventListener("click", function (e) { clickAndDblclickHander("click", e); } );
document.addEventListener("dblclick", function (e) { clickAndDblclickHander("dblclick", e); } );
function clickAndDblclickHander(clickOrDblclick, e) {
if (e._isSynthetic)
return;
//send a synthetic click / dblclick
ee = document.createEvent("MouseEvents");
ee._isSynthetic = true;
x = HTMLCursor.getBoundingClientRect().left;
y = HTMLCursor.getBoundingClientRect().top;
ee.initMouseEvent(clickOrDblclick, 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)
};
document.addEventListener("mousemove", function (e) {
moveHTMLCursorBy(e.movementX, e.movementY);
documentMouseMoveCallCounter++;
displaytext.innerHTML =
" document mousemove listener:<br>" +
" Raw: " + e.screenX + ", " + e.screenY +
" Cursor: "
+ HTMLCursor.getBoundingClientRect().left + ", "
+ HTMLCursor.getBoundingClientRect().top +
" Movement: " + e.movementX + ", " + e.movementY +
", calls: " + documentMouseMoveCallCounter;
});
locktarget1.addEventListener("mousemove", function (e) {
locktarget1.lastElementChild.innerHTML =
" Raw: " + e.screenX + ", " + e.screenY +
" Movement: " + e.movementX + ", " + e.movementY;
});
locktarget2.addEventListener("mousemove", function (e) {
locktarget2.lastElementChild.innerHTML =
" Raw: " + e.screenX + ", " + e.screenY +
" Movement: " + e.movementX + ", " + e.movementY;
});
document.addEventListener("keypress", function (e) {
if (e.keyCode != 116) {
e.preventDefault();
moveHTMLCursorToCenter();
}
});
var clicktarget = document.getElementById("clicktarget");
clicktarget._clicks1 = 0;
clicktarget._clicks2 = 0;
clicktarget._dblclicks1 = 0;
clicktarget._dblclicks2 = 0;
clicktarget.updateText = function () {
clicktarget.innerHTML = "I've been clicked " + clicktarget._clicks1 +
" real times and " + clicktarget._clicks2 + " sythetic times.<br>" +
"I've been dblclicked " + clicktarget._dblclicks1 +
" real times and " + clicktarget._dblclicks2 + " sythetic times.<br>";
}
clicktarget.addEventListener("click", function (e) {
if (e._isSynthetic)
clicktarget._clicks2 += 1;
else
clicktarget._clicks1 += 1;
clicktarget.updateText();
});
clicktarget.addEventListener("dblclick", function (e) {
if (e._isSynthetic)
clicktarget._dblclicks2 += 1;
else
clicktarget._dblclicks1 += 1;
clicktarget.updateText();
});
resetPos.addEventListener("click", function (e) {
moveHTMLCursorTo(e.clientX, e.clientY);
displaytext.innerHTML = displaytext.innerHTML + " mousedown";
});
console.log("document.fullscreenElement = " + document.fullscreenElement);
// Color background when we are the iframe.
if (window.top !== window)
document.getElementById('container').style.backgroundColor="tan";
}
</script>
</head>
<body onload="init()" title="This tooltip should not be shown if the mouse is locked.">
<div id="container">
<div>
<button onclick="ToggleFullscreen(document.getElementById('container'));">Toggle Tab Fullscreen</button>
<button onclick="DelayedFullscreen();">Delayed Fullscreen</button>
<button onclick="DelayedLockMouse1();">Delayed Lock Target1</button>
<button onclick="SpamLockMouse2();">Spam Lock Target2</button>
<button onclick="document.exitPointerLock();">Unlock Pointer</button>
<button onclick="console.log(document.pointerLockElement);">document.pointerLockElement</button>
<button onclick="ToggleFullscreen(document.getElementById('container')); LockMouse(locktarget1);">Toggle Fullscreen & Lock Pointer</button>
<button onclick="RequestFullscreen(document.getElementById('container')); ExitFullscreen();">Request+Exit Fullscreen</button>
<button onclick="ExitFullscreen(); RequestFullscreen(document.getElementById('container')); ">Exit+Request Fullscreen</button>
</div>
<form name="displaytext">...<br>...</form>
<div id="locktarget1" class="locktarget">
Target 1
<button onclick="ToggleFullscreen(document.getElementById('locktarget1'));">Toggle Target1 Fullscreen</button>
<button onclick="ToggleMouseLock(locktarget1);">Toggle Pointer Lock Target1</button>
<button onclick="ToggleFullscreen(document.getElementById('locktarget1')); LockMouse(locktarget1);">Fullscreen & Lock Pointer Target1</button>
<button onclick="ToggleFullscreen(document.getElementById('locktarget2'));">Toggle Target2 Fullscreen</button>
<div>Lock Target Movement:</div>
<div>...</div>
</div><br>
<div id="locktarget2" class="locktarget">
Target 2
<button onclick="ToggleFullscreen(document.getElementById('locktarget2'));">Toggle Target2 Fullscreen</button>
<button onclick="ToggleMouseLock(locktarget2);">Toggle Pointer Lock Target2</button>
<button onclick="ToggleFullscreen(document.getElementById('locktarget2')); LockMouse(locktarget2);">Fullscreen & Lock Pointer Target2</button>
<button onclick="ToggleFullscreen(document.getElementById('locktarget1'));">Toggle Target1 Fullscreen</button>
<div>Lock Target Movement:</div>
<div>...</div>
</div><br>
<table width="100%">
<tr>
<td width="50%">
<div id="isLocked">document.pointerLockElement unlocked</div>
<div id="clicktarget">
click here!<br>
dblclick here!
</div>
<div id="resetPos">
Press a key, or Click here to reset HTMLCursor position.
</div><br>
<div>
<a href="#anchor">#anchor</a>
<a href="PointerLockAndFullscreen.html">PointerLockAndFullscreen.html</a>
<a href="http://scheib.net">scheib.net</a>
<button onclick="history.pushState(null, null, 'pushState')">history.pushState</button>
</div><br>
</td>
<td width="50%">
<div id="logoutput">Log:</div>
</td>
</tr>
</table>
<iframe src="PointerLockAndFullscreen.html" allowfullscreen></iframe>
</div>
<div id="HTMLCursor">HTMLCursor</div><br>
</body>
</html>