一、给画布换个颜色

267 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webgl</title>
</head>
<body>
    <canvas id="canvas" width="800" height="800"></canvas>
    <script>
        const ctx = document.getElementByld('canvas);
        const gl = ctx.getContext('webgl');
        gl.clearColor(1.0,0.0,0.0,1.0);
        gl.clear(gl.COLOR_BUFFER_BIT);
    </script>
</body>
</html>

gl.clearColor(r,g,b,a)

指定清空 的颜色,接收四个参数取值区间为 0.0~1.0);

gl.clear(buffer)

清空 canvas参数分为三项 

  • gl.COLOR_BUFFER_BIT 清空颜色缓存 
  • gl.DEPTH_BUFFER_BIT 清空深度缓冲区  
  • gl.STENCIL_BUFFERBIT 清空模板缓冲区

gl.clear 需要和 glclearColor 提到的函数搭配使用

  • gl.clear(gl.COLOR_BUFFER_BIT)和 glclearColor(0.0,0.0,0.0,1.0)
  • gl.clear(gl.DEPTH_BUFFER_BIT)和 glclearDepth(1.0)
  • gl.clear(gl.STENCIL_BUFFER_BIT)和 glclearStencil(0)