UNPKG

3.29 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.now = now;
7exports.calcMutliFingerStatus = calcMutliFingerStatus;
8exports.calcMoveStatus = calcMoveStatus;
9exports.calcRotation = calcRotation;
10exports.getEventName = getEventName;
11exports.shouldTriggerSwipe = shouldTriggerSwipe;
12exports.shouldTriggerDirection = shouldTriggerDirection;
13exports.getDirection = getDirection;
14exports.getDirectionEventName = getDirectionEventName;
15
16var _config = require('./config');
17
18function _calcTriangleDistance(x, y) {
19 return Math.sqrt(x * x + y * y);
20} /* tslint:disable:no-bitwise */
21
22function _calcAngle(x, y) {
23 var radian = Math.atan2(y, x);
24 return 180 / (Math.PI / radian);
25}
26function now() {
27 return Date.now();
28}
29function calcMutliFingerStatus(touches) {
30 if (touches.length < 2) {
31 return;
32 }
33 var _touches$ = touches[0],
34 x1 = _touches$.x,
35 y1 = _touches$.y;
36 var _touches$2 = touches[1],
37 x2 = _touches$2.x,
38 y2 = _touches$2.y;
39
40 var deltaX = x2 - x1;
41 var deltaY = y2 - y1;
42 return {
43 x: deltaX,
44 y: deltaY,
45 z: _calcTriangleDistance(deltaX, deltaY),
46 angle: _calcAngle(deltaX, deltaY)
47 };
48}
49function calcMoveStatus(startTouches, touches, time) {
50 var _startTouches$ = startTouches[0],
51 x1 = _startTouches$.x,
52 y1 = _startTouches$.y;
53 var _touches$3 = touches[0],
54 x2 = _touches$3.x,
55 y2 = _touches$3.y;
56
57 var deltaX = x2 - x1;
58 var deltaY = y2 - y1;
59 var deltaZ = _calcTriangleDistance(deltaX, deltaY);
60 return {
61 x: deltaX,
62 y: deltaY,
63 z: deltaZ,
64 time: time,
65 velocity: deltaZ / time,
66 angle: _calcAngle(deltaX, deltaY)
67 };
68}
69function calcRotation(startMutliFingerStatus, mutliFingerStatus) {
70 var startAngle = startMutliFingerStatus.angle;
71 var angle = mutliFingerStatus.angle;
72
73 return angle - startAngle;
74}
75function getEventName(prefix, status) {
76 return prefix + status[0].toUpperCase() + status.slice(1);
77}
78function shouldTriggerSwipe(delta, velocity) {
79 return Math.abs(delta) >= _config.SWIPE.threshold && Math.abs(velocity) > _config.SWIPE.velocity;
80}
81function shouldTriggerDirection(direction, directionSetting) {
82 if (directionSetting & direction) {
83 return true;
84 }
85 return false;
86}
87/**
88 * @private
89 * get the direction between two points
90 * @param {Number} x
91 * @param {Number} y
92 * @return {Number} direction
93 */
94function getDirection(x, y) {
95 if (x === y) {
96 return _config.DIRECTION_NONE;
97 }
98 if (Math.abs(x) >= Math.abs(y)) {
99 return x < 0 ? _config.DIRECTION_LEFT : _config.DIRECTION_RIGHT;
100 }
101 return y < 0 ? _config.DIRECTION_UP : _config.DIRECTION_DOWN;
102}
103function getDirectionEventName(direction) {
104 var name = void 0;
105 switch (direction) {
106 case _config.DIRECTION_NONE:
107 break;
108 case _config.DIRECTION_LEFT:
109 name = 'left';
110 break;
111 case _config.DIRECTION_RIGHT:
112 name = 'right';
113 break;
114 case _config.DIRECTION_UP:
115 name = 'up';
116 break;
117 case _config.DIRECTION_DOWN:
118 name = 'down';
119 break;
120 default:
121 }
122 return name;
123}
\No newline at end of file