Improved "load game" button, now with elem swap
Some checks failed
Basic checks / Basic build-and-test supertask (push) Failing after 1m3s

The canvas element no longer exists in the page source. Instead, there
is a div styled to look similar and contain a button. The button is
styled to match the ones in the Bevy app. When pressed, the button
creates the canvas element, replaces the fake canvas div, and loads the
WASM to do it's thing.
This commit is contained in:
2025-12-19 13:27:42 -06:00
parent 6d5afc2445
commit 4d899d3c97

View File

@@ -12,6 +12,7 @@
margin: auto; margin: auto;
padding: 0.5em; padding: 0.5em;
} }
#prestart-controls,
canvas { canvas {
margin-top: 1em; margin-top: 1em;
margin-left: auto; margin-left: auto;
@@ -22,6 +23,26 @@
border-radius: 8px; border-radius: 8px;
background-color: rgb(40%, 40%, 40%); background-color: rgb(40%, 40%, 40%);
} }
#prestart-controls {
width: 800px;
height: 600px;
text-align: center;
align-content: center;
}
button {
font-size: 20px;
text-shadow: 0.2em 0.2em 0px rgba(0, 0, 0, 75%);
padding: 1em;
border-radius: 2em;
border-style: solid;
border-color: black;
color: rgb(90%, 90%, 90%);
background-color: rgb(15%, 15%, 15%);
}
button:hover {
background-color: rgb(25%, 25%, 25%);
border-color: rgb(90%, 90%, 90%);
}
main { main {
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
@@ -41,9 +62,11 @@
<h1> <h1>
Robert's Bad Asteroids Game Robert's Bad Asteroids Game
</h1> </h1>
<canvas id="game-canvas" width="800" height="600"></canvas> <!-- <canvas id="game-canvas" width="800" height="600"></canvas> -->
<main> <div id="prestart-controls">
<button id="gameload-button">Load Game</button> <button id="gameload-button">Load Game</button>
</div>
<main>
<article> <article>
<h2>Description</h2> <h2>Description</h2>
<p> <p>
@@ -91,16 +114,24 @@
<tr> <tr>
<td>Program Version</td> <td>Program Version</td>
<!-- This version text is completely unchecked. I'll need to do something about that. --> <!-- This version text is completely unchecked. I'll need to do something about that. -->
<td><code>v0.5.0</code></td> <td><code>v0.6.0-dev3</code></td>
</tr> </tr>
</table> </table>
</article> </article>
</main> </main>
<script type="module"> <script type="module">
import init from './asteroids.js' import init from './asteroids.js'
document.getElementById("gameload-button").onclick = async function loadGame() { let button = document.getElementById("gameload-button");
button.onclick = async function loadGame() {
console.log("Game Load button was pressed!"); console.log("Game Load button was pressed!");
let canvas = document.createElement("canvas");
// <canvas id="game-canvas" width="800" height="600"></canvas>
canvas.setAttribute("id", "game-canvas");
canvas.setAttribute("width", "800");
canvas.setAttribute("height", "600");
button.parentElement.replaceWith(canvas);
let compressed = await fetch("./asteroids_bg.wasm.gz") let compressed = await fetch("./asteroids_bg.wasm.gz")
let wasm_stream = compressed.body.pipeThrough(new DecompressionStream("gzip")) let wasm_stream = compressed.body.pipeThrough(new DecompressionStream("gzip"))
let blob = await new Response(wasm_stream).blob(); let blob = await new Response(wasm_stream).blob();