Haptic Feedback for Web Apps with the Vibration API

Haptic Feedback for Web Apps with the Vibration API

In case you’re new to the term, haptic feedback is the tactile sensation generated from your mobile devices, which helps give you a sense of touch (vibrations or motions) in response to interactions. It offers a lot of benefits, like enhancing the user experience and creating room for improved immersion in virtual environments. You can integrate haptic feedback into your web applications through the vibration API, as this article will show.

Using the Vibration API for Haptic Feedback in Web Applications

As a developer, you’ve probably heard of haptic feedback and the numerous benefits that come with it. Using the Vibration API, you can control the vibration capabilities of compatible devices. That API is a Javascript API that allows you to control the vibration capabilities of a user’s device from your web application. It allows you to create feedback experiences by triggering vibrations of various durations and intensities. These vibrations respond to a user’s interaction within your web app. It consists of a single method known as navigator.vibrate(), which accepts one or two parameters, which are:

  • pattern: This is an array of numbers representing the vibration pattern. Each number in the array signifies the alternation between the duration of the vibration in milliseconds and the duration of silence between the vibrations. For example, [100, 200, 300] would trigger three vibrations: a 100-millisecond vibration, 200 milliseconds of silence, then a 300-millisecond vibration, and so on.
  • options: The options parameter is an optional object that contains properties used to control vibration behavior. Some of these properties include repeat, which signifies the number of times the pattern should be repeated, and vibrate, which allows you to check if a user’s device supports vibration.

Using the navigator.vibrate() method with your preferred pattern and option, you can have seamless control over the vibrations on the user’s device. You can use the method with a single parameter, which simplifies the process of triggering simple vibrations.

Most modern web browsers, such as Firefox, Chrome, and Safari, all support the Vibration API. On average, the API is supported on mobile devices like smartphones and tablets, as well as desktops and laptop computers. It’s good to note that the support may also vary based on a user’s device and operating system. An uncommon device or one with an old operating system might have support issues with the API. For more explanation, you can check out the vibration API documentation on MDN.

Implementing Haptic Feedback with the Vibration API

When integrated using the vibration API, haptic feedback can serve as a powerful tool to boost user experiences in your web apps. It has many benefits, including an increase in user interactions, enhanced notifications, improved accessibility for users with visual or auditory impairments, and better immersion into the gaming experience. With that said, how do you implement haptic feedback with a vibration API into your web apps?

You need to take a few steps to ensure a seamless integration. It usually involves checking for browser support, defining the vibration pattern, invoking the vibration pattern, and so on. Here’s a more detailed step-by-step guide on how to implement haptic feedback with the vibration API:

Check Browser Support

Before you head on to implementing the haptic feedback, one of the first things you must do involves checking if a user’s browser supports the vibration API. You can use feature detection to determine if the navigator.vibrate() method is available. Example:

