docs.rodeo

MDN Web Docs mirror

XRSession

{{APIRef("WebXR Device API")}} {{SecureContext_Header}} {{SeeCompatTable}} 

The XRSession interface of the WebXR Device API represents an ongoing XR session, providing methods and properties used to interact with and control the session. To open a WebXR session, use the {{domxref("XRSystem")}}  interface’s {{domxref("XRSystem.requestSession", "requestSession()")}}  method.

With XRSession methods, you can poll the viewer’s position and orientation (the {{domxref("XRViewerPose")}} ), gather information about the user’s environment, and present imagery to the user. XRSession supports both inline and immersive virtual and augmented reality modes.

{{InheritanceDiagram}} 

Instance properties

In addition to the properties listed below, XRSession inherits properties from its parent interface, {{domxref("EventTarget")}} .

Instance methods

XRSession provides the following methods in addition to those inherited from its parent interface, {{domxref("EventTarget")}} .

Events

The following events are delivered to XRSession objects.

Example

This example establishes a new XRSession in inline mode so that it can be displayed within an HTML element, avoiding the need for a dedicated AR or VR viewing device such as a headset.

const XR = navigator.xr;

if (XR) {
  XR.requestSession("inline").then((xrSession) => {
    xrSession.requestReferenceSpace("local").then((xrReferenceSpace) => {
      xrSession.requestAnimationFrame((time, xrFrame) => {
        const viewer = xrFrame.getViewerPose(xrReferenceSpace);

        gl.bindFramebuffer(xrWebGLLayer.framebuffer);

        for (const xrView of viewer.views) {
          const xrViewport = xrWebGLLayer.getViewport(xrView);
          gl.viewport(
            xrViewport.x,
            xrViewport.y,
            xrViewport.width,
            xrViewport.height,
          );
        }
      });
    });
  });
} else {
  /* WebXR is not available */
}

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN