WebVR.js 2.5 KB

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