Custom Shaders

Write your own GLSL fragment shader to create fully custom WebGL transitions between slides.

How it works

The @andresclua/sliderkit-webgl renderer passes two textures to every fragment shader — uTexture0 (current slide) and uTexture1 (next slide) — plus a uProgress uniform that animates from 0 to 1 during a transition.

Shader uniforms

UniformTypeDescription
uTexture0sampler2DCurrent (outgoing) slide texture
uTexture1sampler2DNext (incoming) slide texture
uProgressfloatTransition progress 0.0 → 1.0
uResolutionvec2Canvas size in pixels
uTimefloatElapsed time in seconds (permanent effects only)

Minimal example — cross-dissolve

// dissolve.frag
precision mediump float;

uniform sampler2D uTexture0;
uniform sampler2D uTexture1;
uniform float     uProgress;

varying vec2 vUv;

void main() {
  vec4 t0 = texture2D(uTexture0, vUv);
  vec4 t1 = texture2D(uTexture1, vUv);
  gl_FragColor = mix(t0, t1, uProgress);
}

Using a custom shader

import { Slider } from '@andresclua/sliderkit'
import { webglRenderer } from '@andresclua/sliderkit-webgl'
import dissolveShader from './dissolve.frag?raw'

new Slider('#el', {
  plugins: [
    webglRenderer({
      shader: dissolveShader,
      duration: 800,
    }),
  ],
})

Wipe effect example

precision mediump float;

uniform sampler2D uTexture0;
uniform sampler2D uTexture1;
uniform float     uProgress;

varying vec2 vUv;

void main() {
  float edge = smoothstep(uProgress - 0.05, uProgress + 0.05, vUv.x);
  vec4 t0 = texture2D(uTexture0, vUv);
  vec4 t1 = texture2D(uTexture1, vUv);
  gl_FragColor = mix(t0, t1, edge);
}

Importing GLSL in Vite

Use the ?raw suffix to import the shader as a string:

import myShader from './my.frag?raw'

For webpack, use raw-loader:

// webpack.config.js
{ test: /.frag$/, use: 'raw-loader' }

See also