UNPKG

crossbrowdy

Version:

A Multimedia JavaScript framework to create real cross-platform and hybrid game engines, games, emulators, multimedia libraries and apps.

774 lines (661 loc) 89 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>CrossBrowdy API documentation [printable] Source: CrossBase/input/CB_Mouse.js</title> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css"> <link type="text/css" rel="stylesheet" href="styles/site.yeti.css"> </head> <body style="min-width:800px; overflow-wrap:break-word; word-wrap:break-word; word-break:break-word; line-break:strict; hyphens:none; -webkit-hyphens:none; -moz-hyphens:none;"> <div class="navbar navbar-default navbar-fixed-top "> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="index.html">CrossBrowdy API documentation [printable]</a> <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <div class="navbar-collapse collapse" id="topNavigation"> <ul class="nav navbar-nav"> <li class="dropdown"> <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b class="caret"></b></a> <ul class="dropdown-menu inline"> <li><a href="CB_Arrays.html">CB_Arrays</a></li><li><a href="CB_AudioDetector.html">CB_AudioDetector</a></li><li><a href="CB_Client.html">CB_Client</a></li><li><a href="CB_Collisions.html">CB_Collisions</a></li><li><a href="CB_Configuration.html">CB_Configuration</a></li><li><a href="CB_Configuration.CrossBase.html">CB_Configuration.CrossBase</a></li><li><a href="CB_Configuration.CrossBrowdy.html">CB_Configuration.CrossBrowdy</a></li><li><a href="CB_Controllers.html">CB_Controllers</a></li><li><a href="CB_Controllers_Proprietary.html">CB_Controllers_Proprietary</a></li><li><a href="CB_Controllers_Proprietary.WII.html">CB_Controllers_Proprietary.WII</a></li><li><a href="CB_Controllers_Proprietary.WII_U.html">CB_Controllers_Proprietary.WII_U</a></li><li><a href="CB_Device.html">CB_Device</a></li><li><a href="CB_Device.AmbientLight.html">CB_Device.AmbientLight</a></li><li><a href="CB_Device.Battery.html">CB_Device.Battery</a></li><li><a href="CB_Device.Location.html">CB_Device.Location</a></li><li><a href="CB_Device.Motion.html">CB_Device.Motion</a></li><li><a href="CB_Device.Orientation.html">CB_Device.Orientation</a></li><li><a href="CB_Device.Proximity.html">CB_Device.Proximity</a></li><li><a href="CB_Device.Vibration.html">CB_Device.Vibration</a></li><li><a href="CB_Elements.html">CB_Elements</a></li><li><a href="CB_Events.html">CB_Events</a></li><li><a href="CB_Keyboard.html">CB_Keyboard</a></li><li><a href="CB_Keyboard.chars.html">CB_Keyboard.chars</a></li><li><a href="CB_Keyboard.extended.html">CB_Keyboard.extended</a></li><li><a href="CB_Keyboard.keys.html">CB_Keyboard.keys</a></li><li><a href="CB_Modules.html">CB_Modules</a></li><li><a href="CB_Mouse.html">CB_Mouse</a></li><li><a href="CB_Mouse.CursorImage.html">CB_Mouse.CursorImage</a></li><li><a href="CB_Net.html">CB_Net</a></li><li><a href="CB_Net.Fetch.html">CB_Net.Fetch</a></li><li><a href="CB_Net.REST.html">CB_Net.REST</a></li><li><a href="CB_Net.Sockets.html">CB_Net.Sockets</a></li><li><a href="CB_Net.Sockets.SockJS.html">CB_Net.Sockets.SockJS</a></li><li><a href="CB_Net.XHR.html">CB_Net.XHR</a></li><li><a href="CB_Pointer.html">CB_Pointer</a></li><li><a href="CB_Screen.html">CB_Screen</a></li><li><a href="CB_Speaker.html">CB_Speaker</a></li><li><a href="CB_Touch.html">CB_Touch</a></li><li><a href="CB_baseSymbols.html">CB_baseSymbols</a></li> </ul> </li> <li class="dropdown"> <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a> <ul class="dropdown-menu inline"> <li><a href="CB_AudioFile.html">CB_AudioFile</a></li><li><a href="CB_AudioFileCache.html">CB_AudioFileCache</a></li><li><a href="CB_AudioFileSprites.html">CB_AudioFileSprites</a></li><li><a href="CB_AudioFileSpritesPool.html">CB_AudioFileSpritesPool</a></li><li><a href="CB_AudioFile_API.AAPI.html">CB_AudioFile_API.AAPI</a></li><li><a href="CB_AudioFile_API.ACMP.html">CB_AudioFile_API.ACMP</a></li><li><a href="CB_AudioFile_API.SM2.html">CB_AudioFile_API.SM2</a></li><li><a href="CB_AudioFile_API.WAAPI.html">CB_AudioFile_API.WAAPI</a></li><li><a href="CB_Canvas.html">CB_Canvas</a></li><li><a href="CB_GraphicSprites.html">CB_GraphicSprites</a></li><li><a href="CB_GraphicSpritesScene.html">CB_GraphicSpritesScene</a></li> </ul> </li> <li class="dropdown"> <a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a> <ul class="dropdown-menu inline"> <li><a href="global.html#CB_BASE_NAME">CB_BASE_NAME</a></li><li><a href="global.html#CB_CREDITS_DEFAULT">CB_CREDITS_DEFAULT</a></li><li><a href="global.html#CB_NAME">CB_NAME</a></li><li><a href="global.html#CB_OPTIONS">CB_OPTIONS</a></li><li><a href="global.html#CB_VERSION">CB_VERSION</a></li><li><a href="global.html#CB_addCredits">CB_addCredits</a></li><li><a href="global.html#CB_baseToBase">CB_baseToBase</a></li><li><a href="global.html#CB_baseToInt">CB_baseToInt</a></li><li><a href="global.html#CB_br2nl">CB_br2nl</a></li><li><a href="global.html#CB_brToNl">CB_brToNl</a></li><li><a href="global.html#CB_combineArraysOrObjects">CB_combineArraysOrObjects</a></li><li><a href="global.html#CB_combineAutomatically">CB_combineAutomatically</a></li><li><a href="global.html#CB_combineJSON">CB_combineJSON</a></li><li><a href="global.html#CB_combineURIParameters">CB_combineURIParameters</a></li><li><a href="global.html#CB_combineURLParameters">CB_combineURLParameters</a></li><li><a href="global.html#CB_console">CB_console</a></li><li><a href="global.html#CB_copyObject">CB_copyObject</a></li><li><a href="global.html#CB_countDecimalDigits">CB_countDecimalDigits</a></li><li><a href="global.html#CB_countDecimalPart">CB_countDecimalPart</a></li><li><a href="global.html#CB_countDecimals">CB_countDecimals</a></li><li><a href="global.html#CB_countIntegerDigits">CB_countIntegerDigits</a></li><li><a href="global.html#CB_countIntegerPart">CB_countIntegerPart</a></li><li><a href="global.html#CB_credits">CB_credits</a></li><li><a href="global.html#CB_forEach">CB_forEach</a></li><li><a href="global.html#CB_forceString">CB_forceString</a></li><li><a href="global.html#CB_getBase64StringObject">CB_getBase64StringObject</a></li><li><a href="global.html#CB_getCookie">CB_getCookie</a></li><li><a href="global.html#CB_getDatum">CB_getDatum</a></li><li><a href="global.html#CB_getJSONPropertyValue">CB_getJSONPropertyValue</a></li><li><a href="global.html#CB_getLZStringObject">CB_getLZStringObject</a></li><li><a href="global.html#CB_getValueIndex">CB_getValueIndex</a></li><li><a href="global.html#CB_getValuePath">CB_getValuePath</a></li><li><a href="global.html#CB_includeJSFile">CB_includeJSFile</a></li><li><a href="global.html#CB_indexOf">CB_indexOf</a></li><li><a href="global.html#CB_init">CB_init</a></li><li><a href="global.html#CB_intToBase">CB_intToBase</a></li><li><a href="global.html#CB_isArray">CB_isArray</a></li><li><a href="global.html#CB_isEmail">CB_isEmail</a></li><li><a href="global.html#CB_isFileLocal">CB_isFileLocal</a></li><li><a href="global.html#CB_isString">CB_isString</a></li><li><a href="global.html#CB_lastIndexOf">CB_lastIndexOf</a></li><li><a href="global.html#CB_ltrim">CB_ltrim</a></li><li><a href="global.html#CB_nl2br">CB_nl2br</a></li><li><a href="global.html#CB_nlToBr">CB_nlToBr</a></li><li><a href="global.html#CB_numberFormat">CB_numberFormat</a></li><li><a href="global.html#CB_numberOfDecimalDigits">CB_numberOfDecimalDigits</a></li><li><a href="global.html#CB_numberOfDecimals">CB_numberOfDecimals</a></li><li><a href="global.html#CB_numberOfIntegerDigits">CB_numberOfIntegerDigits</a></li><li><a href="global.html#CB_parseJSON">CB_parseJSON</a></li><li><a href="global.html#CB_parseString">CB_parseString</a></li><li><a href="global.html#CB_regularExpressionString">CB_regularExpressionString</a></li><li><a href="global.html#CB_renderString">CB_renderString</a></li><li><a href="global.html#CB_replaceAll">CB_replaceAll</a></li><li><a href="global.html#CB_rtrim">CB_rtrim</a></li><li><a href="global.html#CB_scriptPath">CB_scriptPath</a></li><li><a href="global.html#CB_scriptPathCalculate">CB_scriptPathCalculate</a></li><li><a href="global.html#CB_setCookie">CB_setCookie</a></li><li><a href="global.html#CB_setDatum">CB_setDatum</a></li><li><a href="global.html#CB_sizeOf">CB_sizeOf</a></li><li><a href="global.html#CB_sizeof">CB_sizeof</a></li><li><a href="global.html#CB_stringifyJSON">CB_stringifyJSON</a></li><li><a href="global.html#CB_symmetricCall">CB_symmetricCall</a></li><li><a href="global.html#CB_symmetricCallClear">CB_symmetricCallClear</a></li><li><a href="global.html#CB_this">CB_this</a></li><li><a href="global.html#CB_trim">CB_trim</a></li> </ul> </li> </ul> <div class="col-sm-3 col-md-3"> <form class="navbar-form" role="search"> <div class="input-group"> <input type="text" class="form-control" placeholder="Search" name="q" id="search-input"> <div class="input-group-btn"> <button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button> </div> </div> </form> </div> </div> </div> </div> <div class="container" id="toc-content" style="width:100%;"> <div class="row" style="width:100%;"> <div class="col-md-12"> <div id="main"> <h1 class="page-title">Source: CrossBase/input/CB_Mouse.js</h1> <section> <article> <pre class="sunlight-highlight-javascript linenums">/** * @file Mouse and related management. Contains the {@link CB_Mouse} static class. * @author Joan Alba Maldonado &lt;workindalian@gmail.com> * @license Creative Commons Attribution 4.0 International. See more at {@link https://crossbrowdy.com/about#what_is_the_crossbrowdy_copyright_and_license}. */ /** * Static class to manage the mouse and related. It will return itself if it is tried to be instantiated. * @namespace */ var CB_Mouse = function() { return CB_Mouse; }; { CB_Mouse.initialized = false; //It will tells whether the object has been initialized or not. CB_Mouse._x = 0; //Keeps the X position of the mouse (relative to the window). CB_Mouse._y = 0; //Keeps the X position of the mouse (relative to the window). CB_Mouse._xMovement = 0; //Keeps the X movement of the mouser when its pointer is locked. CB_Mouse._yMovement = 0; //Keeps the Y movement of the mouser when its pointer is locked. CB_Mouse._buttonsDown = { LEFT : false, MIDDLE : false, RIGHT : false }; //Object with the buttons of the mouse which being pressed. CB_Mouse._isLockedNow = false; //Contains whether the mouse pointer is locked or not. CB_Mouse._isLockedPrevious = false; //Contains whether the mouse pointer was locked before or not. CB_Mouse._lockElement = null; //Contains the element that the mouse pointer is locked to (if any). //CB_Mouse._showingCursor = {}; //Tells whether the cursor is showing or not (by default is showing). /** Property that keeps an object to manage the mouse cursor using a [DOM element]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement} containing an image for clients that do not support changing the cursor image by CSS. &lt;br /> Caution: Performance could be dramatically decreased while using this workaround. * @namespace CB_Mouse.CursorImage */ CB_Mouse.CursorImage = {}; //Keeps the cursor object. CB_Mouse.CursorImage._cursorImageDiv = null; //DIV that contains the IMG tag for the fake cursor image. CB_Mouse.CursorImage._cursorImage = null; //IMG that contains the fake cursor image. CB_Mouse.CursorImage._showCursorImage = false; //Defines whether the fake cursor image is shown or not. CB_Mouse.CursorImage._cursorImageSpriteAnimationTimeout = null; //Initializes all values: CB_Mouse.init = function() { if (CB_Mouse.initialized) { return CB_Mouse; } //The object has been initialized: CB_Mouse.initialized = true; //Adds event that updates X and Y position of the mouse when it moves (and moves the cursor image if it is necessary): CB_Events.add(document, "mousemove", function(e) { e = CB_Mouse.normalizeEvent(e); CB_Mouse.getX(e); CB_Mouse.getY(e); CB_Mouse.CursorImage.move(); }, true, true, false); CB_Events.add(window, "scroll", function() { CB_Mouse.CursorImage.move(); }, true, true, false); //Sets the event that will check if a button is down: CB_Events.add ( document, "mousedown", function(e) { e = CB_Mouse.normalizeEvent(e); //Uses setCapture/releaseCapture to force IE and Firefox to release mouse buttons when dragging outside the web client window: //if (typeof(document.body) !== "undefined" &amp;&amp; typeof(document.body.setCapture) !== "undefined") if (typeof(e.target) !== "undefined" &amp;&amp; typeof(e.target.setCapture) !== "undefined") { //document.body.setCapture(); e.target.setCapture(); } CB_Mouse._updateButtonsDown(e, true); }, true, true, false ); //Sets the event that will check if a button is released: CB_Events.add ( document, "mouseup", function(e) { e = CB_Mouse.normalizeEvent(e); //Uses setCapture/releaseCapture to force IE and Firefox to release mouse buttons when dragging outside the web client window: //if (typeof(document.body) !== "undefined" &amp;&amp; typeof(document.body.releaseCapture) !== "undefined") if (typeof(e.target) !== "undefined" &amp;&amp; typeof(e.target.releaseCapture) !== "undefined") { //document.body.releaseCapture(); e.target.releaseCapture(); } CB_Mouse._updateButtonsDown(e, false); }, true, true, false ); //Sets the event for when the lock pointer status changes: var onPointerLockChange = function() { CB_Mouse.isLocked(true); //Also updates the cache of the current lock element (if any). }; CB_Events.add(document, "pointerlockchange", onPointerLockChange, true, true, false); CB_Events.add(document, "mozpointerlockchange", onPointerLockChange, true, true, false); CB_Events.add(document, "webkitpointerlockchange", onPointerLockChange, true, true, false); CB_Events.add(document, "pointerlocklost", onPointerLockChange, true, true, false); CB_Events.add(document, "webkitpointerlocklost", onPointerLockChange, true, true, false); CB_Events.add(document, "mozpointerlocklost", onPointerLockChange, true, true, false); //Clears buttonsDown array when the mouse leaves the web client: //CB_Events.add(window, "mouseleave", function() { CB_Mouse._buttonsDown = { LEFT : false, MIDDLE : false, RIGHT : false }; }, true, true, false); //CB_Events.add(document, "mouseout", function() { CB_Mouse._buttonsDown = { LEFT : false, MIDDLE : false, RIGHT : false }; }, true, true, false); //Sets the focus to force a mousemove event and get the proper mouse coordinates (some web clients at the start don't get the proper mouse coordinates): CB_Screen.focus(); return CB_Mouse; } /** * Tries to return the given [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} with some properties normalized (since different clients can use different values) and perhaps some new properties added (in the case they were missing), when possible. The new attached methods and properties may include polyfills, etc. It also calls the {@link CB_Events.normalize} function internally. Some properties added or affected could be [deltaX]{@link https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaX}, [deltaY]{@link https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaY}, [deltaZ]{@link https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaZ}, [force]{@link https://developer.mozilla.org/es/docs/Web/API/MouseEvent/webkitForce}, [clientX]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/clientX}, [clientY]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/clientY}, etc. * @function * @param {Event} e - [Mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent}. If not provided, it will use the value of "event", "window.event", "Event" or an empty object ("{}"). * @returns {Event} Returns the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} normalized. * @todo Add more properties and methods to normalize ([pageX]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageX}, [pageY]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageY}, [offsetX]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/offsetX}, [offsetY]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/offsetY}, etc.). */ CB_Mouse.normalizeEvent = function(e) { e = CB_Events.normalize(e); //If wheel event is not supported, normalizes the event: var onWheelEventName = CB_Mouse._getOnWheelEventName(); if (onWheelEventName !== "wheel") { if (typeof(e.type) !== "undefined" &amp;&amp; e.type === "MozMousePixelScroll") { e.deltaMode = 0; } else { e.deltaMode = 1; } if (typeof(e.deltaX) === "undefined") { e.deltaX = 0; } if (typeof(e.deltaZ) === "undefined") { e.deltaZ = 0; } if (typeof(e.deltaY) === "undefined") { if (onWheelEventName === "mousewheel" &amp;&amp; typeof(e.wheelDelta) !== "undefined") { e.deltaY = -(1/40 * e.wheelDelta); //e.deltaY = e.wheelDelta / -40; //e.deltaY = e.wheelDelta / 120; if (typeof(e.wheelDeltaX) !== "undefined") { event.deltaX = -(1/40 * e.wheelDeltaX); } } else if (typeof(e.detail) !== "undefined") { e.deltaY = e.detail; //e.deltaY = -40 * e.detail; //e.deltaY = e.detail * -120; //e.deltaY = -e.detail / 3; } else { e.deltaY = 0; } } } //Normalizes the force property (if any): if (typeof(e.force) === "undefined" || e.force === null || isNaN(e.force)) { if (typeof(e.webkitForce) !== "undefined" &amp;&amp; e.webkitForce !== null &amp;&amp; !isNaN(e.webkitForce)) { e.force = e.webkitForce; } else if (CB_Touch._force !== null) { e.force = CB_Touch._force; } //Uses force detected by Pressure.js. } if (!e.forceNormalized) { e.force = CB_Touch.normalizeForce(e.force); } e.forceNormalized = true; //TODO: add more properties and methods to normalize (pageX, pageY, offsetX, offsetY, ). //Normalize other properties: //* Source: http://www.jacklmoore.com/notes/mouse-position/ if (typeof(e.clientX) === "undefined" &amp;&amp; typeof(e.pageX) !== "undefined") { e.clientX = e.pageX; } if (typeof(e.clientY) === "undefined" &amp;&amp; typeof(e.pageY) !== "undefined") { e.clientY = e.pageY; } if (document.body &amp;&amp; document.documentElement) { if (typeof(e.pageX) === "undefined" &amp;&amp; typeof(e.clientX) !== "undefined") { if (typeof(document.body.scrollLeft) !== "undefined" &amp;&amp; typeof(document.documentElement.scrollLeft) !== "undefined") { e.pageX = e.clientX + (document.body.scrollLeft) + document.documentElement.scrollLeft; } else if (typeof(document.body.scrollWidth) !== "undefined" &amp;&amp; typeof(document.documentElement.scrollWidth) !== "undefined") { e.pageX = e.clientX + (document.body.scrollWidth) + document.documentElement.scrollWidth; } } if (typeof(e.pageY) === "undefined" &amp;&amp; typeof(e.clientY) !== "undefined") { if (typeof(document.body.scrollTop) !== "undefined" &amp;&amp; typeof(document.documentElement.scrollTop) !== "undefined") { e.pageY = e.clientY + (document.body.scrollTop) + document.documentElement.scrollTop; } else if (typeof(document.body.scrollHeight) !== "undefined" &amp;&amp; typeof(document.documentElement.scrollHeight) !== "undefined") { e.pageY = e.clientY + (document.body.scrollHeight) + document.documentElement.scrollHeight; } } } try { var targetStyle = CB_Elements.getStyle(e.target, true); } catch(E) {} if (targetStyle !== null &amp;&amp; typeof(e.clientX) !== "undefined" &amp;&amp; e.clientX !== null &amp;&amp; typeof(e.clientY) !== "undefined" &amp;&amp; e.clientY !== null) { try { e.offsetX = e.clientX - parseInt(targetStyle['borderLeftWidth'], 10) - CB_Client.getBoundingClientRectMargin("left"); e.offsetY = e.clientY - parseInt(targetStyle['borderTopWidth'], 10) - CB_Client.getBoundingClientRectMargin("top"); } catch(E) {} } return e; } CB_Mouse._getOnWheelEventNameReturnCache = null; CB_Mouse._getOnWheelEventName = function() { if (typeof(CB_Mouse._getOnWheelEventNameReturnCache) === "undefined" || CB_Mouse._getOnWheelEventNameReturnCache === null) { CB_Mouse._getOnWheelEventNameReturnCache = ("onwheel" in document.createElement("div")) ? "wheel" : //"wheel" supported. (typeof(document.onmousewheel) !== "undefined") ? "mousewheel" : //"onmousewheel" supported. "DOMMouseScroll"; //assumes "DOMMouseScroll" support. } return CB_Mouse._getOnWheelEventNameReturnCache; } //Returns which button is down given a mouse event: CB_Mouse._updateButtonsDown = function(e, buttonDown) { e = CB_Mouse.normalizeEvent(e); var buttonsDown = CB_Mouse._buttonsDown; if (e.which) { if (e.which === 1) { buttonsDown.LEFT = buttonDown; } else if (CB_Client.getBrowser() === "Opera" &amp;&amp; CB_Client.getBrowserVersionMain() &lt; 8) { if (e.which === 2) { buttonsDown.RIGHT = buttonDown; } else if (e.which === 3) { buttonsDown.MIDDLE = buttonDown; } } else { if (e.which === 2) { buttonsDown.MIDDLE = buttonDown; } else if (e.which === 3) { buttonsDown.RIGHT = buttonDown; } } } else if (e.button) { if (CB_Client.getBrowser() === "Explorer") { //TODO: think about performing bitwise operations. if (e.button === 1) { buttonsDown.LEFT = buttonDown; } else if (e.button === 2) { buttonsDown.RIGHT = buttonDown; } else if (e.button === 3) { buttonsDown.LEFT = buttonsDown.RIGHT = buttonDown; } else if (e.button === 4) { buttonsDown.MIDDLE = buttonDown; } else if (e.button === 5) { buttonsDown.LEFT = buttonsDown.MIDDLE = buttonDown; } else if (e.button === 6) { buttonsDown.RIGHT = buttonsDown.MIDDLE = buttonDown; } else if (e.button === 7) { buttonsDown.LEFT = buttonsDown.RIGHT = buttonsDown.MIDDLE = buttonDown; } //else if (e.button === 0) { buttonsDown.LEFT = buttonsDown.RIGHT = buttonsDown.MIDDLE = false; } } else { if (e.button === 0) { buttonsDown.LEFT = buttonDown; } else if (e.button === 1) { buttonsDown.MIDDLE = buttonDown; } else if (e.button === 2) { buttonsDown.RIGHT = buttonDown; } } } CB_Mouse._buttonsDown = buttonsDown; } /** * Alias for {@link CB_Client.getButtons}. * @function CB_Client.getButtonsDown * @see {@link CB_Client.getButtons} */ /** * Tells what mouse buttons are down (LEFT, MIDDLE and/or RIGHT buttons). * @function * @returns {Object} Returns an object using the following format (where "true" means that the button is being pressed): { LEFT : boolean, MIDDLE : boolean, RIGHT : boolean } */ CB_Mouse.getButtons = CB_Mouse.getButtonsDown = function() { return CB_Mouse._buttonsDown; } /** * Gets and returns the X coordinate (horizontal position) of the mouse (relative to the window in desktop) in pixels. * @function * @param {Event} [e] - [Mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent}. If not provided, the returning value will use the previously-cached value (updated the last time that the [onMouseMove]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event} event was fired). * @param {boolean} [ignoreScroll=false] - If set to true, the horizontal scroll position will not be added to the returning value. * @param {boolean} [ignoreLock=false] - If set to true, it will ignore whether the cursor is being locked or not. Otherwise, if set to false and the cursor is locked, the returning value will only have in mind the position in the locking element. * @returns {number} Returns the X coordinate (horizontal position) of the mouse (relative to the window in desktop) in pixels. */ CB_Mouse.getX = function(e, ignoreScroll, ignoreLock) { //If this function has not been called from an event, we return stored values: if (!e) { if (CB_Mouse.isLocked(false) &amp;&amp; !ignoreLock) { return CB_Mouse.getXMovement(e); } else { if (ignoreScroll) { return CB_Mouse._x; } else { return CB_Mouse._x + CB_Screen.getScrollLeft(); } } } var mouseX = 0; //If it is compatible with the W3C draft: if (typeof(e.x) !== "undefined") { mouseX = e.x; } //...otherwise, if we are using Internet Explorer: else if (typeof(e.clientX) !== "undefined") { mouseX = e.clientX;// + document.body.scrollLeft; } //...otherwise we don't use Internet Explorer: else if (typeof(e.pageX) !== "undefined") { //document.captureEvents(Event.MOUSEMOVE); mouseX = e.pageX; } //If the coordinate is lower than zero, it should be zero: if (mouseX &lt; 0) { mouseX = 0; } //We set the value for the property of the class: CB_Mouse._x = mouseX; //Updates the mose movement (useful when the mouse pointer is locked): CB_Mouse._xMovement = CB_Mouse.getXMovement(e); //If the mouse pointer is locked and we do not want to ignore it: if (CB_Mouse.isLocked(false) &amp;&amp; !ignoreLock) { return CB_Mouse._xMovement; } //...otherwise, returns the normal position instead of the movement: else { if (ignoreScroll) { return CB_Mouse._x; } else { return CB_Mouse._x + CB_Screen.getScrollLeft(); } } } /** * Gets and returns the Y coordinate (vertical position) of the mouse (relative to the window in desktop) in pixels. * @function * @param {Event} [e] - [Mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent}. If not provided, the returning value will use the previously-cached value (updated the last time that the [onMouseMove]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event} event was fired). * @param {boolean} [ignoreScroll=false] - If set to true, the vertical scroll position will not be added to the returning value. * @param {boolean} [ignoreLock=false] - If set to true, it will ignore whether the cursor is being locked or not. Otherwise, if set to false and the cursor is locked, the returning value will only have in mind the position in the locking element. * @returns {number} Returns the Y coordinate (vertical position) of the mouse (relative to the window in desktop) in pixels. */ CB_Mouse.getY = function(e, ignoreScroll, ignoreLock) { //If this function has not been called from an event, we return stored values: if (!e) { if (CB_Mouse.isLocked(false) &amp;&amp; !ignoreLock) { return CB_Mouse.getYMovement(e); } else { if (ignoreScroll) { return CB_Mouse._y; } else { return CB_Mouse._y + CB_Screen.getScrollTop(); } } } var mouseY = 0; //If it is compatible with the W3C draft: if (typeof(e.y) !== "undefined") { mouseY = e.y; } //...otherwise, if we are using Internet Explorer: else if (typeof(e.clientY) !== "undefined") { mouseY = e.clientY;// + document.body.scrollTop; } //...otherwise we don't use Internet Explorer: else if (typeof(e.pageY) !== "undefined") { //document.captureEvents(Event.MOUSEMOVE); mouseY = e.pageY; } //If the coordinate is lower than zero, it should be zero: if (mouseY &lt; 0) { mouseY = 0; } //We set the value for the property of the class: CB_Mouse._y = mouseY; //Updates the mose movement (useful when the mouse pointer is locked): CB_Mouse._yMovement = CB_Mouse.getYMovement(e); //If the mouse pointer is locked and we do not want to ignore it: if (CB_Mouse.isLocked(false) &amp;&amp; !ignoreLock) { return CB_Mouse._yMovement; } //...otherwise, returns the normal position instead of the movement: else { if (ignoreScroll) { return CB_Mouse._y; } else { return CB_Mouse._y + CB_Screen.getScrollTop(); } } } /** * Gets and returns the current X coordinate (horizontal position) in pixels of the mouse relative to a given X position. The returning value uses the previously-cached value (updated the last time that the [onMouseMove]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event} event was fired). * @function * @param {number} x - The X coordinate (horizontal position) in pixels. The returning value will be calculated relatively to it. * @param {boolean} [ignoreScroll=false] - If set to true, the horizontal scroll position will not be added to the returning value. * @returns {number} Returns the current X coordinate (horizontal position) in pixels of the mouse relative to a given X position. * @todo Think about allowing to define an "e" parameter with the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent}. */ CB_Mouse.getXRelative = function(x, ignoreScroll) { //Gets the X position of the mouse: var mouseX = CB_Mouse.getX(null, ignoreScroll); if (ignoreScroll) { x -= CB_Screen.getScrollLeft(); } var mouseRelativeX = mouseX - x; return mouseRelativeX; } /** * Gets and returns the current Y coordinate (vertical position) in pixels of the mouse relative to a given Y position. The returning value uses the previously-cached value (updated the last time that the [onMouseMove]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event} event was fired). * @function * @param {number} y - The Y coordinate (vertical position) in pixels. The returning value will be calculated relatively to it. * @param {boolean} [ignoreScroll=false] - If set to true, the vertical scroll position will not be added to the returning value. * @returns {number} Returns the current Y coordinate (vertical position) in pixels of the mouse relative to a given Y position. * @todo Think about allowing to define an "e" parameter with the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent}. */ CB_Mouse.getYRelative = function(y, ignoreScroll) { //Gets the Y position of the mouse: var mouseY = CB_Mouse.getY(null, ignoreScroll); if (ignoreScroll) { y -= CB_Screen.getScrollTop(); } var mouseRelativeY = mouseY - y; return mouseRelativeY; } /** * Returns the current X (horizontal) movement (useful when the mouse pointer is locked) in pixels. More information: [MouseEvent.movementX]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementX}. * @function * @param {Event} [e] - [Mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent}. If not provided, the returning value will use the previously-cached value (updated the last time that the [onMouseMove]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event} event was fired). * @returns {number} Returns the current X (horizontal) movement (useful when the mouse pointer is locked) in pixels. */ CB_Mouse.getXMovement = function(e) { //If this function has been called from an event, updates the current values: if (e) { CB_Mouse._xMovement = e.movementX || e.mozMovementX || e.webkitMovementX || 0; } return CB_Mouse._xMovement; } /** * Returns the current Y (vertical) movement (useful when the mouse pointer is locked) in pixels. More information: [MouseEvent.movementY]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/movementY}. * @function * @param {Event} [e] - [Mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent}. If not provided, the returning value will use the previously-cached value (updated the last time that the [onMouseMove]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event} event was fired). * @returns {number} Returns the current Y (vertical) movement (useful when the mouse pointer is locked) in pixels. */ CB_Mouse.getYMovement = function(e) { //If this function has been called from an event, updates the current values: if (e) { CB_Mouse._yMovement = e.movementY || e.mozMovementY || e.webkitMovementY || 0; } return CB_Mouse._yMovement; } /** * Sets a function to execute when a click happens ([onMouseMove]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onMove = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("mousemove", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when a click happens ([onClick]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onClick = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("click", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when a click happens ([onDblClick]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/dblclick_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onDblClick = CB_Mouse.onDoubleClick = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("dblclick", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when a mouse button is down ([onMouseDown]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousedown_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onButtonDown = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("mousedown", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when a mouse button is up ([onMouseUp]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseup_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onButtonUp = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("mouseup", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when a mouse leaves a [DOM element]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement} ([onMouseLeave]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseleave_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=window] - The target where we want to attach the event listener. */ CB_Mouse.onLeave = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("mouseleave", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when a mouse is over a [DOM element]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement} ([onMouseOver]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseover_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onOver = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("mouseover", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when a mouse gets out of a [DOM element]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement} ([onMouseOut]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseout_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onOut = function(callbackFunction, keepOldFunction, useCapture, target) { return CB_Mouse._setEvent("mouseout", callbackFunction, keepOldFunction, useCapture, target); } /** * Sets a function to execute when the mouse wheel is used ([onWheel]{@link https://developer.mozilla.org/en-US/docs/Web/Events/wheel}, [onMouseWheel]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/mousewheel_event} or [DOMMouseScroll]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/DOMMouseScroll_event} event) or removes it. * @function * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired. The first and unique parameter received for this function will be the [mouse event object]{@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent} (already normalized by the {@link CB_Mouse.normalizeEvent} function). If a null value is used, the event will be removed. * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not. * @param {boolean} [useCapture=false] - Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the [addEventListener]{@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener} method and will be used as its third parameter. * @param {Object} [target=document] - The target where we want to attach the event listener. */ CB_Mouse.onWheel = function(callbackFunction, keepOldFunction, useCapture, target) { var onWheelEventName = CB_Mouse._getOnWheelEventName(); var valueToReturn = CB_Mouse._setEvent(onWheelEventName, callbackFunction, keepOldFunction, useCapture, target); //If DOMMouseScroll is detected, adds MozMousePixelScroll event for older Firefox versions: if (onWheelEventName === "DOMMouseScroll") { valueToReturn = CB_Mouse._setEvent("MozMousePixelScroll", callbackFunction, keepOldFunction, useCapture, target); } return valueToReturn; } CB_Mouse.isLockSupportedReturnCache = null; /** * Tells whether mouse pointer lock is supported or not. More information: [Pointer Lock API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API}. * @function * @returns {boolean} Returns whether mouse pointer lock is supported or not. */ CB_Mouse.isLockSupported = function() { if (CB_Mouse.isLockSupportedReturnCache === null) { CB_Mouse.isLockSupportedReturnCache = "pointerLockElement" in document || "mozPointerLockElement" in document || "webkitPointerLockElement" in document || (navigator &amp;&amp; (navigator.pointer || navigator.webkitPointer || navigator.mozPointer)); } return CB_Mouse.isLockSupportedReturnCache; } /** * Tells whether the mouse pointer is locked or not. More information: [Pointer Lock API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API}. * @function * @param {boolean} [avoidCache=false] - Used as the parameter to call the {@link CB_Mouse.getLockElement} function internally. * @returns {boolean} Returns whether the mouse pointer is locked or not. */ CB_Mouse.isLocked = function(avoidCache) { CB_Mouse._isLockedPrevious = CB_Mouse._isLockedNow; var isLockedNow = (CB_Mouse.getLockElement(avoidCache) !== null); if (!isLockedNow &amp;&amp; navigator) { navigator.pointer = navigator.pointer || navigator.webkitPointer || navigator.mozPointer; if (navigator.pointer) { if (typeof(navigator.pointer.isLocked) === "function") { isLockedNow = navigator.pointer.isLocked(); } else if (typeof(navigator.pointer.islocked) === "function") { isLockedNow = navigator.pointer.islocked(); } else if (typeof(navigator.pointer.isLocked) !== "undefined") { isLockedNow = navigator.pointer.isLocked; } } } CB_Mouse._isLockedNow = isLockedNow; return CB_Mouse._isLockedNow; } /** * Tells whether the mouse pointer was locked before or not when the {@link CB_Mouse.isLocked} function was called the last time. More information: [Pointer Lock API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API}. * @function * @returns {boolean} Returns whether the mouse pointer was locked before or not when the {@link CB_Mouse.isLocked} function was called the last time. */ CB_Mouse.wasLocked = function() { return CB_Mouse._isLockedPrevious; } /** * Gets the lock element for the mouse pointer (if any) or null otherwise. More information: [Pointer Lock API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API}. * @function * @param {boolean} [avoidCache=false] - If set to false, the returning value will use the previously-cached value (updated when this function is called with this parameter set to true or the [onPointerLockChange]{@link https://developer.mozilla.org/en-US/docs/Web/API/Document/pointerlockchange_event} or analog even