if ("vibrate" in navigator) {
  // Vibration API is supported
} else {
  // Vibration API is not supported

Define the Vibration Pattern

You need to determine the vibration pattern to be used for different interactions within your web app. The pattern can range from a simple vibration to a more complex pattern tailored to specific use cases. For example:

// Defining a custom vibration pattern
var customPattern = [100, 200, 300];

Invoke the Vibration API

You must call the navigator.vibrate() method with your desired vibration pattern and options. This is done to trigger haptic feedback. It can be done within event listeners or other relevant parts of the application code. Example:

// Trigger a vibration with the custom pattern

Handle Permissions

If applicable, some browsers may require user permission to enable vibration functionality. So, if permission is required, you will need to prompt the user to grant permission before invoking the vibration API. Example:

// Check if permission is required
if (navigator.vibrate) {
  navigator.vibrate([100]); // Attempt to trigger a vibration
} else {
  // Vibration API is not supported or permission denied
  // Prompt user to enable vibration functionality

Optimize the User Experience

An extra step to follow is to consider the timing and frequency of the haptic feedback. This is to make sure that the user experience is enhanced rather than deteriorated. Use haptic feedback judiciously and sparingly, avoiding excessive or unnecessary vibration that may become annoying. Here’s a simple example for button clicks:

// Trigger haptic feedback for button clicks
document.getElementById("myButton").addEventListener("click", function () {
  navigator.vibrate(50); // Short vibration for button click

Code Examples for Different Scenarios

Implementing haptic feedback using the vibration API involves writing code to trigger vibrations in response to various interactions. Let’s examine some code examples for several scenarios or interactions.


Notifications are an essential aspect of web apps for alerting users to important events or updates. By implementing haptic feedback in notifications, you can ensure that users are alerted to several things they might have missed. Here’s a previously mentioned code example that fits perfectly:

// Trigger a short vibration for notification

Game Interactions

Haptic feedback enhances immersion in gaming applications by simulating physical sensations corresponding to in-game interaction. By triggering vibrations for game actions such as collisions, character actions, or impacts, you can create a more engaging gaming experience for users. Here’s a code example:

// Define a custom vibration pattern for a game action
var customPattern = [100, 200, 100, 300, 200];

// Trigger the custom pattern when a game action occurs
function triggerGameAction() {

Form Submissions

Providing feedback for form submissions can help reassure users that their actions have been processed. Triggering haptic feedback while submitting a form can help provide users with immediate confirmation and increase the user experience. Here’s a code example for form submissions:

// Trigger a vibration when the form is submitted
document.getElementById("myForm").addEventListener("submit", function () {
  navigator.vibrate(100); // Trigger a short vibration

Best Practices for the Vibration API (for Haptic Feedback)

The vibration API can significantly enhance the user experience, but designing effective haptic feedback requires careful consideration of various factors. Why is it so important to design effective feedback? This is because it plays key roles in increasing user benefits, which include:

Usability Enhancements: Haptic feedback should be designed to enhance usability by confirming user interactions. When it is well-designed, interactions become more intuitive and responsive.

  • Accessibility Improvements: A more thoughtful design can improve accessibility for users with disabilities (especially visual or auditory impairments). By implementing tactile feedback, your web apps can provide additional cues and information that make them more accessible to a wider range of users.
  • Engagement and Immersion: Effective haptic feedback can increase a user’s immersion and engagement, particularly in gaming and multimedia applications. Simulating physical sensations can enhance realism and make experiences more captivating.

There are also various factors you will need to consider when designing haptic feedback experiences with the vibration API. You must consider the timing, feedback, consistency, and so on. Here’s a more detailed explanation of some of these factors:

  • Frequency and Intensity: It’s important to ensure the frequency and intensity of vibrations provide meaningful feedback that doesn’t overwhelm or distract users. User Preferences: You should consider giving users the option to customize or disable haptic feedback according to their preferences.
  • Consistency Across Platforms: Test haptic feedback features across several browsers, devices, and operating systems. This is to ensure that its behavior and performance are consistent. It’s important to know the list of browsers that fully support the Vibration API as this will help to work on its consistency.

With the above best practices and factors, you should be able to design an effective haptic feedback experience using the vibration API in your web applications. As technology continues to evolve and users become increasingly accustomed to an interactive and immersive experience, haptic feedback will offer you a valuable opportunity to provide additional layers of engagement.


In conclusion, integrating haptic feedback using the vibration API represents a significant opportunity for you to elevate the user experience in your web apps. You can create a more intuitive, engaging, and immersive interaction across various contexts by leveraging tactile sensations alongside visual and auditory cues. Throughout this article, we have emphasized the importance of a thoughtful design and consideration of factors such as timing, frequency, user preferences, and consistency. It’s important to embrace haptic feedback because, with all the developments going on in the web industry, its roles and benefits are only set to increase and drive more engagement.

Gain control over your UX

See how users are using your site as if you were sitting next to them, learn and iterate faster with OpenReplay. — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data. Check our GitHub repo and join the thousands of developers in our community.