input_Pointer.js.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: input/Pointer.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: input/Pointer.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import {EventManager} from "../utils/EventManager.js";
  20. import {Vector2} from "../math/Vector2.js";
  21. import {Key} from "./Key.js";
  22. /**
  23. * Pointer object is used to called input from the user, works for booth mouse or touch screens.
  24. *
  25. * It is responsible for synchronizing user input with the render of the graphics.
  26. *
  27. * @class
  28. * @param {Element} domElement DOM element to create the pointer events.
  29. * @param {Element} canvas Canvas DOM element where the content is being drawn.
  30. */
  31. function Pointer(domElement, canvas)
  32. {
  33. //Raw data
  34. this._keys = new Array(5);
  35. this._position = new Vector2(0, 0);
  36. this._positionUpdated = false;
  37. this._delta = new Vector2(0, 0);
  38. this._wheel = 0;
  39. this._wheelUpdated = false;
  40. this._doubleClicked = new Array(5);
  41. /**
  42. * Array with pointer buttons status.
  43. *
  44. * @type {number[]}
  45. */
  46. this.keys = new Array(5);
  47. /**
  48. * Pointer position inside of the window (coordinates in window space).
  49. *
  50. * This value is accumulated from multiple mouse triggered events between updated.
  51. *
  52. * @type {Vector2}
  53. */
  54. this.position = new Vector2(0, 0);
  55. /**
  56. * Pointer movement (coordinates in window space). Since the last update.
  57. *
  58. * This value is accumulated from multiple mouse triggered events between updated.
  59. *
  60. * @type {Vector2}
  61. */
  62. this.delta = new Vector2(0, 0);
  63. /**
  64. * Pointer scroll wheel movement, since the last update.
  65. *
  66. * @type {number}
  67. */
  68. this.wheel = 0;
  69. /**
  70. * Indicates a button of the pointer was double clicked.
  71. *
  72. * @type {boolean}
  73. */
  74. this.doubleClicked = new Array(5);
  75. /**
  76. * DOM element where to attach the pointer events.
  77. *
  78. * @type {Element}
  79. */
  80. this.domElement = (domElement !== undefined) ? domElement : window;
  81. /**
  82. * Canvas attached to this pointer instance used to calculate position and delta in element space coordinates.
  83. *
  84. * @type {Element}
  85. */
  86. this.canvas = null;
  87. if(canvas !== undefined)
  88. {
  89. this.setCanvas(canvas);
  90. }
  91. /**
  92. * Event manager responsible for updating the raw data variables.
  93. *
  94. * Different events are used depending on the host platform.
  95. *
  96. * When the update method is called the raw data is reset.
  97. *
  98. * @type {EventManager}
  99. */
  100. this.events = new EventManager();
  101. //Initialize key instances
  102. for(var i = 0; i &lt; 5; i++)
  103. {
  104. this._doubleClicked[i] = false;
  105. this.doubleClicked[i] = false;
  106. this._keys[i] = new Key();
  107. this.keys[i] = new Key();
  108. }
  109. //Self pointer
  110. var self = this;
  111. //Scroll wheel
  112. if(window.onmousewheel !== undefined)
  113. {
  114. //Chrome, edge
  115. this.events.add(this.domElement, "mousewheel", function(event)
  116. {
  117. self._wheel = event.deltaY;
  118. self._wheelUpdated = true;
  119. });
  120. }
  121. else if(window.addEventListener !== undefined)
  122. {
  123. //Firefox
  124. this.events.add(this.domElement, "DOMMouseScroll", function(event)
  125. {
  126. self._wheel = event.detail * 30;
  127. self._wheelUpdated = true;
  128. });
  129. }
  130. else
  131. {
  132. this.events.add(this.domElement, "wheel", function(event)
  133. {
  134. self._wheel = event.deltaY;
  135. self._wheelUpdated = true;
  136. });
  137. }
  138. //Touchscreen input events
  139. if(window.ontouchstart !== undefined || navigator.msMaxTouchPoints > 0)
  140. {
  141. //Auxiliar variables to calculate touch delta
  142. var lastTouch = new Vector2(0, 0);
  143. //Touch start event
  144. this.events.add(this.domElement, "touchstart", function(event)
  145. {
  146. var touch = event.touches[0];
  147. self.updatePosition(touch.clientX, touch.clientY, 0, 0);
  148. self.updateKey(Pointer.LEFT, Key.DOWN);
  149. lastTouch.set(touch.clientX, touch.clientY);
  150. });
  151. //Touch end event
  152. this.events.add(this.domElement, "touchend", function(event)
  153. {
  154. self.updateKey(Pointer.LEFT, Key.UP);
  155. });
  156. //Touch cancel event
  157. this.events.add(this.domElement, "touchcancel", function(event)
  158. {
  159. self.updateKey(Pointer.LEFT, Key.UP);
  160. });
  161. //Touch move event
  162. this.events.add(document.body, "touchmove", function(event)
  163. {
  164. var touch = event.touches[0];
  165. self.updatePosition(touch.clientX, touch.clientY, touch.clientX - lastTouch.x, touch.clientY - lastTouch.y);
  166. lastTouch.set(touch.clientX, touch.clientY);
  167. });
  168. }
  169. //Move
  170. this.events.add(this.domElement, "mousemove", function(event)
  171. {
  172. self.updatePosition(event.clientX, event.clientY, event.movementX, event.movementY);
  173. });
  174. //Button pressed
  175. this.events.add(this.domElement, "mousedown", function(event)
  176. {
  177. self.updateKey(event.which - 1, Key.DOWN);
  178. });
  179. //Button released
  180. this.events.add(this.domElement, "mouseup", function(event)
  181. {
  182. self.updateKey(event.which - 1, Key.UP);
  183. });
  184. //Drag start
  185. this.events.add(this.domElement, "dragstart", function(event)
  186. {
  187. self.updateKey(event.which - 1, Key.UP);
  188. });
  189. //Pointer double click
  190. this.events.add(this.domElement, "dblclick", function(event)
  191. {
  192. self._doubleClicked[event.which - 1] = true;
  193. });
  194. this.create();
  195. }
  196. Pointer.prototype = Pointer;
  197. Pointer.prototype.constructor = Pointer;
  198. /**
  199. * Left pointer button.
  200. *
  201. * @static
  202. * @type {number}
  203. */
  204. Pointer.LEFT = 0;
  205. /**
  206. * Middle pointer button.
  207. *
  208. * @static
  209. * @type {number}
  210. */
  211. Pointer.MIDDLE = 1;
  212. /**
  213. * Right pointer button.
  214. *
  215. * @static
  216. * @type {number}
  217. */
  218. Pointer.RIGHT = 2;
  219. /**
  220. * Back pointer navigation button.
  221. *
  222. * @static
  223. * @type {number}
  224. */
  225. Pointer.BACK = 3;
  226. /**
  227. * Forward pointer navigation button.
  228. *
  229. * @static
  230. * @type {number}
  231. */
  232. Pointer.FORWARD = 4;
  233. /**
  234. * Element to be used for coordinates calculation relative to that canvas.
  235. *
  236. * @param {DOM} element Canvas to be attached to the Pointer instance
  237. */
  238. Pointer.setCanvas = function(element)
  239. {
  240. this.canvas = element;
  241. element.pointerInside = false;
  242. element.addEventListener("mouseenter", function()
  243. {
  244. this.pointerInside = true;
  245. });
  246. element.addEventListener("mouseleave", function()
  247. {
  248. this.pointerInside = false;
  249. });
  250. };
  251. /**
  252. * Check if pointer is inside attached canvas (updated async).
  253. *
  254. * @return {boolean} True if pointer is currently inside the canvas
  255. */
  256. Pointer.insideCanvas = function()
  257. {
  258. return this.canvas !== null &amp;&amp; this.canvas.pointerInside;
  259. };
  260. /**
  261. * Check if pointer button is currently pressed.
  262. *
  263. * @param {Number} button Button to check status of
  264. * @return {boolean} True if button is currently pressed
  265. */
  266. Pointer.buttonPressed = function(button)
  267. {
  268. return this.keys[button].pressed;
  269. };
  270. /**
  271. * Check if pointer button was double clicked.
  272. *
  273. * @param {Number} button Button to check status of
  274. * @return {boolean} True if some pointer button was just double clicked
  275. */
  276. Pointer.buttonDoubleClicked = function(button)
  277. {
  278. return this.doubleClicked[button];
  279. };
  280. /**
  281. * Check if a pointer button was just pressed.
  282. *
  283. * @param {Number} button Button to check status of
  284. * @return {boolean} True if button was just pressed
  285. */
  286. Pointer.buttonJustPressed = function(button)
  287. {
  288. return this.keys[button].justPressed;
  289. };
  290. /**
  291. * Check if a pointer button was just released.
  292. *
  293. * @param {Number} button Button to check status of
  294. * @return {boolean} True if button was just released
  295. */
  296. Pointer.buttonJustReleased = function(button)
  297. {
  298. return this.keys[button].justReleased;
  299. };
  300. /**
  301. * Update pointer position.
  302. *
  303. * @param {Number} x
  304. * @param {Number} y
  305. * @param {Number} xDiff
  306. * @param {Number} yDiff
  307. */
  308. Pointer.updatePosition = function(x, y, xDiff, yDiff)
  309. {
  310. if(this.canvas !== null)
  311. {
  312. var rect = this.canvas.getBoundingClientRect();
  313. x -= rect.left;
  314. y -= rect.top;
  315. }
  316. this._position.set(x, y);
  317. this._delta.x += xDiff;
  318. this._delta.y += yDiff;
  319. this._positionUpdated = true;
  320. };
  321. /**
  322. * Update a pointer button.
  323. *
  324. * @param {Number} button
  325. * @param {Number} action
  326. */
  327. Pointer.updateKey = function(button, action)
  328. {
  329. if(button > -1)
  330. {
  331. this._keys[button].update(action);
  332. }
  333. };
  334. /**
  335. * Update pointer buttons state, position, wheel and delta synchronously.
  336. *
  337. * Should be called every frame on the update loop before reading any values from the pointer.
  338. */
  339. Pointer.update = function()
  340. {
  341. //Update pointer keys state
  342. for(var i = 0; i &lt; 5; i++)
  343. {
  344. if(this._keys[i].justPressed &amp;&amp; this.keys[i].justPressed)
  345. {
  346. this._keys[i].justPressed = false;
  347. }
  348. if(this._keys[i].justReleased &amp;&amp; this.keys[i].justReleased)
  349. {
  350. this._keys[i].justReleased = false;
  351. }
  352. this.keys[i].set(this._keys[i].justPressed, this._keys[i].pressed, this._keys[i].justReleased);
  353. //Update pointer double click
  354. if(this._doubleClicked[i] === true)
  355. {
  356. this.doubleClicked[i] = true;
  357. this._doubleClicked[i] = false;
  358. }
  359. else
  360. {
  361. this.doubleClicked[i] = false;
  362. }
  363. }
  364. //Update pointer wheel
  365. if(this._wheelUpdated)
  366. {
  367. this.wheel = this._wheel;
  368. this._wheelUpdated = false;
  369. }
  370. else
  371. {
  372. this.wheel = 0;
  373. }
  374. //Update pointer Position if needed
  375. if(this._positionUpdated)
  376. {
  377. this.delta.copy(this._delta);
  378. this.position.copy(this._position);
  379. this._delta.set(0,0);
  380. this._positionUpdated = false;
  381. }
  382. else
  383. {
  384. this.delta.x = 0;
  385. this.delta.y = 0;
  386. }
  387. };
  388. /**
  389. * Create pointer events to collect input data.
  390. *
  391. * Should be called before using the pointer object.
  392. */
  393. Pointer.create = function()
  394. {
  395. this.events.create();
  396. };
  397. /**
  398. * Dispose pointer events, should be called after the objects is no longer required.
  399. *
  400. * If not called leaves the window events created leaving a memory/code leak.
  401. */
  402. Pointer.dispose = function()
  403. {
  404. this.events.destroy();
  405. };
  406. export {Pointer};
  407. </code></pre>
  408. </article>
  409. </section>
  410. </div>
  411. <nav>
  412. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</a></li><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="ColorStyle.html">ColorStyle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="LinearGradientStyle.html">LinearGradientStyle</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul><h3>Global</h3><ul><li><a href="global.html#writeFile">writeFile</a></li></ul>
  413. </nav>
  414. <br class="clear">
  415. <footer>
  416. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
  417. </footer>
  418. <script> prettyPrint(); </script>
  419. <script src="scripts/linenumber.js"> </script>
  420. </body>
  421. </html>