Building Single-File HTML5 Games: Structure That Scales From 1 Game to 9
Each game on InstantGames.in is one HTML file. Styles, markup and script live together, there's no build step required to ship it, and the page opens straight in a browser. It sounds like a limitation, but for small game
Each game on InstantGames.in is one HTML file. Styles, markup and script live together, there's no build step required to ship it, and the page opens straight in a browser. It sounds like a limitation, but for small games it's a strength. This post explains the structure that keeps one-file games manageable as the catalog grows to nine.
Why one file?
- Fast to load. One request, nothing blocking.
-
Easy to host. A static folder with an
index.htmlper game. - Easy to reason about. Everything for the game is in one place.
- Portable. You can open the file locally and play it.
For people who want free mini games online that start instantly, those properties are exactly what's needed.
The trade-off
A big file can become messy. The answer is not to split it, it's to impose structure inside.
A section layout I stick to
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Game Name</title>
<style>/* 1. tokens 2. layout 3. components */</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="ui"><!-- menus, HUD --></div>
<script>
// 1. CONFIG constants and tunables
// 2. STATE one object for everything mutable
// 3. STORAGE load and save, try/catch
// 4. INPUT pointer events
// 5. LOGIC update(dt)
// 6. RENDER draw()
// 7. LOOP requestAnimationFrame
// 8. UI menus, buttons
// 9. BOOT init
</script>
</body>
</html>
Numbering the sections as comments makes a long file navigable by search.
One state object
Keep all mutable state in a single object. It makes saving, restarting and undoing simple. In a block puzzle, undo is just restoring a snapshot. In a match-3, "restart level" is just rebuilding state from the level data.
Config at the top
Every number you might tune (scores, timings, probabilities, speeds) belongs in a CONFIG block at the top. Tuning becomes a matter of editing one place, and you can compare games by looking at their configs.
Shared ideas across the puzzle trio
The three puzzle games reuse the same bones:
- Instant Block Puzzle: a 10x10 grid, drag-and-drop pieces, snapshot undo.
- Instant Candy Pop: a grid, a resolve loop, levels as data.
- Instant Mahjong Solitaire: a layout as data, seeded deals, a daily seed.
Different games, same skeleton: data in, state out, render from state.
Keep the DOM out of the loop
Use canvas for the game field and DOM for menus. Updating the DOM in the animation loop is a common cause of jank on phones.
Share code by copying, carefully
A truly shared library would require a build step or extra requests. For small games, I copy small, well-tested helpers (a seeded random number generator, a storage wrapper) between files. The cost is duplication. The benefit is that each game stays independent and can't be broken by a change elsewhere.
When to stop doing this
If a game grows past a few thousand lines, or several people work on it at once, splitting into modules and adding a build step is the right call. One-file is a tool for a certain size, not a religion.
See the results
The structure powers games like Instant Block Puzzle, Instant Candy Pop and Instant Mahjong Solitaire.
How big do your single-file projects get before you split them?
These minigames are free mini games online and single player browser games, and together they cover the best arcade games online style of quick play.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.