forked from tsoding/zozlib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
130 lines (112 loc) · 4.17 KB
/
index.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html>
<head>
<title>Hello, Raylib!</title>
<style>
:root {
--color-dark: #181818;
--color-lite: #e7e7e7;
}
body {
/* Lite Mode */
background: var(--color-lite);
color: var(--color-dark);
/* Dark Mode */
@media (prefers-color-scheme: dark) {
background: var(--color-dark);
color: var(--color-lite);
}
}
#game {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 1px solid var(--color-dark);
@media (prefers-color-scheme: dark) {
border: 1px solid var(--color-lite);
}
}
#raylib-example-select {
display: block;
max-width: 8rem;
}
.not-hosted-msg {
text-align: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.not-hosted-msg .important {
font-weight: bold;
}
@font-face {
font-family: grixel;
src: url(fonts/acme_7_wide_xtnd.woff);
}
</style>
<script src="raylib.js"></script>
</head>
<body>
<label for="raylib-example-select">Choose an Example:</label>
<select id="raylib-example-select" onchange="startRaylib(this.value)">
<!-- This is populated by js -->
</select>
<canvas id="game"></canvas>
<script>
const wasmPaths = {
"tsoding": ["tsoding_ball", "tsoding_snake",],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
"shapes": ["shapes_colors_palette"],
"text": ["text_writing_anim"],
"textures": ["textures_logo_raylib"],
}
const defaultWasm = Object.values(wasmPaths)[0][0];
const raylibExampleSelect = document.getElementById("raylib-example-select");
for (const exampleCategory in wasmPaths){
raylibExampleSelect.innerHTML += `<optgroup label="${exampleCategory}">`
for (const example of wasmPaths[exampleCategory]){
raylibExampleSelect.innerHTML += `<option>${example}</option>`
}
raylibExampleSelect.innerHTML += "</optgroup>"
}
const { protocol } = window.location;
const isHosted = protocol !== "file:";
let raylibJs = undefined;
function startRaylib(selectedWasm){
var queryParams = new URLSearchParams(window.location.search);
queryParams.set("example", selectedWasm);
history.pushState(null, null, "?"+queryParams.toString());
raylibExampleSelect.value = selectedWasm;
if (isHosted) {
if (raylibJs !== undefined) {
raylibJs.stop();
}
raylibJs = new RaylibJs();
raylibJs.start({
wasmPath: `wasm/${selectedWasm}.wasm`,
canvasId: "game",
});
} else {
window.addEventListener("load", () => {
document.body.innerHTML = `
<div class="not-hosted-msg">
<div class="important">
<p>Unfortunately, due to CORs restrictions, the wasm assembly cannot be fetched.</p>
<p>Please navigate to this location using a web server.</p>
<p>If you have Python 3 on your system you can just do:</p>
</div>
<code>$ python3 -m http.server 6969</code>
</div>
`;
});
}
}
let queryParams = new URLSearchParams(window.location.search);
const exampleParam = queryParams.get("example") ?? defaultWasm;
if (Object.values(wasmPaths).flat().includes(exampleParam)) startRaylib(exampleParam);
else startRaylib(defaultWasm);
</script>
</body>
</html>