files/en-us/web/api/xrframe/gethittestresults/index.md
{{APIRef("WebXR Device API")}}{{SeeCompatTable}}{{SecureContext_Header}}
The getHitTestResults() method of the {{domxref("XRFrame")}} interface returns an array of {{domxref("XRHitTestResult")}} objects containing hit test results for a given {{domxref("XRHitTestSource")}}.
getHitTestResults(hitTestSource)
hitTestSource
An array of {{domxref("XRHitTestResult")}} objects.
To request a hit test source, start an {{domxref("XRSession")}} with the hit-test session feature enabled. Next, request a the hit test source with {{domxref("XRSession.requestHitTestSource()")}} and store it for later use in the frame loop. Finally, call getHitTestResults() to obtain the result.
const xrSession = navigator.xr.requestSession("immersive-ar", {
requiredFeatures: ["local", "hit-test"],
});
let hitTestSource = null;
xrSession
.requestHitTestSource({
space: viewerSpace, // obtained from xrSession.requestReferenceSpace("viewer");
offsetRay: new XRRay({ y: 0.5 }),
})
.then((viewerHitTestSource) => {
hitTestSource = viewerHitTestSource;
});
// frame loop
function onXRFrame(time, xrFrame) {
let hitTestResults = xrFrame.getHitTestResults(hitTestSource);
// do things with the hit test results
}
{{Specifications}}
{{Compat}}