UNPKG

3.73 kBJavaScriptView Raw
1/**
2 */
3
4'use strict';
5
6var TouchHistoryMath = {
7 /**
8 * This code is optimized and not intended to look beautiful. This allows
9 * computing of touch centroids that have moved after `touchesChangedAfter`
10 * timeStamp. You can compute the current centroid involving all touches
11 * moves after `touchesChangedAfter`, or you can compute the previous
12 * centroid of all touches that were moved after `touchesChangedAfter`.
13 *
14 * @param {TouchHistoryMath} touchHistory Standard Responder touch track
15 * data.
16 * @param {number} touchesChangedAfter timeStamp after which moved touches
17 * are considered "actively moving" - not just "active".
18 * @param {boolean} isXAxis Consider `x` dimension vs. `y` dimension.
19 * @param {boolean} ofCurrent Compute current centroid for actively moving
20 * touches vs. previous centroid of now actively moving touches.
21 * @return {number} value of centroid in specified dimension.
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; // Yuck, program temporarily in invalid state.
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, // isXAxis
59 true // ofCurrent
60 );
61 },
62
63 currentCentroidYOfTouchesChangedAfter: function (touchHistory, touchesChangedAfter) {
64 return TouchHistoryMath.centroidDimension(touchHistory, touchesChangedAfter, false, // isXAxis
65 true // ofCurrent
66 );
67 },
68
69 previousCentroidXOfTouchesChangedAfter: function (touchHistory, touchesChangedAfter) {
70 return TouchHistoryMath.centroidDimension(touchHistory, touchesChangedAfter, true, // isXAxis
71 false // ofCurrent
72 );
73 },
74
75 previousCentroidYOfTouchesChangedAfter: function (touchHistory, touchesChangedAfter) {
76 return TouchHistoryMath.centroidDimension(touchHistory, touchesChangedAfter, false, // isXAxis
77 false // ofCurrent
78 );
79 },
80
81 currentCentroidX: function (touchHistory) {
82 return TouchHistoryMath.centroidDimension(touchHistory, 0, // touchesChangedAfter
83 true, // isXAxis
84 true // ofCurrent
85 );
86 },
87
88 currentCentroidY: function (touchHistory) {
89 return TouchHistoryMath.centroidDimension(touchHistory, 0, // touchesChangedAfter
90 false, // isXAxis
91 true // ofCurrent
92 );
93 },
94
95 noCentroid: -1
96};
97
98module.exports = TouchHistoryMath;
\No newline at end of file