大网数据广告活动
Web 标准与前端开发

WebGLRenderingContext: blendFuncSeparate() method

· 52CE 我爱测

The **`WebGLRenderingContext.blendFuncSeparate()`** method of the WebGL API defines which function is used for blending pixel arithmetic for RGB and alpha components separately.

WebGL

The WebGLRenderingContext.blendFuncSeparate() method of the WebGL API defines which function is used for blending pixel arithmetic for RGB and alpha components separately.

Syntax

blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha)

Parameters

  • srcRGB
    • : A WebGL_API.Types specifying a multiplier for the red, green and blue (RGB) source blending factors. The default value is gl.ONE. For possible values, see below.
  • dstRGB
    • : A WebGL_API.Types specifying a multiplier for the red, green and blue (RGB) destination blending factors. The default value is gl.ZERO. For possible values, see below.
  • srcAlpha
    • : A WebGL_API.Types specifying a multiplier for the alpha source blending factor. The default value is gl.ONE. For possible values, see below.
  • dstAlpha
    • : A WebGL_API.Types specifying a multiplier for the alpha destination blending factor. The default value is gl.ZERO. For possible values, see below.

Return value

None (undefined).

Exceptions

  • If srcRGB, dstRGB, srcAlpha, or dstAlpha is not one of the listed possible values, a gl.INVALID_ENUM error is thrown.
  • If a constant color and a constant alpha value are used together as source (srcRGB) and destination (dstRGB) factors, a gl.INVALID_ENUM error is thrown.

Constants

The following constants can be used for srcRGB, dstRGB, srcAlpha, and dstAlpha

The formulas for the blending factors can be described like this (all RGBA values are between 0 and 1):

  • color(RGB) = (sourceColor * srcRGB) + (destinationColor * dstRGB)
  • color(A) = (sourceAlpha * srcAlpha) + (destinationAlpha * dstAlpha)

In the following table, R<sub>S</sub>, G<sub>S</sub>, B<sub>S</sub>, A<sub>S</sub> represent respectively the red, green, blue and alpha component of the source, while R<sub>D</sub>, G<sub>D</sub>, B<sub>D</sub>, A<sub>D</sub> represent respectively the red, green, blue and alpha component of the destination. Similarly, R<sub>C</sub>, G<sub>C</sub>, B<sub>C</sub>, A<sub>C</sub> represent respectively the red, green, blue and alpha component of a constant color. They are all values between 0 and 1, included.

