Embedding WebGL into everyday UI. Each card owns a tiny canvas that draws only the border, animates independently, and pauses when off-screen—so the effect scales without burning the GPU.
The shader computes distance to each edge in pixels. If a fragment falls within the border width, it renders a cosine-based gradient; otherwise it outputs transparent black.
// Distance to each edge in pixels
float distToLeft = pixel.x;
float distToRight = cardWidth - pixel.x;
float distToTop = pixel.y;
float distToBottom = cardHeight - pixel.y;
// Only render if inside the border band
bool inCardArea = distToLeft > iBorderWidth && ...;
if (!inCardArea) {
gl_FragColor = vec4(gradientColor, borderAlpha);
} else {
gl_FragColor = vec4(0.0); // transparent
}
Takeaway: Work in pixel coordinates—canvas size equals card size, so no UV math needed.
An IntersectionObserver flips state.visible when the card enters/exits the viewport.
The render loop checks this flag and skips work when false.
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
state.visible = entry.isIntersecting;
});
}, { threshold: 0.01, rootMargin: '50px' });
// In onBeforeFrame callback
if (!state.visible) return; // skip draw
Try it: Scroll this card off-screen and watch the stats panel—this card pauses while others keep running.
Uniforms let you tweak shader parameters every frame without recompiling. Here we push iTime,
iResolution, iBorderWidth, and a per-card iPhase.
uniforms: {
iTime: (gl, loc) => gl.uniform1f(loc, performance.now() / 1000),
iResolution: (gl, loc, ctx) => gl.uniform2f(loc, ctx.width, ctx.height),
iBorderWidth:(gl, loc) => gl.uniform1f(loc, 3.0),
iPhase: (gl, loc) => gl.uniform1f(loc, phaseOffset)
}
Takeaway: Expose any magic number as a uniform—makes A/B testing trivial.
Instead of random timing, each card gets a deterministic phase offset (0°, 60°, 120°…). The shader adds
this to iTime when computing the gradient, so all cards share the same animation but look
different.
// JS: precomputed phase per card
const phaseOffsets = [
0, Math.PI*2/6, Math.PI*4/6, Math.PI*6/6, ...
];
// GLSL: shift the color cycle
float t = iTime * 0.5 + iPhase;
vec3 col = 0.5 + 0.5 * cos(t + uv.xyx * 3.0 + vec3(0,2,4));
Takeaway: Deterministic offsets keep animations synced yet visually distinct.
Each card contains its own <canvas> and WebGL context. This gives you independent
lifecycles,
automatic CSS-matched sizing, and simple cleanup on unmount.
shaderWebBackground.shade({
canvas: document.querySelector('[data-canvas-id="0"]'),
shaders: {
'border-shader': { uniforms: { ... } }
}
});
Trade-off: More contexts = more memory, but each is tiny (border-only draw) and pauses when off-screen, so practical overhead stays low.
The canvas covers the entire card but a clip-path carves out the interior, leaving only a
frame. The card's glass background shows through the hole, content sits on top.
.card-border-canvas {
--offset: 2px;
clip-path: polygon(
0% 0%, 0% 100%,
var(--offset) 100%, var(--offset) var(--offset),
calc(100% - var(--offset)) var(--offset),
calc(100% - var(--offset)) calc(100% - var(--offset)),
0% calc(100% - var(--offset)), 0% 100%,
100% 100%, 100% 0%
);
}
Note: This is experimental—complex polygons can have browser quirks. Alternatives include
shader-based masking or CSS mask-image.