utils.js 1.3 KB

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