Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Last active February 22, 2022 10:40
Show Gist options
  • Save ayamflow/ff298e87595bfc0812438457aa55c0d4 to your computer and use it in GitHub Desktop.
Save ayamflow/ff298e87595bfc0812438457aa55c0d4 to your computer and use it in GitHub Desktop.
webXR mouse/touch events
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