mouse.d.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @deprecated
  3. * Mouse wheel (and 2-finger trackpad) support on the web sucks. It is
  4. * complicated, thus this doc is long and (hopefully) detailed enough to answer
  5. * your questions.
  6. *
  7. * If you need to react to the mouse wheel in a predictable way, this code is
  8. * like your bestest friend. * hugs *
  9. *
  10. * As of today, there are 4 DOM event types you can listen to:
  11. *
  12. * 'wheel' -- Chrome(31+), FF(17+), IE(9+)
  13. * 'mousewheel' -- Chrome, IE(6+), Opera, Safari
  14. * 'MozMousePixelScroll' -- FF(3.5 only!) (2010-2013) -- don't bother!
  15. * 'DOMMouseScroll' -- FF(0.9.7+) since 2003
  16. *
  17. * So what to do? The is the best:
  18. *
  19. * normalizeWheel.getEventType();
  20. *
  21. * In your event callback, use this code to get sane interpretation of the
  22. * deltas. This code will return an object with properties:
  23. *
  24. * spinX -- normalized spin speed (use for zoom) - x plane
  25. * spinY -- " - y plane
  26. * pixelX -- normalized distance (to pixels) - x plane
  27. * pixelY -- " - y plane
  28. *
  29. * Wheel values are provided by the browser assuming you are using the wheel to
  30. * scroll a web page by a number of lines or pixels (or pages). Values can vary
  31. * significantly on different platforms and browsers, forgetting that you can
  32. * scroll at different speeds. Some devices (like trackpads) emit more events
  33. * at smaller increments with fine granularity, and some emit massive jumps with
  34. * linear speed or acceleration.
  35. *
  36. * This code does its best to normalize the deltas for you:
  37. *
  38. * - spin is trying to normalize how far the wheel was spun (or trackpad
  39. * dragged). This is super useful for zoom support where you want to
  40. * throw away the chunky scroll steps on the PC and make those equal to
  41. * the slow and smooth tiny steps on the Mac. Key data: This code tries to
  42. * resolve a single slow step on a wheel to 1.
  43. *
  44. * - pixel is normalizing the desired scroll delta in pixel units. You'll
  45. * get the crazy differences between browsers, but at least it'll be in
  46. * pixels!
  47. *
  48. * - positive value indicates scrolling DOWN/RIGHT, negative UP/LEFT. This
  49. * should translate to positive value zooming IN, negative zooming OUT.
  50. * This matches the newer 'wheel' event.
  51. *
  52. * Why are there spinX, spinY (or pixels)?
  53. *
  54. * - spinX is a 2-finger side drag on the trackpad, and a shift + wheel turn
  55. * with a mouse. It results in side-scrolling in the browser by default.
  56. *
  57. * - spinY is what you expect -- it's the classic axis of a mouse wheel.
  58. *
  59. * - I dropped spinZ/pixelZ. It is supported by the DOM 3 'wheel' event and
  60. * probably is by browsers in conjunction with fancy 3D controllers .. but
  61. * you know.
  62. *
  63. * Implementation info:
  64. *
  65. * Examples of 'wheel' event if you scroll slowly (down) by one step with an
  66. * average mouse:
  67. *
  68. * OS X + Chrome (mouse) - 4 pixel delta (wheelDelta -120)
  69. * OS X + Safari (mouse) - N/A pixel delta (wheelDelta -12)
  70. * OS X + Firefox (mouse) - 0.1 line delta (wheelDelta N/A)
  71. * Win8 + Chrome (mouse) - 100 pixel delta (wheelDelta -120)
  72. * Win8 + Firefox (mouse) - 3 line delta (wheelDelta -120)
  73. *
  74. * On the trackpad:
  75. *
  76. * OS X + Chrome (trackpad) - 2 pixel delta (wheelDelta -6)
  77. * OS X + Firefox (trackpad) - 1 pixel delta (wheelDelta N/A)
  78. *
  79. * On other/older browsers.. it's more complicated as there can be multiple and
  80. * also missing delta values.
  81. *
  82. * The 'wheel' event is more standard:
  83. *
  84. * http://www.w3.org/TR/DOM-Level-3-Events/#events-wheelevents
  85. *
  86. * The basics is that it includes a unit, deltaMode (pixels, lines, pages), and
  87. * deltaX, deltaY and deltaZ. Some browsers provide other values to maintain
  88. * backward compatibility with older events. Those other values help us
  89. * better normalize spin speed. Example of what the browsers provide:
  90. *
  91. * | event.wheelDelta | event.detail
  92. * ------------------+------------------+--------------
  93. * Safari v5/OS X | -120 | 0
  94. * Safari v5/Win7 | -120 | 0
  95. * Chrome v17/OS X | -120 | 0
  96. * Chrome v17/Win7 | -120 | 0
  97. * IE9/Win7 | -120 | undefined
  98. * Firefox v4/OS X | undefined | 1
  99. * Firefox v4/Win7 | undefined | 3
  100. *
  101. * from: https://github.com/facebook/fixed-data-table/blob/master/src/vendor_upstream/dom/normalizeWheel.js
  102. * @param {any} event
  103. * @return {any}
  104. */
  105. export declare function normalizeWheel(event: any): any;
  106. /**
  107. * @deprecated
  108. * Checks if event was issued by a left click
  109. * from https://stackoverflow.com/questions/3944122/detect-left-mouse-button-press
  110. * @param {MouseEvent} mouseEvent
  111. * @returns {boolean}
  112. */
  113. export declare function isLeftClick(mouseEvent: MouseEvent): boolean;
  114. /**
  115. * `True` if the event is close to the original event by X pixels either vertically or horizontally.
  116. * we only start dragging after X pixels so this allows us to know if we should start dragging yet.
  117. * @param {MouseEvent | TouchEvent} e1
  118. * @param {MouseEvent | TouchEvent} e2
  119. * @param {number} pixelCount
  120. * @returns {boolean}
  121. */
  122. export declare function areEventsNear(e1: MouseEvent | Touch, e2: MouseEvent | Touch, pixelCount: number): boolean;