1、调用
2、代码
function shakeMobile()
{
var self = this;
this.SHAKE_THRESHOLD =500;
var _shakeCallback = null;
this.Initialize = function (shakeEventCallback)
{
if (window.DeviceMotionEvent)
{
_shakeCallback = shakeEventCallback;
window.addEventListener(‘devicemotion‘, deviceMotionHandler, false);
return true;
} else
{
return false;
}
};
this.Unitialize = function ()
{
window.removeEventListener(‘devicemotion‘, deviceMotionHandler, false);
};
var last_update = 0;
var x = y = z = last_x = last_y = last_z = 0;
var deviceMotionHandler = function (eventData)
{
var acceleration = eventData.accelerationIncludingGravity;
var curTime = new Date().getTime();
if ((curTime - last_update) > 200)
{
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;
if (speed > self.SHAKE_THRESHOLD)
{
if (_shakeCallback != null)
{
_shakeCallback();
}
}
last_x = x;
last_y = y;
last_z = z;
}
}
}
原文:http://www.cnblogs.com/wl14253/p/5029580.html