|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import { createNoise3D } from 'simplex-noise'; |
| 3 | + |
| 4 | +const WavyBackground = ({ |
| 5 | + children, |
| 6 | + className, |
| 7 | + containerClassName, |
| 8 | + colors, |
| 9 | + waveWidth, |
| 10 | + backgroundFill, |
| 11 | + blur = 10, |
| 12 | + speed = 'fast', |
| 13 | + waveOpacity = 0.5, |
| 14 | + ...props |
| 15 | +}: { |
| 16 | + children?: any; |
| 17 | + className?: string; |
| 18 | + containerClassName?: string; |
| 19 | + colors?: string[]; |
| 20 | + waveWidth?: number; |
| 21 | + backgroundFill?: string; |
| 22 | + blur?: number; |
| 23 | + speed?: 'slow' | 'fast'; |
| 24 | + waveOpacity?: number; |
| 25 | + [key: string]: any; |
| 26 | +}) => { |
| 27 | + const noise = createNoise3D(); |
| 28 | + let w: number, |
| 29 | + h: number, |
| 30 | + nt: number, |
| 31 | + i: number, |
| 32 | + x: number, |
| 33 | + ctx: any, |
| 34 | + canvas: any; |
| 35 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 36 | + const getSpeed = () => { |
| 37 | + switch (speed) { |
| 38 | + case 'slow': |
| 39 | + return 0.001; |
| 40 | + case 'fast': |
| 41 | + return 0.002; |
| 42 | + default: |
| 43 | + return 0.001; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const init = () => { |
| 48 | + canvas = canvasRef.current; |
| 49 | + ctx = canvas.getContext('2d'); |
| 50 | + w = ctx.canvas.width = window.innerWidth; |
| 51 | + h = ctx.canvas.height = window.innerHeight; |
| 52 | + ctx.filter = `blur(${blur}px)`; |
| 53 | + nt = 0; |
| 54 | + window.onresize = function () { |
| 55 | + w = ctx.canvas.width = window.innerWidth; |
| 56 | + h = ctx.canvas.height = window.innerHeight; |
| 57 | + ctx.filter = `blur(${blur}px)`; |
| 58 | + }; |
| 59 | + render(); |
| 60 | + }; |
| 61 | + |
| 62 | + const waveColors = colors ?? [ |
| 63 | + '#38bdf8', |
| 64 | + '#818cf8', |
| 65 | + '#c084fc', |
| 66 | + '#e879f9', |
| 67 | + '#22d3ee', |
| 68 | + ]; |
| 69 | + const drawWave = (n: number) => { |
| 70 | + nt += getSpeed(); |
| 71 | + for (i = 0; i < n; i++) { |
| 72 | + ctx.beginPath(); |
| 73 | + ctx.lineWidth = waveWidth || 50; |
| 74 | + ctx.strokeStyle = waveColors[i % waveColors.length]; |
| 75 | + for (x = 0; x < w; x += 5) { |
| 76 | + const y = noise(x / 600, 0.3 * i, nt) * 100; |
| 77 | + ctx.lineTo(x, y + h * 0.5); // adjust for height, currently at 50% of the container |
| 78 | + } |
| 79 | + ctx.stroke(); |
| 80 | + ctx.closePath(); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + let animationId: number; |
| 85 | + const render = () => { |
| 86 | + ctx.fillStyle = backgroundFill || 'black'; |
| 87 | + ctx.color = backgroundFill || 'black'; |
| 88 | + ctx.globalAlpha = waveOpacity || 0.5; |
| 89 | + ctx.fillRect(0, 0, w, h); |
| 90 | + drawWave(8); |
| 91 | + animationId = requestAnimationFrame(render); |
| 92 | + }; |
| 93 | + |
| 94 | + useEffect(() => { |
| 95 | + init(); |
| 96 | + return () => { |
| 97 | + cancelAnimationFrame(animationId); |
| 98 | + }; |
| 99 | + }, []); |
| 100 | + |
| 101 | + const [isSafari, setIsSafari] = useState(false); |
| 102 | + useEffect(() => { |
| 103 | + // I'm sorry but i have got to support it on safari. |
| 104 | + setIsSafari( |
| 105 | + typeof window !== 'undefined' && |
| 106 | + navigator.userAgent.includes('Safari') && |
| 107 | + !navigator.userAgent.includes('Chrome'), |
| 108 | + ); |
| 109 | + }, []); |
| 110 | + |
| 111 | + return ( |
| 112 | + <div |
| 113 | + className={ |
| 114 | + 'h-screen flex flex-col items-center justify-center mx-auto pb-20' |
| 115 | + } |
| 116 | + style={{ |
| 117 | + zIndex: 0, |
| 118 | + overflowX: 'clip', |
| 119 | + marginTop: '-25vh', |
| 120 | + mixBlendMode: 'lighten', |
| 121 | + }} |
| 122 | + > |
| 123 | + <canvas |
| 124 | + className="inset-0 z-0" |
| 125 | + ref={canvasRef} |
| 126 | + id="canvas" |
| 127 | + style={{ |
| 128 | + ...(isSafari ? { filter: `blur(${blur}px)` } : {}), |
| 129 | + }} |
| 130 | + ></canvas> |
| 131 | + <div className={'z-0'} {...props}> |
| 132 | + {children} |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + ); |
| 136 | +}; |
| 137 | + |
| 138 | +export default WavyBackground; |
0 commit comments