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