Repository navigation
Expand file tree
/
Copy pathbootstrap.js
More file actions
68 lines (59 loc) · 2.11 KB
/
Copy pathbootstrap.js
File metadata and controls
68 lines (59 loc) · 2.11 KB
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
const root = document.documentElement;
const coarsePointer = window.matchMedia?.("(pointer: coarse)").matches;
const requestedInput = new URLSearchParams(window.location.search).get("input");
root.dataset.input =
requestedInput === "touch" || coarsePointer || navigator.maxTouchPoints > 0
? "touch"
: "pointer";
const canvas = document.querySelector("#game-canvas");
canvas?.addEventListener(
"webglcontextlost",
(event) => {
event.preventDefault();
reportFailure(new Error("The graphics context was lost. Reload the page to continue."), true);
},
{ passive: false },
);
canvas?.addEventListener("webglcontextrestored", () => {
reportFailure(new Error("Graphics are available again. Reload the page to rebuild the world."), true);
});
// CI stamps a build id here. glue js and wasm need the same one or
// instantiate() dies on a stale cache.
const build = root.dataset.build;
const version = build && build !== "dev" ? `?v=${build}` : "";
// dynamic import so we can show something useful if pkg/ is missing
async function boot() {
try {
const { default: init } = await import(`./pkg/rusted_cube.js${version}`);
await init(new URL(`./pkg/rusted_cube_bg.wasm${version}`, import.meta.url));
} catch (error) {
console.error(error);
reportFailure(error, false);
}
}
function reportFailure(error, canReload) {
const loader = document.querySelector("#loader");
if (!loader) {
return;
}
loader.replaceChildren();
loader.className = "loader loader--failed";
const message = document.createElement("span");
message.textContent = describe(error);
loader.append(message);
if (canReload) {
const reload = document.createElement("button");
reload.type = "button";
reload.textContent = "Reload";
reload.addEventListener("click", () => window.location.reload());
loader.append(reload);
}
}
function describe(error) {
const message = String(error?.message ?? error);
if (/dynamically imported module|Failed to fetch|NetworkError|404/i.test(message)) {
return "The game files could not be loaded. Check your connection and try again.";
}
return message;
}
boot();