Keyboard movement and mouse looking
In order to move our camera around, we're going to encapsulate some state, so let's define a KeyboardControls class in a new JavaScript file:
function KeyboardControls(object, options) {
this.object = object;
options = options || {};
this.domElement = options.domElement || document;
this.moveSpeed = options.moveSpeed || 1;
this.domElement.addEventListener('keydown', this.onKeyDown.bind(this), false);
this.domElement.addEventListener('keyup', this.onKeyUp.bind(this), false);
}
KeyboardControls.prototype = {
update: function() {
if (this.moveForward) this.object.translateZ(-this.moveSpeed);
if (this.moveBackward) this.object.translateZ( this.moveSpeed);
if (this.moveLeft) this.object.translateX(-this.moveSpeed);
if (this.moveRight) this.object.translateX( this.moveSpeed);
},
onKeyDown: function (event) {
switch (event.keyCode) {
case 38: /*up*/
case 87: /*W*/ this.moveForward = true; break;
...