asafefe.dev
Lab

Lab/24

Magnetic Grid

A grid of short needles, each on its own angular spring, turning to point at the cursor. Color follows the angle within a narrow band around vermilion. At rest they settle into rings.

  • Canvas
  • Interaction
Move to steer the needles
26px
0.10

Installation

Add Magnetic Grid to your project with the shadcn CLI. The files land in components/lab, ready to edit.

$ pnpm dlx shadcn@latest add @soultel/lab-magnetic-grid

Or by URL: https://asafefe.dev/r/lab-magnetic-grid.json

Brings along

  • utils
  • @soultel/lab-canvas

Usage

example.tsx10 lines
import { MagneticGrid } from "@/components/lab/magnetic-grid"

export function Hero() {
  return (
    <section className="relative h-[420px]">
      <MagneticGrid className="absolute inset-0" />
      <h1 className="relative">Hello</h1>
    </section>
  )
}

Props

PropTypeDefaultDescription
spacingnumber26Distance between needles in px.
lengthnumber0.62Needle length as a share of the spacing.
stiffnessnumber0.1Spring pull toward the cursor.
dampingnumber0.8Angular velocity kept per frame.
falloffnumber280Radius where needles grow and brighten.
hueRangenumber34Hue swing either side of the base color, in degrees.
colorstring"var(--primary)"Base color. Its hue sets the range.
restColorstring"var(--foreground)"Color far from the cursor.
lineWidthnumber1.6Needle thickness in px.