0
0

ARButton.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. class ARButton {
  2. static createButton( renderer, sessionInit = {} ) {
  3. const button = document.createElement( 'button' );
  4. function showStartAR( /*device*/ ) {
  5. if ( sessionInit.domOverlay === undefined ) {
  6. var overlay = document.createElement( 'div' );
  7. overlay.style.display = 'none';
  8. document.body.appendChild( overlay );
  9. var 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. var 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. }
  64. };
  65. }
  66. function disableButton() {
  67. button.style.display = '';
  68. button.style.cursor = 'auto';
  69. button.style.left = 'calc(50% - 75px)';
  70. button.style.width = '150px';
  71. button.onmouseenter = null;
  72. button.onmouseleave = null;
  73. button.onclick = null;
  74. }
  75. function showARNotSupported() {
  76. disableButton();
  77. button.textContent = 'AR NOT SUPPORTED';
  78. }
  79. function stylizeElement( element ) {
  80. element.style.position = 'absolute';
  81. element.style.bottom = '20px';
  82. element.style.padding = '12px 6px';
  83. element.style.border = '1px solid #fff';
  84. element.style.borderRadius = '4px';
  85. element.style.background = 'rgba(0,0,0,0.1)';
  86. element.style.color = '#fff';
  87. element.style.font = 'normal 13px sans-serif';
  88. element.style.textAlign = 'center';
  89. element.style.opacity = '0.5';
  90. element.style.outline = 'none';
  91. element.style.zIndex = '999';
  92. }
  93. if ( 'xr' in navigator ) {
  94. button.id = 'ARButton';
  95. button.style.display = 'none';
  96. stylizeElement( button );
  97. navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
  98. supported ? showStartAR() : showARNotSupported();
  99. } ).catch( showARNotSupported );
  100. return button;
  101. } else {
  102. const message = document.createElement( 'a' );
  103. if ( window.isSecureContext === false ) {
  104. message.href = document.location.href.replace( /^http:/, 'https:' );
  105. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  106. } else {
  107. message.href = 'https://immersiveweb.dev/';
  108. message.innerHTML = 'WEBXR NOT AVAILABLE';
  109. }
  110. message.style.left = 'calc(50% - 90px)';
  111. message.style.width = '180px';
  112. message.style.textDecoration = 'none';
  113. stylizeElement( message );
  114. return message;
  115. }
  116. }
  117. }
  118. export { ARButton };