AtomicLoader.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var AtomicLoader;
  2. if (typeof AtomicLoader === 'undefined') AtomicLoader = eval('(function() { try { return AtomicLoader || {} } catch(e) { return {} } })()');
  3. window.addEventListener("keydown", function(e) {
  4. // Disable stock behavior, which was causing scroll errors
  5. // space and arrow keys
  6. if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
  7. e.preventDefault();
  8. }
  9. }, false);
  10. var atomicCanvas = document.getElementById('canvas');
  11. var _backgroundImageData = null;
  12. // <canvas id="loadcanvas" width="800" height="600" oncontextmenu="event.preventDefault()"></canvas>
  13. function DrawProgress(text, current) {
  14. var bg = document.getElementById('loadcanvas');
  15. var ctx = bg.getContext('2d');
  16. var rect = bg.getBoundingClientRect();
  17. var cwidth = rect.width;
  18. var cheight = rect.height;
  19. // for background image
  20. if (!_backgroundImageData)
  21. _backgroundImageData = ctx.getImageData(0, 0, cwidth, cheight);
  22. ctx.putImageData(_backgroundImageData, 0, 0);
  23. ctx.font="30px Arial";
  24. var progressText = (current * 100) + "%";
  25. var textSize = ctx.measureText(progressText);
  26. ctx.fillText(progressText, cwidth/2 - textSize.width/2, cheight/2 + 15);
  27. ctx.beginPath();
  28. ctx.strokeStyle = '#99CC33';
  29. ctx.lineCap = 'square';
  30. ctx.closePath();
  31. ctx.fill();
  32. ctx.lineWidth = 10.0;
  33. var circ = Math.PI * 2;
  34. var quart = Math.PI / 2;
  35. ctx.beginPath();
  36. ctx.arc(cwidth/2, cheight/2, 70, -(quart), ((circ) * current) - quart, false);
  37. ctx.stroke();
  38. }
  39. document.getElementById('canvas').style.display = 'none';
  40. DrawProgress("Downloading", 0.1);
  41. var Module = {
  42. arguments: ["-x", "800", "-y", "600"],
  43. preRun:[function() {
  44. DrawProgress("Initializing", 0.75);
  45. }],
  46. postRun: [function() {
  47. document.getElementById('loadcanvas').style.display = 'none';
  48. document.getElementById('canvas').style.display = 'block';
  49. }],
  50. print: (function() {
  51. var element = document.getElementById('output');
  52. if (element) element.value = ''; // clear browser cache
  53. return function(text) {
  54. if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
  55. // These replacements are necessary if you render to raw HTML
  56. //text = text.replace(/&/g, "&amp;");
  57. //text = text.replace(/</g, "&lt;");
  58. //text = text.replace(/>/g, "&gt;");
  59. //text = text.replace('\n', '<br>', 'g');
  60. console.log(text);
  61. if (element) {
  62. element.value += text + "\n";
  63. element.scrollTop = element.scrollHeight; // focus on bottom
  64. }
  65. };
  66. })(),
  67. printErr: function(text) {
  68. if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
  69. if (0) { // XXX disabled for safety typeof dump == 'function') {
  70. dump(text + '\n'); // fast, straight to the real console
  71. } else {
  72. console.error(text);
  73. }
  74. },
  75. canvas: (function() {
  76. var canvas = document.getElementById('canvas');
  77. // As a default initial behavior, pop up an alert when webgl context is lost. To make your
  78. // application robust, you may want to override this behavior before shipping!
  79. // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
  80. canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
  81. return canvas;
  82. })(),
  83. setStatus: function(text) {
  84. },
  85. totalDependencies: 0,
  86. monitorRunDependencies: function(left) {
  87. this.totalDependencies = Math.max(this.totalDependencies, left);
  88. Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
  89. }
  90. };
  91. Module.setStatus('Downloading...');
  92. window.onerror = function(event) {
  93. // TODO: do not warn on ok events like simulating an infinite loop or exitStatus
  94. Module.setStatus('Exception thrown, see JavaScript console');
  95. Module.setStatus = function(text) {
  96. if (text) Module.printErr('[post-exception status] ' + text);
  97. };
  98. };