Vibration feedback for browser games on phones and controllers. Named patterns (tap, select, pickup, hit, heavy, explosion, success, error), a strength setting, an off switch, limits so buzzes don't pile up, controller rumble with strong and weak motors, the first-tap rule, stopping when the tab is hidden, and a visual fallback where vibration doesn't exist (iPhone, desktops); when haptics help...
Download skill (.zip) Files on GitHubHaptic feedback
Where it works
| Device | Support |
|---|---|
| Android phones (Chrome, Edge, Samsung, Firefox) | navigator.vibrate: on/off pulses, no strength control |
| iPhone and iPad (all browsers) | None. Use a visual cue: screen shake, flash, a squash on the sprite |
| Controllers in Chrome and Edge | Rumble with strong (heavy) and weak (buzzy) motors |
| Desktop browsers | None for the device itself |
So haptics are always extra: the game must feel complete without them.
assets/haptics.js
import { createHaptics, stopWhenHidden } from "./haptics.js";
const haptics = createHaptics({ pads, strength: settings.get("vibration"), onFallback: (name) => camera.shake(name === "heavy" ? 6 : 3) });
stopWhenHidden(haptics);
haptics.play("hit"); // phone vibration
haptics.play("heavy", { pad: 0 }); // controller 0 rumble (gamepad-support's pads)
haptics.play([15, 30, 15]); // your own pattern: on, off, on (ms)
settings.on("vibration", (v) => (haptics.strength = v)); // 0 = off, 0.35..1 shortens pulses on phones
- The same pattern can’t fire again within 60 ms: ten coins in a row are one buzz, not a rattle.
- Phones refuse to vibrate before the first tap on the page; the call then uses the fallback.
- Pass
onFallbackto keep the feel on iPhone: the device lab shakes the view instead.
When to buzz
- Yes: taking damage, a heavy landing, a big explosion, a confirm in a menu (tiny), a perfect hit in a rhythm game, a catch or a goal.
- No: every footstep, every bullet fired, continuous engine buzz on phones (drains battery, numbs the hands), anything more than about 10 times a second.
- Short beats long: 10-40 ms for taps, 60-100 ms for big moments. Long pulses feel like a notification.
- A setting with Off and a strength slider (game-settings-system), default on but light. Respect it on controllers too.
Tested
Node tests: patterns, rate limiting, strength shortening pulses (pauses unchanged) with a minimum, off switch, the first-tap rule, the fallback when there’s no vibration, controller rumble scaled by strength, custom and unknown patterns. Try it on Android and with a controller in the device lab (cross-browser-compatibility).
Pitfalls
Relying on vibration for information, buzzing constantly, ignoring the setting for controllers, vibrating while the tab is in the background, and forgetting iPhone players get nothing (use the fallback).