TouchHistoryMath.js 4.2 KB

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