Vibration API: who needs it and why?
- Tutorial
On February 10, the World Wide Web Consortium announced that the standard describing the API for controlling vibration received the recommended status. The ability to force the browser to vibrate with the device itself has been available for some time, but only now it was finally framed by the recommended specification, so it's time to think about how and where it can be used on a daily basis. However, already at the very beginning, the authors of the standard warn that the API is designed specifically for cases where simple tactile feedback is required, and it is not intended to be used as a general mechanism for notifying the user. For notifications, it is recommended to use the Notifications API.
Those wishing to join the official specification with all its nuances can do it onW3C site , and here we only take a quick look at how all this can be used, and also highlight some points not disclosed in the documentation.
Obviously, desktop computers, laptops, and even just tablets fly by, because they are not able to vibrate if they wish. Yes, and if the system unit would rattle and move around the room like a washing machine, then users most likely did not appreciate it, so it’s hardly worth regretting it. In view of the existence of such fragmentation of devices, as well as the banal unavailability of a number of browsers, it makes sense to start by checking whether vibration control is supported, and this is not in the specification. However, it all comes down to just checking if the navigator has a vibrate () method:
The vibration itself is set by transmitting the navigator.vibrate its duration in milliseconds:
However, everything is not so boring, and it is possible to transmit an entire array, the values of which will be a sequence of durations of vibration signals and pauses between them:
Here the even elements of the array determine the duration of the vibrational signals, and the odd ones determine the duration of the pauses between them (the counting of the elements of the array starts from zero, conventionally considered an even number). That is, the above code will make the device vibrate first for a second, then wait half a second and vibrate for a second again. After two and a half seconds, everything will end, that is, the process must be looped manually by the script.
The vibration process is not blocking, that is, the code will continue to execute while the device vibrates.
If you want to stop the vibration, you should pass the value 0:
Or if you like it more:
The transmission of a zero value immediately cancels all previously started vibrations.
Basically, this is all that can be said about the API for controlling vibration on web pages. And now there is a proposal to brainstorm and think together about why all this can be used. For starters, there are the following ideas:
And now honestly: who and what hooked the potential capabilities of the Vibration API?
Those wishing to join the official specification with all its nuances can do it onW3C site , and here we only take a quick look at how all this can be used, and also highlight some points not disclosed in the documentation.
Obviously, desktop computers, laptops, and even just tablets fly by, because they are not able to vibrate if they wish. Yes, and if the system unit would rattle and move around the room like a washing machine, then users most likely did not appreciate it, so it’s hardly worth regretting it. In view of the existence of such fragmentation of devices, as well as the banal unavailability of a number of browsers, it makes sense to start by checking whether vibration control is supported, and this is not in the specification. However, it all comes down to just checking if the navigator has a vibrate () method:
if ("vibrate" in navigator) {
// Вибрация
}
The vibration itself is set by transmitting the navigator.vibrate its duration in milliseconds:
navigator.vibrate(1000);
However, everything is not so boring, and it is possible to transmit an entire array, the values of which will be a sequence of durations of vibration signals and pauses between them:
navigator.vibrate([1000, 500, 1000]);
Here the even elements of the array determine the duration of the vibrational signals, and the odd ones determine the duration of the pauses between them (the counting of the elements of the array starts from zero, conventionally considered an even number). That is, the above code will make the device vibrate first for a second, then wait half a second and vibrate for a second again. After two and a half seconds, everything will end, that is, the process must be looped manually by the script.
The vibration process is not blocking, that is, the code will continue to execute while the device vibrates.
If you want to stop the vibration, you should pass the value 0:
navigator.vibrate(0);
Or if you like it more:
navigator.vibrate([]);
The transmission of a zero value immediately cancels all previously started vibrations.
Basically, this is all that can be said about the API for controlling vibration on web pages. And now there is a proposal to brainstorm and think together about why all this can be used. For starters, there are the following ideas:
- Using vibration in web games, for example, jittering your device while racing or shooting.
- Vibration alerts when an important change occurs on the page, for example, when a new message appears in the web chat.
- Duplication of sound signals, if they are present in the interface, for example, to provide accessibility for users with hearing impairments (both physical impairments and simply muted sound on the device).
- Just adding cool little ruffles to the interface, for example, vibration pushes when you click buttons on the site.
- Annoying and frightening user spec. effects, for example, violently vibrating the device when the page loads, which will cause about the same negative as unexpectedly turning on the music.
And now honestly: who and what hooked the potential capabilities of the Vibration API?
Only registered users can participate in the survey. Please come in.
What do you think of the Vibration API?
- 18.7% This is an absolutely pointless technology. 75
- 44.1% Maybe this is useful to someone, but in those projects that I do, there is no place for this technology. 176
- 25.5% Perhaps I will find a couple of narrow applications of this technology in those projects that I am engaged in. 102
- 8% I liked it, now I will regularly use this technology. 32
- 3.5% In one way or another, I have been using the Vibration API for several years now and all this is not new to me. 14