web api interface
XRHitTestSource
Secure contextLimited availability
The XRHitTestSource interface of the WebXR Device API handles hit test subscriptions. You can get an XRHitTestSource object by using the requestHitTestSource() method.
This object doesn’t itself contain hit test results, but it is used to compute hit tests for each XRFrame by calling getHitTestResults(), which returns XRHitTestResult objects.
Instance properties
None.
Instance methods
cancel()Experimental- : Unsubscribes from the hit test.
Examples
Getting an XRHitTestSource object for a session
Call requestHitTestSource() to get a hit test source.
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
}
Unsubscribe from hit test
To unsubscribe from a hit test source, call cancel(). Since the object will no longer be usable, you can clean up and set the XRHitTestSource object to null.
hitTestSource.cancel();
hitTestSource = null;
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.