Tilt and motion controls for phone browser games (device orientation and device motion). Turning sensors on inside a tap (the iOS permission prompt), mapping tilt to a -1..1 stick in portrait and landscape, calibration, dead zone, smoothing, sensitivity and invert options, shake gestures, detecting "no sensor" on desktops, iframe and HTTPS limits (Bash preview, itch.io), testing without a phone...
Download skill (.zip) Files on GitHubDevice orientation controls
Tilt is fun on phones, but it’s also the easiest control to get wrong. These are the rules that make it work everywhere:
- Turn it on from a tap. iOS Safari only shows the motion permission prompt when
DeviceOrientationEvent.requestPermission()is called inside a tap or click handler. A call from the game loop, a timer or page load fails silently.tilt.enable()must be called directly in apointerup/clicklistener. - Always offer another control. Desktops have no sensor, some players can’t tilt, and some decline the prompt. Keep keys and a touch stick working at the same time as tilt.
- Calibrate. “Flat” is however the player is holding the phone (often tipped toward them). The first reading becomes neutral, and a Calibrate button resets it.
- Map to the screen, not the device. In landscape the axes swap.
tilt.jsreadsscreen.orientation.angleand remaps, and it recalibrates when the phone rotates.
The module (assets/tilt.js)
import { createTilt } from "./core/tilt.js";
const tilt = createTilt({ maxAngle: 22, deadZone: 0.06, smoothing: 10 }); // degrees for full speed, dead zone 0..1, ease rate
canvas.addEventListener("pointerup", async () => { // inside the tap: iOS can ask
if (tilt.status !== "on") await tilt.enable(); else tilt.calibrate();
});
// each frame
tilt.update(dt);
const { x, y } = tilt.value; // -1..1; x = tilt right, y = top of the phone tipped away (ball rolls "down" the screen)
| Field / method | Meaning |
|---|---|
status |
off, waiting, on, denied (prompt declined), unsupported (no API, or no data within 0.7 s, as on desktops) |
enable() |
Ask permission if needed and start listening. Resolves true when real readings arrive |
calibrate() |
Current angle becomes neutral |
update(dt) |
Smooth toward the latest reading (frame-rate independent) |
onShake |
Set a function to get a debounced shake gesture (device motion) |
invertX, invertY |
Options if a device reports the opposite sign; offer them in settings |
destroy() |
Remove listeners (call in the scene’s exit()) |
Tested in node with a fake sensor: neutral first reading, dead zone, clamping, landscape remap, iOS denied, no-data detection, smoothing, shake debounce, calibration (12 checks).
Starter: tilt maze
assets/starter/ is a ready game for the game-scaffolding template: roll a ball through two mazes, avoid holes, reach the goal.
It shows the tap-to-enable button, calibration, keyboard + stick fallbacks, and wall bounces.
python3 <game-scaffolding>/scripts/scaffold.py my-tilt-game --title "Marble Run" --starter <this skill>/assets/starter
Tunables (config.js): tilt.maxAngle (lower = more sensitive), tilt.deadZone, ball.accel, ball.friction, the mazes
(# wall, o hole, S start, G goal).
Design tips
- Physics, not position: tilt should accelerate things (a rolling ball, a steering car), not place them directly. Raw sensor noise looks jittery when mapped straight to position.
- Sensitivity setting: a slider for
maxAngle(15–35°) and invert toggles. Save them. - Comfortable range: players shouldn’t need more than ~25° of tilt. Keep the screen readable while tilted.
- Don’t fight the screen rotating: for tilt games, ask players to lock rotation, or design for one orientation and show a
“rotate your phone” hint for the other (responsive-game-design).
screen.orientation.lock()only works in fullscreen on Android. - Shake is great for one-off actions (reroll, bomb), bad for anything repeated.
Where tilt works (and doesn’t)
| Place | Notes |
|---|---|
| Direct link to the game on a phone (HTTPS) | Works. Best place to test |
| Bash preview panel | The game runs in an iframe. Browsers only pass motion events into an iframe when the parent page allows them, so tilt may report unsupported there. Open the game’s direct link on the phone to test tilt; keys and stick still work in the preview |
| itch.io embed | Also an iframe: test on the real page after uploading, and keep the fallbacks |
http:// (not secure) |
Sensors are blocked. Use HTTPS |
| Desktop browsers | No data; status becomes unsupported and the fallbacks take over |
Testing
- Logic: inject a fake window (
createTilt({ window: fake })) and call itsdeviceorientationlistener with{ beta, gamma }values (see the tests above). - Browser automation: playwright-game-testing can play the starter with keys. For tilt, replace
scene.tiltwith a fake-window tilt in anevalstep and feed it readings. - Chrome DevTools → Sensors panel can simulate orientation on a desktop.
- Real phone: always do one real check (iOS and Android differ). Use the direct link.
Pitfalls
Calling requestPermission outside a tap (nothing happens on iOS), no fallback controls, no calibration (the ball drifts toward the
player), position mapped directly to tilt (jitter), forgetting landscape remapping, and assuming the preview iframe passes sensor data.