123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>JSDoc: Source: input/Pointer.js</title>
- <script src="scripts/prettify/prettify.js"> </script>
- <script src="scripts/prettify/lang-css.js"> </script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
- <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
- </head>
- <body>
- <div id="main">
- <h1 class="page-title">Source: input/Pointer.js</h1>
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>import {EventManager} from "../utils/EventManager.js";
- import {Vector2} from "../math/Vector2.js";
- import {Key} from "./Key.js";
- /**
- * Pointer object is used to called input from the user, works for booth mouse or touch screens.
- *
- * It is responsible for synchronizing user input with the render of the graphics.
- *
- * @class
- * @param {Element} domElement DOM element to create the pointer events.
- * @param {Element} canvas Canvas DOM element where the content is being drawn.
- */
- function Pointer(domElement, canvas)
- {
- //Raw data
- this._keys = new Array(5);
- this._position = new Vector2(0, 0);
- this._positionUpdated = false;
- this._delta = new Vector2(0, 0);
- this._wheel = 0;
- this._wheelUpdated = false;
- this._doubleClicked = new Array(5);
- /**
- * Array with pointer buttons status.
- *
- * @type {number[]}
- */
- this.keys = new Array(5);
- /**
- * Pointer position inside of the window (coordinates in window space).
- *
- * This value is accumulated from multiple mouse triggered events between updated.
- *
- * @type {Vector2}
- */
- this.position = new Vector2(0, 0);
- /**
- * Pointer movement (coordinates in window space). Since the last update.
- *
- * This value is accumulated from multiple mouse triggered events between updated.
- *
- * @type {Vector2}
- */
- this.delta = new Vector2(0, 0);
- /**
- * Pointer scroll wheel movement, since the last update.
- *
- * @type {number}
- */
- this.wheel = 0;
-
- /**
- * Indicates a button of the pointer was double clicked.
- *
- * @type {boolean}
- */
- this.doubleClicked = new Array(5);
- /**
- * DOM element where to attach the pointer events.
- *
- * @type {Element}
- */
- this.domElement = (domElement !== undefined) ? domElement : window;
- /**
- * Canvas attached to this pointer instance used to calculate position and delta in element space coordinates.
- *
- * @type {Element}
- */
- this.canvas = null;
- if(canvas !== undefined)
- {
- this.setCanvas(canvas);
- }
- /**
- * Event manager responsible for updating the raw data variables.
- *
- * Different events are used depending on the host platform.
- *
- * When the update method is called the raw data is reset.
- *
- * @type {EventManager}
- */
- this.events = new EventManager();
- //Initialize key instances
- for(var i = 0; i < 5; i++)
- {
- this._doubleClicked[i] = false;
- this.doubleClicked[i] = false;
- this._keys[i] = new Key();
- this.keys[i] = new Key();
- }
- //Self pointer
- var self = this;
- //Scroll wheel
- if(window.onmousewheel !== undefined)
- {
- //Chrome, edge
- this.events.add(this.domElement, "mousewheel", function(event)
- {
- self._wheel = event.deltaY;
- self._wheelUpdated = true;
- });
- }
- else if(window.addEventListener !== undefined)
- {
- //Firefox
- this.events.add(this.domElement, "DOMMouseScroll", function(event)
- {
- self._wheel = event.detail * 30;
- self._wheelUpdated = true;
- });
- }
- else
- {
- this.events.add(this.domElement, "wheel", function(event)
- {
- self._wheel = event.deltaY;
- self._wheelUpdated = true;
- });
- }
- //Touchscreen input events
- if(window.ontouchstart !== undefined || navigator.msMaxTouchPoints > 0)
- {
- //Auxiliar variables to calculate touch delta
- var lastTouch = new Vector2(0, 0);
- //Touch start event
- this.events.add(this.domElement, "touchstart", function(event)
- {
- var touch = event.touches[0];
- self.updatePosition(touch.clientX, touch.clientY, 0, 0);
- self.updateKey(Pointer.LEFT, Key.DOWN);
- lastTouch.set(touch.clientX, touch.clientY);
- });
- //Touch end event
- this.events.add(this.domElement, "touchend", function(event)
- {
- self.updateKey(Pointer.LEFT, Key.UP);
- });
- //Touch cancel event
- this.events.add(this.domElement, "touchcancel", function(event)
- {
- self.updateKey(Pointer.LEFT, Key.UP);
- });
- //Touch move event
- this.events.add(document.body, "touchmove", function(event)
- {
- var touch = event.touches[0];
- self.updatePosition(touch.clientX, touch.clientY, touch.clientX - lastTouch.x, touch.clientY - lastTouch.y);
- lastTouch.set(touch.clientX, touch.clientY);
- });
- }
- //Move
- this.events.add(this.domElement, "mousemove", function(event)
- {
- self.updatePosition(event.clientX, event.clientY, event.movementX, event.movementY);
- });
- //Button pressed
- this.events.add(this.domElement, "mousedown", function(event)
- {
- self.updateKey(event.which - 1, Key.DOWN);
- });
- //Button released
- this.events.add(this.domElement, "mouseup", function(event)
- {
- self.updateKey(event.which - 1, Key.UP);
- });
- //Drag start
- this.events.add(this.domElement, "dragstart", function(event)
- {
- self.updateKey(event.which - 1, Key.UP);
- });
- //Pointer double click
- this.events.add(this.domElement, "dblclick", function(event)
- {
- self._doubleClicked[event.which - 1] = true;
- });
- this.create();
- }
- Pointer.prototype = Pointer;
- Pointer.prototype.constructor = Pointer;
- /**
- * Left pointer button.
- *
- * @static
- * @type {number}
- */
- Pointer.LEFT = 0;
- /**
- * Middle pointer button.
- *
- * @static
- * @type {number}
- */
- Pointer.MIDDLE = 1;
- /**
- * Right pointer button.
- *
- * @static
- * @type {number}
- */
- Pointer.RIGHT = 2;
- /**
- * Back pointer navigation button.
- *
- * @static
- * @type {number}
- */
- Pointer.BACK = 3;
- /**
- * Forward pointer navigation button.
- *
- * @static
- * @type {number}
- */
- Pointer.FORWARD = 4;
- /**
- * Element to be used for coordinates calculation relative to that canvas.
- *
- * @param {DOM} element Canvas to be attached to the Pointer instance
- */
- Pointer.setCanvas = function(element)
- {
- this.canvas = element;
- element.pointerInside = false;
- element.addEventListener("mouseenter", function()
- {
- this.pointerInside = true;
- });
- element.addEventListener("mouseleave", function()
- {
- this.pointerInside = false;
- });
- };
- /**
- * Check if pointer is inside attached canvas (updated async).
- *
- * @return {boolean} True if pointer is currently inside the canvas
- */
- Pointer.insideCanvas = function()
- {
- return this.canvas !== null && this.canvas.pointerInside;
- };
- /**
- * Check if pointer button is currently pressed.
- *
- * @param {Number} button Button to check status of
- * @return {boolean} True if button is currently pressed
- */
- Pointer.buttonPressed = function(button)
- {
- return this.keys[button].pressed;
- };
- /**
- * Check if pointer button was double clicked.
- *
- * @param {Number} button Button to check status of
- * @return {boolean} True if some pointer button was just double clicked
- */
- Pointer.buttonDoubleClicked = function(button)
- {
- return this.doubleClicked[button];
- };
- /**
- * Check if a pointer button was just pressed.
- *
- * @param {Number} button Button to check status of
- * @return {boolean} True if button was just pressed
- */
- Pointer.buttonJustPressed = function(button)
- {
- return this.keys[button].justPressed;
- };
- /**
- * Check if a pointer button was just released.
- *
- * @param {Number} button Button to check status of
- * @return {boolean} True if button was just released
- */
- Pointer.buttonJustReleased = function(button)
- {
- return this.keys[button].justReleased;
- };
- /**
- * Update pointer position.
- *
- * @param {Number} x
- * @param {Number} y
- * @param {Number} xDiff
- * @param {Number} yDiff
- */
- Pointer.updatePosition = function(x, y, xDiff, yDiff)
- {
- if(this.canvas !== null)
- {
- var rect = this.canvas.getBoundingClientRect();
- x -= rect.left;
- y -= rect.top;
- }
- this._position.set(x, y);
- this._delta.x += xDiff;
- this._delta.y += yDiff;
- this._positionUpdated = true;
- };
- /**
- * Update a pointer button.
- *
- * @param {Number} button
- * @param {Number} action
- */
- Pointer.updateKey = function(button, action)
- {
- if(button > -1)
- {
- this._keys[button].update(action);
- }
- };
- /**
- * Update pointer buttons state, position, wheel and delta synchronously.
- *
- * Should be called every frame on the update loop before reading any values from the pointer.
- */
- Pointer.update = function()
- {
- //Update pointer keys state
- for(var i = 0; i < 5; i++)
- {
- if(this._keys[i].justPressed && this.keys[i].justPressed)
- {
- this._keys[i].justPressed = false;
- }
- if(this._keys[i].justReleased && this.keys[i].justReleased)
- {
- this._keys[i].justReleased = false;
- }
- this.keys[i].set(this._keys[i].justPressed, this._keys[i].pressed, this._keys[i].justReleased);
- //Update pointer double click
- if(this._doubleClicked[i] === true)
- {
- this.doubleClicked[i] = true;
- this._doubleClicked[i] = false;
- }
- else
- {
- this.doubleClicked[i] = false;
- }
- }
- //Update pointer wheel
- if(this._wheelUpdated)
- {
- this.wheel = this._wheel;
- this._wheelUpdated = false;
- }
- else
- {
- this.wheel = 0;
- }
- //Update pointer Position if needed
- if(this._positionUpdated)
- {
- this.delta.copy(this._delta);
- this.position.copy(this._position);
- this._delta.set(0,0);
- this._positionUpdated = false;
- }
- else
- {
- this.delta.x = 0;
- this.delta.y = 0;
- }
- };
- /**
- * Create pointer events to collect input data.
- *
- * Should be called before using the pointer object.
- */
- Pointer.create = function()
- {
- this.events.create();
- };
- /**
- * Dispose pointer events, should be called after the objects is no longer required.
- *
- * If not called leaves the window events created leaving a memory/code leak.
- */
- Pointer.dispose = function()
- {
- this.events.destroy();
- };
- export {Pointer};
- </code></pre>
- </article>
- </section>
- </div>
- <nav>
- <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>
- </nav>
- <br class="clear">
- <footer>
- 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)
- </footer>
- <script> prettyPrint(); </script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|