input_Pointer.js.html 11 KB

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