A vortex component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import React, { useEffect, useRef } from "react";
4import { createNoise3D } from "simplex-noise";
5import { motion } from "motion/react";
6
7interface VortexProps {
8 children?: any;
9 className?: string;
10 containerClassName?: string;
11 particleCount?: number;
12 rangeY?: number;
13 baseHue?: number;
14 baseSpeed?: number;
15 rangeSpeed?: number;
16 baseRadius?: number;
17 rangeRadius?: number;
18 backgroundColor?: string;
19}
20
21export const Vortex = (props: VortexProps) => {
22 const canvasRef = useRef<HTMLCanvasElement>(null);
23 const containerRef = useRef(null);
24 const animationFrameId = useRef<number>();
25 const particleCount = props.particleCount || 700;
26 const particlePropCount = 9;
27 const particlePropsLength = particleCount * particlePropCount;
28 const rangeY = props.rangeY || 100;
29 const baseTTL = 50;
30 const rangeTTL = 150;
31 const baseSpeed = props.baseSpeed || 0.0;
32 const rangeSpeed = props.rangeSpeed || 1.5;
33 const baseRadius = props.baseRadius || 1;
34 const rangeRadius = props.rangeRadius || 2;
35 const baseHue = props.baseHue || 220;
36 const rangeHue = 100;
37 const noiseSteps = 3;
38 const xOff = 0.00125;
39 const yOff = 0.00125;
40 const zOff = 0.0005;
41 const backgroundColor = props.backgroundColor || "#000000";
42 let tick = 0;
43 const noise3D = createNoise3D();
44 let particleProps = new Float32Array(particlePropsLength);
45 let center: [number, number] = [0, 0];
46
47 const HALF_PI: number = 0.5 * Math.PI;
48 const TAU: number = 2 * Math.PI;
49 const TO_RAD: number = Math.PI / 180;
50 const rand = (n: number): number => n * Math.random();
51 const randRange = (n: number): number => n - rand(2 * n);
52 const fadeInOut = (t: number, m: number): number => {
53 let hm = 0.5 * m;
54 return Math.abs(((t + hm) % m) - hm) / hm;
55 };
56 const lerp = (n1: number, n2: number, speed: number): number =>
57 (1 - speed) * n1 + speed * n2;
58
59 const setup = () => {
60 const canvas = canvasRef.current;
61 const container = containerRef.current;
62 if (canvas && container) {
63 const ctx = canvas.getContext("2d");
64
65 if (ctx) {
66 resize(canvas, ctx);
67 initParticles();
68 draw(canvas, ctx);
69 }
70 }
71 };
72
73 const initParticles = () => {
74 tick = 0;
75 // simplex = new SimplexNoise();
76 particleProps = new Float32Array(particlePropsLength);
77
78 for (let i = 0; i < particlePropsLength; i += particlePropCount) {
79 initParticle(i);
80 }
81 };
82
83 const initParticle = (i: number) => {
84 const canvas = canvasRef.current;
85 if (!canvas) return;
86
87 let x, y, vx, vy, life, ttl, speed, radius, hue;
88
89 x = rand(canvas.width);
90 y = center[1] + randRange(rangeY);
91 vx = 0;
92 vy = 0;
93 life = 0;
94 ttl = baseTTL + rand(rangeTTL);
95 speed = baseSpeed + rand(rangeSpeed);
96 radius = baseRadius + rand(rangeRadius);
97 hue = baseHue + rand(rangeHue);
98
99 particleProps.set([x, y, vx, vy, life, ttl, speed, radius, hue], i);
100 };
101
102 const draw = (canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D) => {
103 tick++;
104
105 ctx.clearRect(0, 0, canvas.width, canvas.height);
106
107 ctx.fillStyle = backgroundColor;
108 ctx.fillRect(0, 0, canvas.width, canvas.height);
109
110 drawParticles(ctx);
111 renderGlow(canvas, ctx);
112 renderToScreen(canvas, ctx);
113
114 animationFrameId.current = window.requestAnimationFrame(() =>
115 draw(canvas, ctx),
116 );
117 };
118
119 const drawParticles = (ctx: CanvasRenderingContext2D) => {
120 for (let i = 0; i < particlePropsLength; i += particlePropCount) {
121 updateParticle(i, ctx);
122 }
123 };
124
125 const updateParticle = (i: number, ctx: CanvasRenderingContext2D) => {
126 const canvas = canvasRef.current;
127 if (!canvas) return;
128
129 let i2 = 1 + i,
130 i3 = 2 + i,
131 i4 = 3 + i,
132 i5 = 4 + i,
133 i6 = 5 + i,
134 i7 = 6 + i,
135 i8 = 7 + i,
136 i9 = 8 + i;
137 let n, x, y, vx, vy, life, ttl, speed, x2, y2, radius, hue;
138
139 x = particleProps[i];
140 y = particleProps[i2];
141 n = noise3D(x * xOff, y * yOff, tick * zOff) * noiseSteps * TAU;
142 vx = lerp(particleProps[i3], Math.cos(n), 0.5);
143 vy = lerp(particleProps[i4], Math.sin(n), 0.5);
144 life = particleProps[i5];
145 ttl = particleProps[i6];
146 speed = particleProps[i7];
147 x2 = x + vx * speed;
148 y2 = y + vy * speed;
149 radius = particleProps[i8];
150 hue = particleProps[i9];
151
152 drawParticle(x, y, x2, y2, life, ttl, radius, hue, ctx);
153
154 life++;
155
156 particleProps[i] = x2;
157 particleProps[i2] = y2;
158 particleProps[i3] = vx;
159 particleProps[i4] = vy;
160 particleProps[i5] = life;
161
162 (checkBounds(x, y, canvas) || life > ttl) && initParticle(i);
163 };
164
165 const drawParticle = (
166 x: number,
167 y: number,
168 x2: number,
169 y2: number,
170 life: number,
171 ttl: number,
172 radius: number,
173 hue: number,
174 ctx: CanvasRenderingContext2D,
175 ) => {
176 ctx.save();
177 ctx.lineCap = "round";
178 ctx.lineWidth = radius;
179 ctx.strokeStyle = `hsla(${hue},100%,60%,${fadeInOut(life, ttl)})`;
180 ctx.beginPath();
181 ctx.moveTo(x, y);
182 ctx.lineTo(x2, y2);
183 ctx.stroke();
184 ctx.closePath();
185 ctx.restore();
186 };
187
188 const checkBounds = (x: number, y: number, canvas: HTMLCanvasElement) => {
189 return x > canvas.width || x < 0 || y > canvas.height || y < 0;
190 };
191
192 const resize = (
193 canvas: HTMLCanvasElement,
194 ctx?: CanvasRenderingContext2D,
195 ) => {
196 const { innerWidth, innerHeight } = window;
197
198 canvas.width = innerWidth;
199 canvas.height = innerHeight;
200
201 center[0] = 0.5 * canvas.width;
202 center[1] = 0.5 * canvas.height;
203 };
204
205 const renderGlow = (
206 canvas: HTMLCanvasElement,
207 ctx: CanvasRenderingContext2D,
208 ) => {
209 ctx.save();
210 ctx.filter = "blur(8px) brightness(200%)";
211 ctx.globalCompositeOperation = "lighter";
212 ctx.drawImage(canvas, 0, 0);
213 ctx.restore();
214
215 ctx.save();
216 ctx.filter = "blur(4px) brightness(200%)";
217 ctx.globalCompositeOperation = "lighter";
218 ctx.drawImage(canvas, 0, 0);
219 ctx.restore();
220 };
221
222 const renderToScreen = (
223 canvas: HTMLCanvasElement,
224 ctx: CanvasRenderingContext2D,
225 ) => {
226 ctx.save();
227 ctx.globalCompositeOperation = "lighter";
228 ctx.drawImage(canvas, 0, 0);
229 ctx.restore();
230 };
231
232 const handleResize = () => {
233 const canvas = canvasRef.current;
234 const ctx = canvas?.getContext("2d");
235 if (canvas && ctx) {
236 resize(canvas, ctx);
237 }
238 };
239
240 useEffect(() => {
241 setup();
242 window.addEventListener("resize", handleResize);
243
244 return () => {
245 window.removeEventListener("resize", handleResize);
246 if (animationFrameId.current) {
247 cancelAnimationFrame(animationFrameId.current);
248 }
249 };
250 }, []);
251
252 return (
253 <div className={cn("relative h-full w-full", props.containerClassName)}>
254 <motion.div
255 initial={{ opacity: 0 }}
256 animate={{ opacity: 1 }}
257 ref={containerRef}
258 className="absolute inset-0 z-0 flex h-full w-full items-center justify-center bg-transparent"
259 >
260 <canvas ref={canvasRef}></canvas>
261 </motion.div>
262
263 <div className={cn("relative z-10", props.className)}>
264 {props.children}
265 </div>
266 </div>
267 );
268};
269