Text & Effects
Caret Field
A background of tiny carets that turn toward the cursor, with an occasional blink.
Move your cursor here
Installation
npx shadcn@latest add https://relic-ui.vercel.app/r/caret-field.jsonOr straight from GitHub: npx shadcn@latest add relicui/relic-ui/caret-field
Manual installation
1. Install the dependencies:
npm install motion2. Copy this code into components/ui/caret-field.tsx:
"use client"
/**
* Relic UI · Caret Field
* https://relic-ui.vercel.app/docs/caret-field
*
* Copyright (c) 2026 Shubham Tambe
* Open source under the MIT License: https://relic-ui.vercel.app/license
*/
import { useEffect, useRef } from "react"
import { useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
const BLINK_DURATION = 1800 // ms a gold caret stays
const BLINK_STEP = 350 // ms per on/off step, like a text cursor
type Cell = { x: number; y: number; t: number; a: number }
type Blink = { index: number; start: number }
/** Wraps an angle into (-π/2, π/2]. A line looks the same rotated by π. */
function wrapHalfTurn(angle: number) {
while (angle > Math.PI / 2) angle -= Math.PI
while (angle <= -Math.PI / 2) angle += Math.PI
return angle
}
/**
* A field of tiny carets that turn toward the cursor like iron filings near
* a magnet. Now and then one blinks in the accent color, as if someone is
* about to type.
*/
export interface CaretFieldProps {
className?: string
/** Distance between carets, in pixels. */
gap?: number
/** Caret length at rest, in pixels. */
length?: number
/** How far the cursor's pull reaches, in pixels. */
radius?: number
/** How many carets may blink in the accent color at once. 0 turns it off. */
maxBlinks?: number
/** Chance per frame that a new blink starts (0–1). */
blinkChance?: number
/** CSS variable for the resting carets. */
colorVar?: string
/** CSS variable for the blinking carets. */
accentVar?: string
/** Carets turn toward the cursor. Off keeps only the blinking. */
interactive?: boolean
/** Line thickness of each caret, in pixels. */
thickness?: number
/** Opacity of carets at rest (0–1). */
restOpacity?: number
/** How much carets grow near the cursor. 1 doubles their length, 0 keeps it. */
growth?: number
/** An invisible cursor drifts around when the real one is still or absent (phones). */
autoCursor?: boolean
}
export function CaretField({
className,
gap: GAP = 28,
length: LENGTH = 7,
radius: RADIUS = 180,
maxBlinks: MAX_BLINKS = 5,
blinkChance: BLINK_CHANCE = 0.025,
colorVar = "--muted-foreground",
accentVar = "--primary",
interactive = true,
thickness = 1.5,
restOpacity = 0.28,
growth = 1,
autoCursor = false,
}: CaretFieldProps) {
const canvasRef = useRef<HTMLCanvasElement>(null)
const reduceMotion = useReducedMotion()
useEffect(() => {
const canvas = canvasRef.current
const ctx = canvas?.getContext("2d")
if (!canvas || !ctx) return
let width = 0
let height = 0
let cells: Cell[] = []
let blinks: Blink[] = []
let frame = 0
let visible = true
const pointer = { x: 0, y: 0, active: false }
let lastRealMove = -Infinity
// The invisible cursor starts where the real one was last seen.
const ghost = { x: 0, y: 0 }
const colors = { line: "gray", brand: "orange" }
function readColors() {
const style = getComputedStyle(canvas!)
colors.line = style.getPropertyValue(colorVar).trim() || "gray"
colors.brand = style.getPropertyValue(accentVar).trim() || "orange"
}
function layout() {
const rect = canvas!.getBoundingClientRect()
width = rect.width
height = rect.height
const dpr = Math.min(window.devicePixelRatio || 1, 2)
canvas!.width = Math.round(width * dpr)
canvas!.height = Math.round(height * dpr)
ctx!.setTransform(dpr, 0, 0, dpr, 0, 0)
const cols = Math.floor(width / GAP) + 1
const rows = Math.floor(height / GAP) + 1
const offsetX = (width - (cols - 1) * GAP) / 2
const offsetY = (height - (rows - 1) * GAP) / 2
cells = []
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
cells.push({ x: offsetX + c * GAP, y: offsetY + r * GAP, t: 0, a: 0 })
}
}
blinks = []
if (lastRealMove === -Infinity) {
ghost.x = width / 2
ghost.y = height / 2
}
}
function line(x: number, y: number, angle: number, length: number) {
const ux = Math.sin(angle) * (length / 2)
const uy = Math.cos(angle) * (length / 2)
ctx!.moveTo(x - ux, y - uy)
ctx!.lineTo(x + ux, y + uy)
}
function draw(now: number) {
// With autoCursor, an invisible cursor drifts around whenever the real
// one has been still for a while (and always on touch screens).
let px = pointer.x
let py = pointer.y
let pActive = pointer.active
if (autoCursor && !reduceMotion && now - lastRealMove > 2500) {
pActive = true
// Ease the invisible cursor toward a slow drifting point, so it never jumps.
const targetX = width * (0.5 + 0.34 * Math.sin(now * 0.00019))
const targetY = height * (0.5 + 0.3 * Math.sin(now * 0.00029 + 1.3))
ghost.x += (targetX - ghost.x) * 0.02
ghost.y += (targetY - ghost.y) * 0.02
px = ghost.x
py = ghost.y
}
ctx!.clearRect(0, 0, width, height)
ctx!.lineCap = "round"
ctx!.lineWidth = thickness
ctx!.strokeStyle = colors.line
// Carets at rest share one path, which keeps drawing cheap.
const resting = new Path2D()
const active: Cell[] = []
for (const cell of cells) {
let targetT = 0
let targetA = 0
if (interactive && pActive && !reduceMotion) {
const dx = px - cell.x
const dy = py - cell.y
const distance = Math.hypot(dx, dy)
if (distance < RADIUS) {
const k = 1 - distance / RADIUS
targetT = k * k * (3 - 2 * k) // smoothstep
targetA = wrapHalfTurn(Math.atan2(dy, dx) - Math.PI / 2)
}
}
cell.t += (targetT - cell.t) * 0.1
cell.a += wrapHalfTurn(targetA - cell.a) * 0.12
if (cell.t < 0.01 && Math.abs(cell.a) < 0.01) {
resting.moveTo(cell.x, cell.y - LENGTH / 2)
resting.lineTo(cell.x, cell.y + LENGTH / 2)
} else {
active.push(cell)
}
}
ctx!.globalAlpha = restOpacity
ctx!.stroke(resting)
for (const cell of active) {
ctx!.globalAlpha = restOpacity + cell.t * (0.85 - restOpacity)
ctx!.beginPath()
line(cell.x, cell.y, cell.a, LENGTH * (1 + cell.t * growth))
ctx!.stroke()
}
// Gold blinking carets.
if (!reduceMotion && cells.length) {
if (blinks.length < MAX_BLINKS && Math.random() < BLINK_CHANCE) {
blinks.push({ index: Math.floor(Math.random() * cells.length), start: now })
}
blinks = blinks.filter((b) => now - b.start < BLINK_DURATION)
ctx!.strokeStyle = colors.brand
ctx!.lineWidth = 2
for (const blink of blinks) {
const on = Math.floor((now - blink.start) / BLINK_STEP) % 2 === 0
if (!on) continue
const cell = cells[blink.index]
ctx!.globalAlpha = 0.95
ctx!.beginPath()
line(cell.x, cell.y, cell.a, LENGTH * 2)
ctx!.stroke()
}
}
ctx!.globalAlpha = 1
}
function loop(now: number) {
draw(now)
if (visible && !document.hidden) frame = requestAnimationFrame(loop)
}
function start() {
cancelAnimationFrame(frame)
if (reduceMotion) draw(performance.now())
else frame = requestAnimationFrame(loop)
}
function handlePointerMove(e: PointerEvent) {
const rect = canvas!.getBoundingClientRect()
lastRealMove = performance.now()
pointer.x = e.clientX - rect.left
pointer.y = e.clientY - rect.top
ghost.x = pointer.x
ghost.y = pointer.y
pointer.active =
pointer.x > -RADIUS &&
pointer.y > -RADIUS &&
pointer.x < width + RADIUS &&
pointer.y < height + RADIUS
}
function handlePointerLeave() {
pointer.active = false
}
readColors()
layout()
start()
const resizeObserver = new ResizeObserver(() => {
layout()
if (reduceMotion) draw(performance.now())
})
resizeObserver.observe(canvas)
// Pause while the hero is scrolled out of view.
const intersectionObserver = new IntersectionObserver(([entry]) => {
visible = entry.isIntersecting
if (visible) start()
else cancelAnimationFrame(frame)
})
intersectionObserver.observe(canvas)
// Redraw with new colors when light/dark mode changes.
const themeObserver = new MutationObserver(() => {
readColors()
if (reduceMotion) draw(performance.now())
})
themeObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class", "style"],
})
const handleVisibility = () => {
if (!document.hidden && visible) start()
}
window.addEventListener("pointermove", handlePointerMove, { passive: true })
document.documentElement.addEventListener("pointerleave", handlePointerLeave)
document.addEventListener("visibilitychange", handleVisibility)
return () => {
cancelAnimationFrame(frame)
resizeObserver.disconnect()
intersectionObserver.disconnect()
themeObserver.disconnect()
window.removeEventListener("pointermove", handlePointerMove)
document.documentElement.removeEventListener("pointerleave", handlePointerLeave)
document.removeEventListener("visibilitychange", handleVisibility)
}
}, [reduceMotion, GAP, LENGTH, RADIUS, MAX_BLINKS, BLINK_CHANCE, colorVar, accentVar, interactive, thickness, restOpacity, growth, autoCursor])
return (
<canvas
ref={canvasRef}
aria-hidden
className={cn("pointer-events-none block size-full", className)}
/>
)
}Usage
import { CaretField } from "@/components/ui/caret-field"
export default function CaretFieldDemo() {
return (
<div className="relative h-64 w-full overflow-hidden rounded-xl border">
<CaretField className="absolute inset-0" />
<p className="absolute inset-0 flex items-center justify-center text-sm text-muted-foreground">
Move your cursor here
</p>
</div>
)
}Customize
Try the options below, then copy the code.
<CaretField
className="absolute inset-0"
/>Props
| Prop | Type | Default |
|---|---|---|
gapDistance between carets, in pixels. | number | 28 |
lengthCaret length at rest, in pixels. | number | 7 |
radiusHow far the cursor's pull reaches, in pixels. | number | 180 |
maxBlinksHow many carets may blink at once. 0 turns blinking off. | number | 5 |
blinkChanceChance per frame that a new blink starts (0–1). | number | 0.025 |
colorVarCSS variable for the resting carets. | string | "--muted-foreground" |
accentVarCSS variable for the blinking carets. | string | "--primary" |
interactiveCarets turn toward the cursor. Off keeps only the blinking. | boolean | true |
thicknessLine thickness of each caret, in pixels. | number | 1.5 |
restOpacityOpacity of carets at rest (0–1). | number | 0.28 |
growthHow much carets grow near the cursor. 1 doubles their length, 0 keeps it. | number | 1 |
classNameSize and place it, usually "absolute inset-0" inside a relative parent. | string | — |
Accessibility
- Purely decorative: hidden from screen readers and never blocks clicks.
- Pauses when scrolled out of view or when the tab is hidden, to save battery.
- With reduced motion on, it's a still pattern.