Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

fullscreen-button.ts 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * @param parent parent node
  55. * @param isVisible
  56. */
  57. init(parent: Node, isVisible: boolean): void
  58. {
  59. parent.appendChild(this._button);
  60. this._button.hidden = !isVisible;
  61. this._viewport.addEventListener('fullscreenchange', this._boundEventHandler);
  62. }
  63. /**
  64. * Release
  65. */
  66. release(): void
  67. {
  68. this._viewport.removeEventListener('fullscreenchange', this._boundEventHandler);
  69. this._button.remove();
  70. }
  71. /**
  72. * Create the <button> element
  73. */
  74. private _createButton(): HTMLButtonElement
  75. {
  76. const button = document.createElement('button');
  77. button.style.position = 'absolute';
  78. button.style.bottom = BUTTON_MARGIN + 'px';
  79. button.style.right = BUTTON_MARGIN + 'px';
  80. button.style.width = BUTTON_SIZE + 'px';
  81. button.style.height = BUTTON_SIZE + 'px';
  82. button.style.opacity = '0.5';
  83. button.style.zIndex = '1000000';
  84. button.style.cursor = 'pointer';
  85. button.style.outline = 'none';
  86. (button.style as any)['-webkit-tap-highlight-color'] = 'transparent';
  87. button.draggable = false;
  88. button.style.backgroundColor = 'transparent';
  89. button.style.backgroundImage = 'url(' + BUTTON_ICON_OFF + ')';
  90. button.style.backgroundSize = 'cover';
  91. button.style.imageRendering = 'pixelated';
  92. button.style.borderColor = 'white';
  93. button.style.borderStyle = 'solid';
  94. button.style.borderWidth = '2px';
  95. button.style.borderRadius = '8px';
  96. const highlight = () => {
  97. button.style.backgroundColor = '#ffd500';
  98. button.style.borderColor = '#ffd500';
  99. button.style.opacity = '1.0';
  100. };
  101. const dehighlight = () => {
  102. button.style.backgroundColor = 'transparent';
  103. button.style.borderColor = 'white';
  104. button.style.opacity = '0.5';
  105. };
  106. button.addEventListener('pointerdown', highlight);
  107. button.addEventListener('pointerup', dehighlight);
  108. button.addEventListener('pointerleave', dehighlight);
  109. button.addEventListener('click', () => {
  110. if(!this._viewport.fullscreen) {
  111. this._viewport.requestFullscreen().catch(err => {
  112. alert(`Can't enable the fullscreen mode. ` + err.toString());
  113. });
  114. }
  115. else {
  116. this._viewport.exitFullscreen();
  117. }
  118. });
  119. return button;
  120. }
  121. /**
  122. * Handle a fullscreenchange event
  123. */
  124. private _handleFullscreenEvent(event: Event): void
  125. {
  126. const img = this._viewport.fullscreen ? BUTTON_ICON_ON : BUTTON_ICON_OFF;
  127. this._button.style.backgroundImage = 'url(' + img + ')';
  128. }
  129. }