Learn WEBGPU with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple WebGPU Triangle
# webgpu/demo/triangle.js
async function initWebGPU() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
// Setup GPU pipeline and render triangle
const context = canvas.getContext('webgpu');
// ... pipeline, buffers, shaders, rendering logic ...
}
initWebGPU();
A basic WebGPU program in JavaScript that renders a colored triangle to a canvas.
2
WebGPU Clear Color
# webgpu/demo/clear_color.js
async function clearColor() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
context.configure({
device: device,
format: 'bgra8unorm'
});
const commandEncoder = device.createCommandEncoder();
const textureView = context.getCurrentTexture().createView();
const renderPass = commandEncoder.beginRenderPass({
colorAttachments: [{
view: textureView,
clearValue: { r: 0, g: 0, b: 1, a: 1 },
loadOp: 'clear',
storeOp: 'store'
}]
});
renderPass.end();
device.queue.submit([commandEncoder.finish()]);
}
clearColor();
Clears the canvas with a solid color using WebGPU.
3
WebGPU Animated Triangle
# webgpu/demo/animated_triangle.js
async function animateTriangle() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
context.configure({device: device, format: 'bgra8unorm'});
let angle = 0;
function frame() {
angle += 0.01;
// update transformation matrices for rotation
// draw triangle
requestAnimationFrame(frame);
}
frame();
}
animateTriangle();
Animates a triangle rotating using WebGPU.
4
WebGPU Rectangle
# webgpu/demo/rectangle.js
async function drawRectangle() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
// Create vertex buffer for rectangle
// Setup pipeline and render rectangle
}
drawRectangle();
Draws a rectangle using WebGPU pipeline and vertex buffer.
5
WebGPU Compute Shader
# webgpu/demo/compute.js
async function runCompute() {
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
// Create buffer, compute pipeline, and dispatch
}
runCompute();
Executes a simple compute shader using WebGPU.
6
WebGPU Texture Load
# webgpu/demo/texture.js
async function loadTexture() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
// Load image texture and render it
}
loadTexture();
Loads an image texture and renders it using WebGPU.
7
WebGPU Indexed Triangle
# webgpu/demo/indexed_triangle.js
async function drawIndexed() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
// Setup vertex buffer, index buffer, and render pipeline
}
drawIndexed();
Draws a triangle using an index buffer.
8
WebGPU Multiple Triangles
# webgpu/demo/multi_triangles.js
async function drawMultiple() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
// Setup multiple vertex buffers for triangles and render
}
drawMultiple();
Renders multiple triangles with different colors.
9
WebGPU Depth Test Example
# webgpu/demo/depth_test.js
async function depthTest() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
// Setup depth buffer and render objects
}
depthTest();
Demonstrates depth testing with WebGPU.
10
WebGPU Offscreen Rendering
# webgpu/demo/offscreen.js
async function offscreenRender() {
const canvas = document.getElementById('canvas');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const context = canvas.getContext('webgpu');
// Create offscreen texture, render, then copy to canvas
}
offscreenRender();
Renders to a texture offscreen and displays it.