index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. System.register(["./application.js"], function (_export, _context) {
  2. "use strict";
  3. var createApplication, canvas, $p, bcr;
  4. function loadJsListFile(url) {
  5. return new Promise(function (resolve, reject) {
  6. var err;
  7. function windowErrorListener(evt) {
  8. if (evt.filename === url) {
  9. err = evt.error;
  10. }
  11. }
  12. window.addEventListener('error', windowErrorListener);
  13. var script = document.createElement('script');
  14. script.charset = 'utf-8';
  15. script.async = true;
  16. script.crossOrigin = 'anonymous';
  17. script.addEventListener('error', function () {
  18. window.removeEventListener('error', windowErrorListener);
  19. reject(Error('Error loading ' + url));
  20. });
  21. script.addEventListener('load', function () {
  22. window.removeEventListener('error', windowErrorListener);
  23. document.head.removeChild(script); // Note that if an error occurs that isn't caught by this if statement,
  24. // that getRegister will return null and a "did not instantiate" error will be thrown.
  25. if (err) {
  26. reject(err);
  27. } else {
  28. resolve();
  29. }
  30. });
  31. script.src = url;
  32. document.head.appendChild(script);
  33. });
  34. }
  35. function fetchWasm(url) {
  36. return fetch(url).then(function (response) {
  37. return response.arrayBuffer();
  38. });
  39. }
  40. function findCanvas() {
  41. // Use canvas in outer context
  42. if (!canvas || canvas.tagName !== 'CANVAS') {
  43. console.error("unknown canvas id:", el);
  44. }
  45. var width = canvas.width;
  46. var height = canvas.height;
  47. var container = document.createElement('div');
  48. if (canvas && canvas.parentNode) {
  49. canvas.parentNode.insertBefore(container, canvas);
  50. }
  51. container.setAttribute('id', 'Cocos3dGameContainer');
  52. container.appendChild(canvas);
  53. var frame = container.parentNode === document.body ? document.documentElement : container.parentNode;
  54. addClass(canvas, 'gameCanvas');
  55. canvas.setAttribute('width', width || '480');
  56. canvas.setAttribute('height', height || '320');
  57. canvas.setAttribute('tabindex', '99');
  58. return {
  59. frame: frame,
  60. canvas: canvas,
  61. container: container
  62. };
  63. }
  64. function addClass(element, name) {
  65. var hasClass = (' ' + element.className + ' ').indexOf(' ' + name + ' ') > -1;
  66. if (!hasClass) {
  67. if (element.className) {
  68. element.className += ' ';
  69. }
  70. element.className += name;
  71. }
  72. }
  73. return {
  74. setters: [function (_applicationJs) {
  75. createApplication = _applicationJs.createApplication;
  76. }],
  77. execute: function () {
  78. canvas = document.getElementById('GameCanvas');
  79. $p = canvas.parentElement;
  80. bcr = $p.getBoundingClientRect();
  81. canvas.width = bcr.width;
  82. canvas.height = bcr.height;
  83. createApplication({
  84. loadJsListFile: loadJsListFile,
  85. fetchWasm: fetchWasm
  86. }).then(function (application) {
  87. return application.start({
  88. findCanvas: findCanvas
  89. });
  90. })["catch"](function (err) {
  91. console.error(err);
  92. });
  93. }
  94. };
  95. });