<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>hello, world — a sliding puzzle</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=Fraunces:opsz,wght@9..144,600;9..144,700&display=swap" rel="stylesheet">
<style>
:root{
--field:#C6CBE4;
--field-deep:#AEB5D9;
--tray:#1A2044;
--tray-lip:#333C74;
--bone:#F2EDE2;
--slate:#98A0C8;
--ink:#141935;
--saffron:#F0A93B;
--rim:clamp(12px, 3.4vw, 18px);
}
*{box-sizing:border-box;}
html,body{height:100%;}
body{
margin:0;
padding:clamp(24px,5vw,64px) clamp(18px,5vw,48px);
background:radial-gradient(120% 90% at 15% 0%, var(--field) 0%, var(--field-deep) 100%);
color:var(--ink);
font-family:"DM Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
display:flex;
align-items:center;
justify-content:center;
}
.page{
width:100%;
max-width:960px;
display:grid;
gap:clamp(28px,5vw,56px);
grid-template-columns:1fr;
align-items:center;
}
@media (min-width:900px){
.page{grid-template-columns:1fr auto; gap:64px;}
}
.eyebrow{
margin:0 0 18px;
font-size:12px;
letter-spacing:.22em;
text-transform:uppercase;
color:#4C548C;
}
h1{
margin:0;
font-family:"Fraunces",Georgia,serif;
font-variation-settings:'SOFT' 60,'WONK' 1,'opsz' 120;
font-weight:700;
font-size:clamp(38px,7.5vw,64px);
line-height:1;
letter-spacing:-.02em;
}
.caret{
display:inline-block;
width:.42em; height:.72em;
margin-left:.12em;
background:var(--saffron);
animation:blink 1.1s steps(1,end) infinite;
}
body[data-state="playing"] .caret{animation:none;}
@keyframes blink{50%{opacity:0;}}
.lede{
margin:20px 0 30px;
max-width:34ch;
font-size:14px;
line-height:1.7;
color:#39406F;
}
.controls{display:flex; flex-wrap:wrap; gap:10px; margin-top:34px;}
button{
font:inherit;
font-size:13px;
padding:12px 18px;
border-radius:8px;
cursor:pointer;
transition:transform .12s ease, box-shadow .12s ease, background .12s ease;
}
.btn-primary{
border:none;
background:var(--tray);
color:var(--bone);
box-shadow:0 3px 0 #0C1030;
}
.btn-primary:hover{background:#232B58;}
.btn-ghost{
border:1px solid #7079AC;
background:transparent;
color:#39406F;
box-shadow:0 3px 0 rgba(20,25,53,.14);
}
.btn-ghost:hover{border-color:var(--tray); color:var(--tray);}
button:active{transform:translateY(3px); box-shadow:0 0 0 rgba(0,0,0,0);}
button:disabled{opacity:.4; cursor:default; transform:none;}
button:focus-visible, .tile:focus-visible{
outline:3px solid var(--saffron);
outline-offset:3px;
}
.status{
margin-top:26px;
display:flex;
align-items:baseline;
gap:10px;
font-size:13px;
color:#4C548C;
min-height:1.4em;
}
.moves{
font-family:"Fraunces",Georgia,serif;
font-variation-settings:'WONK' 1;
font-weight:700;
font-size:22px;
color:var(--tray);
}
.status.win{color:var(--tray);}
.hint{
margin-top:8px;
font-size:11.5px;
letter-spacing:.06em;
color:#6971A4;
}
.tray{
position:relative;
width:min(88vw, 420px);
aspect-ratio:1;
padding:var(--rim);
border-radius:20px;
background:linear-gradient(170deg, var(--tray-lip) 0%, var(--tray) 42%);
box-shadow:
0 22px 40px -18px rgba(15,19,48,.65),
inset 0 2px 0 rgba(255,255,255,.14),
inset 0 -3px 0 rgba(0,0,0,.35);
justify-self:center;
}
.well{
position:relative;
width:100%; height:100%;
border-radius:10px;
box-shadow:inset 0 3px 8px rgba(0,0,0,.45);
}
/* Size and position are written by JS in pixels. */
.tile{
position:absolute;
top:0; left:0;
padding:0;
border:none;
background:none;
transition:transform .16s cubic-bezier(.32,.9,.4,1);
}
body[data-shuffling="yes"] .tile{transition-duration:.08s;}
.cap{
display:grid;
place-items:center;
width:100%; height:100%;
border-radius:9px;
background:var(--slate);
box-shadow:
inset 0 2px 0 rgba(255,255,255,.32),
inset 0 -3px 0 rgba(0,0,0,.2),
0 2px 4px rgba(0,0,0,.3);
transition:transform .12s ease, box-shadow .12s ease;
}
.tile.letter .cap{background:var(--bone);}
.cap span{
font-family:"Fraunces",Georgia,serif;
font-variation-settings:'SOFT' 60,'WONK' 1,'opsz' 144;
font-weight:700;
line-height:1;
color:var(--ink);
}
.tile.movable{cursor:pointer;}
.tile.movable:hover .cap{
transform:translateY(-2px);
box-shadow:
inset 0 2px 0 rgba(255,255,255,.32),
inset 0 -3px 0 rgba(0,0,0,.2),
0 6px 10px rgba(0,0,0,.35);
}
.tile:not(.movable){cursor:default;}
.tile.nope .cap{animation:nope .3s ease;}
@keyframes nope{25%{transform:translateX(-3px);} 75%{transform:translateX(3px);}}
.tile.cheer .cap{animation:cheer .5s ease both;}
@keyframes cheer{40%{transform:translateY(-9px); box-shadow:0 12px 14px rgba(0,0,0,.3);}}
body[data-state="solved"] .tray{
box-shadow:
0 22px 40px -18px rgba(15,19,48,.65),
inset 0 2px 0 rgba(255,255,255,.14),
inset 0 -3px 0 rgba(0,0,0,.35),
0 0 0 3px var(--saffron);
}
@media (prefers-reduced-motion: reduce){
*{animation:none !important; transition:none !important;}
}
</style>
</head>
<body data-state="intro">
<main class="page">
<section>
<p class="eyebrow">15 tiles · 1 empty square</p>
<h1>hello, world<span class="caret" aria-hidden="true"></span></h1>
<div class="controls">
<button class="btn-primary" id="scramble">Scramble the tiles</button>
<button class="btn-ghost" id="restore" disabled>Restore</button>
</div>
<p class="status" id="status" role="status" aria-live="polite">
<span class="moves" id="moves">0</span>
<span id="statusText">moves</span>
</p>
<p class="hint">Tap a tile beside the gap.</p>
</section>
<div class="tray">
<div class="well" id="well"></div>
</div>
</main>
<script>
(function(){
const SIZE = 4;
const TARGET = ['H','E','L','L',
'O','' ,'W','O',
'R','L','D','',
'' ,'' ,'' , null]; // index 15 is the gap
const well = document.getElementById('well');
const movesEl = document.getElementById('moves');
const statusText = document.getElementById('statusText');
const statusEl = document.getElementById('status');
const scrambleBtn = document.getElementById('scramble');
const restoreBtn = document.getElementById('restore');
const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
let board = []; // board[position] = tile id, or null for the gap
let tiles = []; // tiles[id] = { ch, el }
let cell = 0, gap = 0;
let moves = 0;
let busy = false;
function build(){
well.innerHTML = '';
tiles = [];
for (let i = 0; i < 15; i++){
const ch = TARGET[i];
const el = document.createElement('button');
el.className = 'tile' + (ch ? ' letter' : '');
el.type = 'button';
el.innerHTML = '<span class="cap"><span>' + (ch || '') + '</span></span>';
el.addEventListener('click', function(){ attempt(i); });
well.appendChild(el);
tiles.push({ ch: ch, el: el });
}
board = TARGET.map(function(_, i){ return i < 15 ? i : null; });
measure();
}
// All geometry is computed here, in pixels, and re-run on resize.
function measure(){
const w = well.clientWidth;
if (!w) return;
gap = Math.max(5, Math.round(w * 0.025));
cell = (w - gap * 3) / 4;
tiles.forEach(function(t){
t.el.style.width = cell + 'px';
t.el.style.height = cell + 'px';
t.el.querySelector('.cap span').style.fontSize = Math.round(cell * 0.5) + 'px';
});
render();
}
function render(){
const gapAt = board.indexOf(null);
board.forEach(function(id, pos){
if (id === null) return;
const t = tiles[id];
const x = (pos % SIZE) * (cell + gap);
const y = Math.floor(pos / SIZE) * (cell + gap);
t.el.style.transform = 'translate(' + x + 'px,' + y + 'px)';
const near = adjacent(pos, gapAt);
t.el.classList.toggle('movable', near && !busy);
t.el.setAttribute('aria-label',
(t.ch ? 'Tile ' + t.ch : 'Blank tile') +
', row ' + (Math.floor(pos / SIZE) + 1) + ', column ' + (pos % SIZE + 1) +
(near ? ', can slide' : ''));
});
}
function adjacent(a, b){
const ax = a % SIZE, ay = Math.floor(a / SIZE);
const bx = b % SIZE, by = Math.floor(b / SIZE);
return Math.abs(ax - bx) + Math.abs(ay - by) === 1;
}
function legalPositions(){
const gapAt = board.indexOf(null);
const out = [];
for (let p = 0; p < 16; p++) if (adjacent(p, gapAt)) out.push(p);
return out;
}
function slide(pos){
const gapAt = board.indexOf(null);
board[gapAt] = board[pos];
board[pos] = null;
}
function attempt(id){
if (busy) return;
const pos = board.indexOf(id);
const gapAt = board.indexOf(null);
if (!adjacent(pos, gapAt)){
const el = tiles[id].el;
el.classList.remove('nope');
void el.offsetWidth;
el.classList.add('nope');
return;
}
slide(pos);
moves++;
movesEl.textContent = moves;
render();
if (solved()) win();
}
function solved(){
for (let p = 0; p < 16; p++){
const id = board[p];
const here = id === null ? null : tiles[id].ch;
if (p === 15){ if (here !== null) return false; }
else if (here !== TARGET[p]) return false;
}
return true;
}
function win(){
document.body.dataset.state = 'solved';
statusEl.classList.add('win');
statusText.textContent = moves === 1 ? 'move — solved.' : 'moves — solved.';
let n = 0;
board.forEach(function(id){
if (id === null || !tiles[id].ch) return;
const el = tiles[id].el;
el.style.animationDelay = (n++ * 55) + 'ms';
el.classList.remove('cheer');
void el.offsetWidth;
el.classList.add('cheer');
});
}
function clearWin(){
statusEl.classList.remove('win');
board.forEach(function(id){
if (id !== null) tiles[id].el.classList.remove('cheer');
});
}
function scramble(){
if (busy) return;
clearWin();
busy = true;
scrambleBtn.disabled = true;
restoreBtn.disabled = true;
document.body.dataset.state = 'playing';
document.body.dataset.shuffling = 'yes';
statusText.textContent = 'scrambling…';
movesEl.textContent = '0';
const total = 60;
let done = 0;
let last = -1;
function step(){
const options = legalPositions().filter(function(p){ return p !== last; });
const pick = options[Math.floor(Math.random() * options.length)];
last = board.indexOf(null);
slide(pick);
render();
done++;
if (done < total || solved()){
if (reduced) step(); else setTimeout(step, 55);
} else {
finish();
}
}
function finish(){
busy = false;
moves = 0;
movesEl.textContent = '0';
statusText.textContent = 'moves';
scrambleBtn.disabled = false;
restoreBtn.disabled = false;
scrambleBtn.textContent = 'Scramble again';
delete document.body.dataset.shuffling;
render();
}
step();
}
function restore(){
if (busy) return;
clearWin();
board = TARGET.map(function(_, i){ return i < 15 ? i : null; });
moves = 0;
movesEl.textContent = '0';
statusText.textContent = 'moves';
document.body.dataset.state = 'intro';
render();
}
// Arrow keys slide the neighbouring tile toward the gap.
document.addEventListener('keydown', function(e){
const map = { ArrowUp: SIZE, ArrowDown: -SIZE, ArrowLeft: 1, ArrowRight: -1 };
if (!(e.key in map) || busy) return;
e.preventDefault();
const gapAt = board.indexOf(null);
const from = gapAt + map[e.key];
if (from < 0 || from > 15 || !adjacent(from, gapAt)) return;
attempt(board[from]);
});
scrambleBtn.addEventListener('click', scramble);
restoreBtn.addEventListener('click', restore);
window.addEventListener('resize', measure);
build();
// Fonts can change layout width slightly; re-measure once they land.
if (document.fonts && document.fonts.ready) document.fonts.ready.then(measure);
})();
</script>
</body>
</html>
Websites this page contacts when it runs — fonts, chart libraries, data. Found by reading its source.
Published on Globefin under the MIT License. You may copy, change, and reuse this page, including commercially — just keep the notice below with it.
Copyright © 2026 Shane Hadden. Licensed under the MIT License. Published on Globefin.
Provided as is, without warranty. Any external libraries it loads keep their own licences. Terms →
Scripts run, but the demo has its own throwaway origin. It can't read your session or cookies, can't reach the page around it, and can't navigate your tab. Don't enter real passwords or personal data into any demo.