Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

utils.ts 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * utils.ts
  20. * Generic utilities
  21. */
  22. import Speedy from 'speedy-vision';
  23. import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
  24. import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
  25. import { AssertionError, IllegalArgumentError } from './errors';
  26. import { Resolution, computeResolution } from './resolution';
  27. /**
  28. * Nullable type
  29. */
  30. export type Nullable<T> = T | null;
  31. /**
  32. * Generic utilities
  33. */
  34. export class Utils
  35. {
  36. /**
  37. * Log a message
  38. * @param message
  39. * @param args optional additional messages
  40. */
  41. static log(message: string, ...args: any[]): void
  42. {
  43. console.log('[encantar-js]', message, ...args);
  44. }
  45. /**
  46. * Display a warning
  47. * @param message
  48. * @param args optional additional messages
  49. */
  50. static warning(message: string, ...args: any[]): void
  51. {
  52. console.warn('[encantar-js]', message, ...args);
  53. }
  54. /**
  55. * Display an error message
  56. * @param message
  57. * @param args optional additional messages
  58. */
  59. static error(message: string, ...args: any[]): void
  60. {
  61. console.error('[encantar-js]', message, ...args);
  62. }
  63. /**
  64. * Assertion
  65. * @param expr expression
  66. * @param errorMessage optional error message
  67. * @throws {AssertionError}
  68. */
  69. static assert(expr: boolean, errorMessage: string = ''): void
  70. {
  71. if(!expr)
  72. throw new AssertionError(errorMessage);
  73. }
  74. /**
  75. * Generate the range [0, 1, ..., n-1]
  76. * @param n non-negative integer
  77. * @returns range from 0 to n-1, inclusive, as a new array
  78. */
  79. static range(n: number): number[]
  80. {
  81. if((n |= 0) < 0)
  82. throw new IllegalArgumentError();
  83. return Array.from({ length: n }, (_, i) => i);
  84. }
  85. /**
  86. * Shuffle an array
  87. * @param arr array to be shuffled in-place
  88. * @returns shuffled arr
  89. */
  90. static shuffle<T>(arr: T[]): T[]
  91. {
  92. // Fisher-Yattes shuffle
  93. for(let i = arr.length - 1; i >= 1; i--) {
  94. const j = Math.floor(Math.random() * (i + 1)); // 0 <= j <= i
  95. const tmp = arr[i];
  96. arr[i] = arr[j];
  97. arr[j] = tmp;
  98. }
  99. return arr;
  100. }
  101. /**
  102. * Wait a few milliseconds
  103. * @param milliseconds how long should we wait?
  104. * @returns a promise that is resolved soon after the specified time
  105. */
  106. static wait(milliseconds: number): SpeedyPromise<void>
  107. {
  108. return new Speedy.Promise<void>(resolve => {
  109. setTimeout(resolve, milliseconds);
  110. });
  111. }
  112. /**
  113. * Run SpeedyPromises sequentially
  114. * @param promises an array of SpeedyPromises
  115. * @returns a promise that is resolved as soon as all input promises are
  116. * resolved, or that is rejected as soon as an input promise is rejected
  117. */
  118. static runInSequence<T>(promises: SpeedyPromise<T>[]): SpeedyPromise<T>
  119. {
  120. return promises.reduce(
  121. (prev, curr) => prev.then(() => curr),
  122. Speedy.Promise.resolve()
  123. );
  124. }
  125. /**
  126. * Convert a resolution type to a resolution measured in pixels
  127. * @param resolution resolution type
  128. * @param aspectRatio width / height ratio
  129. * @returns resolution measured in pixels
  130. */
  131. static resolution(resolution: Resolution, aspectRatio: number): SpeedySize
  132. {
  133. return computeResolution(resolution, aspectRatio);
  134. }
  135. /**
  136. * Returns a string containing platform brand information
  137. * @returns platform brand information
  138. */
  139. static platformString(): string
  140. {
  141. return ((navigator: any): string =>
  142. typeof navigator.userAgentData === 'object' ? // prefer the NavigatorUAData interface
  143. navigator.userAgentData.platform : // use only low entropy data
  144. navigator.platform // navigator.platform is deprecated
  145. )(navigator);
  146. }
  147. /**
  148. * Checks if we're on iOS
  149. * @returns true if we're on iOS
  150. */
  151. static isIOS(): boolean
  152. {
  153. // at the time of this writing, navigator.userAgentData is not yet
  154. // compatible with Safari. navigator.platform is deprecated, but
  155. // predictable.
  156. if(/(iOS|iPhone|iPad|iPod)/i.test(navigator.platform))
  157. return true;
  158. if(/Mac/i.test(navigator.platform) && navigator.maxTouchPoints !== undefined) // iPad OS 13+
  159. return navigator.maxTouchPoints > 2;
  160. return false;
  161. }
  162. /**
  163. * Checks if we're on a WebKit-based browser
  164. * @returns true if we're on a WebKit-based browser
  165. */
  166. static isWebKit(): boolean
  167. {
  168. // note: navigator.vendor is deprecated
  169. if(/Apple/.test(navigator.vendor))
  170. return true;
  171. // Can a non WebKit-based browser pass this test?
  172. // Test masked GL_RENDERER == "Apple GPU" (valid since Feb 2020)
  173. // https://bugs.webkit.org/show_bug.cgi?id=207608
  174. /*
  175. if(Speedy.Platform.renderer == 'Apple GPU' && Speedy.Platform.vendor == 'Apple Inc.')
  176. return true;
  177. */
  178. // Desktop and Mobile Safari, Epiphany on Linux
  179. if(/AppleWebKit\/.* Version\//.test(navigator.userAgent))
  180. return true;
  181. // Chrome, Firefox, Edge on iOS
  182. if(/(CriOS\/|FxiOS\/|EdgiOS\/)/.test(navigator.userAgent))
  183. return true;
  184. // not WebKit
  185. return false;
  186. }
  187. /**
  188. * Device-specific information for debugging purposes
  189. */
  190. static deviceInfo(): string
  191. {
  192. return 'Device info: ' + JSON.stringify({
  193. isIOS: Utils.isIOS(),
  194. isWebKit: Utils.isWebKit(),
  195. renderer: Speedy.Platform.renderer,
  196. vendor: Speedy.Platform.vendor,
  197. screen: [screen.width, screen.height].join('x'),
  198. platform: [navigator.platform, navigator.vendor].join('; '),
  199. userAgent: navigator.userAgent,
  200. userAgentData: (navigator as any).userAgentData || null,
  201. }, null, 2);
  202. }
  203. }