Preface With the richness of hybrid applications, HTML5 engineers are no longer satisfied with simply porting the desktop experience to the mobile terminal. They covet the humanized operation experience of mobile native applications, especially the rich gesture system that native applications are born with. HTML5 does not provide an out-of-the-box gesture system, but it provides a more basic monitoring of touch events. Based on this, we can make our own gesture library. gesture Commonly used HTML5 gestures can be divided into two categories, single-point gestures and two-point gestures. Single-point gestures include tap, double tap, long tap, swipe, and move. Two-point gestures include pinch and rotate. Next, we will implement a javascript library to detect these gestures and use this gesture library to create cool interactive effects. move We will not go into details about mobile gesture detection here. To sum up, every time a touchmove event occurs, the coordinate positions between the two displacement points are subtracted. Click (tap) The key to gesture detection is to decompose gestures using three events: touchstart, touchmove, and touchend. So how do you decompose the click event?
With the above process, you can start to implement tap event monitoring.
Double tap Like single click, double click event also requires us to quantify and decompose the gesture.
Note that in the double-click event we detect the displacement and time interval between two adjacent touchstart events.
Long press Long press is probably the easiest gesture to decompose. We can decompose it like this: if no touchmove or touchend event occurs for a long period of time after touchstart occurs, then a long press gesture is triggered. Long press is a one-finger action that requires detecting whether there is only one contact point on the screen. If the finger moves in space, the long press event is canceled. If the finger stays on the screen for more than 800ms, a long press gesture is triggered. If the finger stays on the screen for less than 800ms, that is, touchend is triggered within 800ms after touchstart occurs, the long press event is canceled.
Pinch Zooming is a very interesting gesture. Do you still remember the shock brought by pinching to zoom pictures on the first generation iPhone? Even so, the detection of zooming gesture is relatively simple. Zooming is a two-finger behavior and requires detecting whether there are two contact points on the screen. The quantification of the scaling ratio is obtained by the ratio of the distances between two scaling actions, as shown in the following figure. So the core of scaling is to get the straight-line distance between two contact points.
Here xLen is the absolute value of the difference in x coordinates of the two contact points, and yLen is the absolute value of the difference in y coordinates.
In the _onTouchStart function, get and save the distance between the two contact points when touchstart occurs.
Rotate The rotation gesture needs to detect two important values: the rotation angle and the rotation direction (clockwise or counterclockwise). The calculation of the rotation angle and direction needs to be obtained through vector calculation, which will not be expanded in this article. First, you need to get the rotation direction and angle of the vector.
Then, when the finger moves, we call the method to obtain the rotation direction and angle.
Actual Combat Well, our gesture system is now complete. Next, we need to test whether this system is reliable in actual combat and make a simple image browser that supports image zooming, rotation, movement, and long pressing. First, make a good DOM plan. Just like "before", our event monitoring mechanism does not act directly on the image, but on the parent element of the image. Then, you can start using the gesture detection system above.
Because our gesture system detects increments, we cannot directly apply the increments to the object, but need to accumulate these increments. Take rotation as an example:
At this point, our gesture detection is complete. |
<<: You can also play like this: Check out Siri's 12 new features in iOS 11
>>: How to solve the multi-body problem in deep learning
The college entrance examination is here again. F...
Enoki mushroom bacon rolls, bacon rolled mashed p...
How much is the market price of a bottle of 52-de...
People familiar with the matter revealed that for...
How can an operator carry out an activity more sc...
When spring comes, many people start to miss wild...
: : : : : : : : : : : : : : : : : : : : : : : : : ...
Do you still remember the NetEase opening class t...
Wang Tong · Douyin SEO landing practice class, ha...
Why businesses rely on Baidu hosting today With t...
More than 60 years ago, Brendon Grimshaw quit his...
Baidu Netdisk Super 0 Tutorial Course Materials 1...
In recent years, content marketing has become a h...
Apple held its annual shareholders meeting at the...
Honeywell responds to unqualified mask sampling (...