1
+ /**
2
+ * @providesModule TouchHistoryMath
3
+ */
4
+
5
+ 'use strict' ;
6
+
7
+ var TouchHistoryMath = {
8
+ /**
9
+ * This code is optimized and not intended to look beautiful. This allows
10
+ * computing of touch centroids that have moved after `touchesChangedAfter`
11
+ * timeStamp. You can compute the current centroid involving all touches
12
+ * moves after `touchesChangedAfter`, or you can compute the previous
13
+ * centroid of all touches that were moved after `touchesChangedAfter`.
14
+ *
15
+ * @param {TouchHistoryMath } touchHistory Standard Responder touch track
16
+ * data.
17
+ * @param {number } touchesChangedAfter timeStamp after which moved touches
18
+ * are considered "actively moving" - not just "active".
19
+ * @param {boolean } isXAxis Consider `x` dimension vs. `y` dimension.
20
+ * @param {boolean } ofCurrent Compute current centroid for actively moving
21
+ * touches vs. previous centroid of now actively moving touches.
22
+ * @return {number } value of centroid in specified dimension.
23
+ */
24
+ centroidDimension : function ( touchHistory , touchesChangedAfter , isXAxis , ofCurrent ) {
25
+ var touchBank = touchHistory . touchBank ;
26
+ var total = 0 ;
27
+ var count = 0 ;
28
+
29
+ var oneTouchData = touchHistory . numberActiveTouches === 1 ? touchHistory . touchBank [ touchHistory . indexOfSingleActiveTouch ] : null ;
30
+
31
+ if ( oneTouchData !== null ) {
32
+ if ( oneTouchData . touchActive && oneTouchData . currentTimeStamp > touchesChangedAfter ) {
33
+ total += ofCurrent && isXAxis ? oneTouchData . currentPageX : ofCurrent && ! isXAxis ? oneTouchData . currentPageY : ! ofCurrent && isXAxis ? oneTouchData . previousPageX : oneTouchData . previousPageY ;
34
+ count = 1 ;
35
+ }
36
+ } else {
37
+ for ( var i = 0 ; i < touchBank . length ; i ++ ) {
38
+ var touchTrack = touchBank [ i ] ;
39
+ if ( touchTrack !== null && touchTrack !== undefined && touchTrack . touchActive && touchTrack . currentTimeStamp >= touchesChangedAfter ) {
40
+ var toAdd ; // Yuck, program temporarily in invalid state.
41
+ if ( ofCurrent && isXAxis ) {
42
+ toAdd = touchTrack . currentPageX ;
43
+ } else if ( ofCurrent && ! isXAxis ) {
44
+ toAdd = touchTrack . currentPageY ;
45
+ } else if ( ! ofCurrent && isXAxis ) {
46
+ toAdd = touchTrack . previousPageX ;
47
+ } else {
48
+ toAdd = touchTrack . previousPageY ;
49
+ }
50
+ total += toAdd ;
51
+ count ++ ;
52
+ }
53
+ }
54
+ }
55
+ return count > 0 ? total / count : TouchHistoryMath . noCentroid ;
56
+ } ,
57
+
58
+ currentCentroidXOfTouchesChangedAfter : function ( touchHistory , touchesChangedAfter ) {
59
+ return TouchHistoryMath . centroidDimension ( touchHistory , touchesChangedAfter , true , // isXAxis
60
+ true // ofCurrent
61
+ ) ;
62
+ } ,
63
+
64
+ currentCentroidYOfTouchesChangedAfter : function ( touchHistory , touchesChangedAfter ) {
65
+ return TouchHistoryMath . centroidDimension ( touchHistory , touchesChangedAfter , false , // isXAxis
66
+ true // ofCurrent
67
+ ) ;
68
+ } ,
69
+
70
+ previousCentroidXOfTouchesChangedAfter : function ( touchHistory , touchesChangedAfter ) {
71
+ return TouchHistoryMath . centroidDimension ( touchHistory , touchesChangedAfter , true , // isXAxis
72
+ false // ofCurrent
73
+ ) ;
74
+ } ,
75
+
76
+ previousCentroidYOfTouchesChangedAfter : function ( touchHistory , touchesChangedAfter ) {
77
+ return TouchHistoryMath . centroidDimension ( touchHistory , touchesChangedAfter , false , // isXAxis
78
+ false // ofCurrent
79
+ ) ;
80
+ } ,
81
+
82
+ currentCentroidX : function ( touchHistory ) {
83
+ return TouchHistoryMath . centroidDimension ( touchHistory , 0 , // touchesChangedAfter
84
+ true , // isXAxis
85
+ true // ofCurrent
86
+ ) ;
87
+ } ,
88
+
89
+ currentCentroidY : function ( touchHistory ) {
90
+ return TouchHistoryMath . centroidDimension ( touchHistory , 0 , // touchesChangedAfter
91
+ false , // isXAxis
92
+ true // ofCurrent
93
+ ) ;
94
+ } ,
95
+
96
+ noCentroid : - 1
97
+ } ;
98
+
99
+ module . exports = TouchHistoryMath ;
0 commit comments