shadcn.io is an independent project built on shadcn/ui
Background Canvas for Vue
Run your own GLSL fragment shader behind your content, on WebGL2 with no dependency. For Vue with Ark UI, when you'd rather skip the plumbing.
On this page10 sections
Background Canvas runs a GLSL fragment shader on WebGL2 behind its children. You write one function, scene; it handles the rest. It shares one frame loop with every other background on the page, draws only while it's on screen and the tab is visible, holds one still frame with reduced motion, follows the pointer from the window, turns any CSS color into a uniform and reads it again when the theme changes. It has no dependency. Every scene background in this collection is built on it.
Use it for an effect of your own: a brand gradient, a pointer light, a pattern. For a finished effect, pick one from the backgrounds, such as Aurora or Mesh Gradient. For WGSL on WebGPU instead, there's Shader Canvas.
Installation
Usage
<script setup lang="ts">
import { BackgroundCanvas } from "@/components/ui/background-canvas";
</script>
const SCENE = /* glsl */ `
uniform vec3 uTint;
vec4 scene(vec2 frag) {
vec2 p = fitted(frag);
float bands = fbm(vec2(p.x * 1.4 + uTime * 0.05, p.y * 3.0), 4);
return glow(uTint, smoothstep(-0.2, 0.7, bands) * 0.8);
}
`;
<BackgroundCanvas
className="flex min-h-96 items-center justify-center"
colors={{ uTint: "var(--primary)" }}
scene={SCENE}
>
<h1 className="text-5xl font-semibold">Your headline</h1>
</BackgroundCanvas>It's a container: its children sit on top of the canvas, in normal flow. It fills its own box, so give it a height or content. For a backdrop behind the whole page, render it empty with className="fixed inset-0 -z-10".
Writing the scene
- Define
vec4 scene(vec2 frag).fragis the pixel in device pixels from the bottom left, and the result is premultiplied color. Don't write#version,precisionormain: the canvas adds them, and amainthat dithers the result, so gradients never band. - The canvas is transparent and sits on the root's own background. Return
glow(color, amount)for light, which brightens a dark surface and tints a light one, orpaint(color, amount)for pigment, which covers any surface byamount.uDarkis 1 on a dark surface anduSurfaceis its color. - Every scene has
uResolution,uTime(already scaled byspeed),uPixelRatio,uPointer(xyin device pixels,zfrom 0 to 1 while the pointer is over the box),uPointerMove,uPulses[4](the last presses, with their age) anduIntro(0 to 1 as it first shows). - Helpers come with it:
uvOf,fittedandcenteredfor coordinates,hash21,vnoise,snoiseandfbmfor noise,mixColor,rampandloopRampfor colors mixed in OKLab,rotate,luma,over. uniformsfills yourfloatand vector uniforms by name,colorsfillsvec3uniforms from any CSS color, andpalettesfills avec3 name[8]and itsint nameCountfrom a list of them.- Multiply anything sized in pixels by
uPixelRatio, so it keeps its size on every screen and at everyquality.
Examples
Pointer
uPointer follows the pointer, eased, and uPointer.z fades the effect in and out as it enters and leaves. Each press lands in uPulses with its position and age, here drawn as a ring that opens and fades. Presses on links and buttons over the canvas don't count, and touch never moves it.
Palette
palettes takes up to eight CSS colors as one uniform array. loopRamp runs through them and back to the first, mixing in OKLab, so two distant hues meet in a clean color. paint covers the surface, and grain adds film grain on top.
Simulations and glyphs
simulation runs a second shader into a texture each frame, with last frame's result as uState, and your scene reads it back: reaction-diffusion, cellular automata, ripples, trails. Define vec4 simulate(vec2 frag) in its source; uSeed is 1 on the first run, for the starting state. scale is the texture's size as a share of the box, and steps is how many times it runs a frame.
atlas draws a string of characters into a texture once, one square cell each in a row. Sample glyph i at the point f of a cell with texture(uAtlas, vec2((i + f.x) / uAtlasCount, f.y)).a. Matrix is built this way.
Performance
One canvas and one draw call a frame: the cost is your shader's. Every background on a page shares one requestAnimationFrame loop, and a background off screen or in a hidden tab isn't drawn at all. When frames run long for a second, the canvas lowers its own resolution a step, down to half. quality sets the render scale yourself, from 0.25 to 1, and the pixel ratio never goes above 2. On unmount the WebGL context is released at once, since a page can hold only a few.
Browser support
WebGL2 runs in every current browser. Where it doesn't, or when a scene fails to compile, the box keeps its own background and shows fallback if you pass one, a CSS background value. data-state on the root says which: loading, live, still or unsupported. A compile error is logged to the console with the shader's message.
Accessibility
The canvas is decoration: it's hidden from assistive tech and lets every click through to your content. With reduced motion it draws the frame at still seconds and stops, and presses no longer pulse. Keeping flashes under three a second and the copy readable over your scene is up to the shader you write.
FAQ
Why WebGL2 and not WebGPU?
WebGL2 runs in every current browser, on phones too, and needs no library. A background is usually one fragment shader, which WebGL2 draws as fast as anything. For WGSL and WebGPU there's Shader Canvas, in the shaders collection, with a CSS fallback where WebGPU is missing.
Can I paste a Shadertoy shader?
Mostly. Rename mainImage(out vec4 color, in vec2 fragCoord) to return the color from scene(vec2 frag), read uResolution and uTime for iResolution and iTime, and return paint(color.rgb, 1.0) for an opaque picture. uPointer.xy stands in for iMouse.xy. Check the shader's license first.
How do my colors follow the theme?
Pass CSS variables to colors or palettes, such as var(--primary). The canvas resolves them on the element, so oklch(), color-mix() and tokens all work, and it reads them again when the root element's class, style or data-theme changes.
How does one scene work on dark and light pages?
The canvas has no background of its own: your scene returns color with alpha, and the page shows through. glow adds light on a dark surface and tints a light one; paint covers either. For anything finer, branch on uDark, or mix toward uSurface.
How many can I put on one page?
Browsers allow a handful of WebGL contexts a page, around sixteen in Chrome and fewer on phones. Each canvas is one, held only while it's mounted. Three or four backgrounds on a page is fine; for a grid of many, show stills and mount one on hover.
API Reference
BackgroundCanvas
<div> data-slot="background-canvas"
scenestring
The GLSL: the scene's uniforms and vec4 scene(vec2 frag).
atlas?{ chars: string; font?: string; weight?: number }
Glyphs drawn once into a texture the scene samples (uAtlas, one square
cell each in a row, uAtlasCount of them).
- Type
{ chars: string; font?: string; weight?: number }
colors?Record<string, string>
The scene's colour uniforms (vec3), each as any CSS colour.
- Type
Record<string, string>
fade?"none" | "radial" | "top" | "bottom" | "left" | "right"
Which edges fade out to the surface: radial keeps the middle, the
others name the side that fades.
- Type
"none" | "radial" | "top" | "bottom" | "left" | "right"- Default
"none"
fallback?string
What shows until the first frame and in browsers without WebGL2: a CSS
background value that looks like the scene.
grain?number
Film grain over the scene, from 0 to 1.
- Default
0
interactive?boolean
Whether the scene follows the pointer and answers a press. Touch never moves it, so scrolling a page on a phone isn't disturbed.
- Default
true
opacity?number
How strongly the scene shows over the surface, from 0 to 1.
- Default
1
palettes?Record<string, readonly string[]>
The scene's colour-list uniforms (vec3 name[8] and int nameCount),
each up to eight CSS colours.
- Type
Record<string, readonly string[]>
paused?boolean
Holds the current frame.
- Default
false
quality?number
Render scale, from 0.25 to 1, of the screen's pixels (capped at 2×). Lower is cheaper and softer.
- Default
1
simulation?{ scale?: number; source: string; steps?: number }
A simulation the scene reads back as uState: GLSL with
vec4 simulate(vec2 frag), run each frame on last frame's result.
- Type
{ scale?: number; source: string; steps?: number }
speed?number
Animation speed: 1 is normal, 0 holds the frame.
- Default
1
still?number
The moment, in seconds, drawn first and kept with reduced motion.
- Default
0
time?number
A fixed moment, in seconds: draws it and doesn't animate.
uniforms?Record<string, number | boolean | readonly number[]>
The scene's own uniforms: numbers, booleans (0 or 1) and vectors.
- Type
Record<string, number | boolean | readonly number[]>
class?string
Merged with the part's classes.