需要留意的5个HTML5 JavaScript API

电池状态API

电池状态API允许任何网页通过javascript检查设备(笔记本电脑,手机或平板电脑)电池的状态:

1 var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery
2 
3 console.log("Battery charging: ", battery.charging); // true
4 console.log("Battery level: ", battery.level); // 0.58
5 console.log("Battery discharging time: ", battery.dischargingTime);

正如你可以看到,你需要做更多的检查,使你的代码跨浏览器兼容,我已经做了一些研究,我发现了一个电池状态API的小的包装类,battery.js

 1 if(Battery.isSupported()) {
 2     // Get the battery status
 3     var status = Battery.getStatus();
 4     console.log('Level: ' + Math.floor(status.level * 100) + '%'); // 30%
 5     console.log('Charging: ' + status.charging);                   // true
 6     console.log('Time until charged: ' + status.chargingTime);     // 3600 (seconds) or Infinity
 7     console.log('Battery time left: ' + status.dischargingTime);   // 3600 (seconds) or Infinity
 8 
 9     // Register a handler to get notified when battery status changes
10     Battery.onUpdate = function(status) {
11         console.log(status); // {level, charging, chargingTime, dischargingTime}
12     };
13 }

手柄API


手柄API允许基于浏览器的游戏连接你的计算机和控制台游戏手柄。随着HTML5游戏的普及,增加的这个API在将来会非常有用。

 1 navigator.gamepads = navigator.webkitGamepads || navigator.MozGamepads;
 2 
 3 var requestAnimationFrame = window.webkitRequestAnimationFrame ||
 4                             window.mozRequestAnimationFrame;
 5 var cancelAnimationFrame = window.webkitCancelAnimationFrame ||
 6                            window.MozCancelAnimationFrame;
 7 
 8 var controllers = {}; // Stash connected controllers.
 9 var reqId = null;
10 
11 function onConnected(e) {
12     controllers[e.gamepad.index] = e.gamepad;
13     runAnimation();
14 }
15 
16 function onDisconnected(e) {
17     delete controllers[e.gamepad.index];
18     cancelAnimationFrame(reqId);
19 }
20 
21 window.addEventListener('webkitgamepadconnected', onConnected, false);
22 window.addEventListener('webkitgamepaddisconnected', onDisconnected, false);
23 
24 window.addEventListener('MozGamepadDisconnected', onDisconnected, false);
25 window.addEventListener('MozGamepadConnected', onConnected, false);

资料来源The Edge of HTML5
有一个类库叫做gamepadjs,会让你很容易的使用这个API.

设备定向API


使用设备定向API,你能确定设备的方向,以及收集它的运动(α,β和γ)信息。

 

 1 if (window.DeviceOrientationEvent) {
 2     window.addEventListener('deviceorientation', function(event) {
 3         var a = event.alpha,
 4             b = event.beta,
 5             g = event.gamma;
 6         console.log('Orientation - Alpha: ' + a + ', Beta: '+ b + ', Gamma: ' + g);
 7     }, false);
 8 } else {
 9     console.log('This device does not support deviceorientation');
10 }

要使用此功能,你的设备必须具有陀螺仪功能,caniuse.com上可以找到更多浏览器支持

地理位置API


地理位置API可以让你通过信任的网站分享你的位置。也可以在网页中通过JavaScript使用经度度和纬度,将定位到的东西发送回远程Web服务器,例如,发现当地企业或在地图上显示你的位置,它可以用做“Geo-tagging”来处理用户的内容,如照片。

 1 if (navigator.geolocation) {
 2     navigator.geolocation.getCurrentPosition(function(position) {
 3         var lat = position.coords.latitude,
 4             lon = position.coords.longitude;
 5         console.log('Geolocation - Latitude: '+ lat +', Longitude: '+ lon);
 6     });
 7 }
 8 else {
 9     console.log('Geolocation is not supported for this Browser/OS version yet.');
10 }

caniuse.com上可以找到更多浏览器支持

页面可见性API

页面可见性API能让你确定你的页面是可见的或不可见的,当你最小化页面或移动到另一个选项卡时,visibilitychange 事件会被触发,如果你是一个游戏开发者的话,这将非常有用,当用户改变页面时会暂停游戏。

 

1 document.addEventListener('visibilitychange', function(e) {
2     console.log('hidden:' + document.hidden,
3               'state:' + document.visibilityState)
4 }, false);

caniuse.com上可以找到更多浏览器支持

 

 

原文链接:需要留意的5个HTML5 JavaScript API

 

原文链接:http://daker.me/2013/06/5-html5-javascript-apis-to-keep-an-eye-on.html

posted @ 2013-06-04 10:03  【Winco】  阅读(310)  评论(0编辑  收藏  举报