Vibr8 is a lightweight (1.2K gzipped) Vibration API wrapper. It reduces inconsistencies of API’s implementations across browsers and gives you some extra powers.

Install

The easiest way to install Vibr8 is using Bower package manager: bower install vibr8. You also can manually download it's source code with examples or minified version. Vibr8 packaged as UMD module to be loaded anywhere, just plop it into a script tag in some html:

<script src="vibr8.min.js"></script>

A window global named Vibr8 will be created, or if a module system is detected in the host environment, it will be used. You can also require Vibr8 using Browserify.

Documentation

Vibr8 is a singleton class for dealing with Vibration API. You can instantiate it using the Vibr8 constructor. It's a good idea to check for the presence of API support before using it:

Vibr8.vibrationSupported()

Static method, checks whether Vibration API is supported.

new Vibr8([pattern][, period])

Creates new Vibr8 instance with specified or default pattern and vibration cycle duration. First argument must be an array of values from 0 to 100 with sum of 100. The even indexes represent vibration duration, the odd indexes represent a delay before the next vibration in percents. Resulting vibration pattern will be scaled to vibration cycle duration which you can specify with period parameter. For instance the following code makes device to vibrate for 1080ms with 120ms delays:

var vibr8 = new Vibr8([90, 10], 1200)
vibr8.start()

Thus by changing pattern you are able to change form of vibrations, by changing period you affect vibration speed.

vibr8.start([pattern][, period])

Starts vibrations. Repeats vibration pattern continuously until vibr8.stop() call. Optionally you can redefine vibration pattern and vibration cycle duration.

vibr8.repeat(times[, pattern][, period])

Starts vibrations. Repeats vibration pattern for a specified number of times or until vibr8.stop() call. Optionally you can redefine vibration pattern and vibration cycle duration. This method does nothing if vibration already inited by vibr8.start() method, in that case do vibr8.stop() before using this method.

vibr8.stop()

Stops vibrations immediately whether they were initialized by vibr8.start() or vibr8.repeat().

vibr8.setPeriod(period)

Sets a vibration cycle duration, overrides old value. Can be called while vibration is in active state, changes will be applied immediately. Use this method in order to change vibration speed.

vibr8.getPeriod()

Returns current vibration cycle duration.

vibr8.setPattern(pattern)

Sets a vibration pattern, overrides old pattern. Can be called while vibration is in active state, changes will be applied immediately.

vibr8.bind(eventName, handler)

Adds a listener for the specified eventName.

Returns emitter, so calls can be chained like so:

var vibr8 = new Vibr8([50, 50], 1200)
vibr8.bind('started', function () {
  console.log('Device is vibrating')
}).bind('ended', function () {
  console.log('Device is idle')
}).start()

Event list: