Docs
Network Background
Network Background
A dynamic network-style background with animated particles and connections
Glass UI
"I am the one who designs."
"You're darn right."
- Heisenberg, Engineer
Installation
Copy and paste the following code into your project.
"use client"
import React, { useEffect, useRef } from "react"
import { motion } from "framer-motion"
import { useTheme } from "next-themes"
const NetworkBackground: React.FC<{ className?: string }> = ({ className }) => {
const canvasRef = useRef<HTMLCanvasElement>(null)
const { theme } = useTheme()
useEffect(() => {
const canvas = canvasRef.current
if (!canvas) return
const ctx = canvas.getContext("2d")
if (!ctx) return
canvas.width = window.innerWidth
canvas.height = window.innerHeight
const particles: Particle[] = []
const particleCount = 100
const connectionDistance = 150
class Particle {
x: number
y: number
size: number
speedX: number
speedY: number
constructor() {
this.x = Math.random() * canvas.width
this.y = Math.random() * canvas.height
this.size = Math.random() * 3 + 1
this.speedX = Math.random() * 3 - 1.5
this.speedY = Math.random() * 3 - 1.5
}
update() {
this.x += this.speedX
this.y += this.speedY
if (this.x > canvas.width) this.x = 0
else if (this.x < 0) this.x = canvas.width
if (this.y > canvas.height) this.y = 0
else if (this.y < 0) this.y = canvas.height
}
draw(color: string) {
ctx!.fillStyle = color
ctx!.beginPath()
ctx!.arc(this.x, this.y, this.size, 0, Math.PI * 2)
ctx!.fill()
}
}
for (let i = 0; i < particleCount; i++) {
particles.push(new Particle())
}
function animate() {
ctx!.clearRect(0, 0, canvas.width, canvas.height)
const particleColor = "rgba(128, 128, 128, 0.8)" // Changed to gray
const lineColor = theme === "dark" ? "255, 255, 255" : "0, 0, 0"
for (let i = 0; i < particles.length; i++) {
particles[i].update()
particles[i].draw(particleColor)
for (let j = i; j < particles.length; j++) {
const dx = particles[i].x - particles[j].x
const dy = particles[i].y - particles[j].y
const distance = Math.sqrt(dx * dx + dy * dy)
if (distance < connectionDistance) {
ctx!.strokeStyle = `rgba(${lineColor}, ${
1 - distance / connectionDistance
})`
ctx!.lineWidth = 1
ctx!.beginPath()
ctx!.moveTo(particles[i].x, particles[i].y)
ctx!.lineTo(particles[j].x, particles[j].y)
ctx!.stroke()
}
}
}
requestAnimationFrame(animate)
}
animate()
const handleResize = () => {
canvas.width = window.innerWidth
canvas.height = window.innerHeight
}
window.addEventListener("resize", handleResize)
return () => {
window.removeEventListener("resize", handleResize)
}
}, [theme])
const bgClass =
theme === "dark"
? "bg-gradient-to-br from-gray-900 to-gray-800"
: "bg-gradient-to-br from-gray-100 to-white"
return (
<motion.div
className={`absolute inset-0 ${className}`}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 1 }}
>
<canvas ref={canvasRef} className={`w-full h-full ${bgClass}`} />
</motion.div>
)
}
export NetworkBackground
Update the import paths to match your project setup.
Usage
import NetworkBackground from "@/components/ui/network-background"
export default function NetworkBackgroundDemo() {
return (
<div className='relative min-h-screen'>
<NetworkBackground className="z-0" />
<div className='relative z-10 flex flex-col justify-center items-center min-h-screen text-black dark:text-white'>
<h1 className="text-4xl font-bold mb-4 text-gradient from-blue-500 to-purple-500">Glass UI</h1>
<p className="text-lg text-gray-500">Your one stop shop for all your UI needs</p>
</div>
</div>
)
}
Props
The NetworkBackground
component accepts the following props:
className
(optional string): Additional CSS classes to apply to the component.
Features
- Responsive canvas that adjusts to window size
- Animated particles with connecting lines
- Theme-aware coloring (adjusts to light/dark mode)
- Smooth opacity animation on mount
Customization
You can customize the Network Background by modifying the following parameters in the component:
particleCount
: Number of particles in the backgroundconnectionDistance
: Maximum distance for particles to connectparticleColor
: Color of the particleslineColor
: Color of the connecting lines (adjusts based on theme)- Background gradient: Modify the
bgClass
variable to change the background gradient