Using the Vibration API – Part of WebAPI

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

Tagged on: , , ,

18 thoughts on “Using the Vibration API – Part of WebAPI

  1. mike nowak

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

  2. Justin Lebar

    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.

  3. Robert Nyman

    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.

  4. Robert Nyman

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

  5. Robert Nyman

    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.

  6. Justin Lebar

    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.

  7. why

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

  8. DaveC

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

    (Sorry couldn’t resist!)

Leave a Reply