Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. XRSystem

XRSystem

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.

TheWebXR Device API interfaceXRSystem provides methods which let you get access to anXRSession object representing a WebXR session. With thatXRSession in hand, you can use it to interact with the Augmented Reality (AR) or Virtual Reality (VR) device.

EventTarget XRSystem

Instance properties

WhileXRSystem directly offers no properties, it does inherit properties from its parent interface,EventTarget.

Instance methods

In addition to inheriting methods from its parent interface,EventTarget, theXRSystem interface includes the following methods:

isSessionSupported()Experimental

Returns a promise which resolves totrue if the browser supports the given session mode.Resolves tofalse if the specified mode isn't supported.

requestSession()Experimental

Returns a promise that resolves to a newXRSession with the specified session mode.

Events

devicechangeExperimental

Sent when the set of available XR devices has changed.Also available using theondevicechange event handler.

Usage notes

This interface was previously known asXR in earlier versions of the specification; if you see references toXR in code or documentation, replace that withXRSystem.

Examples

The following example shows how to use bothisSessionSupported() andrequestSession().

js
if (navigator.xr) {  immersiveButton.addEventListener("click", onButtonClicked);  navigator.xr.isSessionSupported("immersive-vr").then((isSupported) => {    immersiveButton.disabled = !isSupported;  });}function onButtonClicked() {  if (!xrSession) {    navigator.xr.requestSession("immersive-vr").then((session) => {      // onSessionStarted() not shown for reasons of brevity and clarity.      onSessionStarted(session);    });  } else {    // Shut down the already running XRSession    xrSession.end().then(() => {      // Since there are cases where the end event is not sent, call the handler here as well.      onSessionEnded();    });  }}

This code starts by checking to see if WebXR is available by looking for thenavigator.xr property. If it's found, we know WebXR is present, so we proceed by establishing a handler for the button which the user can click to toggle immersive VR mode on and off.

However, we don't yet know if the desired immersive mode is available. To determine this, we callisSessionSupported(), passing it the desired session option before enabling the button,immersiveButton, which the user can then use to switch to immersive mode only if immersive VR mode is available. If immersive VR isn't available, the button is disabled to prevent its use.

TheonButtonClicked() function checks to see if there's already a session running. If there isn't, we userequestSession() to start one and, once the returned promise resolves, we call a functiononSessionStarted() to set up our session for rendering and so forth.

If, on the other hand, there is already an ongoing XR session, we instead callend() to end the current session. When the current session ends, theend event is sent, so setxrSession tonull in its handler to record the fact that we no longer have an ongoing session. That way, if the user clicks the button again, a new session will start.

Specifications

Specification
WebXR Device API
# xrsystem-interface

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp