normalizeWheel.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2013-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. * @typechecks
  8. */
  9. 'use strict';
  10. var UserAgent = require("./UserAgent");
  11. var isEventSupported = require("./isEventSupported"); // Reasonable defaults
  12. var PIXEL_STEP = 10;
  13. var LINE_HEIGHT = 40;
  14. var PAGE_HEIGHT = 800;
  15. /**
  16. * Mouse wheel (and 2-finger trackpad) support on the web sucks. It is
  17. * complicated, thus this doc is long and (hopefully) detailed enough to answer
  18. * your questions.
  19. *
  20. * If you need to react to the mouse wheel in a predictable way, this code is
  21. * like your bestest friend. * hugs *
  22. *
  23. * As of today, there are 4 DOM event types you can listen to:
  24. *
  25. * 'wheel' -- Chrome(31+), FF(17+), IE(9+)
  26. * 'mousewheel' -- Chrome, IE(6+), Opera, Safari
  27. * 'MozMousePixelScroll' -- FF(3.5 only!) (2010-2013) -- don't bother!
  28. * 'DOMMouseScroll' -- FF(0.9.7+) since 2003
  29. *
  30. * So what to do? The is the best:
  31. *
  32. * normalizeWheel.getEventType();
  33. *
  34. * In your event callback, use this code to get sane interpretation of the
  35. * deltas. This code will return an object with properties:
  36. *
  37. * spinX -- normalized spin speed (use for zoom) - x plane
  38. * spinY -- " - y plane
  39. * pixelX -- normalized distance (to pixels) - x plane
  40. * pixelY -- " - y plane
  41. *
  42. * Wheel values are provided by the browser assuming you are using the wheel to
  43. * scroll a web page by a number of lines or pixels (or pages). Values can vary
  44. * significantly on different platforms and browsers, forgetting that you can
  45. * scroll at different speeds. Some devices (like trackpads) emit more events
  46. * at smaller increments with fine granularity, and some emit massive jumps with
  47. * linear speed or acceleration.
  48. *
  49. * This code does its best to normalize the deltas for you:
  50. *
  51. * - spin is trying to normalize how far the wheel was spun (or trackpad
  52. * dragged). This is super useful for zoom support where you want to
  53. * throw away the chunky scroll steps on the PC and make those equal to
  54. * the slow and smooth tiny steps on the Mac. Key data: This code tries to
  55. * resolve a single slow step on a wheel to 1.
  56. *
  57. * - pixel is normalizing the desired scroll delta in pixel units. You'll
  58. * get the crazy differences between browsers, but at least it'll be in
  59. * pixels!
  60. *
  61. * - positive value indicates scrolling DOWN/RIGHT, negative UP/LEFT. This
  62. * should translate to positive value zooming IN, negative zooming OUT.
  63. * This matches the newer 'wheel' event.
  64. *
  65. * Why are there spinX, spinY (or pixels)?
  66. *
  67. * - spinX is a 2-finger side drag on the trackpad, and a shift + wheel turn
  68. * with a mouse. It results in side-scrolling in the browser by default.
  69. *
  70. * - spinY is what you expect -- it's the classic axis of a mouse wheel.
  71. *
  72. * - I dropped spinZ/pixelZ. It is supported by the DOM 3 'wheel' event and
  73. * probably is by browsers in conjunction with fancy 3D controllers .. but
  74. * you know.
  75. *
  76. * Implementation info:
  77. *
  78. * Examples of 'wheel' event if you scroll slowly (down) by one step with an
  79. * average mouse:
  80. *
  81. * OS X + Chrome (mouse) - 4 pixel delta (wheelDelta -120)
  82. * OS X + Safari (mouse) - N/A pixel delta (wheelDelta -12)
  83. * OS X + Firefox (mouse) - 0.1 line delta (wheelDelta N/A)
  84. * Win8 + Chrome (mouse) - 100 pixel delta (wheelDelta -120)
  85. * Win8 + Firefox (mouse) - 3 line delta (wheelDelta -120)
  86. *
  87. * On the trackpad:
  88. *
  89. * OS X + Chrome (trackpad) - 2 pixel delta (wheelDelta -6)
  90. * OS X + Firefox (trackpad) - 1 pixel delta (wheelDelta N/A)
  91. *
  92. * On other/older browsers.. it's more complicated as there can be multiple and
  93. * also missing delta values.
  94. *
  95. * The 'wheel' event is more standard:
  96. *
  97. * http://www.w3.org/TR/DOM-Level-3-Events/#events-wheelevents
  98. *
  99. * The basics is that it includes a unit, deltaMode (pixels, lines, pages), and
  100. * deltaX, deltaY and deltaZ. Some browsers provide other values to maintain
  101. * backward compatibility with older events. Those other values help us
  102. * better normalize spin speed. Example of what the browsers provide:
  103. *
  104. * | event.wheelDelta | event.detail
  105. * ------------------+------------------+--------------
  106. * Safari v5/OS X | -120 | 0
  107. * Safari v5/Win7 | -120 | 0
  108. * Chrome v17/OS X | -120 | 0
  109. * Chrome v17/Win7 | -120 | 0
  110. * IE9/Win7 | -120 | undefined
  111. * Firefox v4/OS X | undefined | 1
  112. * Firefox v4/Win7 | undefined | 3
  113. *
  114. */
  115. function normalizeWheel(
  116. /*object*/
  117. event)
  118. /*object*/
  119. {
  120. var sX = 0,
  121. sY = 0,
  122. // spinX, spinY
  123. pX = 0,
  124. pY = 0; // pixelX, pixelY
  125. // Legacy
  126. if ('detail' in event) {
  127. sY = event.detail;
  128. }
  129. if ('wheelDelta' in event) {
  130. sY = -event.wheelDelta / 120;
  131. }
  132. if ('wheelDeltaY' in event) {
  133. sY = -event.wheelDeltaY / 120;
  134. }
  135. if ('wheelDeltaX' in event) {
  136. sX = -event.wheelDeltaX / 120;
  137. } // side scrolling on FF with DOMMouseScroll
  138. if ('axis' in event && event.axis === event.HORIZONTAL_AXIS) {
  139. sX = sY;
  140. sY = 0;
  141. }
  142. pX = sX * PIXEL_STEP;
  143. pY = sY * PIXEL_STEP;
  144. if ('deltaY' in event) {
  145. pY = event.deltaY;
  146. }
  147. if ('deltaX' in event) {
  148. pX = event.deltaX;
  149. }
  150. if ((pX || pY) && event.deltaMode) {
  151. if (event.deltaMode == 1) {
  152. // delta in LINE units
  153. pX *= LINE_HEIGHT;
  154. pY *= LINE_HEIGHT;
  155. } else {
  156. // delta in PAGE units
  157. pX *= PAGE_HEIGHT;
  158. pY *= PAGE_HEIGHT;
  159. }
  160. } // Fall-back if spin cannot be determined
  161. if (pX && !sX) {
  162. sX = pX < 1 ? -1 : 1;
  163. }
  164. if (pY && !sY) {
  165. sY = pY < 1 ? -1 : 1;
  166. }
  167. return {
  168. spinX: sX,
  169. spinY: sY,
  170. pixelX: pX,
  171. pixelY: pY
  172. };
  173. }
  174. /**
  175. * The best combination if you prefer spinX + spinY normalization. It favors
  176. * the older DOMMouseScroll for Firefox, as FF does not include wheelDelta with
  177. * 'wheel' event, making spin speed determination impossible.
  178. */
  179. normalizeWheel.getEventType = function ()
  180. /*string*/
  181. {
  182. return UserAgent.isBrowser('Firefox') ? 'DOMMouseScroll' : isEventSupported('wheel') ? 'wheel' : 'mousewheel';
  183. };
  184. module.exports = normalizeWheel;