A
devicechange
event is fired on an
XRSystem
object whenever the whenever the availability of immersive XR devices has changed; for example, a VR headset or AR goggles have been connected or disconnected.
It's a generic
事件
with no added properties.
注意:
Not to be confused with the
MediaDevices
devicechange
事件。
| 冒泡 | No |
|---|---|
| 可取消 | No |
| 接口 |
事件
|
| Event handler |
XRSystem.ondevicechange
|
devicechange
events are not delivered if the document which owns the
XRSystem
object has been granted permission to do so through the
xr-spatial-tracking
feature policy.
You can use this event to, for example, monitor for the availability of a WebXR-compatible device so that you can enable a UI element which the user can use to activate immersive mode. This is shown in the 范例 下文。
The example shown here handles the
devicechange
event by toggling the availability of the "Enter XR" button based on whether or not any immersive devices are currently available.
if (navigator.xr) {
navigator.xr.addEventListener("devicechange", event => {
navigator.xr.isSessionSupported("immersive-vr")
.then(immersiveOK) => {
if (immersiveOK) {
enableXRButton.disabled = false;
} else {
enableXRButton.disabled = true;
}
});
});
}
当
devicechange
is received, the handler set up in this code calls the
XR
方法
isSessionSupported()
to find out if there's a device available that can handle immersive VR presentations. If there is, the button to enter XR mode is enabled; otherwise it's disabled.
还可以使用
ondevicechange
event handler property to set a single handler for
devicechange
事件:
if (navigator.xr) {
navigator.xr.ondevicechange = event => {
/* ... etc ... */
};
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebXR 设备 API
The definition of 'devicechange event' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
devicechange
event
|
Chrome 79 | Edge 79 | Firefox No | IE No | Opera No | Safari No | WebView Android No | Chrome Android 79 | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android 11.2 |
完整支持
不支持
实验。期望将来行为有所改变。
XRSystem
Navigator.xr
WebGLRenderingContext.makeXRCompatible()
XR
XRBoundedReferenceSpace
XRFrame
XRInputSource
XRInputSourceArray
XRInputSourceEvent
XRInputSourcesChangeEvent
XRPose
XRReferenceSpace
XRReferenceSpaceEvent
XRRenderState
XRRigidTransform
XRSession
XRSessionEvent
XRSpace
XRView
XRViewerPose
XRViewport
XRWebGLLayer