ARButton.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. class ARButton {
  2. static createButton( renderer, sessionInit = {} ) {
  3. const button = document.createElement( 'button' );
  4. function showStartAR( /*device*/ ) {
  5. let currentSession = null;
  6. function onSessionStarted( session ) {
  7. session.addEventListener( 'end', onSessionEnded );
  8. renderer.xr.setReferenceSpaceType( 'local' );
  9. renderer.xr.setSession( session );
  10. button.textContent = 'STOP AR';
  11. currentSession = session;
  12. }
  13. function onSessionEnded( /*event*/ ) {
  14. currentSession.removeEventListener( 'end', onSessionEnded );
  15. button.textContent = 'START AR';
  16. currentSession = null;
  17. }
  18. //
  19. button.style.display = '';
  20. button.style.cursor = 'pointer';
  21. button.style.left = 'calc(50% - 50px)';
  22. button.style.width = '100px';
  23. button.textContent = 'START AR';
  24. button.onmouseenter = function () {
  25. button.style.opacity = '1.0';
  26. };
  27. button.onmouseleave = function () {
  28. button.style.opacity = '0.5';
  29. };
  30. button.onclick = function () {
  31. if ( currentSession === null ) {
  32. navigator.xr.requestSession( 'immersive-ar', sessionInit ).then( onSessionStarted );
  33. } else {
  34. currentSession.end();
  35. }
  36. };
  37. }
  38. function disableButton() {
  39. button.style.display = '';
  40. button.style.cursor = 'auto';
  41. button.style.left = 'calc(50% - 75px)';
  42. button.style.width = '150px';
  43. button.onmouseenter = null;
  44. button.onmouseleave = null;
  45. button.onclick = null;
  46. }
  47. function showARNotSupported() {
  48. disableButton();
  49. button.textContent = 'AR NOT SUPPORTED';
  50. }
  51. function stylizeElement( element ) {
  52. element.style.position = 'absolute';
  53. element.style.bottom = '20px';
  54. element.style.padding = '12px 6px';
  55. element.style.border = '1px solid #fff';
  56. element.style.borderRadius = '4px';
  57. element.style.background = 'rgba(0,0,0,0.1)';
  58. element.style.color = '#fff';
  59. element.style.font = 'normal 13px sans-serif';
  60. element.style.textAlign = 'center';
  61. element.style.opacity = '0.5';
  62. element.style.outline = 'none';
  63. element.style.zIndex = '999';
  64. }
  65. if ( 'xr' in navigator ) {
  66. button.id = 'ARButton';
  67. button.style.display = 'none';
  68. stylizeElement( button );
  69. navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
  70. supported ? showStartAR() : showARNotSupported();
  71. } ).catch( showARNotSupported );
  72. return button;
  73. } else {
  74. const message = document.createElement( 'a' );
  75. if ( window.isSecureContext === false ) {
  76. message.href = document.location.href.replace( /^http:/, 'https:' );
  77. message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
  78. } else {
  79. message.href = 'https://immersiveweb.dev/';
  80. message.innerHTML = 'WEBXR NOT AVAILABLE';
  81. }
  82. message.style.left = 'calc(50% - 90px)';
  83. message.style.width = '180px';
  84. message.style.textDecoration = 'none';
  85. stylizeElement( message );
  86. return message;
  87. }
  88. }
  89. }
  90. export { ARButton };