Shared-device co-op and versus play for browser games. Press-to-join player slots across keyboard schemes (WASD, arrows, numpad), gamepads (standard mapping, dead zone, d-pad, unplug handling) and on-screen touch players; colors and labels; a shared camera that zooms to keep everyone in view plus a leash at the zoom limit; split-screen viewports for 2-4 players (side by side or stacked for port...
Download skill (.zip) Files on GitHubLocal co-op
No network: everyone plays on one screen. The hard parts are who controls what, keeping everyone visible, and fair controls.
assets/coop.js
import { createCoop, sharedCamera, leash, splitViewports, clipTo, touchZones, KEYBOARD_SCHEMES } from "./coop.js";
const coop = createCoop({ max: 4, schemes: KEYBOARD_SCHEMES.slice(0, 2) }).attach(window);
// lobby scene: each frame
const { joined } = coop.update(); // pressing A on an unused device (F, K, or a controller's A) takes the next slot
draw: for each slot show "P1 · WASD + F/G" or "Press F, K or A to join"; start when a joined player presses start/A again
// play scene: each frame
coop.update();
for (const p of coop.players) movePlayer(p.slot, p.input); // input: { x, y, a, b, start, aPressed, bPressed, startPressed }
| Device | Join | Notes |
|---|---|---|
| Keyboard left | F | WASD move, F/G buttons, R start |
| Keyboard right | K | Arrows move, K/L, Enter |
| Numpad (optional third) | Numpad 0 | 8456 move |
| Gamepad (each) | A (button 0) | left stick + d-pad, B leaves in the lobby, Start; unplug keeps the slot and shows disconnected |
| Touch | your button | coop.addVirtual("touch-left"), then set p.input from that player’s on-screen controls |
Keeping everyone on screen
- Shared camera (best for co-op):
cam = sharedCamera(positions, { width, height }, { minZoom: 0.6, prev: cam })each frame, thenleash(players, cam, view)so nobody walks off screen when zoomed out fully. - Split screen (racing, big levels):
splitViewports(n, w, h)gives rectangles; for each,ctx.save(); clipTo(ctx, vp);translate to that player’s camera, draw the world,ctx.restore(). Draw HUDs per viewport. Halve effects, since each world draw costs a full frame. - Touch on one phone or tablet:
touchZones(2, w, h)splits the screen; each player’s controls live in their half. Tablets suit two players facing each other (rotate the top player’s HUD).
Fairness and comfort
- Many keyboards can’t register more than 3-6 keys at once (“ghosting”): WASD + arrows mostly works; avoid using keys between the two hands. Let players rebind.
- Colors plus shapes or numbers (P1/P2 labels) for color-blind players.
- Respawn fallen players near the others, and share lives or let them rejoin, so nobody sits out for long.
- A friendly-fire option off by default. Pause pauses everyone; any player can pause.
- Gamepads only appear after a button press (browser privacy rule). Say “press a button on your controller”.
Starters
The party-game genre starter (4 players, one key each, screen corners on touch) is a ready base for local versus games.
For co-op, take a genre starter and replace its single player with coop.players.
Tested
Node tests: keyboard and gamepad joining in slot order, input values and dead zone, unplug keeping the slot, max players, shared camera zoom limit and centering, leash clamping, split layouts for landscape and portrait.
Pitfalls
The first player who touches anything becomes P1 without choosing, slots that shift when someone leaves (slots are stable here),
a camera that zooms so far out nothing is readable, split screen on a phone (too small), and forgetting blur (keys stay held
when the window loses focus; handled).