Randomized Text Unveil
A randomized text effect component that creates dynamic and unpredictable animations for text elements before displaying the user-provided text.
A randomized text effect component that creates dynamic and unpredictable animations for text elements before displaying the user-provided text.
npm install framer-motion @motionone/utils
1'use client';23import React, { useEffect, useState, useCallback } from 'react';45const lettersAndSymbols = 'abcdefghijklmnopqrstuvwxyz!@#$%^&*-_+=;:<>,';67interface AnimatedTextProps {8text: string;9}1011export function RandomizedTextEffect({ text }: AnimatedTextProps) {12const [animatedText, setAnimatedText] = useState('');1314const getRandomChar = useCallback(15() =>16lettersAndSymbols[Math.floor(Math.random() * lettersAndSymbols.length)],17[]18);1920const animateText = useCallback(async () => {21const duration = 50;22const revealDuration = 80;23const initialRandomDuration = 300;2425const generateRandomText = () =>26text27.split('')28.map(() => getRandomChar())29.join('');3031setAnimatedText(generateRandomText());3233const endTime = Date.now() + initialRandomDuration;34while (Date.now() < endTime) {35await new Promise((resolve) => setTimeout(resolve, duration));36setAnimatedText(generateRandomText());37}3839for (let i = 0; i < text.length; i++) {40await new Promise((resolve) => setTimeout(resolve, revealDuration));41setAnimatedText(42(prevText) =>43text.slice(0, i + 1) +44prevText45.slice(i + 1)46.split('')47.map(() => getRandomChar())48.join('')49);50}51}, [text, getRandomChar]);5253useEffect(() => {54animateText();55}, [text, animateText]);5657return <div className='relative inline-block'>{animatedText}</div>;58}