gitflarePublicPartly agent-ready40
neon-drift
Real-time multiplayer arcade racer on Cloudflare Workers and Durable Objects.
- LicenseApache-2.0
- LanguageJavaScript
- Last landed
3efe3daOct 23, 2026
Add the Apache-2.0 licence
3efe3daimport { createFx, cameraTarget } from './fx.js';import { createAudio } from './audio.js';import { activeInput } from './input.js'; const DELAY = 100;const TAU = Math.PI * 2;const DASH = [40, 20], NODASH = []; export function createRenderer(canvas, net, onFrame, inputRef) { let youUntil = 0, youPhase = ''; const ctx = canvas.getContext('2d'); const fxs = createFx(); const audio = createAudio(); let w = 0, h = 0, dpr = 1, scale = 1; const cam = { x: 0, y: 0, zoom: 0.8 }; const scratch = { x: 0, y: 0, heading: 0, speed: 0, boost: 0, meter: 0, alive: 1, score: 0, hue: 0 }; const widths = new Map(); const out = { fps: 0, fx: null, resize }; const ct = { x: 0, y: 0, zoom: 1 }; let lastAlarm = 0, lastNear = 0, nearSpark = 0; let slowUntil = 0, kick = 0, frames = 0, fpsAt = 0, last = 0, lastBoost = 0; const colCache = new Map(); function col(hue, a, l) { const k = (hue | 0) * 1000 + Math.round(a * 20) * 10 + (l > 60 ? 1 : 0); let s = colCache.get(k); if (!s) { s = 'hsla(' + (hue | 0) + ',100%,' + l + '%,' + (Math.round(a * 20) / 20) + ')'; if (colCache.size < 6000) colCache.set(k, s); } return s; } function resize() { dpr = Math.min(2, devicePixelRatio || 1); w = canvas.clientWidth; h = canvas.clientHeight; canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr); scale = Math.min(w, h) / 1100; } if (typeof ResizeObserver !== 'undefined') new ResizeObserver(resize).observe(canvas); resize(); out.fx = function fx(ev) { if (!ev) return; const k = ev[0]; if (k === 'crash') { const t = net.trails.get(ev[1]); fxs.burst(ev[3], ev[4], t ? t.hue : 0, 80, 380); fxs.ring(ev[3], ev[4], t ? t.hue : 0, 220, 0.6); const mine = ev[1] === net.id, rm = activeInput && activeInput.reduceMotion; if (mine && !rm) { slowUntil = performance.now() + 200; kick = 40; } fxs.addShake(rm ? 0 : mine ? 18 : 8); audio.play('crash'); } else if (k === 'orb') { fxs.burst(ev[3], ev[4], 50, 18, 160); fxs.ring(ev[3], ev[4], 50, 60, 0.4); if (ev[1] === net.id) audio.play('orb'); } else if (k === 'warn') audio.play('warn'); else if (k === 'shrink') { fxs.addShake(5); audio.play('shrink'); } else if (k === 'results') audio.play('win'); else if (k === 'phase' && ev[1] === 'countdown') audio.play('countdown'); }; addEventListener('pointerdown', () => audio.unlock(), { once: true }); addEventListener('keydown', () => audio.unlock(), { once: true }); let debug = location.search.includes('debug=1'); addEventListener('keydown', (e) => { if (e.key === 'F3') debug = !debug; }); function drawGrid(vx0, vy0, vx1, vy1) { const g = 100; ctx.strokeStyle = 'rgba(80,120,255,0.10)'; ctx.lineWidth = 1 / cam.zoom; ctx.beginPath(); for (let x = Math.floor(vx0 / g) * g; x <= vx1; x += g) { ctx.moveTo(x, vy0); ctx.lineTo(x, vy1); } for (let y = Math.floor(vy0 / g) * g; y <= vy1; y += g) { ctx.moveTo(vx0, y); ctx.lineTo(vx1, y); } ctx.stroke(); } function drawTrail(t, alpha, vx0, vy0, vx1, vy1) { const p = t.pts, n = p.length; if (n < 4) return; for (let pass = 0; pass < 2; pass++) { ctx.strokeStyle = pass ? col(t.hue, alpha, 75) : col(t.hue, alpha * 0.25, 50); ctx.lineWidth = pass ? 3 : 11; if (pass) { ctx.setLineDash(DASH); ctx.lineDashOffset = -(performance.now() / 12) % 60; } else ctx.setLineDash(NODASH); ctx.beginPath(); let pen = false; for (let i = 0; i < n - 2; i += 2) { const ax = p[i], ay = p[i + 1], bx = p[i + 2], by = p[i + 3]; if ((ax < vx0 && bx < vx0) || (ax > vx1 && bx > vx1) || (ay < vy0 && by < vy0) || (ay > vy1 && by > vy1)) { pen = false; continue; } if (!pen) ctx.moveTo(ax, ay); ctx.lineTo(bx, by); pen = true; } ctx.stroke(); } ctx.setLineDash(NODASH); } function drawCycle(c, id) { const hue = c.hue; ctx.save(); ctx.translate(c.x, c.y); ctx.rotate(c.heading); if (c.boost) { ctx.strokeStyle = col(hue, 0.5, 80); ctx.lineWidth = 2; ctx.beginPath(); for (let i = -1; i <= 1; i++) { ctx.moveTo(-10, i * 6); ctx.lineTo(-40 - ((i + 2) * 9), i * 6); } ctx.stroke(); } ctx.fillStyle = col(hue, id === net.id ? 0.2 : 0.35, 50); if (id === net.id) { ctx.beginPath(); ctx.arc(0, 0, 38, 0, TAU); ctx.fill(); } if (id === net.id && youUntil > performance.now()) { ctx.strokeStyle = '#fff'; ctx.lineWidth = 2.5; ctx.beginPath(); ctx.arc(0, 0, 24, 0, TAU); ctx.stroke(); } ctx.beginPath(); ctx.arc(0, 0, 16, 0, TAU); ctx.fill(); ctx.fillStyle = col(hue, 1, 85); ctx.beginPath(); ctx.moveTo(14, 0); ctx.lineTo(-9, 8); ctx.lineTo(-5, 0); ctx.lineTo(-9, -8); ctx.closePath(); ctx.fill(); ctx.restore(); const name = nameOf(net, id); if (id === net.id && youUntil > performance.now()) { ctx.font = 'bold 12px sans-serif'; ctx.fillStyle = '#fff'; ctx.fillText('YOU', c.x - ctx.measureText('YOU').width / 2, c.y - 30); } if (name && id !== net.id) { let wd = widths.get(name); ctx.font = '12px sans-serif'; if (wd === undefined) { wd = ctx.measureText(name).width; if (widths.size < 200) widths.set(name, wd); } ctx.fillStyle = 'rgba(255,255,255,0.8)'; ctx.fillText(name, c.x - wd / 2, c.y - 24); } } function drawMinimap(R) { const narrow = w <= 640; const s = narrow ? 96 : Math.min(130, w * 0.28), cx = w - s / 2 - 10, cy = narrow ? s / 2 + 56 : h - s / 2 - 10, k = s / 2 / (R || 1000); ctx.fillStyle = 'rgba(0,0,10,0.5)'; ctx.beginPath(); ctx.arc(cx, cy, s / 2, 0, TAU); ctx.fill(); ctx.strokeStyle = 'rgba(0,255,255,0.7)'; ctx.lineWidth = 1.5; ctx.stroke(); const ids = net.cycleIds; for (let i = 0; i < ids.length; i++) { const id = ids[i]; let c = scratch; if (id === net.id && net.me) c = net.me; else if (!net.sampleCycle(id, last - DELAY, scratch)) continue; if (!c.alive) continue; ctx.fillStyle = id === net.id ? '#fff' : col(c.hue, 1, 60); ctx.beginPath(); ctx.arc(cx + c.x * k, cy + c.y * k, id === net.id ? 3.5 : 2.5, 0, TAU); ctx.fill(); } } function frame(now) { requestAnimationFrame(frame); let dt = Math.min(0.05, (now - last) / 1000 || 0.016); last = now; if (now < slowUntil) dt *= 0.5; kick *= Math.pow(0.001, dt); frames++; if (now - fpsAt > 1000) { out.fps = frames; frames = 0; fpsAt = now; } onFrame(now); net.update(now); if (net.phase !== youPhase) { youPhase = net.phase; if (youPhase === 'playing') youUntil = performance.now() + 3000; } fxs.update(dt); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.globalCompositeOperation = 'source-over'; ctx.fillStyle = '#04050d'; ctx.fillRect(0, 0, w, h); const me = net.me; if (me) { cameraTarget(me, net.arena, net.phase, scale, ct); const f = 1 - Math.pow(0.0005, dt); cam.x += (ct.x - cam.x) * f; cam.y += (ct.y - cam.y) * f; cam.zoom += (ct.zoom - cam.zoom) * (1 - Math.pow(0.01, dt)); if (me.boost && !lastBoost) audio.play('boost'); lastBoost = me.boost; audio.engine(me.speed / 272); if (me.alive && now - lastNear > 350) { for (const [tid, t] of net.trails) { if (t.pts.length < 4 || tid === net.id || !t.alive) continue; const p = t.pts; for (let i = p.length - 2; i >= 0; i -= 4) { const dx = p[i] - me.x, dy = p[i + 1] - me.y; if (dx * dx + dy * dy < 196) { lastNear = now; audio.play('whoosh'); fxs.burst(p[i], p[i + 1], t.hue, 6, 120); break; } } } } } const z = cam.zoom, sh = fxs.shake; const sx = sh ? (Math.random() - 0.5) * sh : 0, sy = sh ? (Math.random() - 0.5) * sh : 0; ctx.save(); ctx.translate(w / 2 + sx, h / 2 + sy); ctx.scale(z * (1 + kick * 0.002), z * (1 + kick * 0.002)); ctx.translate(-cam.x, -cam.y); if (kick > 0.5) ctx.translate(0, 0); const vx0 = cam.x - w / 2 / z - 20, vx1 = cam.x + w / 2 / z + 20, vy0 = cam.y - h / 2 / z - 20, vy1 = cam.y + h / 2 / z + 20; drawGrid(vx0, vy0, vx1, vy1); const a = net.arena, R = a.radius || 1000; ctx.strokeStyle = 'rgba(255,40,200,0.25)'; ctx.lineWidth = 14; ctx.beginPath(); ctx.arc(0, 0, R, 0, TAU); ctx.stroke(); ctx.strokeStyle = '#ff4fd8'; ctx.lineWidth = 3; ctx.stroke(); if (a.warn) { const p = 0.5 + 0.5 * Math.sin(now / 90); if (now - lastAlarm > 600) { lastAlarm = now; audio.play('alarm'); } ctx.strokeStyle = 'rgba(255,60,60,' + (0.35 + 0.5 * p) + ')'; ctx.lineWidth = 4 + 4 * p; ctx.beginPath(); ctx.arc(0, 0, a.nextRadius, 0, TAU); ctx.stroke(); } ctx.globalCompositeOperation = 'lighter'; for (const o of net.orbs.values()) { if (o.x < vx0 || o.x > vx1 || o.y < vy0 || o.y > vy1) continue; const r = 7 + 2.5 * Math.sin(now / 150 + o.x); ctx.fillStyle = 'rgba(255,220,60,0.25)'; ctx.beginPath(); ctx.arc(o.x, o.y, r * 2.2, 0, TAU); ctx.fill(); ctx.fillStyle = 'rgba(255,255,200,0.95)'; ctx.beginPath(); ctx.arc(o.x, o.y, r * 0.8, 0, TAU); ctx.fill(); } for (const t of net.trails.values()) { let al = 1; if (!t.alive) { al = 1 - (now - t.deadAt) / 1000; if (al <= 0) continue; if (al > 0.5 && ((now / 60) | 0) % 2) al *= 0.4; } drawTrail(t, al, vx0, vy0, vx1, vy1); } const ids = net.cycleIds; for (let i = 0; i < ids.length; i++) { const id = ids[i]; let c = null; if (id === net.id) c = me; else if (net.sampleCycle(id, now - DELAY, scratch)) c = scratch; if (c && c.alive) drawCycle(c, id); } for (let i = 0; i < fxs.count; i++) { const l = fxs.life[i]; if (l <= 0) continue; ctx.fillStyle = col(fxs.hue[i], Math.min(1, l / fxs.max[i]), 65); ctx.fillRect(fxs.x[i], fxs.y[i], fxs.size[i], fxs.size[i]); } for (let i = 0; i < fxs.ringCount; i++) { const l = fxs.rl[i]; if (l <= 0) continue; const k = 1 - l / fxs.rm[i]; ctx.strokeStyle = col(fxs.rh[i], 1 - k, 70); ctx.lineWidth = 4 * (1 - k) + 1; ctx.beginPath(); ctx.arc(fxs.rx[i], fxs.ry[i], fxs.rr[i] * k, 0, TAU); ctx.stroke(); } ctx.restore(); if (me && me.boost) { ctx.globalCompositeOperation = 'lighter'; ctx.strokeStyle = 'rgba(160,220,255,0.22)'; ctx.lineWidth = 1.5; ctx.beginPath(); const ca = Math.cos(me.heading), sa = Math.sin(me.heading), L = Math.max(w, h); for (let i = 0; i < 14; i++) { const u = ((now * 0.0016 + i * 0.137) % 1), sd = (((i * 7919) % 100) / 100 - 0.5) * L; const px = w / 2 - ca * (u - 0.5) * L * 1.2 - sa * sd, py = h / 2 - sa * (u - 0.5) * L * 1.2 + ca * sd; ctx.moveTo(px, py); ctx.lineTo(px - ca * 60, py - sa * 60); } ctx.stroke(); } ctx.globalCompositeOperation = 'source-over'; drawMinimap(R); const tc = (inputRef || activeInput || {}).touch; if (tc && tc.isTouch) { const zh = Math.min(h * 0.4, 240), zy = h - zh; ctx.fillStyle = 'rgba(255,255,255,' + (tc.side < 0 ? 0.16 : 0.06) + ')'; ctx.fillRect(0, zy, w / 2 - 2, zh); ctx.fillStyle = 'rgba(255,255,255,' + (tc.side > 0 ? 0.16 : 0.06) + ')'; ctx.fillRect(w / 2 + 2, zy, w / 2 - 2, zh); ctx.fillStyle = 'rgba(255,255,255,0.35)'; ctx.font = '28px sans-serif'; ctx.textAlign = 'center'; ctx.fillText('\u25C0', w * 0.25, h - zh / 2 + 10); ctx.fillText('\u25B6', w * 0.75, h - zh / 2 + 10); ctx.textAlign = 'start'; ctx.fillStyle = tc.boost ? 'rgba(255,230,60,0.55)' : 'rgba(255,230,60,' + (me && me.meter >= 99 ? 0.2 + 0.2 * Math.sin(now / 150) : 0.2) + ')'; ctx.beginPath(); ctx.arc(w - 56, h - 150, 40, 0, TAU); ctx.fill(); ctx.strokeStyle = 'rgba(255,230,60,0.7)'; ctx.lineWidth = 2; ctx.stroke(); } if (debug) { ctx.fillStyle = '#0f0'; ctx.font = '12px monospace'; ctx.fillText(out.fps + ' fps ' + (net.rtt | 0) + ' ms', 8, 16); } } requestAnimationFrame(frame); return out;} /** Safe name lookup: net.names is a plain object (id -> name). */export function nameOf(net, id) { const n = net && net.names; if (!n) return ''; const v = typeof n.get === 'function' ? n.get(id) : n[id]; return typeof v === 'string' ? v : '';}