import React, { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
// 网络节点组件 - 代表Agent
const NetworkNode: React.FC<{
id: number;
onConnect: (id: number) => void;
connections: number[];
}> = ({ id, onConnect, connections }) => {
const [position] = useState({
x: Math.random() * 90 + 5,
y: Math.random() * 90 + 5,
size: Math.random() * 8 + 4,
});
const [isActive, setIsActive] = useState(false);
useEffect(() => {
const interval = setInterval(() => {
if (Math.random() < 0.3) {
setIsActive(true);
onConnect(id);
setTimeout(() => setIsActive(false), 2000);
}
}, Math.random() * 8000 + 5000);
return () => clearInterval(interval);
}, [id, onConnect]);
return (
{/* 节点核心 */}
{/* 活跃时的脉冲效果 */}
{isActive && (
)}
);
};
// 数据流粒子组件
const DataParticle: React.FC<{ path: { start: {x: number, y: number}, end: {x: number, y: number} } }> = ({ path }) => {
const [isVisible, setIsVisible] = useState(true);
useEffect(() => {
const timer = setTimeout(() => setIsVisible(false), 3000);
return () => clearTimeout(timer);
}, []);
if (!isVisible) return null;
const distance = Math.sqrt(
Math.pow(path.end.x - path.start.x, 2) + Math.pow(path.end.y - path.start.y, 2)
);
return (
);
};
// 连接线组件
const ConnectionLine: React.FC<{
start: {x: number, y: number};
end: {x: number, y: number};
isActive: boolean;
}> = ({ start, end, isActive }) => {
const length = Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2));
const angle = Math.atan2(end.y - start.y, end.x - start.x) * 180 / Math.PI;
return (
);
};
// 主背景组件
const AgentNetworkBackground: React.FC = () => {
const [activeConnections, setActiveConnections] = useState<{
start: {x: number, y: number};
end: {x: number, y: number};
id: string;
}[]>([]);
const [dataParticles, setDataParticles] = useState<{
path: { start: {x: number, y: number}, end: {x: number, y: number} };
id: string;
}[]>([]);
const nodePositions = React.useRef<{[key: number]: {x: number, y: number}}>({});
// 初始化节点位置
React.useEffect(() => {
for (let i = 0; i < 12; i++) {
nodePositions.current[i] = {
x: Math.random() * 90 + 5,
y: Math.random() * 90 + 5
};
}
}, []);
const handleNodeConnect = (nodeId: number) => {
// 随机选择其他节点连接
const otherNodes = Object.keys(nodePositions.current)
.map(Number)
.filter(id => id !== nodeId);
if (otherNodes.length === 0) return;
const targetId = otherNodes[Math.floor(Math.random() * otherNodes.length)];
const start = nodePositions.current[nodeId];
const end = nodePositions.current[targetId];
if (!start || !end) return;
const connectionId = `${nodeId}-${targetId}-${Date.now()}`;
// 添加连接线
const newConnection = { start, end, id: connectionId };
setActiveConnections(prev => [...prev, newConnection]);
// 添加数据粒子
const particleId = `particle-${Date.now()}`;
setDataParticles(prev => [...prev, { path: { start, end }, id: particleId }]);
// 清除连接线
setTimeout(() => {
setActiveConnections(prev => prev.filter(conn => conn.id !== connectionId));
}, 3000);
// 清除数据粒子
setTimeout(() => {
setDataParticles(prev => prev.filter(p => p.id !== particleId));
}, 3000);
};
return (
{/* 基础深色技术背景 */}
{/* 网格背景 - 体现技术感 */}
{/* 动态光晕效果 */}
{/* 数据流背景效果 */}
{/* 连接线层 */}
{activeConnections.map(connection => (
))}
{/* 网络节点 - Agent */}
{Array.from({ length: 12 }, (_, i) => (
))}
{/* 数据粒子层 */}
{dataParticles.map(particle => (
))}
{/* 顶部渐变遮罩 - 确保文字可读性 */}
{/* 性能优化 */}
);
};
export default AgentNetworkBackground;