WebVR.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. * Based on @tojiro's vr-samples-utils.js
  4. */
  5. var WEBVR = {
  6. isLatestAvailable: function () {
  7. console.warn( 'WEBVR: isLatestAvailable() is being deprecated. Use .isAvailable() instead.' );
  8. return this.isAvailable();
  9. },
  10. isAvailable: function () {
  11. return navigator.getVRDisplays !== undefined;
  12. },
  13. getVRDisplay: function ( onDisplay ) {
  14. if ( 'getVRDisplays' in navigator ) {
  15. navigator.getVRDisplays()
  16. .then( function ( displays ) {
  17. onDisplay( displays[ 0 ] );
  18. } )
  19. .catch( function () {
  20. // no displays
  21. } );
  22. }
  23. },
  24. getMessage: function () {
  25. var message;
  26. if ( navigator.getVRDisplays ) {
  27. navigator.getVRDisplays().then( function ( displays ) {
  28. if ( displays.length === 0 ) message = 'WebVR supported, but no VRDisplays found.';
  29. } );
  30. } else {
  31. message = 'Your browser does not support WebVR. See <a href="http://webvr.info">webvr.info</a> for assistance.';
  32. }
  33. if ( message !== undefined ) {
  34. var container = document.createElement( 'div' );
  35. container.style.position = 'absolute';
  36. container.style.left = '0';
  37. container.style.top = '0';
  38. container.style.right = '0';
  39. container.style.zIndex = '999';
  40. container.align = 'center';
  41. var error = document.createElement( 'div' );
  42. error.style.fontFamily = 'sans-serif';
  43. error.style.fontSize = '16px';
  44. error.style.fontStyle = 'normal';
  45. error.style.lineHeight = '26px';
  46. error.style.backgroundColor = '#fff';
  47. error.style.color = '#000';
  48. error.style.padding = '10px 20px';
  49. error.style.margin = '50px';
  50. error.style.display = 'inline-block';
  51. error.innerHTML = message;
  52. container.appendChild( error );
  53. return container;
  54. }
  55. },
  56. getButton: function ( display, canvas ) {
  57. /*
  58. if ( display instanceof VRDisplay === false ) {
  59. console.error( 'WebVR.getButton() now expects a VRDisplay.' );
  60. return;
  61. }
  62. */
  63. var button = document.createElement( 'button' );
  64. button.style.position = 'absolute';
  65. button.style.left = 'calc(50% - 50px)';
  66. button.style.bottom = '20px';
  67. button.style.width = '100px';
  68. button.style.border = '0';
  69. button.style.padding = '8px';
  70. button.style.cursor = 'pointer';
  71. button.style.backgroundColor = '#000';
  72. button.style.color = '#fff';
  73. button.style.fontFamily = 'sans-serif';
  74. button.style.fontSize = '13px';
  75. button.style.fontStyle = 'normal';
  76. button.style.textAlign = 'center';
  77. button.style.zIndex = '999';
  78. button.textContent = 'ENTER VR';
  79. button.onclick = function () {
  80. display.isPresenting ? display.exitPresent() : display.requestPresent( [ { source: canvas } ] );
  81. };
  82. window.addEventListener( 'vrdisplaypresentchange', function () {
  83. button.textContent = display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  84. }, false );
  85. return button;
  86. }
  87. };