Using the Vibration API – Part of WebAPI

by SchoolofWeb.org on January 27, 2012

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 }

Robert Nyman January 27, 2012 at 5:10 am

The name will likely be changed, though. It’s just a temporary one.

Robert Nyman January 27, 2012 at 5:55 am

It’s a temporary name, though, and will probably be Vibration, in line with W3C: http://www.w3.org/TR/vibration/

Rob Hawkes January 27, 2012 at 6:41 am

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

Robert Nyman January 27, 2012 at 7:10 am

Thanks, I fixed that now. :-)

Pete January 27, 2012 at 7:43 am

“noticifaction” isn’t spelled quite right…

mike nowak January 27, 2012 at 8:41 am

> 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.

Robert Nyman January 27, 2012 at 9:10 am

I understand.
Thanks for the clarification!

Justin Lebar January 27, 2012 at 9:29 am

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.

Robert Nyman January 27, 2012 at 9:57 am

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.

Justin Lebar January 27, 2012 at 10:33 am
Robert Nyman January 27, 2012 at 10:58 am

True, for now. My understanding, though, is that those other two are supposed to work as well.

Robert Nyman January 27, 2012 at 11:23 am

If you give us more information about what didn’t work, on which device and version of Firefox, we might be able to help.

Robert Nyman January 27, 2012 at 11:50 am

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.

Justin Lebar January 27, 2012 at 11:55 am

And if you want to stop the vibration, you can simply call the mozVibrate method with an undefined argument, 0, null or empty pattern, like this:

A null or undefined argument will cause mozVibrate to throw an error. 0 or [] works as intended.

Me January 27, 2012 at 12:26 pm

Didn”t work…

why January 27, 2012 at 12:36 pm

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) …

DaveC January 27, 2012 at 1:05 pm

Wow!… The a Vibrator API… my wife might finally take an interest in what I do!! ;)

(Sorry couldn’t resist!)

Mustafa January 27, 2012 at 1:18 pm

Cool API its just ‘hard’ to remain serious when reading it :D

Leave a Comment

Previous post:

Next post: