1. Detects if the device supports gravity sensing event deviceorientation
Deviceorientation provides the physical orientation information for a device, represented as a spin-angle function Motionhandler (event) {accgravity = event.acceleration;} for a series of local coordinate systems ;} if (Window. devicemotionevent) { Window.addeventlistener ("Devicemotion", Motionhandler, False); } else { Document.body.innerHTML = "What user agent u r using???";}
Simple Start shaking
$ (". Start_btn"). On ("click", Function () {alert ("Turn on Shake Le"), Window.addeventlistener (' devicemotion '), Devicemotionhandler, False);}) var speed = 20;//speedvar x = y = z = lastx = lasty = Lastz = 0;function Devicemotionhandler (eventData) {var acceleration = Eventdata.accelerationincludinggravity;x = Acceleration.x;y = Acceleration.y;z = Acceleration.z;if (Math.abs (x-lastx ) > Speed | | Math.Abs (y-lasty) > Speed | | Math.Abs (Z-LASTZ) > Speed) {//simple shake to Trigger code alert (1); $ ("body"). Prepend ("<p>accelerationx:" + acceleration.x + "& Lt;/p> "); $ (" body "). Prepend (" <p>accelerationy: "+ acceleration.y +" </p> "); $ (" body "). Prepend (" <p >accelerationz: "+ acceleration.z +" </p> ") window.removeeventlistener (' Devicemotion ', Devicemotionhandler , false);} LASTX = X;lasty = Y;lastz = Z;}
Refer to the link please sweep here
Application scenario: One-time or multiple-intermittent shaking of the situation can be used
The principle is compared by comparing the acceleration of XY or Z with a given value. How to be greater than the given value we decided he was going to.
Let's take a look at how the user is constantly shaking without stopping?
we can set a timestamp, every time to detect the XY or z acceleration, to ensure that every time we go to detect his value is greater than a given value, so that the user has been out of the state of shaking: However, if you always stay in the state of shaking, the difficulty will be relatively large. Let's take a compromise and get the average.
Each time I record the acceleration of a given coordinate, and then add up the acceleration value of each record to the total number of times recorded.
Detects whether to start shaking
varSetinterisshake =setinterval (function () {/************_isshakespeed****************/ var_isshakespeedx =accgravity.x; _isshakespeedx=Math.Abs (_isshakespeedx); _isshakexnum++; if(_isshakexnum <5) {_isshakexnums.push (_isshakespeedx); } Else{_isshakexnum=0; _isshakexnum_val=0$.each (_isshakexnums, function (i, n) {_isshakex_val+=N; }); _isshakex_val= _isshakex_val/_isshakexnums.length _isshakexnums= []; //$ (". Debug_box"). Prepend ("<p>_isshakex_val:" + _isshakex_val + "</p>");//Debug 0818 } /****************************/ /************_isshakespeed****************/ var_isshakespeedy =accgravity.y; _isshakespeedy=Math.Abs (_isshakespeedy); if(_isshakex_val >) { //alert ("Start shaking");clearinterval (setIntervalTime2)//there's a change. Start adding time_time + = -; } } /****************************/ }, -);
Detect whether continuous shaking
var_loopnum = 0; var_loopnums = []; var_loopnum_val = 0; functionSetintervaltimefun () {varSetintervaltime = SetInterval (function() {_time+ = 100; //x var_shakespeed =accgravity.x; _shakespeed=Math.Abs (_shakespeed); var_isstartskake = 0; _loopnum++; if(_loopnum < 5) {_loopnums.push (_shakespeed); } Else{_loopnum= 0; _loopnum_val= 0$.each (_loopnums,function(i, N) {_loopnum_val+=N; }); _loopnum_val= _loopnum_val/_loopnums.length _loopnums= [];} _loopnum++; if(_loopnum < 5) {_loopnums.push (_shakespeed); } Else{_loopnum= 0; _loopnum_val= 0$.each (_loopnums,function(i, N) {_loopnum_val+=N; }); _loopnum_val= _loopnum_val/_loopnums.length _loopnums= []; } if(_loopnum_val < 6) {-----Interrupt constant Shake//alert ("less than 6")clearinterval (setintervaltime); _sec= _time/1000; //Start a lotteryTestlotteryfun (_SEC); } Else { //alert ("Both greater than 6")-----Continuous shaking // } } } }, 100); }
Specific effects sweep the QR code below
JS detection phone shake a shake