XRHitTestSource
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
TheXRHitTestSource interface of theWebXR Device API handles hit test subscriptions. You can get anXRHitTestSource object by using theXRSession.requestHitTestSource() method.
This object doesn't itself contain hit test results, but it is used to compute hit tests for eachXRFrame by callingXRFrame.getHitTestResults(), which returnsXRHitTestResult objects.
In this article
Instance properties
None.
Instance methods
XRHitTestSource.cancel()ExperimentalUnsubscribes from the hit test.
Examples
>Getting anXRHitTestSource object for a session
CallXRSession.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 loopfunction 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, callXRHitTestSource.cancel(). Since the object will no longer be usable, you can clean up and set theXRHitTestSource object tonull.
hitTestSource.cancel();hitTestSource = null;Specifications
| Specification |
|---|
| WebXR Hit Test Module> # hit-test-source-interface> |