ARButton.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. class ARButton {
  2. static createButton( renderer, sessionInit = {} ) {
  3. const button = document.createElement( 'button' );
  4. function showStartAR( /*device*/ ) {
  5. if ( sessionInit.domOverlay === undefined ) {
  6. const overlay = document.createElement( 'div' );
  7. overlay.style.display = 'none';
  8. document.body.appendChild( overlay );
  9. const svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' );
  10. svg.setAttribute( 'width', 38 );
  11. svg.setAttribute( 'height', 38 );
  12. svg.style.position = 'absolute';
  13. svg.style.right = '20px';
  14. svg.style.top = '20px';
  15. svg.addEventListener( 'click', function () {
  16. currentSession.end();
  17. } );
  18. overlay.appendChild( svg );
  19. const path = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  20. path.setAttribute( 'd', 'M 12,12 L 28,28 M 28,12 12,28' );
  21. path.setAttribute( 'stroke', '#fff' );
  22. path.setAttribute( 'stroke-width', 2 );
  23. svg.appendChild( path );
  24. if ( sessionInit.optionalFeatures === undefined ) {
  25. sessionInit.optionalFeatures = [];
  26. }
  27. sessionInit.optionalFeatures.push( 'dom-overlay' );
  28. sessionInit.domOverlay = { root: overlay };
  29. }
  30. //
  31. let currentSession = null;
  32. async function onSessionStarted( session ) {
  33. session.addEventListener( 'end', onSessionEnded );
  34. renderer.xr.setReferenceSpaceType( 'local' );
  35. await renderer.xr.setSession( session );
  36. button.textContent = 'STOP AR';
  37. sessionInit.domOverlay.root.style.display = '';
  38. currentSession = session;
  39. }
  40. function onSessionEnded( /*event*/ ) {
  41. currentSession.removeEventListener( 'end', onSessionEnded );
  42. button.textContent = 'START AR';
  43. sessionInit.domOverlay.root.style.display = 'none';
  44. currentSession = null;
  45. }
  46. //
  47. button.style.display = '';
  48. button.style.cursor = 'pointer';
  49. button.style.left = 'calc(50% - 50px)';
  50. button.style.width = '100px';
  51. button.textContent = 'START AR';
  52. button.onmouseenter = function () {
  53. button.style.opacity = '1.0';
  54. };
  55. button.onmouseleave = function () {
  56. button.style.opacity = '0.5';
  57. };
  58. button.onclick = function () {
  59. if ( currentSession === null ) {
  60. navigator.xr.requestSession( 'immersive-ar', sessionInit ).then( onSessionStarted );
  61. } else {
  62. currentSession.end();
  63. if ( navigator.xr.offerSession !== undefined ) {
  64. navigator.xr.offerSession( 'immersive-ar', sessionInit )
  65. .then( onSessionStarted );
  66. }
  67. }
  68. };
  69. if ( navigator.xr.offerSession !== undefined ) {
  70. navigator.xr.offerSession( 'immersive-ar', sessionInit )
  71. .then( onSessionStarted );
  72. }
  73. }
  74. function disableButton() {
  75. button.style.display = '';
  76. button.style.cursor = 'auto';
  77. button.style.left = 'calc(50% - 75px)';
  78. button.style.width = '150px';
  79. button.onmouseenter = null;
  80. button.onmouseleave = null;
  81. button.onclick = null;
  82. }
  83. function showARNotSupported() {
  84. disableButton();
  85. button.textContent = 'AR NOT SUPPORTED';
  86. }
  87. function showARNotAllowed( exception ) {
  88. disableButton();
  89. console.warn( 'Exception when trying to call xr.isSessionSupported', exception );
  90. button.textContent = 'AR NOT ALLOWED';
  91. }
  92. function stylizeElement( element ) {
  93. element.style.position = 'absolute';
  94. element.style.bottom = '20px';
  95. element.style.padding = '12px 6px';
  96. element.style.border = '1px solid #fff';
  97. element.style.borderRadius = '4px';
  98. element.style.background = 'rgba(0,0,0,0.1)';
  99. element.style.color = '#fff';
  100. element.style.font = 'normal 13px sans-serif';
  101. element.style.textAlign = 'center';
  102. element.style.opacity = '0.5';
  103. element.style.outline = 'none';
  104. element.style.zIndex = '999';
  105. }
  106. if ( 'xr' in navigator ) {
  107. button.id = 'ARButton';
  108. button.style.display = 'none';
  109. stylizeElement( button );
  110. navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
  111. supported ? showStartAR() : showARNotSupported();
  112. } ).catch( showARNotAllowed );
  113. return button;
  114. } else {
  115. const message = document.createElement( 'a' );
  116. if ( window.isSecureContext === false ) {
  117. message.href = document.location.href.replace( /^http:/, 'https:' );
  118. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  119. } else {
  120. message.href = 'https://immersiveweb.dev/';
  121. message.innerHTML = 'WEBXR NOT AVAILABLE';
  122. }
  123. message.style.left = 'calc(50% - 90px)';
  124. message.style.width = '180px';
  125. message.style.textDecoration = 'none';
  126. stylizeElement( message );
  127. return message;
  128. }
  129. }
  130. }
  131. export { ARButton };