Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 1x 1x 1x 1x 100x 9470x 9470x 100x 100x 100x 100x 100x 100x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 100x 100x 5150x 1x 100x 100x 4950x 4950x 4950x 4950x 4950x 4950x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 4685x 1x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 1x 1x 1x 1x 1x | import { useEffect, useRef } from "react";
type Node3D = { x: number; y: number; z: number };
interface NetworkGraphProps {
classes?: string;
width?: number;
height?: number;
}
const NUM_NODES = 100;
const NODE_RADIUS = 4;
const CONNECTION_DISTANCE = 150;
function generateNodes(width: number, height: number): Node3D[] {
// We'll create a cube roughly sized proportional to width & height,
// e.g. cube side = average of width and height * 0.7 (adjust as needed)
const cubeSize = ((width + height) / 2) * 0.7;
return Array.from({ length: NUM_NODES }, () => ({
x: Math.random() * cubeSize - cubeSize / 2,
y: Math.random() * cubeSize - cubeSize / 2,
z: Math.random() * cubeSize - cubeSize / 2,
}));
}
function project(
node: Node3D,
width: number,
height: number,
fov: number,
viewerDistance: number,
) {
const factor = fov / (viewerDistance + node.z);
return {
x: node.x * factor + width / 2,
y: node.y * factor + height / 2,
};
}
function rotateY(node: Node3D, angle: number): Node3D {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return {
x: node.x * cos - node.z * sin,
y: node.y,
z: node.x * sin + node.z * cos,
};
}
function rotateX(node: Node3D, angle: number): Node3D {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return {
x: node.x,
y: node.y * cos - node.z * sin,
z: node.y * sin + node.z * cos,
};
}
export function NetworkGraph({
classes = "",
width = 600,
height = 400,
}: NetworkGraphProps) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const nodesRef = useRef<Node3D[]>(generateNodes(width, height));
const rotation = useRef({ x: 0, y: 0 });
const targetRotation = useRef({ x: 0, y: 0 });
const autoAngle = useRef(0);
const breatheTime = useRef(0);
// useEffect(() => {
// // Regenerate nodes if width or height changes
// nodesRef.current = generateNodes(width, height);
// }, [width, height]);
// useEffect(() => {
// if (!canvasRef.current) return;
// const canvas = canvasRef.current;
// // Match drawing resolution to CSS size
// const rect = canvas.getBoundingClientRect();
// canvas.width = rect.width;
// canvas.height = rect.height;
// }, []);
// useEffect(() => {
// function handleResize() {
// if (!canvasRef.current) return;
// const rect = canvasRef.current.getBoundingClientRect();
// canvasRef.current.width = rect.width;
// canvasRef.current.height = rect.height;
// }
// handleResize();
// window.addEventListener("resize", handleResize);
// return () => window.removeEventListener("resize", handleResize);
// }, []);
useEffect(() => {
const canvas = canvasRef.current;
const ctx = canvas?.getContext("2d");
Iif (!ctx || !canvas) return;
canvas.width = width;
canvas.height = height;
const fov = 500;
const viewerDistance = 600;
function onMouseMove(e: MouseEvent) {
if (!canvas) return;
const rect = canvas.getBoundingClientRect();
const mouseX = ((e.clientX - rect.left) / rect.width) * 2 - 1;
const mouseY = ((e.clientY - rect.top) / rect.height) * 2 - 1;
targetRotation.current.y = mouseX * 0.3;
targetRotation.current.x = -mouseY * 0.3;
}
canvas.addEventListener("mousemove", onMouseMove);
function draw() {
Iif (!ctx) return;
ctx.clearRect(0, 0, width, height);
autoAngle.current += 0.0002;
breatheTime.current += 0.0002;
rotation.current.x +=
(targetRotation.current.x - rotation.current.x) * 0.05;
rotation.current.y +=
(targetRotation.current.y - rotation.current.y) * 0.05;
const totalY = rotation.current.y + autoAngle.current;
const totalX =
rotation.current.x + Math.sin(autoAngle.current * 0.4) * 0.08;
const dynamicViewerDistance =
viewerDistance - Math.sin(breatheTime.current) * 80;
const nodes = nodesRef.current;
const rotatedNodes = nodes.map((node) => {
const ry = rotateY(node, totalY);
return rotateX(ry, totalX);
});
const isVisible = (node: Node3D) => dynamicViewerDistance + node.z > 0;
// Draw connections
for (let i = 0; i < rotatedNodes.length; i++) {
Iif (!isVisible(rotatedNodes[i])) continue;
for (let j = i + 1; j < rotatedNodes.length; j++) {
Iif (!isVisible(rotatedNodes[j])) continue;
const dx = rotatedNodes[i].x - rotatedNodes[j].x;
const dy = rotatedNodes[i].y - rotatedNodes[j].y;
const dz = rotatedNodes[i].z - rotatedNodes[j].z;
const dist = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (dist < CONNECTION_DISTANCE) {
const p1 = project(
rotatedNodes[i],
width,
height,
fov,
dynamicViewerDistance,
);
const p2 = project(
rotatedNodes[j],
width,
height,
fov,
dynamicViewerDistance,
);
ctx.strokeStyle = `rgba(100, 150, 255, ${
1 - dist / CONNECTION_DISTANCE
})`;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
}
}
}
// Draw nodes
rotatedNodes.forEach((node) => {
Iif (!isVisible(node)) return;
const p = project(node, width, height, fov, dynamicViewerDistance);
const scale = fov / (dynamicViewerDistance + node.z);
const radius = NODE_RADIUS * scale;
ctx.beginPath();
ctx.fillStyle = "blue";
ctx.shadowColor = "rgba(0, 0, 0, 0.3)";
ctx.shadowBlur = 4;
ctx.arc(p.x, p.y, radius, 0, Math.PI * 2);
ctx.fill();
ctx.shadowBlur = 0;
});
requestAnimationFrame(draw);
}
draw();
return () => {
canvas.removeEventListener("mousemove", onMouseMove);
};
}, [width, height]);
return (
<canvas
ref={canvasRef}
width={width}
height={height}
className={`rounded-lg ${classes}`}
style={{ display: "block", backgroundColor: "transparent" }}
/>
);
}
|