The XRHitTestSource interface of the WebXR Device API handles hit test subscriptions. You can get an XRHitTestSource object by using the XRSession.requestHitTestSource() method.
This object doesn't itself contain hit test results, but it is used to compute hit tests for each XRFrame by calling XRFrame.getHitTestResults(), which returns XRHitTestResult objects.
Instance properties
Instance methods
XRHitTestSource.cancel() Experimental
-
Unsubscribes from the hit test.
Examples
Getting an XRHitTestSource object for a session
Call XRSession.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,
offsetRay: new XRRay({ y: 0.5 }),
})
.then((viewerHitTestSource) => {
hitTestSource = viewerHitTestSource;
});
function onXRFrame(time, xrFrame) {
let hitTestResults = xrFrame.getHitTestResults(hitTestSource);
}
Unsubscribe from hit test
To unsubscribe from a hit test source, call XRHitTestSource.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
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
XRHitTestSource |
81 |
81 |
No |
No |
68 |
No |
No |
81 |
No |
58 |
No |
13.0 |
cancel |
81 |
81 |
No |
No |
68 |
No |
No |
81 |
No |
58 |
No |
13.0 |
See also