Often we play will use to find nearby people, are in the app to achieve the function of shaking the phone. Now, we move this technology to the mobile phone web, for everyone to learn, mainly using the HTML5 important feature is deviceorientation: Official documents
It provides an advanced encapsulation of the underlying directional sensor and motion sensor, providing support for DOM events. This feature consists of two events:
Deviceorientation: Encapsulates the direction sensor data event, can get the mobile phone in the static state of the direction of data, such as the phone's angle, orientation, orientation, and so on.
Devicemotion: The event of moving sensor data is encapsulated, and the motion acceleration of mobile phone can be obtained.
Using it, we can easily realize the interesting functions of gravity sensor, compass and so on, which will be very useful on the phone. For example, the example of a gravity-sensing ball in opera H5 experience is achieved by listening to the Deviceorientation event of the Deviceorientation API.
Let's get started!
The Devicemotionevent (Device motion event) return device has information about acceleration and rotation. The acceleration data will consist of three axes: x, Y and Z (shown as indicated by the axis transverse through the phone screen or notebook keyboard, the Y axis longitudinal through the phone screen or notebook keyboard, the z axis perpendicular to the phone screen or notebook keyboard). Because some devices may not have the hardware to exclude the effects of gravity, the event returns two properties, accelerationincludinggravity (acceleration with gravity) and Acceleration (acceleration), which excludes the effects of gravity.
Let's start by monitoring motion sensing events.
if (window. devicemotionevent) { window.addeventlistener (' devicemotion ', Devicemotionhandler, false); }
Then get the acceleration with gravity.
function Devicemotionhandler (eventData) { var acceleration =eventdata.accelerationincludinggravity; }
Here's how we calculate the principle of the user shaking the phone. The main points to consider are as follows:
1, the user is mostly in a direction of shaking the mobile phone to shake;
2, in the shaking of the three-direction acceleration data will certainly change;
3, we can not misjudge the normal movement of mobile phones, think, if you put your phone in the trouser pocket, walking when it will also have acceleration data changes.
In conclusion, we should calculate the acceleration in three directions, measure them at intervals, examine their rate of change over a fixed period of time, and need to determine a threshold to trigger the action.
We need to define several variables to record the historical x, Y, z-axis data, and the time of the last trigger. The core method implementation code is as follows:
var shake_threshold = xxx; var last_update = 0; var x, y, Z, last_x, last_y, last_z; function Devicemotionhandler (eventData) { var acceleration =eventdata.accelerationincludinggravity; var curtime = Newdate (). GetTime (); if ((curtime-lastupdate) >) { 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 > Shake_threshold) { alert ("You shook it!"); } last_x = x; last_y = y; Last_z = Z; } }
Full code:
<!doctype html>
Open on the phone, do not forget to refer to jquery, of course, you can directly modify the native JS.
After reading this article, do you have any more ideas?
Mobile phone web to realize the function of shaking