WebVR proporciona soporte para la exposición de dispositivos de realidad virtual — por ejemplo el visualizador de Oculus Rift o HTC Vive — para aplicaciones web, permitiendo a los desarrolladores traducir la información de posición y movimiento del visualizador al movimiento alrededor de una escena en 3D. Esto tiene numerosas aplicaciones interesantes, desde visitas virtuales de productos y aplicaciones de capacitación interactiva hasta juegos inmersivos en primera persona.
VRDisplay (en-US) is the central interface in the WebVR API — via its properties and methods you can access functionality to:
- Retrieve useful information to allow us to identify the display, what capabilities it has, controllers associated with it, and more.
frame data(en-US) for each frame of content you you want to present in a display, and submit those frames for display at a consistent rate.
- Start and stop presenting to the display.
A typical (simple) WebVR app would work like so:
Navigator.getVRDisplays()(en-US) is used to get a reference to your VR display.
VRDisplay.requestPresent()(en-US) is used to start presenting to the VR display.
- WebVR's dedicated
VRDisplay.requestAnimationFrame()(en-US) method is used to run the app's rendering loop at the correct refresh rate for the display.
- Inside the rendering loop, you grab the data required to display the current frame (
VRDisplay.getFrameData()(en-US)), 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 (
In addition, WebVR 1.1 adds a number of events on the
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.
- Represents any VR device supported by this API. It includes generic information such as device IDs and descriptions, as well as methods for starting to present a VR scene, retrieving eye parameters and display capabilities, and other important functionality.
- Describes the capabilities of a
VRDisplay(en-US) — it's features can be used to perform VR device capability tests, for example can it return position information.
- Represents the event object of WebVR-related events (see the window object extensions listed below).
- Represents all the information needed to render a single frame of a VR scene; constructed by
- Represents the position state at a given timestamp (which includes orientation, position, velocity, and acceleration.)
- Provides access to all the information required to correctly render a scene for each given eye, including field of view information.
- Represents a field of view defined by 4 different degree values describing the view from a center point.
- Represents a layer to be presented in a
- Represents the values describing the the stage area for devices that support room-scale experiences.
The WebVR API extends the following APIs, adding the listed features.
Gamepad.displayId(en-US) Read only
- Returns the
VRDisplay.displayId(en-US) of the associated
VRDisplay(en-US) — the
VRDisplaythat the gamepad is controlling the displayed scene of.
- Returns an array containing every
VRDisplay(en-US) object that is currently presenting (
- Returns a promise that resolves to an array of
VRDisplay(en-US) objects representing any available VR displays connected to the computer.
- Represents an event handler that will run when the presenting state of a VR display changes — i.e. goes from presenting to not presenting, or vice versa (when the
- Represents an event handler that will run when a compatible VR display has been connected to the computer (when the
- Represents an event handler that will run when a compatible VR display has been disconnected from the computer (when the
- Represents an event handler that will run when a display is able to be presented to (when the
vrdisplayactivateevent fires), for example if an HMD has been moved to bring it out of standby, or woken up by being put on.
- Represents an event handler that will run when a display can no longer be presented to (when the
vrdisplaydeactivateevent fires), for example if an HMD has gone into standby or sleep mode due to a period of inactivity.
Unimplemented window events
The following events are listed in the spec, but do not currently seem to be implemented anywhere as yet.
- Represents an event handler that will run when presentation to a display has been paused for some reason by the browser, OS, or VR hardware (when the
vrdisplayblurevent fires) — for example, while the user is interacting with a system menu or browser, to prevent tracking or loss of experience.
- Represents an event handler that will run when presentation to a display has resumed after being blurred (when the
You can find a number of examples at these locations:
- webvr-tests — very simple examples to accompany the MDN WebVR documentation.
- Carmel starter kit — nice simple, well-commented examples that go along with Carmel, Facebook's WebVR browser.
- WebVR.info samples — slightly more in-depth examples plus source code
- WebVR.rocks Firefox demos — showcase examples
- A-Frame homepage — examples showing A-Frame usage
BCD tables only load in the browser
- vr.mozilla.org — The main Mozilla landing pad for WebVR, with demos, utilities, and other information.
- A-Frame — Open source web framework for building VR experiences.
- webvr.info — Up-to-date information about WebVR, browser setup, and community.
- MozVr.com — Demos, downloads, and other resources from the Mozilla VR team.
- threejs-vr-boilerplate — A useful starter template for writing WebVR apps into.