Docs
Text Cursor Proximity

Text Cursor Proximity

A text component that animates the letters based on the cursor proximity

DIGITALWORKSHOP

Installation


npx shadcn@latest add "https://fancycomponents.dev/r/text-cursor-proximity.json"

Understanding the component


The TextCursorProximity splits its text into letters that respond to cursor movement by adjusting their CSS properties based on the distance between the letter and cursor position.

  1. Splitting text into individual letters
  2. Tracking cursor position relative to each letter
  3. Smoothly transitioning CSS values with motion's useTransform hook
  4. Supporting multiple falloff patterns for the effect

How it works

The component calculates the distance between the cursor and each letter in real-time. When the cursor comes within the specified radius of a letter, that letter's CSS properties (like scale, color, etc.) smoothly interpolate between two states. For this, we use the motion library's useTransform hook, which maps the CSS properties from the styles.*.from state to the styles.*.to state based on the proximity value (which ranges from 0 to 1).

  • Default state: (defined in styles.*.from)
  • Target state (defined in styles.*.to)

You can interpolate any value that motion supports (which is actually any CSS value, even those that can't be animated by the browser, like mask-image).

The closer the cursor gets to a letter, the closer that letter moves toward its target state.

Examples


Falloff

With the falloff prop, you can control the type of falloff. It can be either linear, exponential, or gaussian. The following demo showcases the exponential one. The effects are best observed on a larger block of text.

                                                                                                                                         Just as every problem is novel and different from others. so the grid must be conceived afresh every time so as to meet requirements. This means that the designer must approach each new problem with an open mind and must seek to solve it by analysing it objectively. The difficulties of the task are due to the enormous differences in the demands made on the designer by the various assignments he receives. A small newspaper advertisement does not present the difficulties of designing, say, a daily paper with 10 and more columns. a great variety of subjects, and an additional advertising section. Such a task calls not only for designing talent but also organizing ability since the many constantly changing items of information have to be arranged in a logical order and their priorities reflected in appropriate typography.

Notes


It seems like interpolating on large number of letters simultaneously can be a bit slow, even when we're avoiding re-renders with state updates. If you're experiencing performance issues, try to limit the length of the text you're animating.

Props


PropTypeDefaultDescription
label*string-The text to be displayed and animated
styles*Partial<{ [K in keyof CSSProperties]: { from: string | number, to: string | number } }>-CSS properties to animate and their from/to values
containerRef*React.RefObject<HTMLDivElement>-Reference to the container for mouse tracking
radiusnumber50The radius of the proximity effect in pixels
falloff"linear" | "exponential" | "gaussian""linear"The falloff pattern for the proximity effect
classNamestring-Additional CSS classes for styling