<table class="no-markdown"> <thead> <tr> <th scope="col">Constant</th> <th scope="col">RGB factor</th> <th scope="col">Alpha factor</th> <th scope="col">Description</th> </tr> </thead> <tbody> <tr> <td><code>gl.ZERO</code></td> <td>0,0,0</td> <td>0</td> <td>Multiplies all colors by 0.</td> </tr> <tr> <td><code>gl.ONE</code></td> <td>1,1,1,1</td> <td>1</td> <td>Multiplies all colors by 1.</td> </tr> <tr> <td><code>gl.SRC_COLOR</code></td> <td>R<sub>S</sub>, G<sub>S</sub>, B<sub>S</sub></td> <td>A<sub>S</sub></td> <td>Multiplies all colors by the source colors.</td> </tr> <tr> <td><code>gl.ONE_MINUS_SRC_COLOR</code></td> <td>1-R<sub>S</sub>, 1-G<sub>S</sub>, 1-B<sub>S</sub></td> <td>1-A<sub>S</sub></td> <td>Multiplies all colors by 1 minus each source color.</td> </tr> <tr> <td><code>gl.DST_COLOR</code></td> <td>R<sub>D</sub>, G<sub>D</sub>, B<sub>D</sub></td> <td>A<sub>D</sub></td> <td>Multiplies all colors by the destination color.</td> </tr> <tr> <td><code>gl.ONE_MINUS_DST_COLOR</code></td> <td>1-R<sub>D</sub>, 1-G<sub>D</sub>, 1-B<sub>D</sub></td> <td>1-A<sub>D</sub></td> <td>Multiplies all colors by 1 minus each destination color.</td> </tr> <tr> <td><code>gl.SRC_ALPHA</code></td> <td>A<sub>S</sub>, A<sub>S</sub>, A<sub>S</sub></td> <td>A<sub>S</sub></td> <td>Multiplies all colors by the source alpha color.</td> </tr> <tr> <td><code>gl.ONE_MINUS_SRC_ALPHA</code></td> <td>1-A<sub>S</sub>, 1-A<sub>S</sub>, 1-A<sub>S</sub></td> <td>1-A<sub>S</sub></td> <td>Multiplies all colors by 1 minus the source alpha color.</td> </tr> <tr> <td><code>gl.DST_ALPHA</code></td> <td>A<sub>D</sub>, A<sub>D</sub>, A<sub>D</sub></td> <td>A<sub>D</sub></td> <td>Multiplies all colors by the destination alpha color.</td> </tr> <tr> <td><code>gl.ONE_MINUS_DST_ALPHA</code></td> <td>1-A<sub>D</sub>, 1-A<sub>D</sub>, 1-A<sub>D</sub></td> <td>1-A<sub>D</sub></td> <td>Multiplies all colors by 1 minus the destination alpha color.</td> </tr> <tr> <td><code>gl.CONSTANT_COLOR</code></td> <td>R<sub>C</sub>, G<sub>C</sub>, B<sub>C</sub></td> <td>A<sub>C</sub></td> <td>Multiplies all colors by a constant color.</td> </tr> <tr> <td><code>gl.ONE_MINUS_CONSTANT_COLOR</code></td> <td>1-R<sub>C</sub>, 1-G<sub>C</sub>, 1-B<sub>C</sub></td> <td>1-A<sub>C</sub></td> <td>Multiplies all colors by 1 minus a constant color.</td> </tr> <tr> <td><code>gl.CONSTANT_ALPHA</code></td> <td>A<sub>C</sub>, A<sub>C</sub>, A<sub>C</sub></td> <td>A<sub>C</sub></td> <td>Multiplies all colors by a constant alpha value.</td> </tr> <tr> <td><code>gl.ONE_MINUS_CONSTANT_ALPHA</code></td> <td>1-A<sub>C</sub>, 1-A<sub>C</sub>, 1-A<sub>C</sub></td> <td>1-A<sub>C</sub></td> <td>Multiplies all colors by 1 minus a constant alpha value.</td> </tr> <tr> <td><code>gl.SRC_ALPHA_SATURATE</code></td> <td> min(A<sub>S</sub>, 1 - A<sub>D</sub>), min(A<sub>S</sub>, 1 - A<sub>D</sub>), min(A<sub>S</sub>, 1 - A<sub>D</sub>) </td> <td>1</td> <td> Multiplies the RGB colors by the smaller of either the source alpha color or the value of 1 minus the destination alpha color. The alpha value is multiplied by 1. </td> </tr> </tbody> </table>

Examples

To use the blend function, you first have to activate blending with WebGLRenderingContext.enable() with the argument gl.BLEND.

gl.enable(gl.BLEND);
gl.blendFuncSeparate(gl.SRC_COLOR, gl.DST_COLOR, gl.ONE, gl.ZERO);

To get the current blend function, query the BLEND_SRC_RGB, BLEND_SRC_ALPHA, BLEND_DST_RGB, and BLEND_DST_ALPHA constants which return one of the blend function constants.

gl.enable(gl.BLEND);
gl.blendFuncSeparate(gl.SRC_COLOR, gl.DST_COLOR, gl.ONE, gl.ZERO);
gl.getParameter(gl.BLEND_SRC_RGB) === gl.SRC_COLOR;
// true

Specifications

Browser compatibility

See also

  • WebGLRenderingContext.blendColor()
  • WebGLRenderingContext.blendEquation()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。