| 14 | } |
| 15 | |
| 16 | export const LiquidChrome: React.FC<LiquidChromeProps> = ({ |
| 17 | baseColor = [0.1, 0.1, 0.1], |
| 18 | speed = 0.2, |
| 19 | amplitude = 0.5, |
| 20 | frequencyX = 3, |
| 21 | frequencyY = 2, |
| 22 | interactive = true, |
| 23 | opacity = 1, |
| 24 | ...props |
| 25 | }) => { |
| 26 | const containerRef = useRef<HTMLDivElement | null>(null); |
| 27 | |
| 28 | useEffect(() => { |
| 29 | if (!containerRef.current) return; |
| 30 | |
| 31 | const container = containerRef.current; |
| 32 | |
| 33 | const canvas = document.createElement("canvas"); |
| 34 | const glContext = |
| 35 | canvas.getContext("webgl2", { |
| 36 | alpha: true, |
| 37 | antialias: true, |
| 38 | preserveDrawingBuffer: true, |
| 39 | }) || |
| 40 | canvas.getContext("webgl", { |
| 41 | alpha: true, |
| 42 | antialias: true, |
| 43 | preserveDrawingBuffer: true, |
| 44 | }); |
| 45 | if (!glContext) return; |
| 46 | |
| 47 | const renderer = new Renderer({ |
| 48 | canvas, |
| 49 | antialias: true, |
| 50 | alpha: true, |
| 51 | webgl: 2, |
| 52 | }); |
| 53 | const gl = renderer.gl; |
| 54 | gl.clearColor(0, 0, 0, 0); |
| 55 | const opacityClamped = Math.max(0, Math.min(1, opacity)); |
| 56 | |
| 57 | const vertexShader = ` |
| 58 | attribute vec2 position; |
| 59 | attribute vec2 uv; |
| 60 | varying vec2 vUv; |
| 61 | void main() { |
| 62 | vUv = uv; |
| 63 | gl_Position = vec4(position, 0.0, 1.0); |
| 64 | } |
| 65 | `; |
| 66 | |
| 67 | const fragmentShader = ` |
| 68 | precision highp float; |
| 69 | uniform float uTime; |
| 70 | uniform vec3 uResolution; |
| 71 | uniform vec3 uBaseColor; |
| 72 | uniform float uAmplitude; |
| 73 | uniform float uFrequencyX; |