__start__.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. (function () {
  2. var CANVAS_ID = 'application-canvas';
  3. var canvas, devices, app;
  4. var createCanvas = function () {
  5. canvas = document.createElement('canvas');
  6. canvas.setAttribute('id', CANVAS_ID);
  7. canvas.setAttribute('tabindex', 0);
  8. // canvas.style.visibility = 'hidden';
  9. // Disable I-bar cursor on click+drag
  10. canvas.onselectstart = function () { return false; };
  11. document.body.appendChild(canvas);
  12. return canvas;
  13. };
  14. var createInputDevices = function (canvas) {
  15. var devices = {
  16. elementInput: new pc.ElementInput(canvas, {
  17. useMouse: INPUT_SETTINGS.useMouse,
  18. useTouch: INPUT_SETTINGS.useTouch
  19. }),
  20. keyboard: INPUT_SETTINGS.useKeyboard ? new pc.Keyboard(window) : null,
  21. mouse: INPUT_SETTINGS.useMouse ? new pc.Mouse(canvas) : null,
  22. gamepads: INPUT_SETTINGS.useGamepads ? new pc.GamePads() : null,
  23. touch: INPUT_SETTINGS.useTouch && pc.platform.touch ? new pc.TouchDevice(canvas) : null
  24. };
  25. return devices;
  26. };
  27. var configureCss = function (fillMode, width, height) {
  28. // Configure resolution and resize event
  29. if (canvas.classList) {
  30. canvas.classList.add('fill-mode-' + fillMode);
  31. }
  32. // css media query for aspect ratio changes
  33. var css = "@media screen and (min-aspect-ratio: " + width + "/" + height + ") {";
  34. css += " #application-canvas.fill-mode-KEEP_ASPECT {";
  35. css += " width: auto;";
  36. css += " height: 100%;";
  37. css += " margin: 0 auto;";
  38. css += " }";
  39. css += "}";
  40. // append css to style
  41. if (document.head.querySelector) {
  42. document.head.querySelector('style').innerHTML += css;
  43. }
  44. };
  45. var reflow = function () {
  46. app.resizeCanvas(canvas.width, canvas.height);
  47. canvas.style.width = '';
  48. canvas.style.height = '';
  49. var fillMode = app._fillMode;
  50. if (fillMode == pc.FILLMODE_NONE || fillMode == pc.FILLMODE_KEEP_ASPECT) {
  51. if ((fillMode == pc.FILLMODE_NONE && canvas.clientHeight < window.innerHeight) || (canvas.clientWidth / canvas.clientHeight >= window.innerWidth / window.innerHeight)) {
  52. canvas.style.marginTop = Math.floor((window.innerHeight - canvas.clientHeight) / 2) + 'px';
  53. } else {
  54. canvas.style.marginTop = '';
  55. }
  56. }
  57. };
  58. var displayError = function (html) {
  59. var div = document.createElement('div');
  60. div.innerHTML = [
  61. '<table style="background-color: #8CE; width: 100%; height: 100%;">',
  62. ' <tr>',
  63. ' <td align="center">',
  64. ' <div style="display: table-cell; vertical-align: middle;">',
  65. ' <div style="">' + html + '</div>',
  66. ' </div>',
  67. ' </td>',
  68. ' </tr>',
  69. '</table>'
  70. ].join('\n');
  71. document.body.appendChild(div);
  72. };
  73. canvas = createCanvas();
  74. devices = createInputDevices(canvas);
  75. try {
  76. app = new pc.Application(canvas, {
  77. elementInput: devices.elementInput,
  78. keyboard: devices.keyboard,
  79. mouse: devices.mouse,
  80. gamepads: devices.gamepads,
  81. touch: devices.touch,
  82. graphicsDeviceOptions: window.CONTEXT_OPTIONS,
  83. assetPrefix: window.ASSET_PREFIX || "",
  84. scriptPrefix: window.SCRIPT_PREFIX || "",
  85. scriptsOrder: window.SCRIPTS || []
  86. });
  87. } catch (e) {
  88. if (e instanceof pc.UnsupportedBrowserError) {
  89. displayError('This page requires a browser that supports WebGL.<br/>' +
  90. '<a href="http://get.webgl.org">Click here to find out more.</a>');
  91. } else if (e instanceof pc.ContextCreationError) {
  92. displayError("It doesn't appear your computer can support WebGL.<br/>" +
  93. '<a href="http://get.webgl.org/troubleshooting/">Click here for more information.</a>');
  94. } else {
  95. displayError('Could not initialize application. Error: ' + e);
  96. }
  97. return;
  98. }
  99. app.configure(CONFIG_FILENAME, function (err) {
  100. if (err) {
  101. console.error(err);
  102. }
  103. configureCss(app._fillMode, app._width, app._height);
  104. reflow();
  105. window.addEventListener('resize', reflow, false);
  106. window.addEventListener('orientationchange', reflow, false);
  107. app.preload(function (err) {
  108. if (err) {
  109. console.error(err);
  110. }
  111. app.loadScene(SCENE_PATH, function (err, scene) {
  112. if (err) {
  113. console.error(err);
  114. }
  115. app.start();
  116. });
  117. });
  118. });
  119. }());