utils.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const Utils = { // eslint-disable-line no-unused-vars
  2. createLocateRewrite: function (execName) {
  3. function rw(path) {
  4. if (path.endsWith('.worker.js')) {
  5. return `${execName}.worker.js`;
  6. } else if (path.endsWith('.audio.worklet.js')) {
  7. return `${execName}.audio.worklet.js`;
  8. } else if (path.endsWith('.js')) {
  9. return `${execName}.js`;
  10. } else if (path.endsWith('.side.wasm')) {
  11. return `${execName}.side.wasm`;
  12. } else if (path.endsWith('.wasm')) {
  13. return `${execName}.wasm`;
  14. }
  15. return path;
  16. }
  17. return rw;
  18. },
  19. createInstantiatePromise: function (wasmLoader) {
  20. let loader = wasmLoader;
  21. function instantiateWasm(imports, onSuccess) {
  22. loader.then(function (xhr) {
  23. WebAssembly.instantiate(xhr.response, imports).then(function (result) {
  24. onSuccess(result['instance'], result['module']);
  25. });
  26. });
  27. loader = null;
  28. return {};
  29. }
  30. return instantiateWasm;
  31. },
  32. findCanvas: function () {
  33. const nodes = document.getElementsByTagName('canvas');
  34. if (nodes.length && nodes[0] instanceof HTMLCanvasElement) {
  35. return nodes[0];
  36. }
  37. return null;
  38. },
  39. isWebGLAvailable: function (majorVersion = 1) {
  40. let testContext = false;
  41. try {
  42. const testCanvas = document.createElement('canvas');
  43. if (majorVersion === 1) {
  44. testContext = testCanvas.getContext('webgl') || testCanvas.getContext('experimental-webgl');
  45. } else if (majorVersion === 2) {
  46. testContext = testCanvas.getContext('webgl2') || testCanvas.getContext('experimental-webgl2');
  47. }
  48. } catch (e) {
  49. // Not available
  50. }
  51. return !!testContext;
  52. },
  53. };