Shader Web Background Toolkit

Built with shader-web-background by xemantic • Claude • cursor.com

A complete development environment for creating WebGL shader backgrounds. This project includes AI-assisted Shadertoy conversion rules, a shader registry system, and example implementations ranging from simple gradients to complex raymarched fractals.

πŸš€ Examples:

GitHub Repository: https://github.com/MRKNSTUDIO/shader-background

🎯 What This Project Does

This toolkit solves the common problem of porting Shadertoy shaders to production websites. While Shadertoy uses WebGL 2.0 features, most production sites need WebGL 1.0 compatibility. This project provides:

πŸ“ Project Structure

/shader-background/
β”œβ”€β”€ .cursor/
β”‚   └── rules/
β”‚       β”œβ”€β”€ converting-from-shadertoy.mdc       ← AI conversion rules
β”‚       β”œβ”€β”€ css-styling.mdc                     ← CSS styling guidelines
β”‚       β”œβ”€β”€ interaction.mdc                     ← Mouse/gyro handling
β”‚       └── shader-testing-environment.mdc      ← Project documentation
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ shader-web-background.min.js            ← Core library
β”‚   β”œβ”€β”€ main.js                                 ← ShaderRegistry controller
β”‚   └── styles.css                              ← UI styling
β”œβ”€β”€ shaders/
β”‚   β”œβ”€β”€ gradient.js                             ← Simple single-pass
β”‚   β”œβ”€β”€ feedback-circle.js                      ← Two-pass with buffer
β”‚   β”œβ”€β”€ spectral-feedback.js                    ← Zucconi spectral colors
β”‚   β”œβ”€β”€ interactive-blob.js                     ← Mouse/gyro interaction
β”‚   β”œβ”€β”€ demo-shader.js                          ← Advanced multi-input
β”‚   β”œβ”€β”€ voronoi-dreams.js                       ← Ethereal flowing ribbons
β”‚   └── shadertoy-example-*.js                  ← Converted Shadertoy shaders
β”œβ”€β”€ shadertoy/
β”‚   └── shadertoy-example-*.txt                 ← Original Shadertoy source code
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ minimal-example.html                    ← Minimal integration example
β”‚   β”œβ”€β”€ viewport-aware-example.html             ← Performance-optimized example
β”‚   └── card-borders-experiment.html            ← Shader borders on cards
β”œβ”€β”€ index.html                                  ← Main demo (shader source + UI)
β”œβ”€β”€ README.md                                   ← Project documentation
└── favicon.ico                                 ← Site icon

πŸ€– AI Conversion Rules

The .cursor/rules/converting-from-shadertoy.mdc file contains comprehensive conversion instructions for AI assistants. When working with Shadertoy code, the AI will automatically:

πŸ’‘ Using the Rules in Cursor

Cursor IDE automatically detects the .cursor/rules/ folder. When you paste Shadertoy code and ask the AI to convert it, the rules are applied automatically.

// Just ask:
"Convert this Shadertoy shader to work with shader-web-background"

// The AI will apply all 10 conversion steps:
// 1. Entry point conversion
// 2. iResolution type fix
// 3. For loop restrictions
// 4. Missing function implementations
// 5. Variable declaration cleanup
// 6. Macro expansion
// 7. Name conflict resolution
// 8. Boolean/float type mixing
// 9. Multi-pass buffer setup
// 10. Texture sampling syntax

πŸ”§ Using Rules in Other IDEs

Not using Cursor? Copy the rules to your AI-enabled IDE:

The rules are written as general GLSL conversion instructionsβ€”they work with any AI model that understands code.

πŸ“ Adding Your Own Shaders

Step 1: Add shader source to index.html

<script type="x-shader/x-fragment" id="my-shader-image">
precision highp float;
uniform float iTime;
uniform vec2 iResolution;

