UNPKG

p5.raycaster

Version:

a simple p5js library for semi 3d rendering with ray casting

141 lines (110 loc) 4.98 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: PointerLockControl.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: PointerLockControl.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>import Camera from "./Camera"; class PointerLockControl { /** * * @param {Camera} camera the camera this controller control * @param {HTMLElement} ele the element this controller bind to */ constructor(camera, ele) { this.camera = camera; this.targetElement = ele; this.pointerSpeed = 0.002; this.pointerLocked = false; this.mouseIsDown = false; this.mouseButton = -1; this.invertedX = false; this.invertedY = false; this.switchXY = false; this.regControl(); } regControl() { this.targetElement.ownerDocument.addEventListener("mousemove", this.mouseMove.bind(this)); this.targetElement.ownerDocument.addEventListener("pointerlockchange", this.pointerLockChange.bind(this)); this.targetElement.ownerDocument.addEventListener("pointerlockerror", this.pointerLockError.bind(this)); this.targetElement.ownerDocument.addEventListener("mouseup", this.mouseUp.bind(this)); this.targetElement.ownerDocument.addEventListener("mousedown", this.mouseDown.bind(this)); } removeControl() { this.targetElement.ownerDocument.removeEventListener("mousemove", this.mouseMove.bind(this)); this.targetElement.ownerDocument.removeEventListener("pointerlockchange", this.pointerLockChange.bind(this)); this.targetElement.ownerDocument.removeEventListener("pointerlockerror", this.pointerLockError.bind(this)); this.targetElement.ownerDocument.removeEventListener("mouseup", this.mouseUp.bind(this)); this.targetElement.ownerDocument.removeEventListener("mousedown", this.mouseDown.bind(this)); } lock() { this.targetElement.requestPointerLock(); } unlock() { this.targetElement.ownerDocument.exitPointerLock(); } /** * * @param {number} speed */ setPointerSpeed(speed) { this.pointerSpeed = speed; } pointerLockChange() { if (this.targetElement.ownerDocument.pointerLockElement === this.targetElement) { this.pointerLocked = true; } else { this.pointerLocked = false; } } pointerLockError() { console.error("pointerLock unavailable"); } /** * * @param {MouseEvent} ev * */ mouseMove(ev) { if (!this.pointerLocked) return; const camera = this.camera; const mx = ev.movementX * (this.invertedX ? 1 : -1); const my = ev.movementY * (this.invertedY ? 1 : -1); camera.rotate((this.switchXY ? my : mx) * this.pointerSpeed); camera.tilt((this.switchXY ? mx : my) * this.pointerSpeed); } mouseDown(ev) { this.mouseIsDown = true; this.mouseButton = ev.button; } mouseUp(ev) { this.mouseIsDown = false; } } export default PointerLockControl;</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Camera.html">Camera</a></li><li><a href="KeyboardControl.html">KeyboardControl</a></li><li><a href="MouseControl.html">MouseControl</a></li><li><a href="PointerLockControl.html">PointerLockControl</a></li><li><a href="Sprite.html">Sprite</a></li><li><a href="TransparentWall.html">TransparentWall</a></li><li><a href="Util.html">Util</a></li><li><a href="World.html">World</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createCamera">createCamera</a></li><li><a href="global.html#createSkyBox">createSkyBox</a></li><li><a href="global.html#createSprite">createSprite</a></li><li><a href="global.html#createTextureMap">createTextureMap</a></li><li><a href="global.html#createWorld">createWorld</a></li><li><a href="global.html#initKeyboardControl">initKeyboardControl</a></li><li><a href="global.html#initMouseControl">initMouseControl</a></li><li><a href="global.html#initPointerLockControl">initPointerLockControl</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Feb 01 2024 12:26:09 GMT+0000 (Greenwich Mean Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>