微信摇功能基于HTML5deviceorientation

微信摇加了很长一段时间,很多朋友都在玩,然后基于HTML5 deviceorientation如何实现微信摇一摇下面详细的分享给大家,供大家参考。




在HTML5,提供devicemotion事件包运动传感器装置的deviceorientation特点通过时间、运动、加速度数据采集装置可以改变时间(另一个deviceorientation事件提供设备的角度,定位信息)。

并通过判断装置通过devicemotion运动状态,可以帮助我们在网络上实施的摇一摇;;交互作用。

体育赛事监测


复制代码代码如下所示:
如果(窗口。devicemotionevent){
Window.addEventListener('devicemotion ',devicemotionhandler,假);
{人}
警告:你的手机坏了,买一个新的。;
}


获得的加速度信息

摇的作用是在一定时间内,装置有一定距离的;因此,我们可以检测设备是否有晃动监测x的变化率,Y和Z值在一定的时间范围内,为了防止正常运动的曲解,它必须设置为一个合适的临界值变化率。


复制代码代码如下所示:
Function deviceMotionHandler (eventData) {
VaR加速度= eventdata.accelerationincludinggravity;
无功curtime =新的日期()GetTime();
如果(((curtime - last_update)> 100){
VaR difftime = curtime - last_update;
last_update = curtime;
x =加速度;
y =加速度;
z =加速度;
速度= math.abs(X + Y + Z last_x - last_y - last_z) / difftime * 10000;
无功状态= document.getelementbyid(状态);
如果(速度> shake_threshold){
DoResult();
}
last_x = x;
last_y = Y;
last_z = Z;
}
}


效果如图所示。