files/en-us/web/api/xrsession/requesthittestsource/index.md
{{APIRef("WebXR Device API")}}{{SeeCompatTable}}{{SecureContext_Header}}
The requestHitTestSource() method of the
{{domxref("XRSession")}} interface returns a {{jsxref("Promise")}} that resolves with an {{domxref("XRHitTestSource")}} object that can be passed to {{domxref("XRFrame.getHitTestResults()")}}.
requestHitTestSource(options)
options
space
entityTypes {{Optional_Inline}}
plane type. Possible types:
point: Compute hit test results based on characteristic points detected.plane: Compute hit test results based on real-world planes detected.mesh: Compute hit test results based on meshes detected.offsetRay {{Optional_Inline}}
XRRay object has been provided, a new XRRay object is constructed without any parameters.A {{jsxref("Promise")}} that resolves with an {{domxref("XRHitTestSource")}} object.
Rather than throwing true exceptions, requestHitTestSource() rejects the
returned promise with a {{domxref("DOMException")}}, specifically, one of the following:
NotSupportedError {{domxref("DOMException")}}
hit-test is not an enabled feature in {{domxref("XRSystem.requestSession()")}}.InvalidStateError {{domxref("DOMException")}}
NotAllowedError {{domxref("DOMException")}}
To request a hit test source, start an {{domxref("XRSession")}} with the hit-test session feature enabled. Next, configure the hit test source and store it for later use in the frame loop and call {{domxref("XRFrame.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}}