To clamp the red and blue channels of a rectangle on an HTML canvas using native GPU acceleration (WebGL), avoid JavaScript loops by leveraging shaders. Here's how to do it:
Explanation:
WebGL shaders run on the GPU, allowing per-pixel operations without JavaScript loops. The fragment shader clamps red and blue values between 0xAA (170/255 ≈ 0.666) and 0xFF (1.0). This approach efficiently processes pixels in parallel.
Step-by-Step Code Example:
<canvas id="canvas"></canvas>
<script>
// Get the canvas and WebGL context
const canvas = document.getElementById('canvas');
const gl = canvas.getContext('webgl');
// Vertex Shader (defines rectangle vertices)
const vertexShaderSource = `
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0.0, 1.0);
}
`;
// Fragment Shader (clamps red/blue channels)
const fragmentShaderSource = `
precision mediump float;
uniform vec4 u_color;
void main() {
vec4 color = u_color;
// Clamp red and blue to [0xAA/255, 1.0]
color.r = clamp(color.r, 170.0/255.0, 1.0);
color.b = clamp(color.b, 170.0/255.0, 1.0);
gl_FragColor = color;
}
`;
// Compile shaders and link program
function compileShader(source, type) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
return shader;
}
const vertexShader = compileShader(vertexShaderSource, gl.VERTEX_SHADER);
const fragmentShader = compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
// Set up vertex buffer for a rectangle covering the canvas
const positions = [
-1, 1, // Top-left
-1, -1, // Bottom-left
1, 1, // Top-right
1, -1 // Bottom-right
];
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
const positionLocation = gl.getAttribLocation(program, 'a_position');
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// Define triangle indices
const indices = [0, 1, 2, 2, 1, 3];
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
// Set uniform color (e.g., initial color before clamping)
const colorUniformLocation = gl.getUniformLocation(program, 'u_color');
const color = [1.0, 0.5, 1.0, 1.0]; // Example color (rgba)
gl.uniform4fv(colorUniformLocation, color);
// Draw the rectangle
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
</script>
Key Points:
- WebGL Setup: Creates a WebGL context and defines a rectangle covering the entire canvas.
- Vertex Shader: Positions vertices for the rectangle.
- Fragment Shader: Clamps red and blue channels between
0.666 (170/255) and 1.0 (255/255).
- Uniform Color: Sets the base color of the rectangle, which the fragment shader modifies.
This method uses GPU acceleration for pixel processing, avoiding JavaScript loops and efficiently clamping color channels. Adjust the color array values or vertex positions to customize the rectangle's appearance.