Last active
February 22, 2022 10:40
-
-
Save ayamflow/ff298e87595bfc0812438457aa55c0d4 to your computer and use it in GitHub Desktop.
webXR mouse/touch events
This file contains 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
let session = await navigator.xr.requestSession('immersive-ar'); | |
let rafId = null; | |
session.addEventListener('selectstart', event => { | |
inputSource = event.inputSource | |
onStart(getEvent()); | |
rafId = requestAnimationFrame(updateInput); | |
}); | |
session.addEventListener('selectend', event => { | |
requestAnimationFrame.cancel(rafId); | |
onEnd(getEvent()); | |
inputSource = null; | |
}); | |
function getEvent() { | |
if (!inputSource) return; | |
let axes = inputSource.gamepad.axes; | |
let x = Math.range(axes[0], -1, 1, 0, window.innerWidth); | |
let y = Math.range(axes[1], -1, 1, 0, window.innerHeight); | |
return {x, y}; | |
} | |
function updateInput() { | |
rafId = requestAnimationFrame(updateInput); | |
onMove(getEvent()); | |
} | |
function onStart({x, y}) { | |
... | |
} | |
function onMove({x, y}) {} | |
function onEnd({x, y}) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment