-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Walter Zimmer
committed
Jan 7, 2019
1 parent
c4b79be
commit 7c99190
Showing
9 changed files
with
1,872 additions
and
848 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// THREEx.KeyboardState.js keep the current state of the keyboard. | ||
// It is possible to query it at any time. No need of an event. | ||
// This is particularly convenient in loop driven case, like in | ||
// 3D demos or games. | ||
// | ||
// # Usage | ||
// | ||
// **Step 1**: Create the object | ||
// | ||
// ```var keyboard = new THREEx.KeyboardState();``` | ||
// | ||
// **Step 2**: Query the keyboard state | ||
// | ||
// This will return true if shift and A are pressed, false otherwise | ||
// | ||
// ```keyboard.pressed("shift+A")``` | ||
// | ||
// **Step 3**: Stop listening to the keyboard | ||
// | ||
// ```keyboard.destroy()``` | ||
// | ||
// NOTE: this library may be nice as standaline. independant from three.js | ||
// - rename it keyboardForGame | ||
// | ||
// # Code | ||
// | ||
|
||
/** @namespace */ | ||
var THREEx = THREEx || {}; | ||
|
||
/** | ||
* - NOTE: it would be quite easy to push event-driven too | ||
* - microevent.js for events handling | ||
* - in this._onkeyChange, generate a string from the DOM event | ||
* - use this as event name | ||
*/ | ||
THREEx.KeyboardState = function() | ||
{ | ||
// to store the current state | ||
this.keyCodes = {}; | ||
this.modifiers = {}; | ||
|
||
// create callback to bind/unbind keyboard events | ||
var self = this; | ||
this._onKeyDown = function(event){ self._onKeyChange(event, true); }; | ||
this._onKeyUp = function(event){ self._onKeyChange(event, false);}; | ||
|
||
// bind keyEvents | ||
document.addEventListener("keydown", this._onKeyDown, false); | ||
document.addEventListener("keyup", this._onKeyUp, false); | ||
} | ||
|
||
/** | ||
* To stop listening of the keyboard events | ||
*/ | ||
THREEx.KeyboardState.prototype.destroy = function() | ||
{ | ||
// unbind keyEvents | ||
document.removeEventListener("keydown", this._onKeyDown, false); | ||
document.removeEventListener("keyup", this._onKeyUp, false); | ||
} | ||
|
||
THREEx.KeyboardState.MODIFIERS = ['shift', 'ctrl', 'alt', 'meta']; | ||
THREEx.KeyboardState.ALIAS = { | ||
'left' : 37, | ||
'up' : 38, | ||
'right' : 39, | ||
'down' : 40, | ||
'space' : 32, | ||
'pageup' : 33, | ||
'pagedown' : 34, | ||
'tab' : 9 | ||
}; | ||
|
||
/** | ||
* to process the keyboard dom event | ||
*/ | ||
THREEx.KeyboardState.prototype._onKeyChange = function(event, pressed) | ||
{ | ||
// log to debug | ||
//console.log("onKeyChange", event, pressed, event.keyCode, event.shiftKey, event.ctrlKey, event.altKey, event.metaKey) | ||
|
||
// update this.keyCodes | ||
var keyCode = event.keyCode; | ||
this.keyCodes[keyCode] = pressed; | ||
|
||
// update this.modifiers | ||
this.modifiers['shift']= event.shiftKey; | ||
this.modifiers['ctrl'] = event.ctrlKey; | ||
this.modifiers['alt'] = event.altKey; | ||
this.modifiers['meta'] = event.metaKey; | ||
} | ||
|
||
/** | ||
* query keyboard state to know if a key is pressed of not | ||
* | ||
* @param {String} keyDesc the description of the key. format : modifiers+key e.g shift+A | ||
* @returns {Boolean} true if the key is pressed, false otherwise | ||
*/ | ||
THREEx.KeyboardState.prototype.pressed = function(keyDesc) | ||
{ | ||
var keys = keyDesc.split("+"); | ||
for(var i = 0; i < keys.length; i++){ | ||
var key = keys[i]; | ||
var pressed; | ||
if( THREEx.KeyboardState.MODIFIERS.indexOf( key ) !== -1 ){ | ||
pressed = this.modifiers[key]; | ||
}else if( Object.keys(THREEx.KeyboardState.ALIAS).indexOf( key ) != -1 ){ | ||
pressed = this.keyCodes[ THREEx.KeyboardState.ALIAS[key] ]; | ||
}else { | ||
pressed = this.keyCodes[key.toUpperCase().charCodeAt(0)] | ||
} | ||
if( !pressed) return false; | ||
}; | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @author alteredq / http://alteredqualia.com/ | ||
* @author mr.doob / http://mrdoob.com/ | ||
*/ | ||
|
||
var WEBGL = { | ||
|
||
isWebGLAvailable: function () { | ||
|
||
try { | ||
|
||
var canvas = document.createElement( 'canvas' ); | ||
return !! ( window.WebGLRenderingContext && ( canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) ) ); | ||
|
||
} catch ( e ) { | ||
|
||
return false; | ||
|
||
} | ||
|
||
}, | ||
|
||
isWebGL2Available: function () { | ||
|
||
try { | ||
|
||
var canvas = document.createElement( 'canvas' ); | ||
return !! ( window.WebGL2RenderingContext && canvas.getContext( 'webgl2' ) ); | ||
|
||
} catch ( e ) { | ||
|
||
return false; | ||
|
||
} | ||
|
||
}, | ||
|
||
getWebGLErrorMessage: function () { | ||
|
||
return this.getErrorMessage( 1 ); | ||
|
||
}, | ||
|
||
getWebGL2ErrorMessage: function () { | ||
|
||
return this.getErrorMessage( 2 ); | ||
|
||
}, | ||
|
||
getErrorMessage: function ( version ) { | ||
|
||
var names = { | ||
1: 'WebGL', | ||
2: 'WebGL 2' | ||
}; | ||
|
||
var contexts = { | ||
1: window.WebGLRenderingContext, | ||
2: window.WebGL2RenderingContext | ||
}; | ||
|
||
var message = 'Your $0 does not seem to support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation" style="color:#000">$1</a>'; | ||
|
||
var element = document.createElement( 'div' ); | ||
element.id = 'webglmessage'; | ||
element.style.fontFamily = 'monospace'; | ||
element.style.fontSize = '13px'; | ||
element.style.fontWeight = 'normal'; | ||
element.style.textAlign = 'center'; | ||
element.style.background = '#fff'; | ||
element.style.color = '#000'; | ||
element.style.padding = '1.5em'; | ||
element.style.width = '400px'; | ||
element.style.margin = '5em auto 0'; | ||
|
||
if ( contexts[ version ] ) { | ||
|
||
message = message.replace( '$0', 'graphics card' ); | ||
|
||
} else { | ||
|
||
message = message.replace( '$0', 'browser' ); | ||
|
||
} | ||
|
||
message = message.replace( '$1', names[ version ] ); | ||
|
||
element.innerHTML = message; | ||
|
||
return element; | ||
|
||
} | ||
|
||
}; |
Oops, something went wrong.