123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- (function () {
- var CANVAS_ID = 'application-canvas';
- var canvas, devices, app;
- var createCanvas = function () {
- canvas = document.createElement('canvas');
- canvas.setAttribute('id', CANVAS_ID);
- canvas.setAttribute('tabindex', 0);
- // canvas.style.visibility = 'hidden';
- // Disable I-bar cursor on click+drag
- canvas.onselectstart = function () { return false; };
- document.body.appendChild(canvas);
- return canvas;
- };
- var createInputDevices = function (canvas) {
- var devices = {
- elementInput: new pc.ElementInput(canvas, {
- useMouse: INPUT_SETTINGS.useMouse,
- useTouch: INPUT_SETTINGS.useTouch
- }),
- keyboard: INPUT_SETTINGS.useKeyboard ? new pc.Keyboard(window) : null,
- mouse: INPUT_SETTINGS.useMouse ? new pc.Mouse(canvas) : null,
- gamepads: INPUT_SETTINGS.useGamepads ? new pc.GamePads() : null,
- touch: INPUT_SETTINGS.useTouch && pc.platform.touch ? new pc.TouchDevice(canvas) : null
- };
- return devices;
- };
- var configureCss = function (fillMode, width, height) {
- // Configure resolution and resize event
- if (canvas.classList) {
- canvas.classList.add('fill-mode-' + fillMode);
- }
- // css media query for aspect ratio changes
- var css = "@media screen and (min-aspect-ratio: " + width + "/" + height + ") {";
- css += " #application-canvas.fill-mode-KEEP_ASPECT {";
- css += " width: auto;";
- css += " height: 100%;";
- css += " margin: 0 auto;";
- css += " }";
- css += "}";
- // append css to style
- if (document.head.querySelector) {
- document.head.querySelector('style').innerHTML += css;
- }
- };
- var reflow = function () {
- app.resizeCanvas(canvas.width, canvas.height);
- canvas.style.width = '';
- canvas.style.height = '';
- var fillMode = app._fillMode;
- if (fillMode == pc.FILLMODE_NONE || fillMode == pc.FILLMODE_KEEP_ASPECT) {
- if ((fillMode == pc.FILLMODE_NONE && canvas.clientHeight < window.innerHeight) || (canvas.clientWidth / canvas.clientHeight >= window.innerWidth / window.innerHeight)) {
- canvas.style.marginTop = Math.floor((window.innerHeight - canvas.clientHeight) / 2) + 'px';
- } else {
- canvas.style.marginTop = '';
- }
- }
- };
- var displayError = function (html) {
- var div = document.createElement('div');
- div.innerHTML = [
- '<table style="background-color: #8CE; width: 100%; height: 100%;">',
- ' <tr>',
- ' <td align="center">',
- ' <div style="display: table-cell; vertical-align: middle;">',
- ' <div style="">' + html + '</div>',
- ' </div>',
- ' </td>',
- ' </tr>',
- '</table>'
- ].join('\n');
- document.body.appendChild(div);
- };
- canvas = createCanvas();
- devices = createInputDevices(canvas);
- try {
- app = new pc.Application(canvas, {
- elementInput: devices.elementInput,
- keyboard: devices.keyboard,
- mouse: devices.mouse,
- gamepads: devices.gamepads,
- touch: devices.touch,
- graphicsDeviceOptions: window.CONTEXT_OPTIONS,
- assetPrefix: window.ASSET_PREFIX || "",
- scriptPrefix: window.SCRIPT_PREFIX || "",
- scriptsOrder: window.SCRIPTS || []
- });
- } catch (e) {
- if (e instanceof pc.UnsupportedBrowserError) {
- displayError('This page requires a browser that supports WebGL.<br/>' +
- '<a href="http://get.webgl.org">Click here to find out more.</a>');
- } else if (e instanceof pc.ContextCreationError) {
- displayError("It doesn't appear your computer can support WebGL.<br/>" +
- '<a href="http://get.webgl.org/troubleshooting/">Click here for more information.</a>');
- } else {
- displayError('Could not initialize application. Error: ' + e);
- }
- return;
- }
- app.configure(CONFIG_FILENAME, function (err) {
- if (err) {
- console.error(err);
- }
- configureCss(app._fillMode, app._width, app._height);
- reflow();
- window.addEventListener('resize', reflow, false);
- window.addEventListener('orientationchange', reflow, false);
- app.preload(function (err) {
- if (err) {
- console.error(err);
- }
- app.loadScene(SCENE_PATH, function (err, scene) {
- if (err) {
- console.error(err);
- }
- app.start();
- });
- });
- });
- }());
|