Feel the Web: Adding Haptics Without Native APIs
Let's be honest, haptic feedback on the web has always been a bit of an afterthought. If you wanted to add a tactile buzz or vibration to your web app, you were often stuck with the basic navigator.vibrate() API, which is limited and inconsistent. For anything more advanced, you'd need to look at native SDKs, which means platform-specific code and often, a paid service.
What if you could get rich, expressive haptic effects directly in the browser, for free? That's exactly what the web-haptics library offers. It's a clever JavaScript library that turns the simple vibration API into a powerful tool for creating nuanced tactile experiences.
What It Does
web-haptics is a lightweight JavaScript library that provides a developer-friendly interface for creating complex haptic patterns on supported devices. It sits on top of the Web Vibration API, but instead of just offering a simple "vibrate for 200ms" pattern, it lets you define effects like "sharp click," "soft bump," or a "rumble" using intuitive parameters.
Think of it as a synthesizer for touch feedback. You control the intensity, duration, and sharpness of the vibration to create a wide palette of tactile sensations.
Why It's Cool
The magic here is in the abstraction. The library's author, Lochie, has done the hard work of mapping expressive effect names to the underlying vibration pattern arrays that the browser understands.
- Expressive & Simple API: Instead of fiddling with arrays like
[100, 50, 80], you callhaptics.impact('soft')orhaptics.notification('success'). It's immediately clear what the intent is. - Cross-Platform Foundation: It builds on the web standard (
navigator.vibrate), so it works anywhere that API is supported (primarily on Android and in mobile browsers). - It's a Polyfill (Kind Of): For platforms that don't support the Web Vibration API at all, it fails gracefully without breaking your code. For platforms that only support simple patterns, it still works.
- Lightweight & Zero Dependencies: The entire library is tiny, making it a no-brainer to add to any project for a potential UX upgrade.
The clever part isn't reinventing the wheel—it's giving the existing wheel a much better steering system.
How to Try It
Getting started takes about two minutes.
-
Install it: You can grab it from npm.
npm install web-hapticsOr simply load it via a CDN in your HTML:
<script src="https://unpkg.com/web-haptics/dist/web-haptics.umd.js"></script> -
Use it: Import and start triggering effects.
import { Haptics } from 'web-haptics'; const haptics = new Haptics(); // Attach to a button click document.getElementById('myButton').addEventListener('click', () => { haptics.impact('medium'); }); // Or use a notification effect haptics.notification('warning');
The best way to see it in action is to check out the official demo. Open it on a device that supports vibration (like an Android phone):
View the web-haptics Demo
Tap the buttons on the demo page. You'll feel the distinct difference between a "soft" impact and a "heavy" one.
Final Thoughts
This library is a perfect example of the web platform's ethos: taking a basic, often overlooked API and wrapping it in a layer of developer experience that makes it actually useful. You probably shouldn't use it for critical gameplay mechanics in a web game (yet), but it's fantastic for adding that extra layer of polish to button clicks, toggle switches, notifications, or form validation in your progressive web apps.
It's a small tool that solves a specific problem elegantly. Next time you're building an interactive mobile web experience, consider dropping this in. A little tactile feedback can make your app feel surprisingly more native and responsive.
Found an interesting tool? Share it with us @githubprojects.