Variable Font Cursor Proximity
A text component that animates the font variation settings of letters based on the cursor proximity. Works only with variable fonts.
A generalized version of this component (where you can control any CSS property) is available in the Text Cursor Proximity component.
Installation
npx shadcn@latest add "https://fancycomponents.dev/r/variable-font-cursor-proximity.json"
Understanding the component
The VariableFontCursorProximity
splits its text into letters, that respond to cursor movement by adjusting their font variation settings, based on the distance between the letter and cursor distance. This component works only with variable fonts.
- Splitting text into individual letters
- Tracking cursor position relative to each letter
- Smoothly transitioning font variations based on proximity
- Supporting multiple falloff patterns for the effect
This component requires the use of variable fonts to function properly. Otherwise it will not work.
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 font variations (like weight, slant, etc.) smoothly interpolate between two states:
- The default state (
fromFontVariationSettings
) - The target state (
toFontVariationSettings
)
The closer the cursor gets to a letter, the closer that letter moves toward its target state.
Understanding Variable Fonts
For more information about variable fonts and how they work, please refer to the Variable Font Hover By Letter documentation.
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.
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 experienceing performance issues, try to limit the length of the text you're animating.
Props
Prop | Type | Default | Description |
---|---|---|---|
label* | string | - | The text to be displayed and animated |
fromFontVariationSettings* | string | - | Default font variation settings |
toFontVariationSettings* | string | - | Target font variation settings |
containerRef* | React.RefObject<HTMLElement> | - | Reference to the container for mouse tracking |
radius | number | 50 | The radius of the proximity effect |
falloff | "linear" | "exponential" | "gaussian" | "linear" | The falloff type of the proximity |
className | string | - | Additional CSS classes for styling |