在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,经过改时间能够获取设备的运动状态、加速度等数据(另还有deviceOrientation事件提供了设备角度、朝向等信息)。
而经过DeviceMotion对设备运动状态的判断,则能够帮助咱们在网页上就实现“摇一摇”的交互效果。spa
运动事件监听code
if (window.DeviceMotionEvent) { window.addEventListener('devicemotion', deviceMotionHandler, false); } else { alert('你的手机太差了,买个新的吧。'); }
获取加速度信息
“摇一摇”的动做既“必定时间内设备了必定距离”,所以经过监听上一步获取到的x, y, z 值在必定时间范围内的变化率,便可进行设备是否有进行晃动的判断。而为了防止正常移动的误判,须要给该变化率设置一个合适的临界值。blog
function deviceMotionHandler(eventData) { var acceleration = eventData.accelerationIncludingGravity; var curTime = new Date().getTime(); if ((curTime - last_update) > 100) { var diffTime = curTime - last_update; last_update = curTime; x = acceleration.x; y = acceleration.y; z = acceleration.z; var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000; var status = document.getElementById("status"); if (speed > SHAKE_THRESHOLD) { doResult(); } last_x = x; last_y = y; last_z = z; } }
效果如图所示:事件