This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The WebXR Device API's XRViewport interface provides properties used to describe the size and position of the current viewport within the XRWebGLLayer being used to render the 3D scene.
height Read only
The height, in pixels, of the viewport.
width Read only
The width, in pixels, of the viewport.
x Read only
The offset from the origin of the destination graphics surface (typically a XRWebGLLayer) to the left edge of the viewport, in pixels.
y Read only
The offset from the origin of the viewport to the bottom edge of the viewport; WebGL's coordinate system places (0, 0) at the bottom left corner of the surface.
Currently, the only type of surface available is the XRWebGLLayer. The precise orientation of the coordinate system may vary with other surface types, but in WebGL, the origin (0, 0) is located at the bottom-left corner of the surface. Thus the values specified in an XRViewport define a rectangle whose bottom-left corner is at (x, y) and which extends width pixels toward the left and height pixels upward.
These values may be passed directly into the WebGLRenderingContext.viewport() method:
const xrViewport = xrWebGLLayer.getViewport(xrView); gl.viewport(xrViewport.x, xrViewport.y, xrViewport.width, xrViewport.height);
This example sets up an animation frame callback using requestAnimationFrame(). After initial setup, it iterates over each of the views within the viewer's pose, configuring the viewport as dictated by the XRWebGLLayer.
xrSession.requestAnimationFrame((time, xrFrame) => {
const viewerPose = xrFrame.getViewerPose(xrReferenceSpace);
gl.bindFramebuffer(xrWebGLLayer.framebuffer);
for (const xrView of viewerPose.views) {
const xrViewport = xrWebGLLayer.getViewport(xrView);
gl.viewport(
xrViewport.x,
xrViewport.y,
xrViewport.width,
xrViewport.height,
);
// Now we can use WebGL to draw into a viewport matching
// the viewer's needs
}
});
| Specification |
|---|
| WebXR Device API> # xrviewport-interface> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
XRViewport |
79 | 79 | No | 66 | No | 79 | No | 57 | No | 11.2 | No | No |
height |
79 | 79 | No | 66 | No | 79 | No | 57 | No | 11.2 | No | No |
width |
79 | 79 | No | 66 | No | 79 | No | 57 | No | 11.2 | No | No |
x |
79 | 79 | No | 66 | No | 79 | No | 57 | No | 11.2 | No | No |
y |
79 | 79 | No | 66 | No | 79 | No | 57 | No | 11.2 | No | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XRViewport