MouseControls.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @author dmarcos / http://github.com/dmarcos
  3. *
  4. * This controls allow to change the orientation of the camera using the mouse
  5. */
  6. THREE.MouseControls = function ( object ) {
  7. var scope = this;
  8. var PI_2 = Math.PI / 2;
  9. var mouseQuat = {
  10. x: new THREE.Quaternion(),
  11. y: new THREE.Quaternion()
  12. };
  13. var object = object;
  14. var xVector = new THREE.Vector3( 1, 0, 0 );
  15. var yVector = new THREE.Vector3( 0, 1, 0 );
  16. var onMouseMove = function ( event ) {
  17. if ( scope.enabled === false ) return;
  18. var orientation = scope.orientation;
  19. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  20. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  21. orientation.y += movementX * 0.0025;
  22. orientation.x += movementY * 0.0025;
  23. orientation.x = Math.max( - PI_2, Math.min( PI_2, orientation.x ) );
  24. };
  25. this.enabled = true;
  26. this.orientation = {
  27. x: 0,
  28. y: 0,
  29. };
  30. this.update = function() {
  31. if ( this.enabled === false ) return;
  32. mouseQuat.x.setFromAxisAngle( xVector, this.orientation.x );
  33. mouseQuat.y.setFromAxisAngle( yVector, this.orientation.y );
  34. object.quaternion.copy( mouseQuat.y ).multiply( mouseQuat.x );
  35. return;
  36. };
  37. this.dispose = function() {
  38. document.removeEventListener( 'mousemove', onMouseMove, false );
  39. }
  40. document.addEventListener( 'mousemove', onMouseMove, false );
  41. };