void main() {
    vec2 uv = gl_FragCoord.xy / iResolution;
    vec3 col = 0.5 + 0.5 * cos(iTime + uv.xyx + vec3(0, 2, 4));
    gl_FragColor = vec4(col, 1.0);
}
</script>

Step 2: Create registration file shaders/my-shader.js

ShaderRegistry.register('my-shader', {
  onError: function (error, canvas) {
    // Optional: Handle errors (e.g., WebGL not supported)
    console.error('Shader failed:', error);
  },
  shaders: {
    'my-shader-image': {
      uniforms: {
        iTime: (gl, loc) => gl.uniform1f(loc, performance.now() / 1000),
        iResolution: (gl, loc, ctx) => gl.uniform2f(loc, ctx.width, ctx.height)
      }
    }
  }
}, '<strong>My Shader</strong> Custom description here.');

Step 3: Add dropdown option and script include in index.html

<option value="my-shader">My Shader</option>
<script src="shaders/my-shader.js"></script>

πŸ”„ Multi-Pass Shaders (Feedback Buffers)

For shaders that need previous frame data (trails, blur, reaction-diffusion), use a two-pass setup:

// Buffer pass: reads from itself, writes processed result
'my-shader-buffer': {
  uniforms: {
    iChannel0: (gl, loc, ctx) => ctx.texture(loc, ctx.buffers['my-shader-buffer']),
    iResolution: (gl, loc, ctx) => gl.uniform2f(loc, ctx.width, ctx.height)
  }
}

// Image pass: reads buffer and outputs to screen
'my-shader-image': {
  uniforms: {
    iChannel0: (gl, loc, ctx) => ctx.texture(loc, ctx.buffers['my-shader-buffer']),
    iResolution: (gl, loc, ctx) => gl.uniform2f(loc, ctx.width, ctx.height)
  }
}

The library handles double-buffering automaticallyβ€”you read from the previous frame and write to the current frame.

πŸ–±οΈ Mouse & Gyro Interaction

The library provides coordinate conversion helpers. Handle events in your application code:

// Track mouse position
let mouseX = 0, mouseY = 0;
document.addEventListener('mousemove', (e) => {
    mouseX = e.clientX;
    mouseY = e.clientY;
});

// Convert in onBeforeFrame callback
onBeforeFrame: (ctx) => {
    shaderMouseX = ctx.toShaderX(mouseX);
    shaderMouseY = ctx.toShaderY(mouseY);
}

// Pass as uniforms
uniforms: {
    iMouse: (gl, loc) => gl.uniform2f(loc, shaderMouseX, shaderMouseY)
}

Device orientation uses the same pattern with deviceorientation events and beta/gamma values.

⚠️ Error Handling

The shader-web-background library provides an onError callback that is called when shader initialization fails (e.g., WebGL not supported, missing capabilities). Use this to handle errors gracefully:

shaderWebBackground.shade({
  onError: function (error, canvas) {
    // Create fallback UI element only when needed
    const notification = document.createElement('div');
    notification.className = 'webgl-notification';
    notification.textContent = '⚠️ Shader cannot be played on this device';
    document.body.appendChild(notification);

    console.error('Shader initialization failed:', error);
  },
  shaders: { /* ... */ }
});

Best Practice: Don't create fallback elements in the DOM upfront. Instead, create them dynamically in the onError callback only when the shader cannot be displayed. This keeps your HTML clean and avoids unnecessary DOM elements for users with WebGL support.

The library automatically detects WebGL support and capabilities, so you don't need to manually check for WebGL availability before calling shade().

⚑ Quick Shadertoy Conversion Checklist

🎨 Included Shader Examples

Use the dropdown at the top to switch between shaders:

πŸ“š Resources

πŸš€ Getting Started

  1. Download or clone this repository from GitHub
  2. Open index.html in a browser (use a local server for best results)
  3. Use the dropdown to switch between shader examples
  4. Copy any shader as a template for your own creations
  5. Paste Shadertoy code and let AI handle the conversion using the included rules

Made with ❀️ for the shader community