wheelevent.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2014 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview This class aims to smooth out inconsistencies between browser
  16. * handling of wheel events by providing an event that is similar to that
  17. * defined in the standard, but also easier to consume.
  18. *
  19. * It is based upon the WheelEvent, which allows for up to 3 dimensional
  20. * scrolling events that come in units of either pixels, lines or pages.
  21. * http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/#interface-WheelEvent
  22. *
  23. * The significant difference here is that it also provides reasonable pixel
  24. * deltas for clients that do not want to treat line and page scrolling events
  25. * specially.
  26. *
  27. * Clients of this code should be aware that some input devices only fire a few
  28. * discrete events (such as a mouse wheel without acceleration) whereas some can
  29. * generate a large number of events for a single interaction (such as a
  30. * touchpad with acceleration). There is no signal in the events to reliably
  31. * distinguish between these.
  32. *
  33. * @see ../demos/wheelhandler.html
  34. */
  35. goog.provide('goog.events.WheelEvent');
  36. goog.require('goog.asserts');
  37. goog.require('goog.events.BrowserEvent');
  38. /**
  39. * A common class for wheel events. This is used with the WheelHandler.
  40. *
  41. * @param {Event} browserEvent Browser event object.
  42. * @param {goog.events.WheelEvent.DeltaMode} deltaMode The delta mode units of
  43. * the wheel event.
  44. * @param {number} deltaX The number of delta units the user in the X axis.
  45. * @param {number} deltaY The number of delta units the user in the Y axis.
  46. * @param {number} deltaZ The number of delta units the user in the Z axis.
  47. * @constructor
  48. * @extends {goog.events.BrowserEvent}
  49. * @final
  50. */
  51. goog.events.WheelEvent = function(
  52. browserEvent, deltaMode, deltaX, deltaY, deltaZ) {
  53. goog.events.WheelEvent.base(this, 'constructor', browserEvent);
  54. goog.asserts.assert(browserEvent, 'Expecting a non-null browserEvent');
  55. /** @type {goog.events.WheelEvent.EventType} */
  56. this.type = goog.events.WheelEvent.EventType.WHEEL;
  57. /**
  58. * An enum corresponding to the units of this event.
  59. * @type {goog.events.WheelEvent.DeltaMode}
  60. */
  61. this.deltaMode = deltaMode;
  62. /**
  63. * The number of delta units in the X axis.
  64. * @type {number}
  65. */
  66. this.deltaX = deltaX;
  67. /**
  68. * The number of delta units in the Y axis.
  69. * @type {number}
  70. */
  71. this.deltaY = deltaY;
  72. /**
  73. * The number of delta units in the Z axis.
  74. * @type {number}
  75. */
  76. this.deltaZ = deltaZ;
  77. // Ratio between delta and pixel values.
  78. var pixelRatio = 1; // Value for DeltaMode.PIXEL
  79. switch (deltaMode) {
  80. case goog.events.WheelEvent.DeltaMode.PAGE:
  81. pixelRatio *= goog.events.WheelEvent.PIXELS_PER_PAGE_;
  82. break;
  83. case goog.events.WheelEvent.DeltaMode.LINE:
  84. pixelRatio *= goog.events.WheelEvent.PIXELS_PER_LINE_;
  85. break;
  86. }
  87. /**
  88. * The number of delta pixels in the X axis. Code that doesn't want to handle
  89. * different deltaMode units can just look here.
  90. * @type {number}
  91. */
  92. this.pixelDeltaX = this.deltaX * pixelRatio;
  93. /**
  94. * The number of pixels in the Y axis. Code that doesn't want to
  95. * handle different deltaMode units can just look here.
  96. * @type {number}
  97. */
  98. this.pixelDeltaY = this.deltaY * pixelRatio;
  99. /**
  100. * The number of pixels scrolled in the Z axis. Code that doesn't want to
  101. * handle different deltaMode units can just look here.
  102. * @type {number}
  103. */
  104. this.pixelDeltaZ = this.deltaZ * pixelRatio;
  105. };
  106. goog.inherits(goog.events.WheelEvent, goog.events.BrowserEvent);
  107. /**
  108. * Enum type for the events fired by the wheel handler.
  109. * @enum {string}
  110. */
  111. goog.events.WheelEvent.EventType = {
  112. /** The user has provided wheel-based input. */
  113. WHEEL: 'wheel'
  114. };
  115. /**
  116. * Units for the deltas in a WheelEvent.
  117. * @enum {number}
  118. */
  119. goog.events.WheelEvent.DeltaMode = {
  120. /** The units are in pixels. From DOM_DELTA_PIXEL. */
  121. PIXEL: 0,
  122. /** The units are in lines. From DOM_DELTA_LINE. */
  123. LINE: 1,
  124. /** The units are in pages. From DOM_DELTA_PAGE. */
  125. PAGE: 2
  126. };
  127. /**
  128. * A conversion number between line scroll units and pixel scroll units. The
  129. * actual value per line can vary a lot between devices and font sizes. This
  130. * number can not be perfect, but it should be reasonable for converting lines
  131. * scroll events into pixels.
  132. * @const {number}
  133. * @private
  134. */
  135. goog.events.WheelEvent.PIXELS_PER_LINE_ = 15;
  136. /**
  137. * A conversion number between page scroll units and pixel scroll units. The
  138. * actual value per page can vary a lot as many different devices have different
  139. * screen sizes, and the window might not be taking up the full screen. This
  140. * number can not be perfect, but it should be reasonable for converting page
  141. * scroll events into pixels.
  142. * @const {number}
  143. * @private
  144. */
  145. goog.events.WheelEvent.PIXELS_PER_PAGE_ =
  146. 30 * goog.events.WheelEvent.PIXELS_PER_LINE_;