XRSession
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The WebXR Device API's XRSession
interface represents an ongoing XR session, providing methods and properties used to interact with and control the session. To open a WebXR session, use the XRSystem
interface's requestSession()
method.
With XRSession
methods, you can poll the viewer's position and orientation (the 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.
Properties
In addition to the properties listed below, XRSession
inherits properties from its parent interface, EventTarget
.
-
depthDataFormat
Read only -
Returns the depth-sensing data format with which the session was configured.
-
depthUsage
Read only -
Returns the depth-sensing usage with which the session was configured.
-
domOverlayState
Read only -
Provides information about the DOM overlay, if the feature is enabled.
-
environmentBlendMode
Read only -
Returns this session's blend mode which denotes how much of the real-world environment is visible through the XR device and how the device will blend the device imagery with it.
-
inputSources
Read only -
Returns a list of this session's
XRInputSource
s, each representing an input device used to control the camera and/or scene. -
interactionMode
Read only -
Returns this session's interaction mode, which describes the best space (according to the user agent) for the application to draw interactive UI for the current session.
-
preferredReflectionFormat
Read only -
Returns this session's preferred reflection format used for lighting estimation texture data.
-
renderState
Read only -
An
XRRenderState
object which contains options affecting how the imagery is rendered. This includes things such as the near and far clipping planes (distances defining how close and how far away objects can be and still get rendered), as well as field of view information. -
visibilityState
Read only -
A string indicating whether or not the session's imagery is visible to the user, and if so, if it's being visible but not currently the target for user events.
Methods
XRSession
provides the following methods in addition to those inherited from its parent interface, EventTarget
.
cancelAnimationFrame()
-
Removes a callback from the animation frame painting callback from
XRSession
's set of animation frame rendering callbacks, given the identifying handle returned by a previous call torequestAnimationFrame()
. end()
-
Ends the WebXR session. Returns a
promise
which resolves when the session has been shut down. requestAnimationFrame()
-
Schedules the specified method to be called the next time the user agent is working on rendering an animation frame for the WebXR device. Returns an integer value which can be used to identify the request for the purposes of canceling the callback using
cancelAnimationFrame()
. This method is comparable to theWindow.requestAnimationFrame()
method. requestHitTestSource()
-
Requests an
XRHitTestSource
object that handles hit test subscription. requestHitTestSourceForTransientInput()
-
Requests an
XRTransientInputHitTestSource
object that handles hit test subscription for a transient input source. requestLightProbe()
-
Requests an
XRLightProbe
that estimates lighting information at a given point in the user's environment. requestReferenceSpace()
-
Requests that a new
XRReferenceSpace
of the specified type be created. Returns a promise which resolves with theXRReferenceSpace
orXRBoundedReferenceSpace
which was requested, or throws aNotSupportedError
DOMException
if the requested space type isn't supported by the device. updateRenderState()
-
Updates the properties of the session's render state.
Events
The following events are delivered to XRSession
objects.
end
-
Sent to the
XRSession
object after the WebXR session has ended and all hardware-related functions have completed. The event is represented by an object of typeXRSessionEvent
. Also available through theonend
event handler property. inputsourceschange
-
An event of type
XRInputSourcesChangeEvent
sent to theXRSession
when the list of active XR input sources has changed. Also available through theoninputsourceschange
event handler property. select
-
An event of type
XRInputSourceEvent
which is sent to the session when one of the session's input sources has successfully completed a primary action. This generally corresponds to the user pressing a trigger, touchpad, or button, speaks a command, or performs a recognizable gesture. Theselect
event is sent after theselectstart
event is sent and immediately before theselectend
event is sent. Ifselect
is not sent, then the select action was aborted before being completed. Also available through theonselect
event handler property. selectend
-
An event of type
XRInputSourceEvent
which gets sent to the session object when one of its input devices finishes its primary action or gets disconnected while in the process of handling a primary action. For example: for button or trigger actions, this means the button has been released; for spoken commands, it means the user has finished speaking. This is the last of the threeselect*
events to be sent. Also available through theonselectend
event handler property. selectstart
-
An event of type
XRInputSourceEvent
which is sent to the session object when one of its input devices is first engaged by the user in such a way as to cause the primary action to begin. This is the first of thesession*
event to be sent. Also available through theonselectstart
event handler property. squeeze
-
An
XRInputSourceEvent
sent to indicate that a primary squeeze action has successfully completed. This indicates that the device being squeezed has been released, and may represent dropping a grabbed object, for example. It is sent immediately before thesqueezeend
event is sent to indicate that the squeeze action is over. Also available through theonsqueeze
event handler property. squeezeend
-
An
XRInputSourceEvent
sent to theXRSession
when the primary squeeze action ends, whether or not the action was successful. Also available using theonsqueezeend
event handler property. squeezestart
-
An event of type
XRInputSourceEvent
which is sent to theXRSession
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 theonsqueezestart
event handler property. visibilitychange
-
An
XRSessionEvent
which is sent to the session when its visibility state as indicated by thevisibilityState
changes. Also available through theonvisibilitychange
event handler property.
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) => { let viewer = xrFrame.getViewerPose(xrReferenceSpace); gl.bindFramebuffer(xrWebGLLayer.framebuffer); for (xrView of viewer.views) { let xrViewport = xrWebGLLayer.getViewport(xrView); gl.viewport(xrViewport.x, xrViewport.y, xrViewport.width, xrViewport.height); } }); }); }); } else { /* WebXR is not available */ }
Specifications
Specification |
---|
WebXR Device API # xrsession-interface |
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 | |
XRSession |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
cancelAnimationFrame |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
depthDataFormat |
90 |
90 |
No |
No |
No |
No |
No |
90 |
No |
No |
No |
No |
depthUsage |
90 |
90 |
No |
No |
No |
No |
No |
90 |
No |
No |
No |
No |
domOverlayState |
83 |
83 |
No |
No |
No |
No |
No |
83 |
No |
No |
No |
No |
end |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
end_event |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
environmentBlendMode |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
inputSources |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
inputsourceschange_event |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
interactionMode |
84 |
84 |
No |
No |
No |
No |
No |
84 |
No |
No |
No |
No |
onend |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
oninputsourceschange |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
onselect |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
onselectend |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
onselectstart |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
onsqueeze |
83 |
83 |
No |
No |
No |
No |
No |
83 |
No |
No |
No |
13.0 |
onsqueezeend |
83 |
83 |
No |
No |
No |
No |
No |
83 |
No |
No |
No |
13.0 |
onsqueezestart |
83 |
83 |
No |
No |
No |
No |
No |
83 |
No |
No |
No |
13.0 |
onvisibilitychange |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
preferredReflectionFormat |
90 |
90 |
No |
No |
No |
No |
No |
90 |
No |
No |
No |
No |
renderState |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
requestAnimationFrame |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
requestHitTestSource |
81 |
81 |
No |
No |
No |
No |
No |
81 |
No |
No |
No |
12.1 |
requestHitTestSourceForTransientInput |
81 |
81 |
No |
No |
No |
No |
No |
81 |
No |
No |
No |
13.0 |
requestLightProbe |
90 |
90 |
No |
No |
No |
No |
No |
90 |
No |
No |
No |
No |
requestReferenceSpace |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
select_event |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
selectend_event |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
selectstart_event |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
squeeze_event |
83 |
83 |
No |
No |
No |
No |
No |
83 |
No |
No |
No |
13.0 |
squeezeend_event |
83 |
83 |
No |
No |
No |
No |
No |
83 |
No |
No |
No |
13.0 |
squeezestart_event |
83 |
83 |
No |
No |
No |
No |
No |
83 |
No |
No |
No |
13.0 |
updateRenderState |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
visibilitychange_event |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
visibilityState |
79 |
79 |
No |
No |
No |
No |
No |
79 |
No |
No |
No |
11.2 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XRSession