HTML 5 Implementation of the phone shake a shake

Source: Internet
Author: User

Since the deviceorientation of HTML 5 has been developed, many developers have started to study their features and have developed applications such as shaking, pedometer, and so on. Small series recently Idle bored, also developed a shake a shake Web application, interested in small partners can experience.


Sweep code Access

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/76/98/wKioL1ZW8YvxmGApAAA62P0h3Dc166.png "title=" Ishake.png "alt=" Wkiol1zw8yvxmgapaaa62p0h3dc166.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/76/98/wKiom1ZW8UbSmKVHAAF8YNjx7ug945.gif "title=" 1448536487702.gif "alt=" Wkiom1zw8ubsmkvhaaf8ynjx7ug945.gif "/>


Technical Analysis


(1) deviceorientation

Deviceorientation is one of the most important features of HTML 5, which provides an advanced encapsulation of the underlying directional sensor and motion sensor, providing support for DOM two events:

1. Deviceorientation: It encapsulates the data event of the directional sensor, which can obtain direction data in the stationary state of the mobile phone, such as the tilt angle and direction of the phone.

2. Devicemotion: It encapsulates data events of motion sensors and can obtain data such as motion acceleration during mobile phone movement.

Through these two events, we were able to obtain data such as gravity sensing, compass direction, etc. of the mobile phone, and then use the data to do some interesting things. For example, the gravity-sensing ball on a cell phone is using deviceorientation.


Deviceorientation API Events


Monitor Mobile Events

if (window. Devicemotionevent) {
Window.addeventlistener (' devicemotion ', Devicemotionhandler, false);
}

get acceleration of gravity

function Devicemotionhandler (eventData) {
var acceleration =eventdata.accelerationincludinggravity;
}

(2) Several considerations for confirming the user's shaking the phone:


1. Most users shake the phone with a main direction.

2. In the process of shaking the phone acceleration data in the x, Y, z direction data will definitely change.

3. The normal mobile phone movement process cannot be misjudged. For example, the mobile phone in the pocket, when walking, the acceleration data will also change.


Therefore, the process of calculating the mobile phone is not only to calculate the change in the speed of the mobile phone in x, Y, z three, the time interval, but also the speed of the change in the fixed time interval to trigger the event.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/76/98/wKioL1ZW8mXxhEp2AADodVTDXjQ742.png "title=" Xyz.png "alt=" Wkiol1zw8mxxhep2aadodvtdxjq742.png "/>

Source is hosted on GitHub




This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1717205

HTML 5 Implementation of the phone shake a shake

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.