WebGLRenderingContext: getContextAttributes() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

注意:此功能在 Web Workers 中可用。

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

语法

js
getContextAttributes()

参数

无。

返回值

一个 WebGLContextAttributes 对象,其中包含实际的上下文参数,或者在上下文丢失时返回 null

示例

给定此 <canvas> 元素

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

并给定此 WebGL 上下文

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

getContextAttributes 方法返回一个对象,该对象描述了设置在此上下文上的属性,例如

json
{
  "alpha": true,
  "antialias": true,
  "depth": true,
  "failIfMajorPerformanceCaveat": false,
  "powerPreference": "default",
  "premultipliedAlpha": true,
  "preserveDrawingBuffer": false,
  "stencil": false,
  "desynchronized": false
}

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

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

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

规范

规范
WebGL 规范
# 5.14.2

浏览器兼容性

另见