input_Pointer.js.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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>"use strict";
  20. import {EventManager} from "../EventManager.js";
  21. import {Vector2} from "../math/Vector2.js";
  22. import {Key} from "./Key.js";
  23. /**
  24. * Pointer object is used to colled input from the user, works for booth mouse or touch screens.
  25. *
  26. * It is responsible for syncronizing user input with the render of the graphics.
  27. *
  28. * @class
  29. * @param {DOM} domElement DOM element to craete the pointer events.
  30. * @param {DOM} canvas Canvas DOM element where the content is being drawn.
  31. */
  32. function Pointer(domElement, canvas)
  33. {
  34. //Raw data
  35. this._keys = new Array(5);
  36. this._position = new Vector2(0, 0);
  37. this._positionUpdated = false;
  38. this._delta = new Vector2(0, 0);
  39. this._wheel = 0;
  40. this._wheelUpdated = false;
  41. this._doubleClicked = new Array(5);
  42. /**
  43. * Array with pointer buttons status.
  44. */
  45. this.keys = new Array(5);
  46. /**
  47. * Pointer position inside of the window (coordinates in window space).
  48. */
  49. this.position = new Vector2(0, 0);
  50. /**
  51. * Pointer movement (coordinates in window space).
  52. */
  53. this.delta = new Vector2(0, 0);
  54. /**
  55. * Pointer scroll wheel movement.
  56. */
  57. this.wheel = 0;
  58. /**
  59. * Indicates a button of the pointer was double clicked.
  60. */
  61. this.doubleClicked = new Array(5);
  62. /**
  63. * DOM element where to attach the pointer events.
  64. */
  65. this.domElement = (domElement !== undefined) ? domElement : window;
  66. /**
  67. * Canvas attached to this pointer instance used to calculate position and delta in element space coordinates.
  68. */
  69. this.canvas = null;
  70. if(canvas !== undefined)
  71. {
  72. this.setCanvas(canvas);
  73. }
  74. /**
  75. * Event manager responsible for updating the raw data variables.
  76. *
  77. * Diferent events are used depending on the host platform.
  78. *
  79. * When the update method is called the raw data is reset.
  80. */
  81. this.events = new EventManager();
  82. //Initialize key instances
  83. for(var i = 0; i &lt; 5; i++)
  84. {
  85. this._doubleClicked[i] = false;
  86. this.doubleClicked[i] = false;
  87. this._keys[i] = new Key();
  88. this.keys[i] = new Key();
  89. }
  90. //Self pointer
  91. var self = this;
  92. //Scroll wheel
  93. if(window.onmousewheel !== undefined)
  94. {
  95. //Chrome, edge
  96. this.events.add(this.domElement, "mousewheel", function(event)
  97. {
  98. self._wheel = event.deltaY;
  99. self._wheelUpdated = true;
  100. });
  101. }
  102. else if(window.addEventListener !== undefined)
  103. {
  104. //Firefox
  105. this.events.add(this.domElement, "DOMMouseScroll", function(event)
  106. {
  107. self._wheel = event.detail * 30;
  108. self._wheelUpdated = true;
  109. });
  110. }
  111. else
  112. {
  113. this.events.add(this.domElement, "wheel", function(event)
  114. {
  115. self._wheel = event.deltaY;
  116. self._wheelUpdated = true;
  117. });
  118. }
  119. //Touchscreen input events
  120. if(window.ontouchstart !== undefined || navigator.msMaxTouchPoints > 0)
  121. {
  122. //Auxiliar variables to calculate touch delta
  123. var lastTouch = new Vector2(0, 0);
  124. //Touch start event
  125. this.events.add(this.domElement, "touchstart", function(event)
  126. {
  127. var touch = event.touches[0];
  128. self.updatePosition(touch.clientX, touch.clientY, 0, 0);
  129. self.updateKey(Pointer.LEFT, Key.DOWN);
  130. lastTouch.set(touch.clientX, touch.clientY);
  131. });
  132. //Touch end event
  133. this.events.add(this.domElement, "touchend", function(event)
  134. {
  135. self.updateKey(Pointer.LEFT, Key.UP);
  136. });
  137. //Touch cancel event
  138. this.events.add(this.domElement, "touchcancel", function(event)
  139. {
  140. self.updateKey(Pointer.LEFT, Key.UP);
  141. });
  142. //Touch move event
  143. this.events.add(document.body, "touchmove", function(event)
  144. {
  145. var touch = event.touches[0];
  146. self.updatePosition(touch.clientX, touch.clientY, touch.clientX - lastTouch.x, touch.clientY - lastTouch.y);
  147. lastTouch.set(touch.clientX, touch.clientY);
  148. });
  149. }
  150. //Move
  151. this.events.add(this.domElement, "mousemove", function(event)
  152. {
  153. self.updatePosition(event.clientX, event.clientY, event.movementX, event.movementY);
  154. });
  155. //Button pressed
  156. this.events.add(this.domElement, "mousedown", function(event)
  157. {
  158. self.updateKey(event.which - 1, Key.DOWN);
  159. });
  160. //Button released
  161. this.events.add(this.domElement, "mouseup", function(event)
  162. {
  163. self.updateKey(event.which - 1, Key.UP);
  164. });
  165. //Drag start
  166. this.events.add(this.domElement, "dragstart", function(event)
  167. {
  168. self.updateKey(event.which - 1, Key.UP);
  169. });
  170. //Pointer double click
  171. this.events.add(this.domElement, "dblclick", function(event)
  172. {
  173. self._doubleClicked[event.which - 1] = true;
  174. });
  175. this.create();
  176. }
  177. Pointer.prototype = Pointer;
  178. Pointer.prototype.constructor = Pointer;
  179. /**
  180. * Left pointer button.
  181. */
  182. Pointer.LEFT = 0;
  183. /**
  184. * Middle pointer button.
  185. */
  186. Pointer.MIDDLE = 1;
  187. /**
  188. * Right pointer button.
  189. */
  190. Pointer.RIGHT = 2;
  191. /**
  192. * Back pointer navigation button.
  193. */
  194. Pointer.BACK = 3;
  195. /**
  196. * Forward pointer navigation button.
  197. */
  198. Pointer.FORWARD = 4;
  199. /**
  200. * Element to be used for coordinates calculation relative to that canvas.
  201. *
  202. * @param {DOM} canvas Canvas to be attached to the Pointer instance
  203. */
  204. Pointer.setCanvas = function(element)
  205. {
  206. this.canvas = element;
  207. element.pointerInside = false;
  208. element.addEventListener("mouseenter", function()
  209. {
  210. this.pointerInside = true;
  211. });
  212. element.addEventListener("mouseleave", function()
  213. {
  214. this.pointerInside = false;
  215. });
  216. };
  217. /**
  218. * Check if pointer is inside attached canvas (updated async).
  219. *
  220. * @return {boolean} True if pointer is currently inside the canvas
  221. */
  222. Pointer.insideCanvas = function()
  223. {
  224. return this.canvas !== null &amp;&amp; this.canvas.pointerInside;
  225. };
  226. /**
  227. * Check if pointer button is currently pressed.
  228. *
  229. * @param {Number} button Button to check status of
  230. * @return {boolean} True if button is currently pressed
  231. */
  232. Pointer.buttonPressed = function(button)
  233. {
  234. return this.keys[button].pressed;
  235. };
  236. /**
  237. * Check if pointer button was double clicked.
  238. *
  239. * @param {Number} button Button to check status of
  240. * @return {boolean} True if some pointer button was just double clicked
  241. */
  242. Pointer.buttonDoubleClicked = function(button)
  243. {
  244. return this.doubleClicked[button];
  245. };
  246. /**
  247. * Check if a pointer button was just pressed.
  248. *
  249. * @param {Number} button Button to check status of
  250. * @return {boolean} True if button was just pressed
  251. */
  252. Pointer.buttonJustPressed = function(button)
  253. {
  254. return this.keys[button].justPressed;
  255. };
  256. /**
  257. * Check if a pointer button was just released.
  258. *
  259. * @param {Number} button Button to check status of
  260. * @return {boolean} True if button was just released
  261. */
  262. Pointer.buttonJustReleased = function(button)
  263. {
  264. return this.keys[button].justReleased;
  265. };
  266. /**
  267. * Update pointer position.
  268. *
  269. * Automatically called by the runtime.
  270. *
  271. * @param {Number} x
  272. * @param {Number} y
  273. * @param {Number} xDiff
  274. * @param {Number} yDiff
  275. */
  276. Pointer.updatePosition = function(x, y, xDiff, yDiff)
  277. {
  278. if(this.canvas !== null)
  279. {
  280. var rect = this.canvas.getBoundingClientRect();
  281. x -= rect.left;
  282. y -= rect.top;
  283. }
  284. this._position.set(x, y);
  285. this._delta.x += xDiff;
  286. this._delta.y += yDiff;
  287. this._positionUpdated = true;
  288. };
  289. /**
  290. * Update a pointer button.
  291. *
  292. * Automatically called by the runtime.
  293. *
  294. * @param {Number} button
  295. * @param {Number} action
  296. */
  297. Pointer.updateKey = function(button, action)
  298. {
  299. if(button > -1)
  300. {
  301. this._keys[button].update(action);
  302. }
  303. };
  304. /**
  305. * Update pointer buttons state, position, wheel and delta synchronously.
  306. */
  307. Pointer.update = function()
  308. {
  309. //Update pointer keys state
  310. for(var i = 0; i &lt; 5; i++)
  311. {
  312. if(this._keys[i].justPressed &amp;&amp; this.keys[i].justPressed)
  313. {
  314. this._keys[i].justPressed = false;
  315. }
  316. if(this._keys[i].justReleased &amp;&amp; this.keys[i].justReleased)
  317. {
  318. this._keys[i].justReleased = false;
  319. }
  320. this.keys[i].set(this._keys[i].justPressed, this._keys[i].pressed, this._keys[i].justReleased);
  321. //Update pointer double click
  322. if(this._doubleClicked[i] === true)
  323. {
  324. this.doubleClicked[i] = true;
  325. this._doubleClicked[i] = false;
  326. }
  327. else
  328. {
  329. this.doubleClicked[i] = false;
  330. }
  331. }
  332. //Update pointer wheel
  333. if(this._wheelUpdated)
  334. {
  335. this.wheel = this._wheel;
  336. this._wheelUpdated = false;
  337. }
  338. else
  339. {
  340. this.wheel = 0;
  341. }
  342. //Update pointer Position if needed
  343. if(this._positionUpdated)
  344. {
  345. this.delta.copy(this._delta);
  346. this.position.copy(this._position);
  347. this._delta.set(0,0);
  348. this._positionUpdated = false;
  349. }
  350. else
  351. {
  352. this.delta.x = 0;
  353. this.delta.y = 0;
  354. }
  355. };
  356. /**
  357. * Create pointer events.
  358. */
  359. Pointer.create = function()
  360. {
  361. this.events.create();
  362. };
  363. /**
  364. * Dispose pointer events.
  365. */
  366. Pointer.dispose = function()
  367. {
  368. this.events.destroy();
  369. };
  370. export {Pointer};</code></pre>
  371. </article>
  372. </section>
  373. </div>
  374. <nav>
  375. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><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="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</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="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="Renderer.html">Renderer</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>
  376. </nav>
  377. <br class="clear">
  378. <footer>
  379. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Tue Jun 25 2019 14:27:21 GMT+0100 (Western European Summer Time)
  380. </footer>
  381. <script> prettyPrint(); </script>
  382. <script src="scripts/linenumber.js"> </script>
  383. </body>
  384. </html>