normalizeWheel.js 6.5 KB

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