Last active
January 25, 2019 14:16
-
-
Save dghez/1cbe6745c3a64244bcf8de8381ce427b to your computer and use it in GitHub Desktop.
Different values from mouse position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const windowSizes = { | |
| w: document.body.clientWidth, | |
| h: window.innerHeight, | |
| } | |
| // pixels pos | |
| const mousePosPixels = { | |
| x: 0, | |
| y: 0, | |
| } | |
| // % viewport | |
| const mousePosPerc = { | |
| x: 0, | |
| y: 0, | |
| } | |
| // normalized for quad-UV | |
| const mouseUvCentered = { | |
| x: 0, | |
| y: 0, | |
| } | |
| window.addEventListener('mousemove', (evt) => { | |
| // PIXELS (top,left) | |
| mousePosPixels.x = evt.clientX | |
| mousePosPixels.y = evt.clientY | |
| // PERCENTUAGE (top,left) | |
| mousePosPerc.x = evt.clientX / windowSizes.w | |
| mousePosPerc.y = evt.clientY / windowSizes.h | |
| // UV RAYCAST | |
| mouseUvCentered.x = (evt.clientX / window.innerWidth) * 2 - 1 | |
| mouseUvCentered.y = -(evt.clientY / window.innerHeight) * 2 + 1 | |
| }) | |
| window.addEventListener('resize', (evt) => { | |
| windowSizes.w = document.body.clientWidth | |
| windowSizes.h = window.innerHeight | |
| }) | |
| export { mousePosPixels, mousePosPerc, mouseUvCentered } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment