此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

WebGLRenderingContext.getContextAttributes()

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

WebGLRenderingContext.getContextAttributes() 方法返回一个包含实际上下文参数的 WebGLContextAttributes 对象。如果上下文丢失,可能返回 null

语法

gl.getContextAttributes();

返回值

一个包含实际上下文参数的 WebGLContextAttributes 的对象,或 null(如果上下文丢失)。

实例

给定 <canvas> 元素

html
<canvas id="canvas"></canvas>

和给定 WebGL 上下文

js
var canvas = document.getElementById("canvas");
var gl = canvas.getContext("webgl");
gl.getContextAttributes();

getContextAttributes 方法返回描述在此上下文中设置的属性的对象,例如:

js
{
  alpha: true,
  antialias: true,
  depth: true,
  failIfMajorPerformanceCaveat: false,
  premultipliedAlpha: true,
  preserveDrawingBuffer: false,
  stencil: false
}

上下文的属性可以在用 HTMLCanvasElement.getContext() 方法创建上下文时设置:

js
canvas.getContext("webgl", { antialias: false, depth: false });

有关各个属性的更多信息,请参阅 getContext()

规范

规范
WebGL Specification
# 5.14.2

浏览器兼容性

参见