Usage Examples
CDN •
Vanilla •
React •
Vue
source in ./examples
Share your seeds & creations!
One script tag:
<script type="module" src="https://unpkg.com/gradient-gl?seed=c1.eba9"></script>npm install gradient-glimport gradientGL from "gradient-gl";
// Mounts to <body>
gradientGL("c1.eba9");
// Mounts inside #app
gradientGL("c1.eba9", "#app");
// Uses <canvas id="gradient">
gradientGL("c1.eba9", "canvas#gradient");
// Returns the program
const program = await gradientGL("c1.eba9");- No selector: creates and styles a
<canvas>in<body> - Selector to an element: creates and styles a
<canvas>inside it - Selector to a
<canvas>: uses that canvas. A sized canvas is left alone. An unsized one getswidthandheightof100%.
Styles are overridable. Defaults are inline, so override them with
!important.
Await initialization and destroy the program when its page or component unmounts:
const program = await gradientGL("c1.915c", "#meeting-gradient");
// Call from your unmount cleanup
program.destroy();Only one program is active at a time. Calls with the same shader reuse it only while it is alive and mounted to the requested target. After cleanup or target replacement, the next call creates a new program. Library-created canvases are removed on cleanup; caller-provided canvases stay in the DOM and can be reused. If unmount happens before initialization resolves, destroy the returned program when the promise settles.
Default canvas style: z-index: -1, no mix-blend-mode.
An opaque html/body background covers a -1 canvas. Raise z-index for fixed chrome or stacking contexts that sit above it. Set mix-blend-mode when the gradient should grade against page content.
#gradient-gl {
z-index: 0 !important;
mix-blend-mode: color !important;
}Defaults are inline. A normal rule does not override them. Use !important.
export default {
build: {
target: "esnext",
},
};<script type="module">
import gradientGL from "https://unpkg.com/gradient-gl";
gradientGL("c1.eba9");
</script><script type="module">
import gradientGL from "https://esm.sh/gradient-gl";
gradientGL("c1.eba9");
</script><!-- latest, mounts on body -->
<script type="module" src="https://unpkg.com/gradient-gl?seed=c1.eba9"></script>
<!-- pin a version -->
<script
type="module"
src="https://unpkg.com/gradient-gl@1.4.1?seed=c1.eba9"
></script>
<!-- mount inside <main> -->
<script
type="module"
src="https://unpkg.com/gradient-gl?seed=c1.eba9&selector=main"
></script>
<!-- any CSS selector works -->
<script
type="module"
src="https://unpkg.com/gradient-gl?seed=c1.eba9&selector=.wrapper>.content"
></script>
<!-- # must be escaped: #app → %23app -->
<script
type="module"
src="https://unpkg.com/gradient-gl?seed=c1.eba9&selector=%23app"
></script>{shader}.{speed}{hue}{sat}{light}
- Shader:
[a-z][0-9](e.g.,c1,n3) - Options:
[0-9a-f](hex values)
Explore and generate seeds in the playground
IDs are alphabetical. Import the live list with import { shaderIds } from 'gradient-gl'.
| Series | IDs | Character |
|---|---|---|
| b | b1 b2 b3 b4 |
classic layered / radial |
| c | c1 c4 |
contrast duotone flow |
| f | f1 f3 |
fluid RGB noise |
| l | l1 l3 l5 |
liquid warp |
| n | n1 n2 n3 n4 |
noisy color islands |
| s | s1 s3 |
soft shade blobs |
n1/n2 are softer blends; n3/n4 are sharper islands.
Animated gradient background techniques, slowest to fastest:
SVG: CPU-only, DOM-heavy, poor scaling, high memoryCanvas 2D: CPU-only, main-thread load, imperative updatesCSS: GPU-composited, limited complexity, best for static workWebGL: GPU shaders, wide supportWebGPU: faster where available, thinner browser support
Note
WebGPU is faster where it runs. WebGL covers more browsers and is enough for animated gradients.
TODO: Interactive benchmark app
