html5 重力感应
deviceorientation和 MozOrientation(firefox)
deviceorientation事件可获得三个值alpha,beta,gamma,分别代表绕Z轴的
旋转角度(0~360),绕X轴的旋转角度(-180~180),绕Y轴的旋转角度(-90~90)
MozOrientation事件中可获得三个值z,x,y,分别代表垂直加速度,左右的
斜角度,前后的倾斜角度(取值范围:-1~1
浏览器兼容性处理
window.addEventListener('deviceorientation', orientationListener, false); //方向感应器
window.addEventListener('MozOrientation', orientationListener, false); //方向感应器 for firefox
window.addEventListener('devicemotion', orientationListener, false); //重力加速感应器 for iphone, android
三个旋转方向,z,x,y轴,对应分别是:Alpha,Beta,Gamma
function orientationListener(event) {
var absolute = event.absolute;
var alpha = event.alpha;
var beta = event.beta;
var gamma = event.gamma;
...
}
w3c api:http://w3c.github.io/deviceorientation/spec-source-orientation.html
鬼道分享ppt: http://2014.jsconf.cn/slides/luics-hybrid-api.html
实例:http://www.html5tricks.com/demo/html5-ball-pool/index.html
http://www.pjhome.net/web/Orientation.html
Written on August 22, 2014