| name | dev-patterns-mobile-haptics |
| description | Haptic feedback patterns for mobile games using Vibration API. Use when adding tactile feedback. |
| category | patterns |
Mobile Haptics
"Haptic feedback transforms flat screens into tactile experiences."
When to Use
Use when:
- Building mobile/touch controls
- Adding game feel to actions
- Providing feedback for UI interactions
- Enhancing immersion on mobile devices
Quick Start
function triggerHaptic(duration = 10) {
if ('vibrate' in navigator) {
navigator.vibrate(duration);
}
}
<button onClick={() => triggerHaptic(10)}>Tap</button>
Browser Support
| Browser | Android | iOS | Notes |
|---|
| Chrome | ✅ Full | ❌ No | Full Vibration API support |
| Firefox | ✅ Full | ❌ No | Full Vibration API support |
| Safari | ✅ Full | ❌ No | Desktop only |
| Edge | ✅ Full | ❌ No | Chromium-based |
iOS Note: iOS does NOT support the Vibration API. Use Taptic Engine feedback via native wrappers (Capacitor, React Native) for iOS.
Tap Feedback (UI Interactions)
function tapFeedback() {
navigator.vibrate(10);
}
function toggleFeedback() {
navigator.vibrate(25);
}
function confirmFeedback() {
navigator.vibrate(50);
}
Impact Patterns (Game Actions)
function lightImpact() {
navigator.vibrate(15);
}
function mediumImpact() {
navigator.vibrate(30);
}
function heavyImpact() {
navigator.vibrate([50, 30, 50]);
}
Success Pattern
function successHaptic() {
navigator.vibrate([10, 50, 10, 50, 10]);
}
function levelCompleteHaptic() {
navigator.vibrate([10, 50, 20, 50, 30]);
}
Error Pattern
function errorHaptic() {
navigator.vibrate([50, 100, 50]);
}
function denyHaptic() {
navigator.vibrate(100);
}
Game-Specific Patterns
function shootFeedback() {
navigator.vibrate(15);
}
function emptyClickFeedback() {
navigator.vibrate([10, 30, 10]);
}
function lightDamageFeedback() {
navigator.vibrate(25);
}
function heavyDamageFeedback() {
navigator.vibrate([80, 20, 80]);
}
function deathFeedback() {
navigator.vibrate([100, 50, 100, 50, 200]);
}
Movement Feedback
let stepCount = 0;
function stepFeedback() {
if (stepCount % 3 === 0) {
navigator.vibrate(5);
}
stepCount++;
}
function jumpFeedback() {
navigator.vibrate([10, 20, 30]);
}
function landFeedback() {
const intensity = Math.min(fallDistance / 10, 1);
navigator.vibrate(20 + intensity * 40);
}
Best Practices
- Don't overuse - Too much vibration desensitizes users
- Match action intensity - Heavy actions = stronger haptics
- Consider battery - Haptics consume battery
- Provide settings - Allow users to disable or adjust intensity
- Test on devices - Emulators don't show haptics
Reference