Scrambled Text

Characters near the pointer scramble into random glyphs and resolve back to the original text.

pnpm dlx shadcn@latest add @leo/scrambled-text

Props

NameTypeDefaultDescription
childrenReactNodeMove your pointer over this text to scramble it.
classNamestring | undefined-
durationnumber | undefined1.2
radiusnumber | undefined100
scrambleCharsstring | undefined.:
scrambleOnMountboolean | undefinedfalseScramble every character once on mount, so the effect is visible without a pointer.
speednumber | undefined0.5
styleCSSProperties | undefined-

Dependencies

  • gsap@^3.13.0

Source

"use client";

import { gsap } from "gsap";
import { ScrambleTextPlugin } from "gsap/ScrambleTextPlugin";
import { SplitText } from "gsap/SplitText";
import type React from "react";
import { useEffect, useRef } from "react";
import { cn } from "@/lib/utils";

gsap.registerPlugin(SplitText, ScrambleTextPlugin);

export interface ScrambledTextProps {
  radius?: number;
  duration?: number;
  speed?: number;
  scrambleChars?: string;
  /** Scramble every character once on mount, so the effect is visible without a pointer. */
  scrambleOnMount?: boolean;
  className?: string;
  style?: React.CSSProperties;
  children?: React.ReactNode;
}

const ScrambledText: React.FC<ScrambledTextProps> = ({
  radius = 100,
  duration = 1.2,
  speed = 0.5,
  scrambleChars = ".:",
  scrambleOnMount = false,
  className,
  style,
  children = "Move your pointer over this text to scramble it.",
}) => {
  const rootRef = useRef<HTMLDivElement | null>(null);

  useEffect(() => {
    if (!rootRef.current) return;

    const split = SplitText.create(rootRef.current.querySelector("p"), {
      type: "chars",
      charsClass: "inline-block will-change-transform",
    });

    split.chars.forEach((el) => {
      const c = el as HTMLElement;
      gsap.set(c, { attr: { "data-content": c.innerHTML } });
    });

    if (scrambleOnMount) {
      split.chars.forEach((el) => {
        const c = el as HTMLElement;
        gsap.to(c, {
          duration,
          scrambleText: { text: c.dataset.content || "", chars: scrambleChars, speed },
          ease: "none",
        });
      });
    }

    const handleMove = (e: PointerEvent) => {
      split.chars.forEach((el) => {
        const c = el as HTMLElement;
        const { left, top, width, height } = c.getBoundingClientRect();
        const dx = e.clientX - (left + width / 2);
        const dy = e.clientY - (top + height / 2);
        const dist = Math.hypot(dx, dy);

        if (dist < radius) {
          gsap.to(c, {
            overwrite: true,
            duration: duration * (1 - dist / radius),
            scrambleText: {
              text: c.dataset.content || "",
              chars: scrambleChars,
              speed,
            },
            ease: "none",
          });
        }
      });
    };

    const el = rootRef.current;
    el.addEventListener("pointermove", handleMove);

    return () => {
      el.removeEventListener("pointermove", handleMove);
      gsap.killTweensOf(split.chars);
      split.revert();
    };
  }, [radius, duration, speed, scrambleChars, scrambleOnMount]);

  return (
    <div
      ref={rootRef}
      className={cn(
        "m-[7vw] max-w-[800px] font-mono text-[clamp(14px,4vw,32px)] text-white",
        className,
      )}
      style={style}
    >
      <p>{children}</p>
    </div>
  );
};

export { ScrambledText };
export default ScrambledText;