您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

fullscreen-button.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * fullscreen-button.ts
  20. * A built-in fullscreen button introduced as a convenience
  21. */
  22. import { Viewport } from '../core/viewport';
  23. /** Button icon to be displayed when the fullscreen mode is disabled */
  24. const BUTTON_ICON_OFF = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAbUlEQVRYR+2WOQ4AIAgE5f+PVhobDZANBZAsraAwXMoqFil+f9GBj8BW8dIiKt45at/XgShStHgvmfdekwAdIIEyAmh1Z/U5ikmABPoRsLZWtt+5DUlgHgGr6qM1Pf9XnO131L7fJEQjyOqXEzjP1YAhNmUTrgAAAABJRU5ErkJggg==';
  25. /** Button icon to be displayed when the fullscreen mode is enabled */
  26. const BUTTON_ICON_ON = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAZElEQVRYR+2WwRIAEAhE9f8fTQ5OhtkLxbzOyc5rJSvBYcH3FwTIBKpHb5d57Nqm5o0aCIBAPgLDxSunq69APT8RCBdwezTLHjglDAEQgEC+QZR2EqqbjprHRgSB9wjwHX9LoAHP1YAhXF4Z/QAAAABJRU5ErkJggg==';
  27. /** Button size, in pixels */
  28. const BUTTON_SIZE = 64;
  29. /** Button margin, in pixels */
  30. const BUTTON_MARGIN = 24;
  31. /**
  32. * Built-in fullscreen button
  33. */
  34. export class FullscreenButton
  35. {
  36. /** The viewport associated to this panel */
  37. private readonly _viewport: Viewport;
  38. /** The HTML element of the button */
  39. private readonly _button: HTMLButtonElement;
  40. /** Bound event handler */
  41. private readonly _boundEventHandler: EventListener;
  42. /**
  43. * Constructor
  44. * @param viewport Viewport
  45. */
  46. constructor(viewport: Viewport)
  47. {
  48. this._viewport = viewport;
  49. this._button = this._createButton();
  50. this._boundEventHandler = this._handleFullscreenEvent.bind(this);
  51. }
  52. /**
  53. * Initialize
  54. */
  55. init(): void
  56. {
  57. this._viewport.hud.container.appendChild(this._button);
  58. this._viewport.addEventListener('fullscreenchange', this._boundEventHandler);
  59. }
  60. /**
  61. * Release
  62. */
  63. release(): void
  64. {
  65. this._viewport.removeEventListener('fullscreenchange', this._boundEventHandler);
  66. this._button.remove();
  67. }
  68. /**
  69. * Create the <button> element
  70. */
  71. private _createButton(): HTMLButtonElement
  72. {
  73. const button = document.createElement('button');
  74. button.style.position = 'absolute';
  75. button.style.bottom = BUTTON_MARGIN + 'px';
  76. button.style.right = BUTTON_MARGIN + 'px';
  77. button.style.width = BUTTON_SIZE + 'px';
  78. button.style.height = BUTTON_SIZE + 'px';
  79. button.style.opacity = '0.5';
  80. button.style.cursor = 'pointer';
  81. button.style.outline = 'none';
  82. (button.style as any)['-webkit-tap-highlight-color'] = 'transparent';
  83. button.draggable = false;
  84. button.style.backgroundColor = 'transparent';
  85. button.style.backgroundImage = 'url(' + BUTTON_ICON_OFF + ')';
  86. button.style.backgroundSize = 'cover';
  87. button.style.imageRendering = 'pixelated';
  88. button.style.borderColor = 'white';
  89. button.style.borderStyle = 'solid';
  90. button.style.borderWidth = '2px';
  91. button.style.borderRadius = '8px';
  92. const highlight = () => {
  93. button.style.backgroundColor = '#ffd500';
  94. button.style.borderColor = '#ffd500';
  95. button.style.opacity = '1.0';
  96. };
  97. const dehighlight = () => {
  98. button.style.backgroundColor = 'transparent';
  99. button.style.borderColor = 'white';
  100. button.style.opacity = '0.5';
  101. };
  102. button.addEventListener('pointerdown', highlight);
  103. button.addEventListener('pointerup', dehighlight);
  104. button.addEventListener('pointerleave', dehighlight);
  105. button.addEventListener('click', () => {
  106. if(!this._viewport.fullscreen) {
  107. this._viewport.requestFullscreen().catch(err => {
  108. alert(`Can't enable the fullscreen mode. ` + err.toString());
  109. });
  110. }
  111. else {
  112. this._viewport.exitFullscreen();
  113. }
  114. });
  115. return button;
  116. }
  117. /**
  118. * Handle a fullscreenchange event
  119. */
  120. private _handleFullscreenEvent(event: Event): void
  121. {
  122. const img = this._viewport.fullscreen ? BUTTON_ICON_ON : BUTTON_ICON_OFF;
  123. this._button.style.backgroundImage = 'url(' + img + ')';
  124. }
  125. }