ARButton.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. sessionInit.optionalFeatures = [ 'dom-overlay' ];
  25. sessionInit.domOverlay = { root: overlay };
  26. }
  27. //
  28. let currentSession = null;
  29. async function onSessionStarted( session ) {
  30. session.addEventListener( 'end', onSessionEnded );
  31. renderer.xr.setReferenceSpaceType( 'local' );
  32. await renderer.xr.setSession( session );
  33. button.textContent = 'STOP AR';
  34. sessionInit.domOverlay.root.style.display = '';
  35. currentSession = session;
  36. }
  37. function onSessionEnded( /*event*/ ) {
  38. currentSession.removeEventListener( 'end', onSessionEnded );
  39. button.textContent = 'START AR';
  40. sessionInit.domOverlay.root.style.display = 'none';
  41. currentSession = null;
  42. }
  43. //
  44. button.style.display = '';
  45. button.style.cursor = 'pointer';
  46. button.style.left = 'calc(50% - 50px)';
  47. button.style.width = '100px';
  48. button.textContent = 'START AR';
  49. button.onmouseenter = function () {
  50. button.style.opacity = '1.0';
  51. };
  52. button.onmouseleave = function () {
  53. button.style.opacity = '0.5';
  54. };
  55. button.onclick = function () {
  56. if ( currentSession === null ) {
  57. navigator.xr.requestSession( 'immersive-ar', sessionInit ).then( onSessionStarted );
  58. } else {
  59. currentSession.end();
  60. }
  61. };
  62. }
  63. function disableButton() {
  64. button.style.display = '';
  65. button.style.cursor = 'auto';
  66. button.style.left = 'calc(50% - 75px)';
  67. button.style.width = '150px';
  68. button.onmouseenter = null;
  69. button.onmouseleave = null;
  70. button.onclick = null;
  71. }
  72. function showARNotSupported() {
  73. disableButton();
  74. button.textContent = 'AR NOT SUPPORTED';
  75. }
  76. function stylizeElement( element ) {
  77. element.style.position = 'absolute';
  78. element.style.bottom = '20px';
  79. element.style.padding = '12px 6px';
  80. element.style.border = '1px solid #fff';
  81. element.style.borderRadius = '4px';
  82. element.style.background = 'rgba(0,0,0,0.1)';
  83. element.style.color = '#fff';
  84. element.style.font = 'normal 13px sans-serif';
  85. element.style.textAlign = 'center';
  86. element.style.opacity = '0.5';
  87. element.style.outline = 'none';
  88. element.style.zIndex = '999';
  89. }
  90. if ( 'xr' in navigator ) {
  91. button.id = 'ARButton';
  92. button.style.display = 'none';
  93. stylizeElement( button );
  94. navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
  95. supported ? showStartAR() : showARNotSupported();
  96. } ).catch( showARNotSupported );
  97. return button;
  98. } else {
  99. const message = document.createElement( 'a' );
  100. if ( window.isSecureContext === false ) {
  101. message.href = document.location.href.replace( /^http:/, 'https:' );
  102. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  103. } else {
  104. message.href = 'https://immersiveweb.dev/';
  105. message.innerHTML = 'WEBXR NOT AVAILABLE';
  106. }
  107. message.style.left = 'calc(50% - 90px)';
  108. message.style.width = '180px';
  109. message.style.textDecoration = 'none';
  110. stylizeElement( message );
  111. return message;
  112. }
  113. }
  114. }
  115. export { ARButton };