files/en-us/web/api/xrsession/index.md
{{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}}
In addition to the properties listed below, XRSession inherits properties from its parent interface, {{domxref("EventTarget")}}.
XRSession provides the following methods in addition to those inherited from its parent interface, {{domxref("EventTarget")}}.
XRSession's set of animation frame rendering callbacks, given the identifying handle returned by a previous call to {{domxref("XRSession.requestAnimationFrame", "requestAnimationFrame()")}}.cancelAnimationFrame(). This method is comparable to the {{domxref("Window.requestAnimationFrame()")}} method.XRReferenceSpace or {{domxref("XRBoundedReferenceSpace")}} which was requested, or throws a NotSupportedError {{domxref("DOMException")}} if the requested space type isn't supported by the device.The following events are delivered to XRSession objects.
XRSession object after the WebXR session has ended and all hardware-related functions have completed. The event is represented by an object of type {{domxref("XRSessionEvent")}}. Also available through the onend event handler property.XRSession when the list of active XR input sources has changed. Also available through the oninputsourceschange event handler property.select event is sent after the selectstart event is sent and immediately before the selectend event is sent. If select is not sent, then the select action was aborted before being completed. Also available through the onselect event handler property.select* events to be sent. Also available through the onselectend event handler property.session* event to be sent. Also available through the onselectstart event handler property.squeezeend event is sent to indicate that the squeeze action is over. Also available through the onsqueeze event handler property.XRSession when the primary squeeze action ends, whether or not the action was successful. Also available using the onsqueezeend event handler property.XRSession when the user initially squeezes a squeezable controller. This may be, for example, a trigger which is used to represent grabbing objects, or might represent actual squeezing when wearing a haptic glove. Also available through the onsqueezestart event handler property.onvisibilitychange event handler property.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}}
{{Compat}}