Lane Runner Endless Game — HTML Canvas Neon 3D Runner
Lane Runner Endless Game · Games · Plain HTML, CSS & JS · Live preview
About this UI Snippet
Lane Runner — A Pseudo-3D Endless Runner with Perspective Track, Coins, Shields and Swipe Controls

Endless runners are the most touch-native game genre there is: the whole game is a handful of instant decisions — left, right, jump, slide — against an obstacle stream that never stops speeding up. This snippet builds a complete, polished one on a single HTML5 <canvas>: a synthwave track in perspective, a runner with a real run cycle, three obstacle types that each demand a different move, coins, a shield power-up, swipe and tap controls, synthesized sound, pause and a saved high score.
Pseudo-3D from one formula
There is no 3D engine. Every object has a distance d ahead of the runner, and one function turns it into a scale: t = FOCAL / (FOCAL + d). At the runner t is 1; far away it shrinks toward 0 at the horizon. Screen y is HORIZON + (BASE_Y − HORIZON) × t and screen x is the lane offset multiplied by t, so lanes converge on the vanishing point and objects accelerate toward you naturally. The track is drawn as scrolling quads between depth slices, alternating two shades with dashed lane markers, and a magenta grid, a glowing edge, a retro striped sun and a parallax skyline complete the scene.
Three obstacles, three verbs
A green barrier must be jumped, an amber overhead gate must be slid under, and red blocks fill every lane but one, so you must switch lanes. Some rows span all three lanes and force a jump or slide. Collisions are simple depth checks: an obstacle only matters when its d is within about ±1.3 of the runner and its lane is within 0.55 of the runner's smoothed lane position. Jump and slide are timed windows, and sliding in mid-air cancels the jump so the runner drops immediately, which makes the controls feel responsive.
Controls built for thumbs
Every action is a one-shot event, not a held key. The on-screen buttons fire on pointerdown, the keyboard uses ← → ↑ ↓ plus WASD and Space, and the track itself accepts swipes: a horizontal swipe changes lane, up jumps, down slides, and a short tap jumps. The lane is an integer used for logic while the drawn position eases toward it, so lane changes slide instead of snapping. Arrow and Space keys call preventDefault so the page never scrolls while you play.
Coins, shield and fair spawning
Rows are chosen from patterns: single obstacles with coins in the free lanes, double blocks with coins in the open lane, full-width walls, pure coin lines and an occasional shield. The gap between rows is tied to speed (speed × 0.62 plus a little randomness), so there is always time to react at high speed. A shield absorbs one hit, gives 1.3 seconds of blink-invulnerability and pops with a particle ring. Score is distance in metres plus ten per coin, and speed ramps smoothly with distance up to a cap, shown as a bar in the HUD.
An autopilot demo behind the title screen
While you are on the start screen the game plays itself: a few lines of autopilot look ahead at the next obstacle in the current lane, jump a barrier, slide a gate or pick the nearest free lane for a block. The same update code runs with crashes disabled, so the title screen shows a live scene instead of a static image.
Juice, sound and accessibility
Coins spin by squashing their width with a cosine, speed lines streak from the vanishing point as you accelerate, crashes flash, shake and burst into particles, and dust and a shadow ground the runner. Sound is synthesized with the Web Audio API (jump, slide, lane tick, coin pitch that rises with your streak, shield, crash) with a saved mute toggle, created on the first click. The canvas is scaled by devicePixelRatio for sharp lines, the game pauses on blur or a hidden tab, and prefers-reduced-motion disables screen shake. For related canvas games see the Dodge the falling blocks game and the Frog crossing game.
What's included
Features
Step by step
How to Use
- 1Start runningClick "Run" (or press Enter, Space or ↑). The title screen already shows an autopilot demo of the game. The runner auto-advances, you only steer and dodge.
- 2Switch lanesPress ← → (or A and D), tap ◀ ▶, or swipe left or right on the track. The runner eases smoothly into the new lane.
- 3Jump the green barriersPress ↑, Space or JUMP, tap the track or swipe up. Timing matters: the jump is a fixed window, so clear the barrier before you land.
- 4Slide under the amber gatesPress ↓ or S, tap SLIDE, or swipe down. If you are in the air a slide drops you straight down, which helps you chain moves quickly.
- 5Dodge blocks, grab coins and shieldsRed blocks fill all lanes but one. Collect coins for 10 points each and the cyan shield to survive one crash. Your coin streak raises the pitch of the pickup sound.
- 6Beat your bestScore is metres plus coins. The speed bar in the corner fills as you accelerate. P or Esc pauses, M mutes, and your best is saved in localStorage under lane-runner-best.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
Every object stores its distance ahead of the runner. The scale is t = FOCAL / (FOCAL + d), screen y is HORIZON + (BASE_Y - HORIZON) * t and screen x is the lane offset times t. Objects therefore shrink toward a vanishing point and speed up as they approach, exactly like old arcade racers.
Every action is instantaneous: a lane change nudges an index, and jump and slide start fixed-length timers. Nothing depends on a key staying pressed, so buttons, swipes and keys all share the same handlers.
The lane is an integer used for collision, and the drawn lane position eases toward it with laneF += (lane - laneF) * k each frame. Collision uses the smoothed value, so a runner halfway between lanes is only hit by an obstacle that is close enough.
Pressing slide while jumping cancels the jump so the runner drops immediately, then starts the slide. It makes the game more responsive than forcing the player to wait until landing.
Speed rises with distance up to a cap, and the gap between obstacle rows is set from the current speed, so the time to react never collapses. Rows always leave at least one safe move.
In the idle state the normal update runs with crashes and scoring disabled and a small autopilot looks at the nearest obstacle in the runner's lane. It jumps barriers, slides gates and steers to a free lane around blocks.
Yes. The best score is stored in localStorage under lane-runner-best and read on load, wrapped in try/catch so the game still runs where storage is blocked.
Render the canvas and overlays, create the context in a mount effect and run the requestAnimationFrame loop there, keeping game state in refs. Cancel the loop and remove the key, pointer and visibility listeners in the cleanup. Use the export buttons on this page for the markup and styles.
Copy & paste
Source Code
<div class="game-card">
<div class="game-header">
<div class="game-title">
<span class="logo" aria-hidden="true"><svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 20 9 4h6l6 16"/><path d="M12 8v3M12 14v3"/></svg></span>
<div>
<h2>Lane Runner</h2>
<p class="tagline">Neon endless runner</p>
</div>
</div>
<div class="head-right">
<span class="badge">Best <b id="best-score">0</b></span>
<button class="icon-btn" id="btn-sound" type="button" aria-pressed="true" aria-label="Sound on" title="Sound (M)">🔊</button>
<button class="icon-btn" id="btn-pause" type="button" aria-label="Pause" title="Pause (P)">❚❚</button>
</div>
</div>
<div class="canvas-wrap">
<canvas id="game-canvas" width="360" height="480" aria-label="Lane Runner game canvas"></canvas>
<div class="hud" aria-live="off">
<span class="hud-score"><small>SCORE</small><b id="hud-score">0</b></span>
<span class="hud-coins"><i></i><b id="hud-coins">0</b></span>
<span class="hud-speed" title="Speed"><em id="hud-speed"></em></span>
</div>
<div class="overlay" id="start-overlay">
<div class="overlay-card">
<p class="overlay-kicker">Endless runner</p>
<p class="overlay-title">Lane <span>Runner</span></p>
<p class="overlay-sub">Switch lanes, <b>jump</b> the green barriers, <b>slide</b> under the amber gates and dodge the red blocks. Grab coins and shields. It never stops speeding up.</p>
<ul class="keys"><li><kbd>←</kbd><kbd>→</kbd> lanes</li><li><kbd>↑</kbd> jump</li><li><kbd>↓</kbd> slide</li><li>or swipe / tap</li></ul>
<button class="btn btn-primary" id="btn-start" type="button">Run</button>
</div>
</div>
<div class="overlay hidden" id="pause-overlay">
<div class="overlay-card">
<p class="overlay-title">Paused</p>
<button class="btn btn-primary" id="btn-resume" type="button">Resume</button>
</div>
</div>
<div class="overlay hidden" id="gameover-overlay">
<div class="overlay-card">
<p class="overlay-title over">Crashed!</p>
<p class="overlay-score" id="final-score">0</p>
<p class="overlay-sub" id="final-detail"></p>
<p class="overlay-best" id="best-msg"></p>
<button class="btn btn-primary" id="btn-retry" type="button">Run again</button>
</div>
</div>
</div>
<div class="controls">
<div class="dpad">
<button class="ctrl-btn" id="btn-left" type="button" aria-label="Move left">◀</button>
<button class="ctrl-btn" id="btn-right" type="button" aria-label="Move right">▶</button>
</div>
<div class="actions">
<button class="act-btn jump" id="btn-jump" type="button" aria-label="Jump">JUMP</button>
<button class="act-btn slide" id="btn-slide" type="button" aria-label="Slide">SLIDE</button>
</div>
</div>
<p class="hint-text">Keys: ← → lanes · ↑ / Space jump · ↓ slide · P pause · M sound — or swipe on the track</p>
</div>*{box-sizing:border-box;margin:0;padding:0}
body{font-family:system-ui,-apple-system,'Segoe UI',sans-serif;background:radial-gradient(700px 420px at 50% 0%,rgba(236,72,153,.28),transparent 70%),#08031a;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:20px;color:#e9e4ff}
.game-card{width:100%;max-width:400px;background:linear-gradient(180deg,#171036,#0d0824);border-radius:22px;border:1px solid #2e2260;box-shadow:0 24px 60px -20px rgba(236,72,153,.4);padding:16px;user-select:none;-webkit-user-select:none}
.game-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:12px;gap:10px;flex-wrap:wrap}
.game-title{display:flex;align-items:center;gap:10px}
.logo{display:grid;place-items:center;width:38px;height:38px;border-radius:12px;color:#67e8f9;background:linear-gradient(135deg,rgba(236,72,153,.3),rgba(34,211,238,.2));border:1px solid rgba(103,232,249,.4);box-shadow:0 0 16px rgba(34,211,238,.3)}
.game-title h2{font-size:16px;font-weight:800;color:#fff;letter-spacing:-.01em}
.tagline{font-size:11px;color:#8f84c4;margin-top:1px}
.head-right{display:flex;align-items:center;gap:6px}
.badge{font-size:11px;font-weight:700;color:#b6abe6;background:#1b1342;border:1px solid #2e2260;padding:5px 10px;border-radius:999px;white-space:nowrap}
.badge b{color:#fff;font-variant-numeric:tabular-nums}
.icon-btn{width:30px;height:28px;border-radius:9px;border:1px solid #2e2260;background:#1b1342;color:#d4ccff;font-size:12px;cursor:pointer;transition:background .15s,border-color .15s}
.icon-btn:hover{background:#271b5c;border-color:#5b47b8}
.icon-btn[aria-pressed="false"]{opacity:.55}
.icon-btn:focus-visible,.btn:focus-visible,.ctrl-btn:focus-visible,.act-btn:focus-visible{outline:2px solid #67e8f9;outline-offset:2px}
.canvas-wrap{position:relative;border-radius:16px;overflow:hidden;line-height:0;touch-action:none;background:#08031a;border:1px solid #3b2b7a;box-shadow:0 0 0 1px rgba(0,0,0,.6),0 0 30px -8px rgba(236,72,153,.55)}
canvas{display:block;width:100%;height:auto}
.hud{position:absolute;inset:10px 14px auto 14px;display:flex;align-items:center;justify-content:space-between;gap:10px;pointer-events:none;z-index:2;line-height:1.2}
.hud-score{display:flex;flex-direction:column}
.hud-score small{font-size:9px;font-weight:800;letter-spacing:.14em;color:#c4b5fd}
.hud-score b{font-size:22px;font-weight:900;color:#fff;font-variant-numeric:tabular-nums;text-shadow:0 0 12px rgba(236,72,153,.9)}
.hud-coins{display:flex;align-items:center;gap:6px;font-size:15px;font-weight:900;color:#fde047;font-variant-numeric:tabular-nums;text-shadow:0 0 10px rgba(250,204,21,.7)}
.hud-coins i{width:14px;height:14px;border-radius:50%;background:radial-gradient(circle at 35% 30%,#fff7c2,#facc15 60%,#b45309);box-shadow:0 0 8px rgba(250,204,21,.8)}
.hud-speed{width:56px;height:6px;border-radius:9px;background:rgba(255,255,255,.14);overflow:hidden}
.hud-speed em{display:block;height:100%;width:0;border-radius:9px;background:linear-gradient(90deg,#22d3ee,#ec4899)}
.overlay{position:absolute;inset:0;background:rgba(8,3,26,.62);backdrop-filter:blur(2px);display:flex;align-items:center;justify-content:center;padding:20px;line-height:1.4;z-index:3}
.overlay.hidden{display:none}
.overlay-card{text-align:center;max-width:300px}
.overlay-kicker{font-size:10.5px;font-weight:800;letter-spacing:.2em;text-transform:uppercase;color:#67e8f9;margin-bottom:6px}
.overlay-title{font-size:32px;font-weight:900;letter-spacing:-.02em;color:#fff;margin-bottom:8px}
.overlay-title span{background:linear-gradient(90deg,#f472b6,#22d3ee);-webkit-background-clip:text;background-clip:text;color:transparent}
.overlay-title.over{color:#fb7185;text-shadow:0 0 22px rgba(251,113,133,.6)}
.overlay-sub{font-size:12.5px;color:#c9c1ee;margin:0 auto}
.overlay-sub b{color:#fff}
.overlay-score{font-size:46px;font-weight:900;color:#fff;font-variant-numeric:tabular-nums;text-shadow:0 0 24px rgba(236,72,153,.8);line-height:1.1}
.overlay-best{font-size:12.5px;color:#fbbf24;font-weight:800;margin-top:6px;min-height:18px}
.keys{list-style:none;display:flex;flex-wrap:wrap;gap:6px 12px;justify-content:center;margin:12px 0 0;font-size:11.5px;color:#a79cd6}
.keys li{display:flex;align-items:center;gap:4px}
kbd{font:700 11px system-ui;color:#efeaff;background:#241a55;border:1px solid #4a3a9c;border-bottom-width:2px;border-radius:5px;padding:1px 6px}
.btn{padding:11px 34px;font-size:14px;font-weight:800;border-radius:12px;cursor:pointer;font-family:inherit;transition:transform .12s,box-shadow .2s;border:none;margin-top:16px}
.btn-primary{background:linear-gradient(135deg,#ec4899,#8b5cf6);color:#fff;box-shadow:0 6px 22px -6px rgba(236,72,153,.9)}
.btn-primary:hover{transform:translateY(-1px)}
.controls{display:flex;align-items:center;justify-content:space-between;gap:10px;margin-top:14px}
.dpad,.actions{display:flex;gap:10px}
.ctrl-btn,.act-btn{-webkit-tap-highlight-color:transparent;touch-action:none;cursor:pointer;font-family:inherit;border:1px solid #3a2c80;transition:transform .06s,background .12s,box-shadow .12s}
.ctrl-btn{width:58px;height:60px;border-radius:15px;background:#1b1342;color:#d4ccff;font-size:20px;font-weight:700;box-shadow:0 3px 0 #0b0720}
.ctrl-btn:active{transform:translateY(3px);box-shadow:none;background:#2e2268}
.act-btn{width:78px;height:60px;border-radius:15px;color:#fff;font-size:12.5px;font-weight:900;letter-spacing:.08em}
.act-btn.jump{background:linear-gradient(180deg,#22c55e,#16a34a);box-shadow:0 3px 0 #14532d,0 0 18px -4px rgba(34,197,94,.6);border-color:#4ade80}
.act-btn.slide{background:linear-gradient(180deg,#f59e0b,#d97706);box-shadow:0 3px 0 #78350f,0 0 18px -4px rgba(245,158,11,.6);border-color:#fbbf24}
.act-btn:active{transform:translateY(3px);box-shadow:none}
.hint-text{font-size:11px;color:#7466ad;text-align:center;margin-top:10px}
@media(max-width:400px){.game-card{padding:12px}.overlay-title{font-size:26px}.badge{display:none}}const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const W = 360, H = 480, DPR = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = W * DPR; canvas.height = H * DPR; ctx.setTransform(DPR, 0, 0, DPR, 0, 0); // logic stays 360x480, pixels stay sharp
const BEST_KEY = 'lane-runner-best', MUTE_KEY = 'lane-runner-muted';
const LANES = 3;
const HORIZON = 176, BASE_Y = H - 86, SPACING = 98, FOCAL = 22, SPAWN_D = 150; // pseudo-3D camera: d = distance ahead of the runner
const JUMP_MS = 640, SLIDE_MS = 560;
const reduce = window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches;
// Perspective: depth d -> scale t (1 at the runner, ~0 at the horizon) -> screen y and x
function proj(d) { const t = FOCAL / (FOCAL + d); return { t: t, y: HORIZON + (BASE_Y - HORIZON) * t }; }
function sx(laneF, t) { return W / 2 + (laneF - 1) * SPACING * t; }
const $ = id => document.getElementById(id);
let state = 'idle'; // idle (autopilot demo) | play | paused | over
let laneF, lane, jumpT, slideT, obstacles, coinsList, pickups, particles, streaks = [], stars = [], skyline = [];
let distance, speedU, score, coinCount, shield, invuln, shake = 0, spawnAcc, nextGap, lastFrame = 0, runClock = 0, flash = 0, streak = 0;
function getBest() { try { return parseInt(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } }
function setBest(v) { try { localStorage.setItem(BEST_KEY, String(v)); } catch (e) {} }
function rand(a, b) { return a + Math.random() * (b - a); }
// ── Sound: small WebAudio synth, created on the first click ──
let actx = null, muted = false;
try { muted = localStorage.getItem(MUTE_KEY) === '1'; } catch (e) {}
function audio() {
if (!actx) { try { actx = new (window.AudioContext || window.webkitAudioContext)(); } catch (e) {} }
if (actx && actx.state === 'suspended') actx.resume();
return actx;
}
function tone(f0, f1, dur, type, vol) {
if (muted) return; const a = audio(); if (!a) return;
const o = a.createOscillator(), g = a.createGain(), t = a.currentTime;
o.type = type; o.frequency.setValueAtTime(f0, t); o.frequency.exponentialRampToValueAtTime(Math.max(30, f1), t + dur);
g.gain.setValueAtTime(vol, t); g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
o.connect(g); g.connect(a.destination); o.start(t); o.stop(t + dur);
}
function noise(dur, vol, cutoff) {
if (muted) return; const a = audio(); if (!a) return;
const len = Math.floor(a.sampleRate * dur), buf = a.createBuffer(1, len, a.sampleRate), d = buf.getChannelData(0);
for (let i = 0; i < len; i++) d[i] = (Math.random() * 2 - 1) * (1 - i / len);
const s = a.createBufferSource(), f = a.createBiquadFilter(), g = a.createGain();
s.buffer = buf; f.type = 'lowpass'; f.frequency.value = cutoff; g.gain.value = vol; s.connect(f); f.connect(g); g.connect(a.destination); s.start();
}
const sfx = {
jump: () => tone(300, 760, 0.16, 'sine', 0.07),
slide: () => noise(0.22, 0.06, 2400),
lane: () => tone(520, 380, 0.06, 'triangle', 0.035),
coin: n => { tone(880 + Math.min(n, 8) * 70, 1320 + Math.min(n, 8) * 70, 0.1, 'square', 0.03); },
shield: () => { tone(420, 1100, 0.3, 'sawtooth', 0.045); },
crash: () => { noise(0.5, 0.14, 800); tone(220, 40, 0.5, 'sawtooth', 0.08); },
pop: () => { noise(0.25, 0.1, 3000); tone(900, 200, 0.25, 'triangle', 0.05); },
};
// ── World setup ──
function makeScenery() {
stars = []; for (let i = 0; i < 46; i++) stars.push({ x: rand(0, W), y: rand(0, HORIZON - 30), r: rand(0.5, 1.5), p: rand(0, 6) });
skyline = []; for (let x = -30; x < W + 60; x += 0) { const w = rand(16, 34), h = rand(18, 76); skyline.push({ x, w, h, win: Math.random() }); x += w + rand(0, 4); }
streaks = []; for (let i = 0; i < 16; i++) streaks.push({ a: rand(0, Math.PI * 2), r: rand(40, 400), len: rand(20, 60) });
}
function resetState() {
lane = 1; laneF = 1; jumpT = 0; slideT = 0; obstacles = []; coinsList = []; pickups = []; particles = [];
distance = 0; speedU = 34; score = 0; coinCount = 0; shield = 0; invuln = 0; spawnAcc = 0; nextGap = 40; streak = 0; flash = 0;
syncHud();
}
function syncHud() { $('hud-score').textContent = score.toLocaleString('en-US'); $('hud-coins').textContent = coinCount; $('hud-speed').style.width = Math.round(((speedU - 34) / 72) * 100) + '%'; }
// ── Spawning: rows of obstacles, coin lines and shield pickups ──
function addCoins(l, n, d0) { for (let i = 0; i < n; i++) coinsList.push({ lane: l, d: d0 + i * 6, phase: rand(0, 6), taken: false }); }
function spawnRow() {
const r = Math.random(), d = SPAWN_D;
if (r < 0.3) { // one barrier or gate, coins in the other lanes
const l = (Math.random() * LANES) | 0, type = Math.random() < 0.5 ? 'barrier' : 'gate';
obstacles.push({ lane: l, d, type }); addCoins((l + 1 + ((Math.random() * 2) | 0)) % LANES, 4, d - 4);
} else if (r < 0.58) { // two solid blocks, one lane left open (with coins in it)
const open = (Math.random() * LANES) | 0;
for (let i = 0; i < LANES; i++) if (i !== open) obstacles.push({ lane: i, d, type: 'block' });
addCoins(open, 5, d - 6);
} else if (r < 0.72) { // a wall across every lane: jump it or slide it
const type = Math.random() < 0.5 ? 'barrier' : 'gate';
for (let i = 0; i < LANES; i++) obstacles.push({ lane: i, d, type });
} else if (r < 0.9) { // a clean coin line to reward lane choice
addCoins((Math.random() * LANES) | 0, 7, d);
} else if (!shield) {
pickups.push({ lane: (Math.random() * LANES) | 0, d, type: 'shield' });
} else {
addCoins((Math.random() * LANES) | 0, 5, d);
}
nextGap = Math.max(30, speedU * 0.62) + rand(0, 14); // the gap grows with speed so there is always time to react
}
// ── Actions: all one-shot taps ──
function moveLane(dir) { const n = Math.max(0, Math.min(LANES - 1, lane + dir)); if (n !== lane) { lane = n; if (state === 'play') sfx.lane(); } }
function doJump() { if (state !== 'play' || jumpT > 0) return; slideT = 0; jumpT = JUMP_MS; sfx.jump(); }
function doSlide() { if (state !== 'play' || slideT > 0) return; jumpT = 0; slideT = SLIDE_MS; sfx.slide(); } // sliding in mid-air drops you straight down
function burst(x, y, color, n, speed) { for (let i = 0; i < n; i++) { const a = rand(0, Math.PI * 2), s = rand(0.3, 1) * (speed || 3); particles.push({ x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s, life: 1, decay: rand(0.02, 0.05), color }); } }
// Demo autopilot: dodges so the title screen shows a live, playing scene
function autopilot() {
let near = null;
obstacles.forEach(o => { if (o.d > 0 && o.d < 30 && Math.abs(o.lane - lane) < 0.5 && (!near || o.d < near.d)) near = o; });
if (!near) return;
if (near.type === 'block') {
const free = [0, 1, 2].filter(l => !obstacles.some(o => o.type === 'block' && Math.abs(o.d - near.d) < 8 && o.lane === l));
if (free.length) { const target = free.reduce((a, b) => Math.abs(b - lane) < Math.abs(a - lane) ? b : a); lane += Math.sign(target - lane); }
} else if (near.d < 11) { near.type === 'barrier' ? (jumpT <= 0 && (jumpT = JUMP_MS)) : (slideT <= 0 && (slideT = SLIDE_MS)); }
}
function crash() {
burst(sx(laneF, 1), BASE_Y - 30, '#f472b6', 30, 5); burst(sx(laneF, 1), BASE_Y - 30, '#22d3ee', 16, 3);
shake = 14; flash = 1; sfx.crash(); endGame();
}
// ── Simulation ──
function update(dt) {
const ms = dt * 1000, demo = state === 'idle';
speedU = Math.min(106, 34 + distance * 0.012);
distance += speedU * dt; runClock += dt * (0.7 + speedU / 60);
if (!demo) { score = Math.floor(distance / 10) + coinCount * 10; }
laneF += (lane - laneF) * Math.min(1, dt * 16);
if (demo) autopilot();
if (jumpT > 0) jumpT -= ms; if (slideT > 0) slideT -= ms; if (invuln > 0) invuln -= dt;
spawnAcc += speedU * dt; if (spawnAcc >= nextGap) { spawnAcc = 0; spawnRow(); }
const mv = speedU * dt;
obstacles.forEach(o => o.d -= mv); coinsList.forEach(c => c.d -= mv); pickups.forEach(p => p.d -= mv);
const jumping = jumpT > 0 && Math.sin((1 - jumpT / JUMP_MS) * Math.PI) > 0.28, sliding = slideT > 0;
// obstacles: same lane and passing through the runner's depth
for (let i = obstacles.length - 1; i >= 0; i--) {
const o = obstacles[i];
if (o.d < -1.3 || o.d > 1.3 || Math.abs(o.lane - laneF) > 0.55) continue;
if (o.type === 'barrier' && jumping) continue;
if (o.type === 'gate' && sliding) continue;
if (demo || invuln > 0) continue;
if (shield) { shield = 0; invuln = 1.3; burst(sx(o.lane, 1), BASE_Y - 24, '#67e8f9', 24, 4); burst(sx(o.lane, 1), BASE_Y - 24, '#ffffff', 10, 2.5); obstacles.splice(i, 1); shake = 7; sfx.pop(); continue; }
crash(); return;
}
// coins and pickups
coinsList.forEach(c => {
if (c.taken || c.d > 2.5 || c.d < -1.5 || Math.abs(c.lane - laneF) > 0.6) return;
c.taken = true; if (demo) return;
coinCount++; streak++; score = Math.floor(distance / 10) + coinCount * 10; sfx.coin(streak);
const p = proj(0); burst(sx(c.lane, 1), p.y - 28, '#fde047', 7, 2.4);
});
pickups.forEach(p => { if (!p.taken && p.d < 2.5 && p.d > -1.5 && Math.abs(p.lane - laneF) < 0.6) { p.taken = true; if (!demo) { shield = 1; sfx.shield(); burst(sx(p.lane, 1), BASE_Y - 30, '#67e8f9', 18, 3.4); } } });
coinsList.forEach(c => { if (!c.taken && c.d < -1.5) streak = 0; });
obstacles = obstacles.filter(o => o.d > -6); coinsList = coinsList.filter(c => c.d > -6 && !c.taken); pickups = pickups.filter(p => p.d > -6 && !p.taken);
if (!demo) syncHud();
}
function moveVisuals(dt) {
particles.forEach(p => { p.x += p.vx; p.y += p.vy; p.vy += 0.08; p.life -= p.decay; }); particles = particles.filter(p => p.life > 0);
const k = Math.min(1, (speedU - 34) / 72 + 0.15);
streaks.forEach(s => { s.r += (120 + 520 * k) * dt; if (s.r > 430) { s.r = rand(30, 90); s.a = rand(0, Math.PI * 2); } });
shake *= Math.pow(0.88, dt * 60); flash *= Math.pow(0.9, dt * 60);
}
// ── Rendering ──
const SKY = '#2a0a55';
function drawSky(now) {
const g = ctx.createLinearGradient(0, 0, 0, HORIZON); g.addColorStop(0, '#07031a'); g.addColorStop(.55, SKY); g.addColorStop(1, '#c0266d');
ctx.fillStyle = g; ctx.fillRect(0, 0, W, HORIZON + 1);
ctx.fillStyle = '#fff'; stars.forEach(s => { ctx.globalAlpha = 0.35 + 0.4 * Math.sin(now / 700 + s.p); ctx.fillRect(s.x, s.y, s.r, s.r); }); ctx.globalAlpha = 1;
const cx = W / 2, cy = HORIZON - 36, r = 60; // retro sun with cut stripes
ctx.save(); ctx.beginPath(); ctx.arc(cx, cy, r, 0, 7); ctx.clip();
const sg = ctx.createLinearGradient(0, cy - r, 0, cy + r); sg.addColorStop(0, '#ffe36b'); sg.addColorStop(.55, '#ff7a59'); sg.addColorStop(1, '#ff2d95');
ctx.fillStyle = sg; ctx.fillRect(cx - r, cy - r, r * 2, r * 2);
ctx.fillStyle = '#3a0c5f'; for (let i = 0; i < 6; i++) ctx.fillRect(cx - r, cy + 4 + i * 11 + ((now / 120) % 1) * 2, r * 2, 2 + i * 1.6);
ctx.restore();
const off = (laneF - 1) * -8; // skyline parallax follows the lane
ctx.fillStyle = '#12072e'; skyline.forEach(b => ctx.fillRect(b.x + off, HORIZON - b.h, b.w, b.h + 1));
ctx.fillStyle = 'rgba(255,184,107,.55)'; skyline.forEach((b, i) => { if (b.win > 0.45) for (let k = 0; k < 3; k++) ctx.fillRect(b.x + off + 4 + k * 8, HORIZON - b.h + 6 + ((i + k) % 4) * 9, 3, 3); });
}
function drawGround() {
const g = ctx.createLinearGradient(0, HORIZON, 0, H); g.addColorStop(0, '#2a0f55'); g.addColorStop(1, '#0d0624');
ctx.fillStyle = g; ctx.fillRect(0, HORIZON, W, H - HORIZON);
const yb = HORIZON + (BASE_Y - HORIZON) * 1.4; // the grid outside the track
ctx.lineWidth = 1; ctx.strokeStyle = 'rgba(255,70,200,.2)';
for (let m = -9; m <= 9; m++) { ctx.beginPath(); ctx.moveTo(W / 2, HORIZON); ctx.lineTo(W / 2 + m * SPACING * 1.4 * 1.5, yb); ctx.stroke(); }
const L = 10, base = Math.floor(distance / L); // scrolling bands: alternate shade, dashes on every other one
for (let k = 0; k < 34; k++) {
const dN = k * L - (distance % L), dF = dN + L, pN = proj(Math.max(dN, -8)), pF = proj(dF), par = (base + k) % 2;
const hwN = 1.5 * SPACING * pN.t, hwF = 1.5 * SPACING * pF.t;
ctx.fillStyle = par ? '#1c1247' : '#251a5c'; ctx.beginPath(); ctx.moveTo(W / 2 - hwF, pF.y); ctx.lineTo(W / 2 + hwF, pF.y); ctx.lineTo(W / 2 + hwN, pN.y); ctx.lineTo(W / 2 - hwN, pN.y); ctx.closePath(); ctx.fill();
ctx.strokeStyle = 'rgba(255,70,200,.14)'; ctx.beginPath(); ctx.moveTo(0, pN.y); ctx.lineTo(W, pN.y); ctx.stroke();
if (!par) { ctx.fillStyle = 'rgba(180,200,255,.5)'; [0.5, 1.5].forEach(lf => { const wN = 1.6 * pN.t, wF = 1.6 * pF.t; ctx.beginPath(); ctx.moveTo(sx(lf, pF.t) - wF, pF.y); ctx.lineTo(sx(lf, pF.t) + wF, pF.y); ctx.lineTo(sx(lf, pN.t) + wN, pN.y); ctx.lineTo(sx(lf, pN.t) - wN, pN.y); ctx.closePath(); ctx.fill(); }); }
}
ctx.save(); ctx.lineWidth = 3; ctx.shadowBlur = 12; // glowing track edges
[[-0.5, '#22d3ee'], [2.5, '#ec4899']].forEach(e => { ctx.strokeStyle = e[1]; ctx.shadowColor = e[1]; ctx.beginPath(); ctx.moveTo(sx(e[0], 0.02), HORIZON + 1); ctx.lineTo(sx(e[0], 1.4), yb); ctx.stroke(); });
ctx.restore();
}
function drawSpeedLines() {
const k = (speedU - 34) / 72; if (k < 0.2) return;
ctx.save(); ctx.lineCap = 'round'; ctx.lineWidth = 1.5; ctx.strokeStyle = 'rgba(255,255,255,' + (0.1 + k * 0.28) + ')';
streaks.forEach(s => { const x0 = W / 2 + Math.cos(s.a) * s.r, y0 = HORIZON + Math.sin(s.a) * s.r * 0.6, x1 = W / 2 + Math.cos(s.a) * (s.r + s.len * k), y1 = HORIZON + Math.sin(s.a) * (s.r + s.len * k) * 0.6; if (y0 > HORIZON) { ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(x1, y1); ctx.stroke(); } });
ctx.restore();
}
function shadow(x, y, w, a) { ctx.fillStyle = 'rgba(0,0,0,' + a + ')'; ctx.beginPath(); ctx.ellipse(x, y, w, w * 0.22, 0, 0, 7); ctx.fill(); }
function drawObstacle(o) {
const p = proj(o.d), t = p.t, x = sx(o.lane, t), y = p.y, w = SPACING * 0.8 * t; if (t < 0.03) return;
ctx.save(); ctx.globalAlpha = Math.min(1, t * 5 + 0.15); shadow(x, y, w * 0.55, 0.35);
if (o.type === 'barrier') { // low wall: jump
const h = 30 * t, g = ctx.createLinearGradient(0, y - h, 0, y); g.addColorStop(0, '#4ade80'); g.addColorStop(1, '#15803d');
ctx.shadowColor = '#22c55e'; ctx.shadowBlur = 12 * t; ctx.fillStyle = g; ctx.fillRect(x - w / 2, y - h, w, h); ctx.shadowBlur = 0;
ctx.fillStyle = 'rgba(5,46,22,.85)'; for (let i = 0; i < 5; i++) { ctx.beginPath(); const bx = x - w / 2 + i * w / 5; ctx.moveTo(bx, y); ctx.lineTo(bx + w / 10, y - h); ctx.lineTo(bx + w / 5 - w / 10 + 0.1, y - h); ctx.lineTo(bx + w / 10, y); ctx.fill(); }
ctx.fillStyle = 'rgba(255,255,255,.4)'; ctx.fillRect(x - w / 2, y - h, w, 2 * t + 0.5);
} else if (o.type === 'gate') { // overhead beam: slide under
const ph = 84 * t, bh = 15 * t, pw = 8 * t;
ctx.shadowColor = '#f59e0b'; ctx.shadowBlur = 12 * t; ctx.fillStyle = '#b45309'; ctx.fillRect(x - w / 2, y - ph, pw, ph); ctx.fillRect(x + w / 2 - pw, y - ph, pw, ph);
const g = ctx.createLinearGradient(0, y - ph, 0, y - ph + bh); g.addColorStop(0, '#fcd34d'); g.addColorStop(1, '#d97706'); ctx.fillStyle = g; ctx.fillRect(x - w / 2 - 3 * t, y - ph, w + 6 * t, bh); ctx.shadowBlur = 0;
ctx.fillStyle = '#451a03'; for (let i = 0; i < 6; i++) ctx.fillRect(x - w / 2 + i * (w / 6) + 1 * t, y - ph + 2 * t, 7 * t, bh - 4 * t);
} else { // solid block: change lane
const h = 60 * t, g = ctx.createLinearGradient(0, y - h, 0, y); g.addColorStop(0, '#fb7185'); g.addColorStop(1, '#be123c');
ctx.shadowColor = '#f43f5e'; ctx.shadowBlur = 14 * t; ctx.fillStyle = g; ctx.fillRect(x - w / 2, y - h, w, h); ctx.shadowBlur = 0;
ctx.fillStyle = '#fda4af'; ctx.beginPath(); ctx.moveTo(x - w / 2, y - h); ctx.lineTo(x + w / 2, y - h); ctx.lineTo(x + w / 2 * 0.86, y - h - 7 * t); ctx.lineTo(x - w / 2 * 0.86, y - h - 7 * t); ctx.closePath(); ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,.55)'; ctx.lineWidth = Math.max(1, 3 * t); ctx.beginPath(); ctx.moveTo(x - w * 0.3, y - h * 0.8); ctx.lineTo(x + w * 0.3, y - h * 0.2); ctx.moveTo(x + w * 0.3, y - h * 0.8); ctx.lineTo(x - w * 0.3, y - h * 0.2); ctx.stroke();
}
ctx.restore();
}
function drawCoin(c, now) {
const p = proj(c.d), t = p.t, x = sx(c.lane, t), y = p.y - 26 * t, r = 9 * t, sq = Math.abs(Math.cos(now / 240 + c.phase)); if (t < 0.04) return;
ctx.save(); ctx.globalAlpha = Math.min(1, t * 5); ctx.shadowColor = '#facc15'; ctx.shadowBlur = 10 * t;
const g = ctx.createRadialGradient(x - r * 0.3, y - r * 0.3, 1, x, y, r); g.addColorStop(0, '#fff7c2'); g.addColorStop(.6, '#facc15'); g.addColorStop(1, '#b45309');
ctx.fillStyle = g; ctx.beginPath(); ctx.ellipse(x, y, Math.max(1, r * (0.25 + 0.75 * sq)), r, 0, 0, 7); ctx.fill(); ctx.restore();
}
function drawPickup(pk, now) {
const p = proj(pk.d), t = p.t, x = sx(pk.lane, t), y = p.y - 30 * t - Math.sin(now / 300) * 4 * t, r = 14 * t; if (t < 0.04) return;
ctx.save(); ctx.shadowColor = '#22d3ee'; ctx.shadowBlur = 16 * t; ctx.fillStyle = 'rgba(34,211,238,.25)'; ctx.strokeStyle = '#67e8f9'; ctx.lineWidth = Math.max(1, 2 * t);
ctx.beginPath(); ctx.arc(x, y, r, 0, 7); ctx.fill(); ctx.stroke();
ctx.beginPath(); ctx.moveTo(x, y - r * 0.6); ctx.lineTo(x + r * 0.5, y - r * 0.3); ctx.lineTo(x + r * 0.4, y + r * 0.3); ctx.lineTo(x, y + r * 0.6); ctx.lineTo(x - r * 0.4, y + r * 0.3); ctx.lineTo(x - r * 0.5, y - r * 0.3); ctx.closePath(); ctx.fillStyle = '#e0faff'; ctx.fill(); ctx.restore();
}
function drawRunner(now) {
const x = sx(laneF, 1), jp = jumpT > 0 ? Math.sin((1 - jumpT / JUMP_MS) * Math.PI) : 0, lift = jp * 52, y0 = BASE_Y - lift;
const sliding = slideT > 0, ph = runClock * 14, show = !(invuln > 0 && Math.floor(invuln * 12) % 2);
shadow(x, BASE_Y + 1, 17 - jp * 6, 0.45 - jp * 0.15);
if (!show) return;
ctx.save(); ctx.lineCap = 'round'; ctx.lineJoin = 'round'; ctx.shadowColor = '#22d3ee'; ctx.shadowBlur = 10;
if (sliding) { // low slide pose
ctx.strokeStyle = '#0ea5e9'; ctx.lineWidth = 8; ctx.beginPath(); ctx.moveTo(x - 8, y0 - 6); ctx.lineTo(x - 15, y0 + 1); ctx.moveTo(x + 8, y0 - 6); ctx.lineTo(x + 15, y0 + 1); ctx.stroke();
const g = ctx.createLinearGradient(0, y0 - 24, 0, y0 - 6); g.addColorStop(0, '#38bdf8'); g.addColorStop(1, '#2563eb'); ctx.fillStyle = g;
ctx.beginPath(); ctx.roundRect ? ctx.roundRect(x - 19, y0 - 24, 38, 18, 8) : ctx.rect(x - 19, y0 - 24, 38, 18); ctx.fill();
ctx.fillStyle = '#1e1b4b'; ctx.beginPath(); ctx.arc(x, y0 - 29, 8, 0, 7); ctx.fill(); ctx.fillStyle = '#ec4899'; ctx.fillRect(x - 3, y0 - 32, 6, 2.5);
} else {
const swing = jp > 0 ? 0 : Math.sin(ph), l1 = jp > 0 ? 7 : Math.max(0, swing) * 10, l2 = jp > 0 ? 7 : Math.max(0, -swing) * 10, bob = jp > 0 ? 0 : Math.abs(Math.sin(ph)) * 2;
const hipY = y0 - 25 - bob;
ctx.strokeStyle = '#0369a1'; ctx.lineWidth = 8; // legs
ctx.beginPath(); ctx.moveTo(x - 5, hipY); ctx.lineTo(x - 6 - (jp > 0 ? 3 : 0), y0 - l1); ctx.moveTo(x + 5, hipY); ctx.lineTo(x + 6 + (jp > 0 ? 3 : 0), y0 - l2); ctx.stroke();
ctx.strokeStyle = '#ec4899'; ctx.lineWidth = 3.5; ctx.beginPath(); ctx.moveTo(x - 6, y0 - l1 - 1); ctx.lineTo(x - 6 - (jp > 0 ? 3 : 0), y0 - l1); ctx.moveTo(x + 6, y0 - l2 - 1); ctx.lineTo(x + 6 + (jp > 0 ? 3 : 0), y0 - l2); ctx.stroke();
const ay = jp > 0 ? -7 : Math.sin(ph + Math.PI) * 6; // arms
ctx.strokeStyle = '#38bdf8'; ctx.lineWidth = 6; ctx.beginPath(); ctx.moveTo(x - 11, y0 - 48 - bob); ctx.lineTo(x - 17, y0 - 36 - bob + ay); ctx.moveTo(x + 11, y0 - 48 - bob); ctx.lineTo(x + 17, y0 - 36 - bob - ay); ctx.stroke();
const g = ctx.createLinearGradient(0, y0 - 54, 0, y0 - 24); g.addColorStop(0, '#38bdf8'); g.addColorStop(1, '#2563eb'); ctx.fillStyle = g; // torso
ctx.beginPath(); ctx.roundRect ? ctx.roundRect(x - 12, y0 - 54 - bob, 24, 30, 9) : ctx.rect(x - 12, y0 - 54 - bob, 24, 30); ctx.fill();
ctx.fillStyle = '#ec4899'; ctx.fillRect(x - 2, y0 - 52 - bob, 4, 24); // jacket stripe
ctx.fillStyle = '#1e1b4b'; ctx.beginPath(); ctx.arc(x, y0 - 63 - bob, 9, 0, 7); ctx.fill(); // helmet seen from behind
ctx.fillStyle = '#22d3ee'; ctx.fillRect(x - 5, y0 - 65 - bob, 10, 2.5); ctx.fillStyle = '#f472b6'; ctx.fillRect(x - 2, y0 - 72 - bob, 4, 5);
}
ctx.restore();
if (shield) { ctx.save(); const pulse = 1 + Math.sin(now / 160) * 0.05; ctx.strokeStyle = 'rgba(103,232,249,.9)'; ctx.fillStyle = 'rgba(34,211,238,.14)'; ctx.lineWidth = 2; ctx.shadowColor = '#22d3ee'; ctx.shadowBlur = 14; ctx.beginPath(); ctx.arc(x, y0 - 34, 36 * pulse, 0, 7); ctx.fill(); ctx.stroke(); ctx.restore(); }
}
function draw(now) {
ctx.fillStyle = '#08031a'; ctx.fillRect(0, 0, W, H);
ctx.save(); if (shake > 0.4 && !reduce) ctx.translate(rand(-shake, shake) / 2, rand(-shake, shake) / 2);
drawSky(now); drawGround(); drawSpeedLines();
const far = [], near = [];
obstacles.forEach(o => (o.d > 0 ? far : near).push({ d: o.d, f: () => drawObstacle(o) }));
coinsList.forEach(c => (c.d > 0 ? far : near).push({ d: c.d, f: () => drawCoin(c, now) }));
pickups.forEach(p => (p.d > 0 ? far : near).push({ d: p.d, f: () => drawPickup(p, now) }));
far.sort((a, b) => b.d - a.d).forEach(e => e.f());
if (state !== 'idle') drawRunner(now);
near.sort((a, b) => b.d - a.d).forEach(e => e.f()); // things already behind the runner's line draw on top
particles.forEach(p => { ctx.globalAlpha = Math.max(0, p.life); ctx.fillStyle = p.color; ctx.fillRect(p.x - 1.5, p.y - 1.5, 3, 3); }); ctx.globalAlpha = 1;
ctx.restore();
if (flash > 0.02) { ctx.fillStyle = 'rgba(255,255,255,' + flash * 0.5 + ')'; ctx.fillRect(0, 0, W, H); }
}
// One permanent loop: simulate while playing (or as the idle demo), otherwise only draw
function frame(ts) {
requestAnimationFrame(frame);
if (!lastFrame) lastFrame = ts;
const dt = Math.min((ts - lastFrame) / 1000, 0.05); lastFrame = ts;
if (state === 'play' || state === 'idle') update(dt);
if (state !== 'paused') moveVisuals(dt);
draw(ts);
}
// ── State changes ──
function startGame() {
audio(); resetState(); state = 'play'; lastFrame = 0;
['start-overlay', 'pause-overlay', 'gameover-overlay'].forEach(id => $(id).classList.add('hidden'));
}
function pauseGame() { if (state !== 'play') return; state = 'paused'; $('pause-overlay').classList.remove('hidden'); $('btn-pause').setAttribute('aria-label', 'Resume'); }
function resumeGame() { if (state !== 'paused') return; state = 'play'; lastFrame = 0; $('pause-overlay').classList.add('hidden'); $('btn-pause').setAttribute('aria-label', 'Pause'); }
function endGame() {
state = 'over'; score = Math.floor(distance / 10) + coinCount * 10; syncHud();
const best = getBest();
$('final-score').textContent = score.toLocaleString('en-US');
$('final-detail').textContent = Math.floor(distance / 10) + ' m · ' + coinCount + ' coins';
if (score > best) { setBest(score); $('best-score').textContent = score; $('best-msg').textContent = '🏆 New high score!'; } else $('best-msg').textContent = 'Best ' + best;
setTimeout(() => { if (state === 'over') $('gameover-overlay').classList.remove('hidden'); }, 450); // let the crash play out first
}
// ── Input ──
const SCROLL_KEYS = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Space', 'PageUp', 'PageDown'];
document.addEventListener('keydown', e => {
if (e.ctrlKey || e.metaKey || e.altKey) return;
if (SCROLL_KEYS.indexOf(e.code) !== -1) e.preventDefault(); // arrows and Space must never scroll the page
if (e.repeat) return;
if (e.code === 'ArrowLeft' || e.code === 'KeyA') moveLane(-1);
else if (e.code === 'ArrowRight' || e.code === 'KeyD') moveLane(1);
else if (e.code === 'ArrowUp' || e.code === 'KeyW' || e.code === 'Space') { if (state === 'idle' || state === 'over') startGame(); else if (state === 'paused') resumeGame(); else doJump(); }
else if (e.code === 'ArrowDown' || e.code === 'KeyS') doSlide();
else if (e.code === 'KeyP' || e.code === 'Escape') state === 'play' ? pauseGame() : resumeGame();
else if (e.code === 'KeyM') toggleSound();
else if (e.code === 'Enter' && (state === 'idle' || state === 'over')) startGame();
});
window.addEventListener('blur', pauseGame);
document.addEventListener('visibilitychange', () => { if (document.hidden) pauseGame(); });
function tap(id, fn) { const el = $(id); el.addEventListener('pointerdown', e => { e.preventDefault(); fn(); }); el.addEventListener('contextmenu', e => e.preventDefault()); }
tap('btn-left', () => moveLane(-1)); tap('btn-right', () => moveLane(1)); tap('btn-jump', doJump); tap('btn-slide', doSlide);
// Swipe on the track: left / right = lane, up = jump, down = slide, a short tap = jump
let sw = null;
const wrapEl = document.querySelector('.canvas-wrap');
wrapEl.addEventListener('pointerdown', e => { if (e.target.closest('.overlay,.hud') && state !== 'play') return; sw = { x: e.clientX, y: e.clientY, id: e.pointerId }; });
wrapEl.addEventListener('pointerup', e => {
if (!sw || sw.id !== e.pointerId || state !== 'play') { sw = null; return; }
const dx = e.clientX - sw.x, dy = e.clientY - sw.y; sw = null;
if (Math.max(Math.abs(dx), Math.abs(dy)) < 18) doJump();
else if (Math.abs(dx) > Math.abs(dy)) moveLane(dx > 0 ? 1 : -1);
else dy < 0 ? doJump() : doSlide();
});
function toggleSound() { muted = !muted; try { localStorage.setItem(MUTE_KEY, muted ? '1' : '0'); } catch (e) {} syncSound(); if (!muted) tone(660, 880, 0.08, 'triangle', 0.05); }
function syncSound() { const b = $('btn-sound'); b.textContent = muted ? '🔇' : '🔊'; b.setAttribute('aria-pressed', String(!muted)); b.setAttribute('aria-label', muted ? 'Sound off' : 'Sound on'); }
$('btn-sound').addEventListener('click', toggleSound);
$('btn-pause').addEventListener('click', () => { state === 'play' ? pauseGame() : resumeGame(); });
$('btn-start').addEventListener('click', startGame); $('btn-retry').addEventListener('click', startGame); $('btn-resume').addEventListener('click', resumeGame);
$('best-score').textContent = getBest();
syncSound(); makeScenery(); resetState();
requestAnimationFrame(frame);
/* wdp-share:start */
(function () {
var TITLE = 'Lane Runner Endless Game', URL = 'https://webdevpuneet.com/ui-snippets/lane-runner-game/';
if (window.__wdpShare) return;
window.__wdpShare = true;
var blob = null, file = null, lastInfo = null;
/* ---- UI ---- */
var st = document.createElement('style');
st.textContent =
'.wdps-in,.wdps-btn,.wdps-box{touch-action:manipulation;-webkit-tap-highlight-color:transparent}.wdps-in{display:block;margin:12px auto 0;border:0;border-radius:12px;padding:11px 22px;font:800 15px/1 system-ui,sans-serif;background:#16a34a;color:#fff;cursor:pointer;box-shadow:0 3px 0 #11793a;pointer-events:auto;position:relative;z-index:5}' +
'.wdps-in:hover{background:#15803d}.wdps-in:active{transform:translateY(2px);box-shadow:0 1px 0 #11793a}' +
'.wdps-btn{position:fixed;right:12px;bottom:12px;z-index:2147483000;border:0;border-radius:99px;padding:9px 16px;font:700 13px/1 system-ui,sans-serif;background:#16a34a;color:#fff;cursor:pointer;box-shadow:0 3px 12px rgba(0,0,0,.3)}' +
'.wdps-btn.wdps-hot{animation:wdps-p 1.4s ease-in-out infinite}' +
'@keyframes wdps-p{0%,100%{box-shadow:0 0 0 0 rgba(22,163,74,.6),0 3px 12px rgba(0,0,0,.3)}50%{box-shadow:0 0 0 9px rgba(22,163,74,0),0 3px 12px rgba(0,0,0,.3)}}' +
'.wdps-btn:active{transform:translateY(1px)}' +
'.wdps-in[hidden],.wdps-btn[hidden],.wdps-box[hidden],.wdps-box [hidden]{display:none}' +
'.wdps-box{position:fixed;left:50%;bottom:12px;transform:translateX(-50%);z-index:2147483001;width:max-content;max-width:calc(100vw - 24px);box-sizing:border-box;background:#fff;color:#64748b;border:1px solid #d5e2ee;border-radius:12px;padding:8px 12px;font:700 12px/1.4 system-ui,sans-serif;box-shadow:0 8px 26px rgba(15,23,42,.25);display:flex;flex-wrap:wrap;gap:8px;align-items:center}' +
'.wdps-box button,.wdps-box a{border:1px solid #d5e2ee;background:#f8fafc;color:#1e293b;border-radius:99px;padding:5px 12px;font:700 12px system-ui,sans-serif;text-decoration:none;cursor:pointer}' +
'.wdps-box .wdps-x{border:0;background:none;font-size:19px;line-height:1;padding:0 4px;margin-left:auto;color:#475569}' +
'@media (max-width:700px){.wdps-box{width:calc(100vw - 24px);justify-content:center;padding:10px 12px}.wdps-box>span{width:100%;text-align:center}.wdps-box button,.wdps-box a{padding:9px 14px;font-size:13px}.wdps-box .wdps-x{position:absolute;top:4px;right:4px}.wdps-in{width:100%;max-width:320px}}';
document.head.appendChild(st);
var pill = document.createElement('button');
pill.type = 'button'; pill.className = 'wdps-btn'; pill.textContent = 'Share result';
var inl = document.createElement('button');
inl.type = 'button'; inl.className = 'wdps-in'; inl.textContent = 'Share result'; inl.hidden = true;
var box = document.createElement('div');
box.className = 'wdps-box'; box.hidden = true;
box.setAttribute('role', 'dialog'); box.setAttribute('aria-label', 'Share your result');
box.innerHTML =
'<span data-a="msg">Share your result:</span>' +
'<button type="button" data-a="copy">Copy image</button>' +
'<a data-a="dl" download="result.png">Download image</a>' +
'<button type="button" data-a="more" hidden>More…</button>' +
'<a data-a="wa" target="_blank" rel="noopener">WhatsApp</a>' +
'<a data-a="x" target="_blank" rel="noopener">X</a>' +
'<a data-a="fb" target="_blank" rel="noopener">Facebook</a>' +
'<button type="button" class="wdps-x" data-a="close" aria-label="Close share options">×</button>';
document.body.appendChild(box);
document.body.appendChild(pill);
function q(a) { return box.querySelector('[data-a="' + a + '"]'); }
function ours(el) { return el === inl || el === pill || !!el.closest('.wdps-box'); }
/* ---- reading the result from the screen ---- */
function shown(el) {
if (!el || !el.getClientRects().length) return false;
for (var n = el; n && n !== document.body; n = n.parentElement) {
var cs = getComputedStyle(n);
if (cs.display === 'none' || cs.visibility === 'hidden' || parseFloat(cs.opacity) < 0.1) return false;
if (n.hidden || n.getAttribute('aria-hidden') === 'true') return false;
}
return true;
}
function txt(el) { return (el.textContent || '').replace(/\s+/g, ' ').trim(); }
function sig(el) { return ((el.id || '') + ' ' + (typeof el.className === 'string' ? el.className : '')).toLowerCase(); }
var OUTCOME = /(you win|you won|win!|wins|victory|game over|time.?s up|cleared|complete|solved|defeat|you lose|you lost|lost|draw|crash|failed|success|congrat|score|result|finished|beat|all matched|well done|perfect|new best)/i;
var HEADSIG = /(title|headline|msg|message|banner|result|verdict|status)/;
var WIN = /(you win|you won|win!|wins|victory|cleared|complete|solved|success|congrat|beat|matched|perfect|new best|record)/i;
var LOSS = /(game over|time.?s up|defeat|you lose|you lost|lost|crash|failed|out of)/i;
var BOX = /(overlay|modal|banner|result|over|end|dialog|screen|popup|panel|card|finish|win)/;
function readResult() {
var head = '', sub = '', root = null, headEl = null;
var all = document.body.querySelectorAll('h1,h2,h3,h4,p,strong,div,span');
for (var i = 0; i < all.length && !head; i++) {
var el = all[i];
if (ours(el) || el.children.length > 2) continue;
if (!HEADSIG.test(sig(el))) continue;
/* a banner that holds its own title: use the title inside it instead */
var inner = false;
for (var c = 0; c < el.children.length; c++) if (HEADSIG.test(sig(el.children[c]))) inner = true;
if (inner) continue;
var t = txt(el);
if (t.length < 3 || t.length > 70 || !OUTCOME.test(t) || !shown(el)) continue;
head = t; headEl = el; root = el.parentElement;
}
if (root) {
var kids = root.querySelectorAll('p,span,div');
for (var j = 0; j < kids.length && !sub; j++) {
var k = kids[j];
if (ours(k) || k.children.length > 2 || !/(sub|text|summary|note|desc|detail)/.test(sig(k))) continue;
var tt = txt(k);
if (tt && tt !== head && tt.length >= 5 && tt.length <= 160 && shown(k)) sub = tt;
}
}
/* the result screen the button goes into: the nearest overlay / banner / result box around the headline */
var host = null;
for (var n = headEl, d = 0; n && n !== document.body && d < 4; n = n.parentElement, d++) {
if (d > 0 && BOX.test(sig(n)) && n.tagName !== 'BUTTON') { host = n; break; }
}
return { head: head, sub: sub, host: host };
}
var STATRE = /(score|moves|time|wpm|best|level|streak|accuracy|combo|coins|distance|height|points|stars|lives|wins|attempt|guess)/;
function readStats() {
var out = [], seen = {};
var all = document.body.querySelectorAll('span,strong,b,div,p,output');
for (var i = 0; i < all.length && out.length < 4; i++) {
var el = all[i];
if (ours(el) || el.children.length > 1) continue;
if (!STATRE.test(sig(el))) continue;
var t = txt(el);
if (!t || t.length > 28 || !/[0-9]/.test(t) || !shown(el)) continue;
if (!/[a-zA-Z]/.test(t) && el.parentElement) {
var parts = [], ch = el.parentElement.childNodes;
for (var p = 0; p < ch.length; p++) { var pv = (ch[p].textContent || '').replace(/\s+/g, ' ').trim(); if (pv) parts.push(pv); }
var pt = parts.join(' ');
if (pt.length <= 30 && /[a-zA-Z]/.test(pt)) t = pt;
}
var key = t.toLowerCase();
if (seen[key]) continue;
seen[key] = 1; out.push(t);
}
return out;
}
function bestCanvas() {
var cs = document.querySelectorAll('canvas'), best = null, area = 0;
for (var i = 0; i < cs.length; i++) {
var r = cs[i].getBoundingClientRect(), a = r.width * r.height;
if (a > area && a > 8000 && shown(cs[i])) { area = a; best = cs[i]; }
}
return best;
}
/* ---- show the button when a result is on screen ---- */
var lastHost = null, timer = 0;
function sync() {
timer = 0;
var r = readResult();
if (r.head && r.host) {
if (inl.parentElement !== r.host) r.host.appendChild(inl);
if (inl.hidden) inl.hidden = false;
if (!pill.hidden) pill.hidden = true;
} else {
if (!inl.hidden) inl.hidden = true;
if (pill.hidden) pill.hidden = false;
pill.classList.toggle('wdps-hot', !!r.head);
}
if (lastHost && !r.head && !box.hidden) box.hidden = true;
lastHost = r.head ? (r.host || document.body) : null;
}
function later() { if (!timer) timer = setTimeout(sync, 200); }
try {
new MutationObserver(function (list) {
for (var i = 0; i < list.length; i++) {
var t = list[i].target;
if (t.nodeType !== 1) t = t.parentElement;
if (t && !ours(t)) { later(); return; }
}
}).observe(document.body, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ['class', 'style', 'hidden'] });
} catch (e) {}
setInterval(later, 1500);
later();
/* ---- the card (1080 x 1080, drawn in memory, light like Pocket Artillery's) ---- */
var outline = 0;
function put(g, t, x, y) { if (outline) { g.lineJoin = 'round'; g.lineWidth = outline; g.strokeStyle = 'rgba(8,12,28,.85)'; g.strokeText(t, x, y); } g.fillText(t, x, y); }
function wrap(g, text, x, y, maxW, lh, maxLines) {
var words = text.split(' '), line = '', n = 0;
for (var i = 0; i < words.length; i++) {
var tst = line ? line + ' ' + words[i] : words[i];
if (g.measureText(tst).width > maxW && line) {
put(g, line, x, y); y += lh; n++; line = words[i];
if (n >= maxLines - 1) { line = words.slice(i).join(' '); break; }
} else line = tst;
}
if (line) {
while (line.length > 3 && g.measureText(line).width > maxW) line = line.slice(0, -2);
put(g, line, x, y); y += lh;
}
return y;
}
/* Biggest-screenshot card: the game frame fills the whole image, the text sits on top of it as an overlay. */
function bgOf(el) {
for (var n = el; n; n = n.parentElement) {
var c = getComputedStyle(n).backgroundColor;
if (c && c !== 'transparent' && c.slice(-4) !== ', 0)') return c;
}
return '#0f172a';
}
function drawBig(info, cv) {
var W = 1080, r = cv.width / cv.height, H = Math.max(810, Math.min(1350, Math.round(W / r)));
var c = document.createElement('canvas'); c.width = W; c.height = H;
var g = c.getContext('2d');
g.fillStyle = bgOf(cv.parentElement || cv); g.fillRect(0, 0, W, H);
var w = W, h = w / r;
if (h > H) { h = H; w = h * r; }
g.imageSmoothingEnabled = true; g.imageSmoothingQuality = 'high';
try { g.drawImage(cv, (W - w) / 2, (H - h) / 2, w, h); } catch (e) { return null; }
/* dark fades under the text so it stays readable on any game colours */
var g1 = g.createLinearGradient(0, 0, 0, H * 0.55); g1.addColorStop(0, 'rgba(8,12,28,.74)'); g1.addColorStop(1, 'rgba(8,12,28,0)');
g.fillStyle = g1; g.fillRect(0, 0, W, H * 0.55);
var g2 = g.createLinearGradient(0, H - 200, 0, H); g2.addColorStop(0, 'rgba(8,12,28,0)'); g2.addColorStop(1, 'rgba(8,12,28,.85)');
g.fillStyle = g2; g.fillRect(0, H - 200, W, 200);
var head = info.head || 'Can you beat me?';
var kind = /draw|tie/i.test(head) ? 'draw' : LOSS.test(head) && !WIN.test(head) ? 'loss' : 'win';
g.textAlign = 'center';
var y = 36;
g.fillStyle = 'rgba(255,255,255,.93)'; g.font = '800 34px system-ui, sans-serif'; outline = 7;
y = wrap(g, TITLE.toUpperCase(), W / 2, y + 34, 1000, 42, 1);
g.fillStyle = kind === 'draw' ? '#e2e8f0' : kind === 'loss' ? '#9db8ff' : '#ffc233';
var big = head.length > 22 ? 84 : 112; g.font = '900 ' + big + 'px system-ui, sans-serif'; outline = 16;
y = wrap(g, head, W / 2, y + big * 0.85, 1000, big * 1.05, 2);
if (info.sub) { g.fillStyle = '#fff'; g.font = '700 40px system-ui, sans-serif'; outline = 9; y = wrap(g, info.sub, W / 2, y + 6, 980, 50, 2); }
outline = 0;
if (info.stats.length) {
g.font = '800 38px system-ui, sans-serif';
var gap = 16, pad = 26, ws = [], tot = -gap;
for (var i = 0; i < info.stats.length; i++) { ws.push(g.measureText(info.stats[i]).width + pad * 2); tot += ws[i] + gap; }
var sc = tot > 960 ? 960 / tot : 1, px = (W - tot * sc) / 2, py = y + 14;
for (var k = 0; k < info.stats.length; k++) {
var w0 = ws[k] * sc;
g.fillStyle = 'rgba(8,12,28,.72)'; g.strokeStyle = 'rgba(255,255,255,.55)'; g.lineWidth = 3;
g.beginPath();
if (g.roundRect) g.roundRect(px, py, w0, 68, 34); else g.rect(px, py, w0, 68);
g.fill(); g.stroke();
g.fillStyle = '#fff'; g.font = '800 ' + Math.round(38 * sc) + 'px system-ui, sans-serif';
g.fillText(info.stats[k], px + w0 / 2, py + 47);
px += w0 + gap * sc;
}
}
outline = 8;
g.fillStyle = '#fff'; g.font = '900 52px system-ui, sans-serif'; put(g, 'Can you beat it?', W / 2, H - 84);
g.fillStyle = 'rgba(255,255,255,.92)'; g.font = '700 34px system-ui, sans-serif'; outline = 6; put(g, 'Play free at webdevpuneet.com', W / 2, H - 34);
outline = 0;
return c;
}
function finish(c, cb) {
var dataUrl = '';
try { dataUrl = c.toDataURL('image/png'); } catch (e) {}
try { c.toBlob(function (b) { cb(b, dataUrl); }, 'image/png'); } catch (e) { cb(null, dataUrl); }
}
function makeCard(info, cb) {
var cv0 = bestCanvas();
if (cv0) { var bc = drawBig(info, cv0); if (bc) { finish(bc, cb); return; } }
var S = 1080, c = document.createElement('canvas');
c.width = S; c.height = S;
var g = c.getContext('2d');
var gr = g.createLinearGradient(0, 0, 0, S);
gr.addColorStop(0, '#eaf4ff'); gr.addColorStop(1, '#fff3ef');
g.fillStyle = gr; g.fillRect(0, 0, S, S);
var cv = bestCanvas();
var head = info.head || 'Can you beat me?';
var kind = /draw|tie/i.test(head) ? 'draw' : LOSS.test(head) && !WIN.test(head) ? 'loss' : 'win';
g.textAlign = 'center';
g.fillStyle = '#1e293b'; g.font = '800 60px system-ui, sans-serif';
var y = wrap(g, TITLE, S / 2, cv ? 120 : 200, 980, 68, 1);
g.fillStyle = kind === 'draw' ? '#475569' : kind === 'loss' ? '#3b63e0' : '#e8553a';
g.font = '800 ' + (head.length > 22 ? 76 : 100) + 'px system-ui, sans-serif';
y = wrap(g, head, S / 2, y + (head.length > 22 ? 70 : 90), 980, head.length > 22 ? 84 : 108, 2);
if (info.sub) { g.fillStyle = '#475569'; g.font = '700 38px system-ui, sans-serif'; y = wrap(g, info.sub, S / 2, y + 4, 940, 48, 2); }
if (info.stats.length) {
g.font = '800 36px system-ui, sans-serif';
var gap = 18, pad = 26, ws = [], tot = -gap;
for (var i = 0; i < info.stats.length; i++) { ws.push(g.measureText(info.stats[i]).width + pad * 2); tot += ws[i] + gap; }
var sc = tot > 1000 ? 1000 / tot : 1, px = (S - tot * sc) / 2, py = y + 14;
for (var k = 0; k < info.stats.length; k++) {
var w0 = ws[k] * sc;
g.fillStyle = '#ffffff'; g.strokeStyle = '#d5e2ee'; g.lineWidth = 3;
g.beginPath();
if (g.roundRect) g.roundRect(px, py, w0, 64, 32); else g.rect(px, py, w0, 64);
g.fill(); g.stroke();
g.fillStyle = '#1e293b'; g.font = '800 ' + Math.round(36 * sc) + 'px system-ui, sans-serif';
g.fillText(info.stats[k], px + w0 / 2, py + 44);
px += w0 + gap * sc;
}
y = py + 64 + 10;
}
if (cv) {
try {
var maxW = 960, maxH = Math.max(180, 930 - y - 30), r = cv.width / cv.height;
var w = maxW, h = w / r;
if (h > maxH) { h = maxH; w = h * r; }
var x = (S - w) / 2, yy = y + 20 + Math.max(0, (maxH - h) / 2);
g.save(); g.shadowColor = 'rgba(60,100,140,0.3)'; g.shadowBlur = 30; g.shadowOffsetY = 10;
g.fillStyle = '#fff'; g.fillRect(x - 8, yy - 8, w + 16, h + 16); g.restore();
g.drawImage(cv, x, yy, w, h);
} catch (e) {}
}
g.fillStyle = '#1e293b'; g.font = '800 46px system-ui, sans-serif';
g.fillText('Can you beat it?', S / 2, 1000);
g.fillStyle = '#64748b'; g.font = '700 34px system-ui, sans-serif';
g.fillText('Play free at webdevpuneet.com', S / 2, 1050);
var dataUrl = '';
try { dataUrl = c.toDataURL('image/png'); } catch (e) {}
try { c.toBlob(function (b) { cb(b, dataUrl); }, 'image/png'); } catch (e) { cb(null, dataUrl); }
}
function shareText(info) {
var bits = [];
if (info.head) bits.push(info.head);
if (info.stats.length) bits.push(info.stats.join(', '));
return TITLE + ': ' + (bits.length ? bits.join(' - ') + '. ' : '') + 'Can you beat it? ' + URL;
}
function msg(t) { q('msg').textContent = t; }
function openFallback() {
var t = encodeURIComponent(shareText(lastInfo));
q('wa').href = 'https://wa.me/?text=' + t;
q('x').href = 'https://twitter.com/intent/tweet?text=' + t;
q('fb').href = 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(URL) + '"e=' + t;
box.hidden = false;
}
function nativeShare() {
/* a user cancel does nothing; anything else (blocked in an iframe, no permission) shows Copy / Download / links */
try {
navigator.share({ files: [file], title: TITLE, text: shareText(lastInfo) }).catch(function (e) { if (!e || e.name !== 'AbortError') openFallback(); });
} catch (e) { openFallback(); }
}
function onShare() {
var info = readResult(); info.stats = readStats();
lastInfo = info;
msg('Share your result:');
makeCard(info, function (b, dataUrl) {
blob = b; file = null;
if (b) { try { file = new File([b], 'result.png', { type: 'image/png' }); } catch (e) {} }
if (dataUrl) q('dl').href = dataUrl;
q('dl').hidden = !dataUrl;
var canNative = !!(file && navigator.share && navigator.canShare && navigator.canShare({ files: [file] }));
var touch = !!(window.matchMedia && window.matchMedia('(pointer: coarse)').matches);
q('more').hidden = !canNative;
if (canNative && touch) nativeShare();
else openFallback();
});
}
/* Games often have document/window level touch and pointer handlers (some call preventDefault, which cancels the tap's
click on phones, others restart the game on touchstart). So: keep every pointer event on our controls away from the
game, and fire on touchend as well as click (guarded so one tap shares once). */
var busy = 0;
function trigger() { if (busy) return; busy = 1; setTimeout(function () { busy = 0; }, 700); onShare(); }
function guard(el, act) {
var evs = ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'touchstart', 'touchend', 'click'];
for (var i = 0; i < evs.length; i++) el.addEventListener(evs[i], function (e) { e.stopPropagation(); });
if (act) {
el.addEventListener('touchend', function (e) { if (e.cancelable) e.preventDefault(); act(); });
el.addEventListener('click', function () { act(); });
}
}
guard(pill, trigger);
guard(inl, trigger);
guard(box);
q('more').addEventListener('click', function () { if (file) nativeShare(); });
q('close').addEventListener('click', function () { box.hidden = true; });
document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && !box.hidden) box.hidden = true; });
q('copy').addEventListener('click', function () {
try {
navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]).then(
function () { msg('Image copied. Paste it into your chat or post.'); },
function () { msg('Could not copy here. Try Download image.'); }
);
} catch (e) { msg('Copying images is not supported here. Try Download image.'); }
});
})();
/* wdp-share:end */Extend with AI
Take This Snippet Further With AI
Paste this snippet's HTML, CSS and JS into an AI assistant like Claude and ask it to explain how one formula, t = FOCAL / (FOCAL + d), produces the perspective track, why the gap between obstacle rows scales with speed, or how the autopilot picks a free lane around a block row. It is a strong base to extend: ask for a magnet or double-coin power-up, daily missions, curved road sections, character skins, a boss wall every 1000 metres, or an online leaderboard. You could also ask it to add gamepad support, replace the drawn runner with a sprite sheet, or port the loop into a React component using useRef and useEffect. Treat it as a working prototype to question and rebuild.
Prompt to recreate it
Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:
Build a polished, mobile-friendly pseudo-3D endless runner on an HTML5 canvas, as ONE self-contained HTML file (inline CSS and JS, no frameworks, no asset files). Dark neon synthwave look.
CAMERA AND TRACK
- Logic in a 360x480 space, canvas backing store scaled by devicePixelRatio (max 2). Pseudo-3D: every object has a distance d ahead of the runner; t = FOCAL / (FOCAL + d); screen y = HORIZON + (BASE_Y - HORIZON) * t; screen x = centre + (lane - 1) * SPACING * t. Three lanes converge on a vanishing point.
- Draw: a sky gradient with twinkling stars, a retro striped sun and a parallax skyline; a track made of scrolling quads between depth slices (two alternating shades, dashed lane markers on every other slice); a magenta perspective grid outside the track; glowing cyan and magenta track edges; and speed lines radiating from the vanishing point that grow with speed.
GAMEPLAY
- Fixed pool of one-shot actions: moveLane(-1/+1), jump (about 640ms window, clears barriers only near the top of the arc), slide (about 560ms). Sliding during a jump cancels the jump. Lane is an integer for logic, with a smoothed float used for drawing and collisions.
- Obstacles: a low green barrier (jump), an overhead amber gate with posts (slide), a tall red block with a lit top face and X mark (change lane). Row patterns: single barrier or gate with coins in other lanes; two blocks leaving one open lane with coins in it; a full-width wall of barriers or gates; coin lines; and an occasional cyan shield pickup. Row gap = max(30, speed * 0.62) + random.
- Speed = min(106, 34 + distance * 0.012). Collision when an obstacle's d is within +-1.3 and its lane is within 0.55 of the runner's lane. A shield absorbs one hit (pop particles, 1.3s blinking invulnerability). Coins give 10 points, with a streak that raises the pickup pitch. Score = metres + 10 per coin. Best score in localStorage with try/catch.
- Draw order far to near; objects behind the runner's line draw over it. Obstacles fade in with distance and have contact shadows.
- The runner is drawn from behind with a run cycle (alternating legs and arms), a tucked jump pose, a low slide pose, a shadow, a helmet with a glowing visor and a shield bubble. Coins spin by scaling their width with |cos|.
- Crashes: screen shake (off for prefers-reduced-motion), a white flash and particle bursts, then the game-over screen after about 450ms.
STATES AND INPUT
- Idle: the real game runs as a demo with a small autopilot (look ahead at the nearest obstacle in the runner's lane: jump barriers, slide gates, steer to the nearest free lane for blocks) with crashes and scoring disabled, behind a start overlay. Then play, paused (P, Esc, window blur, hidden tab) and game over (score, metres, coins, best).
- Controls: arrow keys or WASD and Space (preventDefault on arrows and Space so the page never scrolls), on-screen buttons firing on pointerdown, swipe gestures on the canvas (left/right lane, up jump, down slide, short tap jump). M toggles sound.
- Sound synthesized with the Web Audio API (no files): jump sweep, slide noise, lane tick, coin ping with rising pitch, shield sweep, crash noise plus saw drop, shield pop. AudioContext created on the first click; mute saved to localStorage.
Use one permanent requestAnimationFrame loop with delta-time scaling. HUD: score, coin count and a speed bar. Return only the finished single HTML file.Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.