1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | var TouchHistoryMath = {
|
7 | |
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | centroidDimension: function (touchHistory, touchesChangedAfter, isXAxis, ofCurrent) {
|
24 | var touchBank = touchHistory.touchBank;
|
25 | var total = 0;
|
26 | var count = 0;
|
27 |
|
28 | var oneTouchData = touchHistory.numberActiveTouches === 1 ? touchHistory.touchBank[touchHistory.indexOfSingleActiveTouch] : null;
|
29 |
|
30 | if (oneTouchData !== null) {
|
31 | if (oneTouchData.touchActive && oneTouchData.currentTimeStamp > touchesChangedAfter) {
|
32 | total += ofCurrent && isXAxis ? oneTouchData.currentPageX : ofCurrent && !isXAxis ? oneTouchData.currentPageY : !ofCurrent && isXAxis ? oneTouchData.previousPageX : oneTouchData.previousPageY;
|
33 | count = 1;
|
34 | }
|
35 | } else {
|
36 | for (var i = 0; i < touchBank.length; i++) {
|
37 | var touchTrack = touchBank[i];
|
38 | if (touchTrack !== null && touchTrack !== undefined && touchTrack.touchActive && touchTrack.currentTimeStamp >= touchesChangedAfter) {
|
39 | var toAdd;
|
40 | if (ofCurrent && isXAxis) {
|
41 | toAdd = touchTrack.currentPageX;
|
42 | } else if (ofCurrent && !isXAxis) {
|
43 | toAdd = touchTrack.currentPageY;
|
44 | } else if (!ofCurrent && isXAxis) {
|
45 | toAdd = touchTrack.previousPageX;
|
46 | } else {
|
47 | toAdd = touchTrack.previousPageY;
|
48 | }
|
49 | total += toAdd;
|
50 | count++;
|
51 | }
|
52 | }
|
53 | }
|
54 | return count > 0 ? total / count : TouchHistoryMath.noCentroid;
|
55 | },
|
56 |
|
57 | currentCentroidXOfTouchesChangedAfter: function (touchHistory, touchesChangedAfter) {
|
58 | return TouchHistoryMath.centroidDimension(touchHistory, touchesChangedAfter, true,
|
59 | true
|
60 | );
|
61 | },
|
62 |
|
63 | currentCentroidYOfTouchesChangedAfter: function (touchHistory, touchesChangedAfter) {
|
64 | return TouchHistoryMath.centroidDimension(touchHistory, touchesChangedAfter, false,
|
65 | true
|
66 | );
|
67 | },
|
68 |
|
69 | previousCentroidXOfTouchesChangedAfter: function (touchHistory, touchesChangedAfter) {
|
70 | return TouchHistoryMath.centroidDimension(touchHistory, touchesChangedAfter, true,
|
71 | false
|
72 | );
|
73 | },
|
74 |
|
75 | previousCentroidYOfTouchesChangedAfter: function (touchHistory, touchesChangedAfter) {
|
76 | return TouchHistoryMath.centroidDimension(touchHistory, touchesChangedAfter, false,
|
77 | false
|
78 | );
|
79 | },
|
80 |
|
81 | currentCentroidX: function (touchHistory) {
|
82 | return TouchHistoryMath.centroidDimension(touchHistory, 0,
|
83 | true,
|
84 | true
|
85 | );
|
86 | },
|
87 |
|
88 | currentCentroidY: function (touchHistory) {
|
89 | return TouchHistoryMath.centroidDimension(touchHistory, 0,
|
90 | false,
|
91 | true
|
92 | );
|
93 | },
|
94 |
|
95 | noCentroid: -1
|
96 | };
|
97 |
|
98 | module.exports = TouchHistoryMath; |
\ | No newline at end of file |