ARButton.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. .catch( ( err ) => {
  67. console.warn( err );
  68. } );
  69. }
  70. }
  71. };
  72. if ( navigator.xr.offerSession !== undefined ) {
  73. navigator.xr.offerSession( 'immersive-ar', sessionInit )
  74. .then( onSessionStarted )
  75. .catch( ( err ) => {
  76. console.warn( err );
  77. } );
  78. }
  79. }
  80. function disableButton() {
  81. button.style.display = '';
  82. button.style.cursor = 'auto';
  83. button.style.left = 'calc(50% - 75px)';
  84. button.style.width = '150px';
  85. button.onmouseenter = null;
  86. button.onmouseleave = null;
  87. button.onclick = null;
  88. }
  89. function showARNotSupported() {
  90. disableButton();
  91. button.textContent = 'AR NOT SUPPORTED';
  92. }
  93. function showARNotAllowed( exception ) {
  94. disableButton();
  95. console.warn( 'Exception when trying to call xr.isSessionSupported', exception );
  96. button.textContent = 'AR NOT ALLOWED';
  97. }
  98. function stylizeElement( element ) {
  99. element.style.position = 'absolute';
  100. element.style.bottom = '20px';
  101. element.style.padding = '12px 6px';
  102. element.style.border = '1px solid #fff';
  103. element.style.borderRadius = '4px';
  104. element.style.background = 'rgba(0,0,0,0.1)';
  105. element.style.color = '#fff';
  106. element.style.font = 'normal 13px sans-serif';
  107. element.style.textAlign = 'center';
  108. element.style.opacity = '0.5';
  109. element.style.outline = 'none';
  110. element.style.zIndex = '999';
  111. }
  112. if ( 'xr' in navigator ) {
  113. button.id = 'ARButton';
  114. button.style.display = 'none';
  115. stylizeElement( button );
  116. navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
  117. supported ? showStartAR() : showARNotSupported();
  118. } ).catch( showARNotAllowed );
  119. return button;
  120. } else {
  121. const message = document.createElement( 'a' );
  122. if ( window.isSecureContext === false ) {
  123. message.href = document.location.href.replace( /^http:/, 'https:' );
  124. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  125. } else {
  126. message.href = 'https://immersiveweb.dev/';
  127. message.innerHTML = 'WEBXR NOT AVAILABLE';
  128. }
  129. message.style.left = 'calc(50% - 90px)';
  130. message.style.width = '180px';
  131. message.style.textDecoration = 'none';
  132. stylizeElement( message );
  133. return message;
  134. }
  135. }
  136. }
  137. export { ARButton };