This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
WebVR provides support for exposing virtual reality devices — for example head-mounted displays like the Oculus Rift or HTC Vive — to web apps, enabling developers to translate position and movement information from the display into movement around a 3D scene. This has numerous, interesting applications, from virtual product tours and interactive training apps to immersive first person games.
Any VR devices attached to your computer will be returned by the Navigator.getVRDisplays()
method; each one will be represented by a VRDisplay
object.
VRDisplay
is the central interface in the WebVR API — via its properties and methods you can access functionality to:
frame data
for each frame of content you you want to present in a display, and submit those frames for display at a consistent rate.A typical (simple) WebVR app would work like so:
Navigator.getVRDisplays()
is used to get a reference to your VR display.VRDisplay.requestPresent()
is used to start presenting to the VR display.VRDisplay.requestAnimationFrame()
method is used to run the app's rendering loop at the correct refresh rate for the display.VRDisplay.getFrameData()
), draw the displayed scene twice — once for the view in each eye, then submit the rendered view to the display to show to the user (VRDisplay.submitFrame()
).In addition, WebVR 1.1 adds a number of events on the Window
object to allow JavaScript to respond to changes to the status of the display.
Note: You can find a lot more out about how the API works in our Using the WebVR API and WebVR Concepts articles.
Many WebVR hardware setups feature controllers that go along with the headset. These can be used in WebVR apps via the Gamepad API, and specifically the Gamepad Extensions API that adds API features for accessing controller pose, haptic actuators, and more.
Note: Our Using VR controllers with WebVR article explains the basics of how to use VR controllers with WebVR apps.
VRDisplay
VRDisplayCapabilities
VRDisplay
— it's features can be used to perform VR device capability tests, for example can it return position information.VRDisplayEvent
VRFrameData
VRDisplay.getFrameData()
.VRPose
VREyeParameters
VRFieldOfView
VRLayerInit
VRDisplay
.VRStageParameters
The WebVR API extends the following APIs, adding the listed features.
Gamepad.displayId
Read only
VRDisplay.displayId
of the associated VRDisplay
— the VRDisplay
that the gamepad is controlling the displayed scene of.Navigator.activeVRDisplays
Read only
VRDisplay
object that is currently presenting (VRDisplay.ispresenting
is true
).Navigator.getVRDisplays()
VRDisplay
objects representing any available VR displays connected to the computer.Window.onvrdisplaypresentchange
vrdisplaypresentchange
event fires).Window.onvrdisplayconnect
vrdisplayconnect
event fires).Window.onvrdisplaydisconnect
vrdisplaydisconnect
event fires).Window.onvrdisplayactivate
vrdisplayactivate
event fires), for example if an HMD has been moved to bring it out of standby, or woken up by being put on.Window.onvrdisplaydeactivate
vrdisplaydeactivate
event fires), for example if an HMD has gone into standby or sleep mode due to a period of inactivity.The following events are listed in the spec, but do not currently seem to be implemented anywhere as yet.
Window.onvrdisplayblur
vrdisplayblur
event fires) — for example, while the user is interacting with a system menu or browser, to prevent tracking or loss of experience.Window.onvrdisplayfocus
vrdisplayfocus
event fires).You can find a number of examples at these locations:
Specification | Status | Comment |
---|---|---|
Gamepad Extensions | Editor's Draft | Defines the Experimental Gamepad extensions. |
WebVR 1.1 | Draft | Initial definition |
Navigator.getVRDisplays
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | No
|
Yes | 55
|
No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes
|
No | 55 | No | No | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebVR_API