Skip to content

Instantly share code, notes, and snippets.

@Muscipular
Created January 25, 2016 11:03
Show Gist options
  • Save Muscipular/6f86bbea43eec19a0a85 to your computer and use it in GitHub Desktop.
Save Muscipular/6f86bbea43eec19a0a85 to your computer and use it in GitHub Desktop.
var SHAKE_THRESHOLD = 800;
var last_update = 0;
var x = y = z = last_x = last_y = last_z = 0;
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
alert('本设备不支持devicemotion事件');
}
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;
}
}
function doResult() {
document.getElementById("result").className = "result";
document.getElementById("loading").className = "loading loading-show";
setTimeout(function(){
//document.getElementById("hand").className = "hand";
document.getElementById("result").className = "result result-show";
document.getElementById("loading").className = "loading";
}, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment