Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a loading screen to the web page #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assets/
bevy_game.js
bevy_game_bg.wasm
50 changes: 50 additions & 0 deletions wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="./loading_screen.mjs"></script>

<script type="module">
// Load the game asynchronously
import("./bevy_game.js").then((game) => {
game.default();
});
</script>

<style>
body {
margin: 0px;
height: 100vh;
width: 100vw;
overflow: hidden;

background-color: white;
color: #023e8a;
}

#loading-screen {
width: 100%;
height: 100%;

display: flex;
flex-flow: column;
justify-content: center;
align-items: center;

font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

#loading-indicator {
font-size: 4em;
}
</style>
</head>

<body id="body">
<div id="loading-screen">
<div id="loading-indicator">
Loading<span id="loading-dots">...</span>
</div>
<div>Made with Bevy</div>
</div>
</body>
</html>
58 changes: 58 additions & 0 deletions wasm/loading_screen.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const MAX_DOT_COUNT = 3;

window.addEventListener("DOMContentLoaded", () => {
const body = document.getElementById("body");
const loadingScreen = document.getElementById("loading-screen");
const dotElement = document.getElementById("loading-dots");

if (body === null || loadingScreen === null || dotElement === null) {
console.error("Can't find loading screen elements!");
return;
};

let dotCount = 0;

function animateLoadingDots() {
dotElement.innerText = ".".repeat(dotCount).padEnd(3, "\u{00A0}");

dotCount += 1;

if (dotCount > MAX_DOT_COUNT) {
dotCount = 0;
}
}

function removeLoadingScreen() {
clearInterval(loadingInterval);
loadingScreen.remove();
}

// Wait for the `canvas` element to be created
// This tells us that the game has been fully loaded
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
console.debug("addedNode", addedNode, addedNode.ELEMENT_NODE);
if (addedNode.ELEMENT_NODE === 1) {
/** @type HTMLElement */
const element = addedNode;

console.debug(element.tagName);

if (element.tagName === "CANVAS") {
// The game has been loaded!
// We don't need the loading screen anymore
removeLoadingScreen();
observer.disconnect();
}
}
}
}
});

observer.observe(body, {
childList: true,
});

const loadingInterval = setInterval(animateLoadingDots, 1000);
});