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