Building the loading screen in HTML or React
In short
Plain HTML, CSS and JavaScript are enough for a very good loading screen. React with Vite earns its place once the screen has tabs, carousels and state to keep track of.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" />
</head>
<body>
<video class="bg" src="assets/bg.webm" poster="assets/poster.webp" autoplay muted loop playsinline></video>
<main class="panel">
<img class="logo" src="assets/logo.png" alt="Night City RP" />
<p id="status">Loading…</p>
<div class="bar"><span id="fill"></span></div>
</main>
<audio id="music" src="assets/music.mp3" autoplay loop></audio>
<script src="script.js"></script>
</body>
</html>const fill = document.getElementById('fill');
const status = document.getElementById('status');
const music = document.getElementById('music');
let shown = 0;
music.volume = Number(localStorage.getItem('vol') ?? 0.3);
window.addEventListener('message', (e) => {
const d = e.data;
if (d.eventName === 'loadProgress') {
shown = Math.max(shown, d.loadFraction); // never go backwards
fill.style.width = `${Math.round(shown * 100)}%`;
}
if (d.eventName === 'onLogLine') status.textContent = d.message;
});
const data = window.nuiHandoverData || {};
if (data.name) status.textContent = `Welcome, ${data.name}`;The script listens for two of the messages the client posts to the page. loadProgress carries loadFraction, a number from 0 to 1, and the Math.max keeps the bar from ever moving backwards. onLogLine carries a message that works as a status line. The remaining messages are listed in the cheat sheet on the handbook page.
- With React and Vite, set
base: './'so that asset paths come out relative, pointloadscreenat the builtindex.htmland list the build folder infiles. - Mock the loading messages while
npm run devis running, so the screen can be designed in an ordinary browser. - The player can read handover data, so send only small values that are not sensitive.