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!

JSFiddle demo.

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.

About Robert Nyman [Editor emeritus]

Technical Evangelist & Editor of Mozilla Hacks. Gives talks & blogs about HTML5, JavaScript & the Open Web. Robert is a strong believer in HTML5 and the Open Web and has been working since 1999 with Front End development for the web - in Sweden and in New York City. He regularly also blogs at http://robertnyman.com and loves to travel and meet people.

More articles by Robert Nyman [Editor emeritus]…


34 comments

  1. Mustafa

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

    January 26th, 2012 at 07:32

    1. Robert Nyman

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

      January 26th, 2012 at 12:57

  2. DaveC

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

    (Sorry couldn’t resist!)

    January 26th, 2012 at 07:40

    1. Robert Nyman

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

      January 26th, 2012 at 12:59

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

    January 26th, 2012 at 07:59

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

      January 26th, 2012 at 08:48

  4. Me

    Didn”t work…

    January 26th, 2012 at 08:19

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

      January 26th, 2012 at 08:49

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

    January 26th, 2012 at 08:34

    1. Robert Nyman

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

      January 26th, 2012 at 08:50

      1. Justin Lebar

        Nope.

        http://hg.mozilla.org/mozilla-central/file/e758551e3924/dom/base/Navigator.cpp#l672

        January 26th, 2012 at 08:52

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

          January 26th, 2012 at 08:56

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

    January 26th, 2012 at 09:06

    1. Robert Nyman

      I understand.
      Thanks for the clarification!

      January 26th, 2012 at 09:09

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

    January 26th, 2012 at 09:21

    1. Rob Hawkes

      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

      January 26th, 2012 at 10:03

  8. Pete

    “noticifaction” isn’t spelled quite right…

    January 26th, 2012 at 09:59

    1. Robert Nyman

      Thanks, I fixed that now. :-)

      January 26th, 2012 at 10:01

  9. Max

    Hej,

    thanks for the status update. I just installed Aurora and ran some tests, especially the ones written by John Hammink. One of these (or the combination of them?) led my phone to vibrate endlessly.

    Even when I killed the Aurora app the phone still vibrated and only stopped after several seconds (in which I nearly approached the point of rebooting).

    I couldn’t find any discussion or sth. about the security considerations relating to the Vibration API, even the W3C doesn’t mention such aspects in http://www.w3.org/TR/vibration/ .

    How is the Firefox team’s attitude towards these implications? At least https://wiki.mozilla.org/Security/Firefox/Fennec/WebVibrator#Threat_Brainstorming mentions some aspects but these don’t seem to be solved.

    Cheers!
    Max

    January 27th, 2012 at 01:41

    1. Robert Nyman

      Hi,

      Thanks for asking and testing!
      John’s test might not be entirely 1:1 with the current implementation, since it was a couple of months ago they were written, but I can confirm that something like that could happen.

      When it comes to security, I know that things like the tab should be visible has been discussed. I’ll ask someone in the team to reply, but also, feel free to discuss it in the WebAPI mailing list.

      January 27th, 2012 at 02:49

    2. Justin Lebar

      Max, can you please file a bug on this? This shouldn’t happen, but I need some more information to understand what’s going wrong.

      http://bugzilla.mozilla.org

      January 27th, 2012 at 07:39

    3. Jonas Sicking

      The security model for the vibration API is that we turn off the vibrator as soon as the user switches tabs or leaves/closes the page. In other words, if a page turns on the vibrator and the user is annoyed or worried about battery usage, simply leaving or closing whatever page the user is currently looking at should turn off the vibrator.

      So you can’t end up with the situation that happens in flash right now that “I have 50 tabs open and one of them is playing some sound, but I have no idea which”. A background tab can never turn on the vibrator. If the vibrator is on, it’s because of the page you are looking at right now. Just close it and the vibrator will turn off.

      January 30th, 2012 at 01:31

  10. Adrian von Gegerfelt

    What about the intensity of the vibration?

    Being a jQuery fan, I can’t help but think that adding lots of parameters if you wish to add pauses is a bit messy, I’d rather have something like

    vibrate[time,intensity]
    [something similar to wait()]
    vibrate[time,intensity]

    or like jQuery would do it with animation:

    navigator.vibrate(time, intensity).delay(time).vibrate(time, intensity).delay(time) etc…

    January 27th, 2012 at 05:48

    1. Justin Lebar

      Most hardware supports just one vibration intensity, so that’s what we started with.

      January 27th, 2012 at 07:38

  11. Max

    Justin,

    I’m gonna try to reproduce it and then file a bug. I’ll post the URL here when I’m done.

    January 27th, 2012 at 07:56

    1. Robert Nyman

      Thank you!

      January 30th, 2012 at 23:54

  12. Max

    Jonas,

    that was what I expected but as I stated in my comment didn’t experience. I killed the app and the vibrator stayed on for some seconds. I still have to reproduce it, though.

    Max

    January 30th, 2012 at 02:43

    1. Robert Nyman

      Yes, Jonas’ reply was more in general to your question about security implications for the Vibration API.

      January 30th, 2012 at 23:53

  13. Pavel Pavlov

    How do you turn it offf !? ..Please reply ASAP !

    February 1st, 2012 at 06:27

    1. Robert Nyman

      Do you mean programmatically (which is covered in the blog post) or as an end user?

      February 1st, 2012 at 06:34

  14. Ollie Parsley

    FYI, I just released a simple wrapper JS wrapper for the API called Vibrate.js. http://vibratejs.com

    Ollie

    February 22nd, 2012 at 14:54

    1. Robert Nyman

      Great, thank you!

      February 23rd, 2012 at 02:18

  15. Alejandro Lechuga

    Do I have to enable some kind of flag on my Firefox Beta for Android ?

    January 24th, 2013 at 18:29

    1. Robert Nyman [Editor]

      Not to my knowledge. It should work right away.

      January 25th, 2013 at 01:55

Comments are closed for this article.