Join our Discord Community

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:

Loading component...

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

PropTypeDefaultDescription
imageSrcstring-Required - URL or path to image source
gridnumber15Grid density (higher = more detail)
mousenumber0.1Mouse influence radius (0-1)
strengthnumber0.15Distortion intensity multiplier
relaxationnumber0.9Energy decay rate (0-1)
classNamestring-Additional CSS classes
...propsHTMLAttributes<HTMLDivElement>-All standard div props

Grid Density Settings

Different grid values for various detail levels:

GridDetailPerformanceUse Case
10LowHighSimple effects, mobile
15MediumGoodBalanced quality/performance
25HighMediumDesktop, detailed distortion
40Very HighLowHigh-end devices only

Distortion Parameters

Different parameter combinations for various effects:

EffectMouseStrengthRelaxationDescription
Subtle0.050.10.95Gentle ripples
Standard0.10.150.9Balanced interaction
Dramatic0.20.30.85Strong distortions
Fluid0.150.250.92Liquid-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

Questions developers actually ask