input_Pointer.js.html 10 KB

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