Building fast, deterministic timing and local 2-player games in vanilla HTML5 <canvas> looks deceptively simple—until a player on a 144Hz ProMotion display experiences double-speed rotation, or two players sharing one laptop keyboard get hit by membrane key ghosting.
While shipping a 100-level rotating-target timing game and same-screen duel mode, we ran into three subtle physics and input bugs. Here are the clean math and DOM patterns that solved them with zero external libraries.
1. Exact Chord-to-Angle Collision (2 * Math.asin(r / R))
In rotating-pin games (inspired by classic aa / Coreball mechanics), pins stick into a spinning central wheel at orbit radius R, each with a circular pinhead of radius r.
If you integrate (x, y) positions frame-by-frame, floating-point rounding causes attached pins to subtly drift apart over long levels. Instead, store each attached pin purely as a relative polar angle θ, and derive the exact minimum safe angular separation from the chord triangle:
const TWO_PI = Math.PI * 2; export function shortestAngularDistance(a: number, b: number): number { let delta = (a - b) % TWO_PI; if (delta > Math.PI) delta -= TWO_PI; if (delta < -Math.PI) delta += TWO_PI; return Math.abs(delta); } export function isPinColliding( attachedAngles: number[], candidateAngle: number, pinHeadRadius: number, orbitRadius: number ): boolean { // Exact chord geometry: sin(theta / 2) = r / R const minAngleRad = 2 * Math.asin(pinHeadRadius / orbitRadius); return attachedAngles.some( (angle) => shortestAngularDistance(angle, candidateAngle) < minAngleRad ); } Because Math.asin(r / R) uses exact chord geometry rather than an arc-length approximation, two adjacent pinheads kiss pixel-perfectly on screen regardless of orbit radius. You can feel this exact collision threshold across 100 speed-changing stages and split-screen duel mode in our browser build of Coreball Game on 2P Duel.
2. Sub-Frame Arrival Interpolation on High-Refresh Screens
When a projectile travels upward at v = 1100 px/s, a single 16.67ms frame moves the pin 18.3 pixels. Checking collision only at the end of the frame means the core rotated an extra ω * dt after the pin actually crossed the orbit boundary—making tight gaps feel unfair at 60Hz vs. 120Hz.
Fix this in four lines by computing the exact fractional sub-frame tImpact ∈ [0, 1] when the pin crosses impactY:
const prevY = projectile.y; const nextY = prevY - projectile.speed * dt; if (prevY > impactY && nextY <= impactY) { const tImpact = (prevY - impactY) / (prevY - nextY); const exactCoreAngle = prevCoreAngle + angularVelocity * (dt * tImpact); const localHitAngle = normalizeAngle(Math.PI / 2 - exactCoreAngle); resolvePinHit(localHitAngle); } Now a 60Hz Chromebook and a 144Hz desktop evaluate the exact same impact angle down to the millisecond.
3. Avoiding Membrane Keyboard Ghosting in Local 2-Player Games
Most non-mechanical laptop keyboards use a 2-key or 3-key rollover row/column matrix. If Player 1 holds W + D while Player 2 presses I + J, shared matrix traces can silently drop the third keydown.
Three rules keep same-screen browser multiplayer responsive—which we use across 2P Duel (2pduel.com) and knockback physics arenas like Shove Kings:
- Bind
KeyboardEvent.code, neverevent.key:KeyWmaps to the same physical top-left key on QWERTY, AZERTY (Z), and CapsLock-active keyboards. - Split quadrants: Put Player 1 on left-matrix keys (
KeyW/KeyA/KeyS/KeyDorKeyF) and Player 2 on the isolated hardware arrow cluster (ArrowUp/ArrowLeft/ArrowRight) orEnter. - Track active keys in a
Set<string>rather than relying on OS key-repeat intervals (keydownauto-repeat has a 250ms–500ms OS delay before repeating!).
const pressed = new Set<string>(); window.addEventListener("keydown", (e) => { if (["Space", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.code)) { e.preventDefault(); } if (!e.repeat) pressed.add(e.code); }); window.addEventListener("keyup", (e) => { pressed.delete(e.code); }); Summary
- Store rotating attachments in polar coordinates and test
2 * Math.asin(r / R)instead of(x, y)circles. - Interpolate
tImpactwithindtso 60Hz and 144Hz players get identical hitboxes. - Use
e.code+ isolated left/arrow keyboard quadrants for zero-ghosting local 2-player games.
