-
Notifications
You must be signed in to change notification settings - Fork 23
/
game-shim.js
361 lines (306 loc) · 13.4 KB
/
game-shim.js
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
/**
* @fileoverview game-shim - Shims to normalize gaming-related APIs to their respective specs
* @author Brandon Jones
* @version 1.2
*/
/* Copyright (c) 2012, Brandon Jones. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
(function(global) {
"use strict";
var elementPrototype = (global.HTMLElement || global.Element)["prototype"];
var getter;
var GameShim = global.GameShim = {
supports: {
fullscreen: true,
pointerLock: true,
gamepad: true,
highResTimer: true
}
};
//=====================
// Animation
//=====================
// window.requestAnimaionFrame, credit: Erik Moller
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
(function() {
var lastTime = 0;
var vendors = ["webkit", "moz", "ms", "o"];
var x;
for(x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+"RequestAnimationFrame"];
}
window.cancelAnimationFrame = window.cancelAnimationFrame || window.cancelRequestAnimationFrame; // Check for older syntax
for(x = 0; x < vendors.length && !window.cancelAnimationFrame; ++x) {
window.cancelAnimationFrame = window[vendors[x]+"CancelAnimationFrame"] || window[vendors[x]+"CancelRequestAnimationFrame"];
}
// Manual fallbacks
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
var currTime = Date.now();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
// window.animationStartTime
if(!window.animationStartTime) {
getter = (function() {
for(x = 0; x < vendors.length; ++x) {
if(window[vendors[x] + "AnimationStartTime"]) {
return function() { return window[vendors[x] + "AnimationStartTime"]; };
}
}
return function() { return Date.now(); };
})();
Object.defineProperty(window, "animationStartTime", {
enumerable: true, configurable: false, writeable: false,
get: getter
});
}
}());
//=====================
// Fullscreen
//=====================
// document.fullscreenEnabled
if(!document.hasOwnProperty("fullscreenEnabled")) {
getter = (function() {
// These are the functions that match the spec, and should be preferred
if("webkitIsFullScreen" in document) {
return function() { return document.webkitFullscreenEnabled; };
}
if("mozFullScreenEnabled" in document) {
return function() { return document.mozFullScreenEnabled; };
}
GameShim.supports.fullscreen = false;
return function() { return false; }; // not supported, never fullscreen
})();
Object.defineProperty(document, "fullscreenEnabled", {
enumerable: true, configurable: false, writeable: false,
get: getter
});
}
if(!document.hasOwnProperty("fullscreenElement")) {
getter = (function() {
// These are the functions that match the spec, and should be preferred
var i=0, name=["webkitCurrentFullScreenElement", "webkitFullscreenElement", "mozFullScreenElement"];
for (; i<name.length; i++)
{
if (name[i] in document)
{
return function() { return document[name[i]]; };
}
}
return function() { return null; }; // not supported
})();
Object.defineProperty(document, "fullscreenElement", {
enumerable: true, configurable: false, writeable: false,
get: getter
});
}
// Document event: fullscreenchange
function fullscreenchange(oldEvent) {
var newEvent = document.createEvent("CustomEvent");
newEvent.initCustomEvent("fullscreenchange", true, false, null);
// TODO: Any need for variable copy?
document.dispatchEvent(newEvent);
}
document.addEventListener("webkitfullscreenchange", fullscreenchange, false);
document.addEventListener("mozfullscreenchange", fullscreenchange, false);
// Document event: fullscreenerror
function fullscreenerror(oldEvent) {
var newEvent = document.createEvent("CustomEvent");
newEvent.initCustomEvent("fullscreenerror", true, false, null);
// TODO: Any need for variable copy?
document.dispatchEvent(newEvent);
}
document.addEventListener("webkitfullscreenerror", fullscreenerror, false);
document.addEventListener("mozfullscreenerror", fullscreenerror, false);
// element.requestFullScreen
if(!elementPrototype.requestFullScreen) {
elementPrototype.requestFullScreen = (function() {
if(elementPrototype.webkitRequestFullScreen) {
return function() {
this.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
};
}
if(elementPrototype.mozRequestFullScreen) {
return function() {
this.mozRequestFullScreen();
};
}
return function(){ /* unsupported, fail silently */ };
})();
}
// document.cancelFullScreen
if(!document.cancelFullScreen) {
document.cancelFullScreen = (function() {
return document.webkitCancelFullScreen ||
document.mozCancelFullScreen ||
function(){ /* unsupported, fail silently */ };
})();
}
//=====================
// Pointer Lock
//=====================
var mouseEventPrototype = global.MouseEvent.prototype;
if(!("movementX" in mouseEventPrototype)) {
Object.defineProperty(mouseEventPrototype, "movementX", {
enumerable: true, configurable: false, writeable: false,
get: function() { return this.webkitMovementX || this.mozMovementX || 0; }
});
}
if(!("movementY" in mouseEventPrototype)) {
Object.defineProperty(mouseEventPrototype, "movementY", {
enumerable: true, configurable: false, writeable: false,
get: function() { return this.webkitMovementY || this.mozMovementY || 0; }
});
}
// Navigator pointer is not the right interface according to spec.
// Here for backwards compatibility only
if(!navigator.pointer) {
navigator.pointer = navigator.webkitPointer || navigator.mozPointer;
}
// Document event: pointerlockchange
function pointerlockchange(oldEvent) {
var newEvent = document.createEvent("CustomEvent");
newEvent.initCustomEvent("pointerlockchange", true, false, null);
document.dispatchEvent(newEvent);
}
document.addEventListener("webkitpointerlockchange", pointerlockchange, false);
document.addEventListener("webkitpointerlocklost", pointerlockchange, false);
document.addEventListener("mozpointerlockchange", pointerlockchange, false);
document.addEventListener("mozpointerlocklost", pointerlockchange, false);
// Document event: pointerlockerror
function pointerlockerror(oldEvent) {
var newEvent = document.createEvent("CustomEvent");
newEvent.initCustomEvent("pointerlockerror", true, false, null);
document.dispatchEvent(newEvent);
}
document.addEventListener("webkitpointerlockerror", pointerlockerror, false);
document.addEventListener("mozpointerlockerror", pointerlockerror, false);
if(!document.hasOwnProperty("pointerLockElement")) {
getter = (function() {
// These are the functions that match the spec, and should be preferred
if("webkitPointerLockElement" in document) {
return function() { return document.webkitPointerLockElement; };
}
if("mozPointerLockElement" in document) {
return function() { return document.mozPointerLockElement; };
}
return function() { return null; }; // not supported
})();
Object.defineProperty(document, "pointerLockElement", {
enumerable: true, configurable: false, writeable: false,
get: getter
});
}
// element.requestPointerLock
if(!elementPrototype.requestPointerLock) {
elementPrototype.requestPointerLock = (function() {
if(elementPrototype.webkitRequestPointerLock) {
return function() {
this.webkitRequestPointerLock();
};
}
if(elementPrototype.mozRequestPointerLock) {
return function() {
this.mozRequestPointerLock();
};
}
if(navigator.pointer) {
return function() {
var elem = this;
navigator.pointer.lock(elem, pointerlockchange, pointerlockerror);
};
}
GameShim.supports.pointerLock = false;
return function(){}; // not supported
})();
}
// document.exitPointerLock
if(!document.exitPointerLock) {
document.exitPointerLock = (function() {
return document.webkitExitPointerLock ||
document.mozExitPointerLock ||
function(){
if(navigator.pointer) {
var elem = this;
navigator.pointer.unlock();
}
};
})();
}
//=====================
// Gamepad
//=====================
// NOTE: This API is still very much in flux!
// See https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html for latest spec proposal
if(!navigator.getGamepads) {
navigator.getGamepads = (function() {
// These are the functions that match the spec, and should be preferred
if("webkitGetGamepads" in navigator) {
return navigator.webkitGetGamepads;
}
if("mozGetGamepads" in navigator) {
return navigator.mozGetGamepads;
}
// Older property-based methods
if("webkitGamepads" in navigator) {
return function() { return navigator.webkitGamepads; };
}
if("mozGamepads" in navigator) {
return function() { return navigator.mozGamepads; };
}
// Not supported, return empty array
GameShim.supports.gamepad = false;
var gamepads = [];
return function() { return gamepads; };
})();
}
//=======================
// High Resolution Timer
//=======================
if(!window.performance) {
window.performance = {};
}
if(!window.performance.timing) {
window.performance.timing = {
navigationStart: Date.now() // Terrible approximation, I know. Sorry.
};
}
if(!window.performance.now) {
window.performance.now = (function() {
// FYI: Mozilla supports high-res timers without prefixes.
if(window.performance.webkitNow) {
return window.performance.webkitNow;
}
GameShim.supports.highResTimer = false;
return function(){ // not supported, return a low-resolution approximation
return Date.now() - window.performance.timing.navigationStart;
};
})();
}
})((typeof(exports) != 'undefined') ? global : window); // Account for CommonJS environments