React Grid Distortion Background
React grid distortion background with Three.js shader effects. Features mouse-responsive image warping, customizable grid density, and fluid distortion patterns for dynamic visual experiences.
Creating interactive image distortions?
Join our Discord community for help from other developers working with Three.js shaders and image manipulation effects.
Ever wanted to create those mesmerizing image distortion effects where your cursor seems to ripple and warp the image like it's liquid? Those fluid interactions where mouse movement creates dynamic waves that propagate and decay naturally, transforming static images into living, responsive canvases? The way the distortion follows your movement with realistic physics-like behavior that feels organic and satisfying? Most image effects rely on simple CSS transforms or filters, but they lack the sophisticated grid-based displacement that creates truly immersive visual experiences.
This React component creates that captivating grid distortion effect using Three.js with custom shaders, featuring mouse-responsive image warping, configurable grid density, and smooth relaxation physics that bring dynamic fluidity to any image.
Mouse-responsive image warping with grid displacement
Watch images ripple and distort in real-time following your cursor with fluid physics:
Built for React applications with TypeScript and optimized Three.js rendering. Uses custom fragment shaders for grid-based displacement while providing intuitive controls for distortion strength, mouse sensitivity, and relaxation timing. Perfect for hero sections, portfolio showcases, or any interface needing sophisticated image interaction effects.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/grid-distortion.json
npx shadcn@latest add https://www.shadcn.io/registry/grid-distortion.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/grid-distortion.json
bunx shadcn@latest add https://www.shadcn.io/registry/grid-distortion.json
Why static images feel lifeless
Developers add CSS hover effects to images and consider them interactive. Maybe they include some basic transforms or opacity changes. The problem? Static image effects can't capture the organic, fluid nature of truly responsive visuals that react dynamically to user interaction with realistic physics behavior.
Grid-based image distortion with WebGL shaders changes everything. It taps into our fascination with liquid dynamics—the way distortions propagate through space, how energy dissipates naturally over time, the hypnotic flow that makes digital surfaces feel tangible. The mathematical algorithms use grid displacement and relaxation physics to create authentic fluid-like responses.
This component handles complex shader mathematics automatically. Grid coordinate mapping, mouse velocity calculations, distance-based influence, and exponential decay all work together seamlessly. The result feels like touching actual liquid through your screen.
Features
- Three.js shader rendering with custom fragment shaders for real-time grid displacement
- Mouse-responsive distortion with velocity-based influence and distance calculations
- Grid-based displacement using data textures for precise per-vertex control
- Relaxation physics with exponential decay for natural energy dissipation
- Image aspect preservation with automatic scaling and orthographic projection
- Performance optimization with efficient WebGL context management and cleanup
- Responsive design with ResizeObserver support for dynamic container sizing
- Touch support with mouse leave handling and state reset functionality
- Customizable parameters for grid density, mouse sensitivity, and distortion strength
- Memory management with proper texture disposal and resource cleanup
API Reference
GridDistortion
Prop | Type | Default | Description |
---|---|---|---|
imageSrc | string | - | Required - URL or path to image source |
grid | number | 15 | Grid density (higher = more detail) |
mouse | number | 0.1 | Mouse influence radius (0-1) |
strength | number | 0.15 | Distortion intensity multiplier |
relaxation | number | 0.9 | Energy decay rate (0-1) |
className | string | - | Additional CSS classes |
...props | HTMLAttributes<HTMLDivElement> | - | All standard div props |
Grid Density Settings
Different grid values for various detail levels:
Grid | Detail | Performance | Use Case |
---|---|---|---|
10 | Low | High | Simple effects, mobile |
15 | Medium | Good | Balanced quality/performance |
25 | High | Medium | Desktop, detailed distortion |
40 | Very High | Low | High-end devices only |
Distortion Parameters
Different parameter combinations for various effects:
Effect | Mouse | Strength | Relaxation | Description |
---|---|---|---|---|
Subtle | 0.05 | 0.1 | 0.95 | Gentle ripples |
Standard | 0.1 | 0.15 | 0.9 | Balanced interaction |
Dramatic | 0.2 | 0.3 | 0.85 | Strong distortions |
Fluid | 0.15 | 0.25 | 0.92 | Liquid-like behavior |
Shader mathematics
Grid Displacement: Uses data textures to store per-vertex displacement vectors, enabling precise control over individual grid points with real-time updates.
Mouse Influence: Calculates distance-based influence with inverse square falloff, ensuring realistic force distribution around cursor position.
Velocity Integration: Tracks mouse velocity to determine distortion direction and magnitude, creating natural push/pull effects based on movement speed.
Relaxation Physics: Implements exponential decay with configurable relaxation factor, allowing energy to dissipate naturally over time like real fluid dynamics.
Performance considerations
Grid Resolution Impact: Higher grid values create more detailed distortion but increase computational cost quadratically. Balance detail with performance requirements.
Shader Optimization: Uses efficient WebGL rendering with minimal texture updates and optimized fragment shader calculations for consistent 60fps performance.
Memory Management: Properly disposes of Three.js resources including geometries, materials, textures, and WebGL contexts to prevent memory leaks.
ResizeObserver: Uses ResizeObserver when available for efficient resize handling, falling back to window resize events for better performance.
Common gotchas
Image Loading: Component requires successful image loading before distortion works. Ensure image URLs are accessible and use appropriate CORS headers for cross-origin images.
Grid Size Limits: Very high grid values (>50) can cause performance issues on lower-end devices. Test across different hardware capabilities.
Container Sizing: Component fills its container completely. Ensure parent element has defined dimensions for proper distortion calculation and rendering.
Mouse Coordinates: Uses normalized coordinates for consistent behavior across different container sizes and device pixel ratios.
WebGL Context: Requires WebGL-enabled browser. Component will fail silently if WebGL is not available or context creation fails.
Relaxation Range: Relaxation values outside 0.8-0.99 range may cause unstable behavior. Values too low prevent settling, values too high create sluggish response.
You might also like
Liquid Chrome Background
Metallic liquid effects with WebGL rendering
Grid Motion Background
Interactive grid animations with mouse parallax
Plasma Background
Fluid plasma effects with raymarching shaders
Orb
Dynamic orb with organic noise morphing
Questions developers actually ask
React Background Gradient
React radial gradient background with hover animations. Framer Motion creates premium showcase effects for cards with TypeScript and shadcn/ui.
React Grid Motion Background
React grid motion background with GSAP animation. Features mouse-responsive grid movement, customizable content items, and smooth inertia-based parallax effects.