One cool feature of HTML5 is 'Vibration API'. Currently it is only supported in newer builds of firefox and chrome only. It can be used to improve user engagement in your mobile web app.
For example :
- You can use vibration in your mobile web game to show a hit with an enemy or when your car crashes etc.
- You can create a vibration when there is a new message in your chat app
- Or when there is a task going on in your app, and you can vibrate the phone when it is completed
Detecting if vibration is supported or not?
Vibration API is a part of Navigator API. So to check its availability, use the following code
if ("vibrate" in navigator) { // vibration API supported
}
|
// enable vibration support
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate; if (navigator.vibrate) { // vibration API supported
}
|
Vibration API is very easy to use. All you need to do is call vibrate() function passing number of milliseconds or an array of milliseconds.
// vibrate for two seconds
navigator.vibrate(2000); |
// vibrate for one second
navigator.vibrate([200, 100, 400, 100, 200]); |
Stopping a Vibration
Stopping a vibration is as easy as creating it. Simply call again vibrate() function with 0 as parameter or an empty array.
// Either of these stop vibration
navigator.vibrate(0); navigator.vibrate([]); |
Go on to create your next amazing mobile web app or game and don't forget to integrate it with our App42 Cloud APIs and AppWarp SDKs. If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com
Source : http://blogs.shephertz.com/2013/12/23/using-vibrations-in-html5-mobile-web-appgame/
No comments:
Post a Comment