Tag: Time Initialize res FFT ima innerhtml ret com detection device
HTML5 has added some JavaScript API interfaces, such as geo-positioning, gravity sensing, and so on. Today's main introduction to the Devicemotion event (device event), which provides acceleration information for a device, is expressed as a coordinate system defined on the device. It also provides the rotation rate of the device in the coordinate system. If feasible, the event should provide acceleration information at the center of the device. This article has explained the HTML5 realizes the mobile phone to shake the effect.
mobile phone Shake the realization of the idea:1. Whether the testing equipment supports gravity sensing;
1 //monitor motion sensing events to see if hardware motion is supported 2 3 if (window. devicemotionevent) { 4 5alert ("Your device supports hardware calls"); 6 7 } else { 8 9alert ("Your device does not support hardware calls"); Ten One}
View Code2, bound Motion Sensing (devimotion) event;
1 false);
View Code3, according to the Devimotion event object, obtains three direction the acceleration of gravity;
1 /*2 3 * [Devicemotionhandler shake processing function]4 5 * @param {[type]} EventData [Event Object]6 7 * @return {[type]} [no return value]8 9 * @author Liu Guoli, Chen Nenburg, HTML5 AcademyTen One */ A - functionDevicemotionhandler (eventData) { - the //Acceleration Acceleration - - varAcceleration =eventdata.accelerationincludinggravity; - +$ (". Wrap") [0].innerhtml = acceleration.x + "+ acceleration.y +" "+acceleration.z; - +}View Code4, in order to prevent the system think that the simple change is also shaking the phone, so at every interval to carry out the calculation of three direction values;
1 //Get current Time2 3Curtime =NewDate (). GetTime ();4 5 if(Curtime-lasttime > 100) {6 7 //calculate the time off8 9Difftime = Curtime-Lasttime;Ten One //record the last time A -Lasttime =Curtime; - the //gets the value of the current three direction - -x =acceleration.x; - +y =ACCELERATION.Y; - +z =acceleration.z; A at //calculation speed, in order to prevent negative numbers, the absolute value - -Speed = Math.Abs ((x + y + z-lastx-lasty-lastz)/difftime * 10000); - - //record values for the last three directions - inLASTX =x; - toLasty =y; + -Lastz =Z; the *};View Code5, when the calculated value is greater than the predetermined value (threshold), perform the corresponding operation.
1 if (Speed > shake_threshold) {23 alert ("Shake-Shake"); 4 5 };
View Code
a complete example
1 varcon = document.getElementById ("Con");2 3(function(){4 5 //Monitor motion sensing events to see if hardware motion is supported6 7 if(window. Devicemotionevent) {8 9Window.addeventlistener (' devicemotion ', Devicemotionhandler,false);Ten One}Else { A -Alert ("Your device does not support hardware calls"); - the } - - //Initialization of variables - + varx = 0, - +y = 0, A atz = 0, - -LASTX = 0, - -Lasty = 0, - inLastz = 0, - toCurtime = 0, + -Lasttime = 0, the *Difftime = 0, $ Panax NotoginsengSpeed = 0; - the //set a threshold value + A varShake_threshold = 800;//set the shaking threshold to 600 to run the appropriate action the + /* - $ * Function: Calculate three direction of acceleration of gravity, achieve a certain value to the corresponding operation $ - * HTML5 Academy, Liu Guoli, Chen Nenburg - the * - Wuyi */ the - functionDevicemotionhandler (eventData) { Wu - varAcceleration =eventdata.accelerationincludinggravity; About $ //Get current Time - -Curtime =NewDate (). GetTime (); - A //calculates the time difference when the difference is greater than a certain value to perform the calculation of three directions of speed + the if((curtime-lasttime) > 100) { - $ //record the last time the theDifftime = Curtime-Lasttime; the theLasttime =Curtime; - in //gets the value of the current three direction the thex =acceleration.x; About they =ACCELERATION.Y; the thez =acceleration.z; + - //calculation speed, in order to prevent negative numbers, the absolute value the BayiSpeed = Math.Abs ((x + y + z-lastx-lasty-lastz)/difftime * 8000); the the if(Speed >shake_threshold) { - -Alert ("I shook it."); the the }; the the //record values for the last three directions - theLASTX =x; the theLasty =y;94 theLastz =Z; the the };98 About } - 101})();View Code
HTML5 realize mobile phone shake a shake