utils.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var Utils = {
  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('.wasm')) {
  11. return execName + '.wasm';
  12. }
  13. }
  14. return rw;
  15. },
  16. createInstantiatePromise: function(wasmLoader) {
  17. function instantiateWasm(imports, onSuccess) {
  18. wasmLoader.then(function(xhr) {
  19. WebAssembly.instantiate(xhr.response, imports).then(function(result) {
  20. onSuccess(result['instance'], result['module']);
  21. });
  22. });
  23. wasmLoader = null;
  24. return {};
  25. };
  26. return instantiateWasm;
  27. },
  28. findCanvas: function() {
  29. var nodes = document.getElementsByTagName('canvas');
  30. if (nodes.length && nodes[0] instanceof HTMLCanvasElement) {
  31. return nodes[0];
  32. }
  33. throw new Error("No canvas found");
  34. },
  35. isWebGLAvailable: function(majorVersion = 1) {
  36. var testContext = false;
  37. try {
  38. var testCanvas = document.createElement('canvas');
  39. if (majorVersion === 1) {
  40. testContext = testCanvas.getContext('webgl') || testCanvas.getContext('experimental-webgl');
  41. } else if (majorVersion === 2) {
  42. testContext = testCanvas.getContext('webgl2') || testCanvas.getContext('experimental-webgl2');
  43. }
  44. } catch (e) {}
  45. return !!testContext;
  46. }
  47. };