|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +const TouchHistoryMath = { |
| 9 | + /** |
| 10 | + * This code is optimized and not intended to look beautiful. This allows |
| 11 | + * computing of touch centroids that have moved after `touchesChangedAfter` |
| 12 | + * timeStamp. You can compute the current centroid involving all touches |
| 13 | + * moves after `touchesChangedAfter`, or you can compute the previous |
| 14 | + * centroid of all touches that were moved after `touchesChangedAfter`. |
| 15 | + * |
| 16 | + * @param {TouchHistoryMath} touchHistory Standard Responder touch track |
| 17 | + * data. |
| 18 | + * @param {number} touchesChangedAfter timeStamp after which moved touches |
| 19 | + * are considered "actively moving" - not just "active". |
| 20 | + * @param {boolean} isXAxis Consider `x` dimension vs. `y` dimension. |
| 21 | + * @param {boolean} ofCurrent Compute current centroid for actively moving |
| 22 | + * touches vs. previous centroid of now actively moving touches. |
| 23 | + * @return {number} value of centroid in specified dimension. |
| 24 | + */ |
| 25 | + centroidDimension: function( |
| 26 | + touchHistory, |
| 27 | + touchesChangedAfter, |
| 28 | + isXAxis, |
| 29 | + ofCurrent, |
| 30 | + ) { |
| 31 | + const touchBank = touchHistory.touchBank; |
| 32 | + let total = 0; |
| 33 | + let count = 0; |
| 34 | + |
| 35 | + const oneTouchData = |
| 36 | + touchHistory.numberActiveTouches === 1 |
| 37 | + ? touchHistory.touchBank[touchHistory.indexOfSingleActiveTouch] |
| 38 | + : null; |
| 39 | + |
| 40 | + if (oneTouchData !== null) { |
| 41 | + if ( |
| 42 | + oneTouchData.touchActive && |
| 43 | + oneTouchData.currentTimeStamp > touchesChangedAfter |
| 44 | + ) { |
| 45 | + total += |
| 46 | + ofCurrent && isXAxis |
| 47 | + ? oneTouchData.currentPageX |
| 48 | + : ofCurrent && !isXAxis |
| 49 | + ? oneTouchData.currentPageY |
| 50 | + : !ofCurrent && isXAxis |
| 51 | + ? oneTouchData.previousPageX |
| 52 | + : oneTouchData.previousPageY; |
| 53 | + count = 1; |
| 54 | + } |
| 55 | + } else { |
| 56 | + for (let i = 0; i < touchBank.length; i++) { |
| 57 | + const touchTrack = touchBank[i]; |
| 58 | + if ( |
| 59 | + touchTrack !== null && |
| 60 | + touchTrack !== undefined && |
| 61 | + touchTrack.touchActive && |
| 62 | + touchTrack.currentTimeStamp >= touchesChangedAfter |
| 63 | + ) { |
| 64 | + let toAdd; // Yuck, program temporarily in invalid state. |
| 65 | + if (ofCurrent && isXAxis) { |
| 66 | + toAdd = touchTrack.currentPageX; |
| 67 | + } else if (ofCurrent && !isXAxis) { |
| 68 | + toAdd = touchTrack.currentPageY; |
| 69 | + } else if (!ofCurrent && isXAxis) { |
| 70 | + toAdd = touchTrack.previousPageX; |
| 71 | + } else { |
| 72 | + toAdd = touchTrack.previousPageY; |
| 73 | + } |
| 74 | + total += toAdd; |
| 75 | + count++; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return count > 0 ? total / count : TouchHistoryMath.noCentroid; |
| 80 | + }, |
| 81 | + |
| 82 | + currentCentroidXOfTouchesChangedAfter: function( |
| 83 | + touchHistory, |
| 84 | + touchesChangedAfter, |
| 85 | + ) { |
| 86 | + return TouchHistoryMath.centroidDimension( |
| 87 | + touchHistory, |
| 88 | + touchesChangedAfter, |
| 89 | + true, // isXAxis |
| 90 | + true, // ofCurrent |
| 91 | + ); |
| 92 | + }, |
| 93 | + |
| 94 | + currentCentroidYOfTouchesChangedAfter: function( |
| 95 | + touchHistory, |
| 96 | + touchesChangedAfter, |
| 97 | + ) { |
| 98 | + return TouchHistoryMath.centroidDimension( |
| 99 | + touchHistory, |
| 100 | + touchesChangedAfter, |
| 101 | + false, // isXAxis |
| 102 | + true, // ofCurrent |
| 103 | + ); |
| 104 | + }, |
| 105 | + |
| 106 | + previousCentroidXOfTouchesChangedAfter: function( |
| 107 | + touchHistory, |
| 108 | + touchesChangedAfter, |
| 109 | + ) { |
| 110 | + return TouchHistoryMath.centroidDimension( |
| 111 | + touchHistory, |
| 112 | + touchesChangedAfter, |
| 113 | + true, // isXAxis |
| 114 | + false, // ofCurrent |
| 115 | + ); |
| 116 | + }, |
| 117 | + |
| 118 | + previousCentroidYOfTouchesChangedAfter: function( |
| 119 | + touchHistory, |
| 120 | + touchesChangedAfter, |
| 121 | + ) { |
| 122 | + return TouchHistoryMath.centroidDimension( |
| 123 | + touchHistory, |
| 124 | + touchesChangedAfter, |
| 125 | + false, // isXAxis |
| 126 | + false, // ofCurrent |
| 127 | + ); |
| 128 | + }, |
| 129 | + |
| 130 | + currentCentroidX: function(touchHistory) { |
| 131 | + return TouchHistoryMath.centroidDimension( |
| 132 | + touchHistory, |
| 133 | + 0, // touchesChangedAfter |
| 134 | + true, // isXAxis |
| 135 | + true, // ofCurrent |
| 136 | + ); |
| 137 | + }, |
| 138 | + |
| 139 | + currentCentroidY: function(touchHistory) { |
| 140 | + return TouchHistoryMath.centroidDimension( |
| 141 | + touchHistory, |
| 142 | + 0, // touchesChangedAfter |
| 143 | + false, // isXAxis |
| 144 | + true, // ofCurrent |
| 145 | + ); |
| 146 | + }, |
| 147 | + |
| 148 | + noCentroid: -1, |
| 149 | +}; |
| 150 | + |
| 151 | +module.exports = TouchHistoryMath; |
1 commit comments
sjmueller commentedon Apr 6, 2018
You forgot to change the import in
PanResponder.js
to use a./
:Results in the following error: