WebGLRenderingContext.viewport()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
WebGL API 的 WebGLRenderingContext.viewport()
方法,用来设置视口,即指定从标准设备到窗口坐标的 x、y 仿射变换。
语法
void gl.viewport(x, y, width, height);
参数
返回值
None.
异常错误
只要宽度或高度有一个为负值,gl.INVALID_VALUE
错误将被抛出。
样例
当你第一次创建 WebGL 上下文的时候,视口的大小将和 canvas
的大小是匹配的。然而,如果你重新改变了 canvas
的大小,你需要告诉 WebGL 上下文设定新的视口。在这里,你可以使用 gl.viewport
。
js
gl.viewport(0, 0, canvas.width, canvas.height);
视口的宽度和高度的设定范围是依赖于底层如何实现的。如果你要获取这个范围,你可以查询MAX_VIEWPORT_DIMS
常量,它将返回 Int32Array
。
js
gl.getParameter(gl.MAX_VIEWPORT_DIMS);
// e.g. Int32Array[16384, 16384]
如果要获取当前的视口,则可以查询VIEWPORT
常量。
js
gl.getParameter(gl.VIEWPORT);
// e.g. Int32Array[0, 0, 640, 480]
规范
Specification |
---|
WebGL Specification # 5.14.4 |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
viewport |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.