@shader-core/svelte
v1.0.0
Published
Svelte action for Shader Core: use:shader on a canvas, with the stack as data.
Maintainers
Readme
@shader-core/svelte
A Svelte action for Shader Core: an animated WebGL2 background composed from layers that stack.
npm install @shader-core/svelteSvelte 3, 4 and 5 all take it. It is a plain function, so there is nothing to
compile and no .svelte file in the package.
Use
<script>
import { shader } from '@shader-core/svelte';
import gradient from '@shader-core/layers/gradient';
import curtains from '@shader-core/layers/curtains';
import grain from '@shader-core/layers/grain';
let intensity = $state(0.05);
</script>
<canvas
style="position: fixed; inset: 0; z-index: -1; display: block; width: 100%; height: 100%"
use:shader={{
stack: [
{ layer: gradient, colorA: '#04060f', colorB: '#0a1230' },
{ layer: curtains, scale: 3.8, colorSheet: '#3ddc97' },
{ layer: grain, intensity },
],
}}
></canvas>The stack is data: layers bottom to top, each with its params and colours flat
beside it, the same shape @shader-core/js takes. Anything left out keeps the
layer's default, including the blend it was designed for.
It is reactive the way any action parameter is. Change a value — intensity
above — and a uniform moves. Add, remove or reorder a layer and the stack
recompiles once, then it is cached.
The canvas is yours, so size it: a canvas is inline and 300×150 until told otherwise.
Why an action, not a component
React and Vue hand a component its children as data, which is how their
<Shader> reads a stack out of layer children without rendering them. Svelte
does not — a slot or a snippet renders, it does not describe — so a <Shader>
with layer children would have to mount each layer to learn what it was. An
action, handed the canvas and the stack, is the direct way to say the same
thing.
Parameters
| Parameter | What it does |
| --------------- | ----------------------------------------------------------------------------------------------- |
| stack | The layers. An empty stack is allowed; the renderer starts with the first one that has a layer. |
| paused | Stops the animation and keeps everything else. Unset it to resume. |
| quality | auto (default), high or low. Caps the pixel budget on modest hardware. |
| reducedMotion | static (default) or animate, under prefers-reduced-motion. |
| staticTime | Renders one frame at this time, in seconds, and never animates. |
| onError | Called for anything that goes wrong. Without it, errors go to the console. |
quality, reducedMotion and staticTime are read when the action starts.
They belong to the GL context, and a canvas cannot get a context back once it
has been released, so changing one means a new canvas — wrap it in
{#key quality}…{/key}. The stack and paused are live.
A background is not worth taking a page down for: a device with no WebGL2, or a layer that will not compile, is reported and leaves the canvas blank rather than throwing.
Layers
Every layer is its own module under @shader-core/layers/<id> — gradient,
mesh, silk, sheen, glow, ribbon, grain, tone and the rest; the
editor exports this code
for any stack you build. Import only what you use.
Licence
Free to use, including commercially, on one condition: the credit comment stays in shader source you copy. Full terms in LICENSE.md.
