Bash.tv → GitHub → itch.io

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 GitHub

Device 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:

  1. 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 a pointerup / click listener.
  2. 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.
  3. 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.
  4. Map to the screen, not the device. In landscape the axes swap. tilt.js reads screen.orientation.angle and 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

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

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.