NetworkGraph
threecn/network-graphFreeComponent
A living plexus of drifting nodes that link up when close, fading from --primary to --muted-foreground.
Install it
npx shadcn@latest add https://threecn.dev/r/network-graph.jsonSource
1"use client"23import * as React from "react"4import { useFrame } from "@react-three/fiber"5import * as THREE from "three"67import {8 SceneContainer,9 type SceneContainerProps,10} from "@/components/threecn/scene-container"11import {12 useShadcnTheme,13 type ThemeMode,14} from "@/components/hooks/use-shadcn-theme"1516const RADIUS = 2.71718function seedPositions(count: number) {19 const positions = new Float32Array(count * 3)20 for (let i = 0; i < count; i++) {21 // Uniform-ish distribution inside a sphere.22 const r = Math.cbrt(Math.random()) * RADIUS * 0.9223 const theta = Math.random() * Math.PI * 224 const phi = Math.acos(2 * Math.random() - 1)25 positions[i * 3] = r * Math.sin(phi) * Math.cos(theta)26 positions[i * 3 + 1] = r * Math.sin(phi) * Math.sin(theta)27 positions[i * 3 + 2] = r * Math.cos(phi)28 }29 return positions30}3132function seedVelocities(count: number) {33 const velocities = new Float32Array(count * 3)34 for (let i = 0; i < velocities.length; i++) {35 velocities[i] = (Math.random() - 0.5) * 0.3536 }37 return velocities38}3940function Graph({41 nodes,42 linkDistance,43 speed,44 theme,45}: {46 nodes: number47 linkDistance: number48 speed: number49 theme: ThemeMode50}) {51 const { primaryColor, mutedColor } = useShadcnTheme(theme)52 const groupRef = React.useRef<THREE.Group>(null)53 const pointGeoRef = React.useRef<THREE.BufferGeometry>(null)54 const lineGeoRef = React.useRef<THREE.BufferGeometry>(null)55 /** Node velocities, re-seeded whenever the position buffer is replaced. */56 const velocitiesRef = React.useRef<{57 key: THREE.BufferAttribute58 values: Float32Array59 } | null>(null)60 const tmpColor = React.useMemo(() => new THREE.Color(), [])6162 const maxEdges = nodes * 863 const positions = React.useMemo(() => seedPositions(nodes), [nodes])64 const linePositions = React.useMemo(65 () => new Float32Array(maxEdges * 6),66 [maxEdges]67 )68 const lineColors = React.useMemo(69 () => new Float32Array(maxEdges * 6),70 [maxEdges]71 )7273 useFrame((_, delta) => {74 const pointGeo = pointGeoRef.current75 const lineGeo = lineGeoRef.current76 if (!pointGeo || !lineGeo) return7778 const posAttr = pointGeo.getAttribute("position") as THREE.BufferAttribute79 const pos = posAttr.array as Float32Array80 if (!velocitiesRef.current || velocitiesRef.current.key !== posAttr) {81 velocitiesRef.current = { key: posAttr, values: seedVelocities(nodes) }82 }83 const vel = velocitiesRef.current.values8485// … truncated
What it pulls in
npm packages
Files it writes
More from threecn
All 28 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| AuroraRibbonsaurora-ribbons | threecn | Components | Free | 3 deps · 3 files | |
| Boidsboids | threecn | Components | Free | 3 deps · 3 files | |
| Clothcloth | threecn | Components | Free | 3 deps · 3 files | |
| Crystalcrystal | threecn | Components | Free | 3 deps · 3 files | |
| CubeWavecube-wave | threecn | Components | Free | 3 deps · 3 files | |
| CurlFlowcurl-flow | threecn | Components | Free | 3 deps · 3 files | |
| DnaHelixdna-helix | threecn | Components | Free | 3 deps · 3 files | |
| FloatingCard3Dfloating-card-3d | threecn | Components | Free | 3 deps · 3 files |
