首页 > 移动平台 > 详细

JS 手机摇动

时间:2014-07-23 12:51:46      阅读:305      评论:0      收藏:0      [点我收藏+]
var SHAKE_THRESHOLD = 800;//摇动强度
var lastUpdate = 0;
var x, y, z, last_x, last_y, last_z;
var canshake = false;
function deviceMotionHandler(eventData) {
    var acceleration = eventData.accelerationIncludingGravity;
    var curTime = new Date().getTime();
    if ((curTime - lastUpdate) > 100) {
        var diffTime = (curTime - lastUpdate);
        lastUpdate = 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;
        if (canshake && speed > SHAKE_THRESHOLD) {
            canshake = false;
            //DO STH
       if (window.DeviceMotionEvent) { window.removeEventListener(‘devicemotion‘, deviceMotionHandler, false); }
        }
        last_x = x;
        last_y = y;
        last_z = z;
    }
}

JS 手机摇动,布布扣,bubuko.com

JS 手机摇动

原文:http://www.cnblogs.com/allvie/p/3862571.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!