--- title: WebGLRenderingContext.clearColor() slug: Web/API/WebGLRenderingContext/clearColor translation_of: Web/API/WebGLRenderingContext/clearColor ---
{{APIRef("WebGL")}}

WebGL API 的 WebGLRenderingContext.clearColor() 方法用于设置清空颜色缓冲时的颜色值。

这指定调用 {{domxref("WebGLRenderingContext.clear", "clear()")}} 方法时使用的颜色值。这些值在0到1的范围间。

句法

void gl.clearColor(red, green, blue, alpha);

参数

red
一个 {{domxref("GLclampf")}} 类型的值,指定清除缓冲时的红色值。默认值:0。
green
一个 {{domxref("GLclampf")}} 类型的值,指定清除缓冲时的绿色值。默认值:0。
blue
一个 {{domxref("GLclampf")}} 类型的值,指定清除缓冲时的蓝色值。默认值:0。
alpha
一个 {{domxref("GLclampf")}} 类型的值,指定清除缓冲时的不透明度。默认值:0。

返回值

无。

示例

gl.clearColor(1, 0.5, 0.5, 3);

要获取当前的清除颜色,传入COLOR_CLEAR_VALUE常量,返回 {{jsxref("Float32Array")}}。

gl.getParameter(gl.COLOR_CLEAR_VALUE);
// Float32Array[1, 0.5, 0.5, 1]

规范

Specification Status Comment
{{SpecName('WebGL', "#5.14.3", "clearColor")}} {{Spec2('WebGL')}} Initial definition.
{{SpecName('OpenGL ES 2.0', "glClearColor.xml", "glClearColor")}} {{Spec2('OpenGL ES 2.0')}} Man page of the OpenGL API.

浏览器兼容性

{{Compat("api/WebGLRenderingContext", "WebGLRenderingContext.clearColor")}}

相关内容