Source: interaction/InteractionData.js

interaction/InteractionData.js

  1. import * as core from '../core';
  2. /**
  3. * Holds all information related to an Interaction event
  4. *
  5. * @class
  6. * @memberof PIXI.interaction
  7. */
  8. export default class InteractionData
  9. {
  10. /**
  11. *
  12. */
  13. constructor()
  14. {
  15. /**
  16. * This point stores the global coords of where the touch/mouse event happened
  17. *
  18. * @member {PIXI.Point}
  19. */
  20. this.global = new core.Point();
  21. /**
  22. * The target DisplayObject that was interacted with
  23. *
  24. * @member {PIXI.DisplayObject}
  25. */
  26. this.target = null;
  27. /**
  28. * When passed to an event handler, this will be the original DOM Event that was captured
  29. *
  30. * @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
  31. * @see https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent
  32. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent
  33. * @member {MouseEvent|TouchEvent|PointerEvent}
  34. */
  35. this.originalEvent = null;
  36. /**
  37. * Unique identifier for this interaction
  38. *
  39. * @member {number}
  40. */
  41. this.identifier = null;
  42. /**
  43. * Indicates whether or not the pointer device that created the event is the primary pointer.
  44. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/isPrimary
  45. * @type {Boolean}
  46. */
  47. this.isPrimary = false;
  48. /**
  49. * Indicates which button was pressed on the mouse or pointer device to trigger the event.
  50. * @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
  51. * @type {number}
  52. */
  53. this.button = 0;
  54. /**
  55. * Indicates which buttons are pressed on the mouse or pointer device when the event is triggered.
  56. * @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
  57. * @type {number}
  58. */
  59. this.buttons = 0;
  60. /**
  61. * The width of the pointer's contact along the x-axis, measured in CSS pixels.
  62. * radiusX of TouchEvents will be represented by this value.
  63. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/width
  64. * @type {number}
  65. */
  66. this.width = 0;
  67. /**
  68. * The height of the pointer's contact along the y-axis, measured in CSS pixels.
  69. * radiusY of TouchEvents will be represented by this value.
  70. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/height
  71. * @type {number}
  72. */
  73. this.height = 0;
  74. /**
  75. * The angle, in degrees, between the pointer device and the screen.
  76. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/tiltX
  77. * @type {number}
  78. */
  79. this.tiltX = 0;
  80. /**
  81. * The angle, in degrees, between the pointer device and the screen.
  82. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/tiltY
  83. * @type {number}
  84. */
  85. this.tiltY = 0;
  86. /**
  87. * The type of pointer that triggered the event.
  88. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType
  89. * @type {string}
  90. */
  91. this.pointerType = null;
  92. /**
  93. * Pressure applied by the pointing device during the event. A Touch's force property
  94. * will be represented by this value.
  95. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pressure
  96. * @type {number}
  97. */
  98. this.pressure = 0;
  99. /**
  100. * From TouchEvents (not PointerEvents triggered by touches), the rotationAngle of the Touch.
  101. * @see https://developer.mozilla.org/en-US/docs/Web/API/Touch/rotationAngle
  102. * @type {number}
  103. */
  104. this.rotationAngle = 0;
  105. /**
  106. * Twist of a stylus pointer.
  107. * @see https://w3c.github.io/pointerevents/#pointerevent-interface
  108. * @type {number}
  109. */
  110. this.twist = 0;
  111. /**
  112. * Barrel pressure on a stylus pointer.
  113. * @see https://w3c.github.io/pointerevents/#pointerevent-interface
  114. * @type {number}
  115. */
  116. this.tangentialPressure = 0;
  117. }
  118. /**
  119. * The unique identifier of the pointer. It will be the same as `identifier`.
  120. * @readonly
  121. * @member {number}
  122. * @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerId
  123. */
  124. get pointerId()
  125. {
  126. return this.identifier;
  127. }
  128. /**
  129. * This will return the local coordinates of the specified displayObject for this InteractionData
  130. *
  131. * @param {PIXI.DisplayObject} displayObject - The DisplayObject that you would like the local
  132. * coords off
  133. * @param {PIXI.Point} [point] - A Point object in which to store the value, optional (otherwise
  134. * will create a new point)
  135. * @param {PIXI.Point} [globalPos] - A Point object containing your custom global coords, optional
  136. * (otherwise will use the current global coords)
  137. * @return {PIXI.Point} A point containing the coordinates of the InteractionData position relative
  138. * to the DisplayObject
  139. */
  140. getLocalPosition(displayObject, point, globalPos)
  141. {
  142. return displayObject.worldTransform.applyInverse(globalPos || this.global, point);
  143. }
  144. /**
  145. * Copies properties from normalized event data.
  146. *
  147. * @param {Touch|MouseEvent|PointerEvent} event The normalized event data
  148. */
  149. copyEvent(event)
  150. {
  151. // isPrimary should only change on touchstart/pointerdown, so we don't want to overwrite
  152. // it with "false" on later events when our shim for it on touch events might not be
  153. // accurate
  154. if (event.isPrimary)
  155. {
  156. this.isPrimary = true;
  157. }
  158. this.button = event.button;
  159. // event.buttons is not available in all browsers (ie. Safari), but it does have a non-standard
  160. // event.which property instead, which conveys the same information.
  161. this.buttons = Number.isInteger(event.buttons) ? event.buttons : event.which;
  162. this.width = event.width;
  163. this.height = event.height;
  164. this.tiltX = event.tiltX;
  165. this.tiltY = event.tiltY;
  166. this.pointerType = event.pointerType;
  167. this.pressure = event.pressure;
  168. this.rotationAngle = event.rotationAngle;
  169. this.twist = event.twist || 0;
  170. this.tangentialPressure = event.tangentialPressure || 0;
  171. }
  172. /**
  173. * Resets the data for pooling.
  174. */
  175. reset()
  176. {
  177. // isPrimary is the only property that we really need to reset - everything else is
  178. // guaranteed to be overwritten
  179. this.isPrimary = false;
  180. }
  181. }