DeviceOrientationControls.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @author richt / http://richt.me
  3. *
  4. * W3C Device Orientation control (http://www.w3.org/TR/orientation-event/)
  5. */
  6. THREE.DeviceOrientationControls = function ( object ) {
  7. this.object = object;
  8. this.degtorad = Math.PI / 180;
  9. this.freeze = true;
  10. this.orientationData = {};
  11. this.eulerRotationOrder = "ZXY";
  12. this.registerOrientationData = function( data ) {
  13. this.freeze = false;
  14. this.orientationData = data;
  15. };
  16. this.update = function( delta ) {
  17. if ( this.freeze ) {
  18. return;
  19. }
  20. this.object.rotation.set(
  21. (this.orientationData['beta'] || 0) * this.degtorad,
  22. (this.orientationData['gamma'] || 0) * this.degtorad,
  23. (this.orientationData['alpha'] || 0) * this.degtorad,
  24. this.eulerRotationOrder
  25. );
  26. };
  27. function bind( scope, fn ) {
  28. return function () {
  29. fn.apply( scope, arguments );
  30. };
  31. };
  32. this.connect = function() {
  33. window.addEventListener('deviceorientation', bind(this, this.registerOrientationData), false);
  34. };
  35. this.disconnect = function() {
  36. window.removeEventListener('deviceorientation', bind(this, this.registerOrientationData), false);
  37. this.freeze = true;
  38. };
  39. };