As part of Mozillas WebAPI effort, we have been working with bringing a Vibration API to all devices that support it.
The idea with the Vibration API is to be able to give the user a notification, in a game or other use case, by telling the device to vibrate. It accesses the native vibrator and tells it how long it should vibrate.
Examples
The way to do this is quite simple – in this example the parameter is how long it should vibrate, i.e. the number of milliseconds:
navigator.mozVibrate(1000);
Another way of controlling vibration is giving a vibration pattern, switching between vibrating and being still. The odd parameters in the list is vibration time, the even ones are pauses:
navigator.mozVibrate([200, 100, 200, 100]);
And if you want to stop the vibration, you can simply call the mozVibrate method with an argument of 0 or an empty pattern, like this:
navigator.mozVibrate(0);
navigator.mozVibrate([]);
Try it out!
If you want to try this out right now, you can do so in Firefox Aurora, which is planned to become Firefox 11. Currently, it naturally only works on devices that support vibration, which means Firefox on (most) Android phones.
Note: a possbile caveat could be if you have haptic feedback turned on on your Android device, which then might cancel out the vibration.
Demo
I put together a little demo where you can see the code needed and test it in place. Please play around with this and let us know what you think!
Edit: Our temporary implementation name was Vibrator API, but since it gave the wrong impression, we now call it Vibration API – which is also more in line with the W3C Vibration API draft.
View full post on Mozilla Hacks – the Web developer blog


{ 18 comments… read them below or add one }
The name will likely be changed, though. It’s just a temporary one.
It’s a temporary name, though, and will probably be Vibration, in line with W3C: http://www.w3.org/TR/vibration/
Hi Mike, there is work currently ongoing to bring what is known right now as rumble support to the gamepad. Right now it’s a different implementation to the Vibrator API but we’re aware that we need to find a common ground somewhere.
Here are some links to the progress on gamepad rumble support:
- http://blog.robothaus.org/2011/12/09/gamepad-vibration-zero-to-hero/
- https://bugzilla.mozilla.org/show_bug.cgi?id=680289
Thanks, I fixed that now.
“noticifaction” isn’t spelled quite right…
> Currently, it naturally only works on devices that support vibration, which means Firefox on (most) Android phones.
Any plans on integrating it with the progressing game-pad support API? Seems like a natural fit there.
I understand.
Thanks for the clarification!
There’s a really long thread in the w3c list about this, but the gist is, it’s hard to make null and undefined work with how WebIDL is specified, so we dropped them.
(Also, accepting undefined would mean that vibrate() makes the device stop vibrating, which is pretty weird.)
The big comment taken from Mozilla’s source in the vibrator spec should be removed. It’s non-normative, old, incorrect, and misleading.
I’ve e-mailed the spec’s author to try to get this clarified.
Right, that has changed then. Looking at the W3C work, based on Mozilla’s, states that it would work: http://www.w3.org/TR/vibration/
I’ll update the post to remove it then. Any reasoning for why it wouldn’t work would also be interesting, if you know more about it.
Nope.
http://hg.mozilla.org/mozilla-central/file/e758551e3924/dom/base/Navigator.cpp#l672
True, for now. My understanding, though, is that those other two are supposed to work as well.
If you give us more information about what didn’t work, on which device and version of Firefox, we might be able to help.
In the future, there might might be more parameters – like the number a pattern should be repeated – so just having numbers wouldn’t do then.
A null or undefined argument will cause mozVibrate to throw an error. 0 or [] works as intended.
Didn”t work…
i dont get the point of giving an array where i could give a “rest” style signature, which could be solved as an array with a simple [].slice.call(arguments) …
Wow!… The a Vibrator API… my wife might finally take an interest in what I do!!
(Sorry couldn’t resist!)
Cool API its just ‘hard’ to remain serious when reading it