-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.html
More file actions
104 lines (91 loc) · 2.57 KB
/
Copy pathmain.html
File metadata and controls
104 lines (91 loc) · 2.57 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Shandalar 30</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
background: #111;
color: #fff;
font-family: sans-serif;
}
#loader {
position: fixed;
inset: 0;
display: grid;
place-content: center;
gap: 12px;
text-align: center;
}
progress {
width: 320px;
height: 20px;
}
</style>
</head>
<body>
<div id="loader">
<div id="status">Downloading game…</div>
<progress id="progress" max="100"></progress>
<div id="downloaded"></div>
</div>
<script src="wasm_exec.js"></script>
<script>
async function start() {
const status = document.getElementById("status");
const progress = document.getElementById("progress");
const downloaded = document.getElementById("downloaded");
try {
const response = await fetch("s30.wasm");
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const total = Number(response.headers.get("Content-Length"));
const reader = response.body.getReader();
const chunks = [];
let received = 0;
while (true) {
const {done, value} = await reader.read();
if (done) {
break;
}
chunks.push(value);
received += value.length;
if (total) {
const percent = Math.min(100, received / total * 100);
progress.value = percent;
downloaded.textContent = `${Math.round(percent)}%`;
} else {
progress.removeAttribute("value");
downloaded.textContent = `${(received / 1024 / 1024).toFixed(1)} MB`;
}
}
status.textContent = "Starting game…";
progress.value = 100;
downloaded.textContent = "";
const wasm = new Uint8Array(received);
let offset = 0;
for (const chunk of chunks) {
wasm.set(chunk, offset);
offset += chunk.length;
}
const go = new Go();
const result = await WebAssembly.instantiate(wasm, go.importObject);
document.getElementById("loader").remove();
await go.run(result.instance);
} catch (error) {
status.textContent = `Unable to load game: ${error.message}`;
progress.hidden = true;
downloaded.textContent = "";
console.error(error);
}
}
start();
</script>
</body>
</html>