perf(animation): optimize text animations for mobile phones with snappy word-level staggering and instant 120fps physics

This commit is contained in:
2026-11-05 12:45:11 +05:30
parent ec1f5c5c6a
commit f579b263b4
8 changed files with 110 additions and 53 deletions
+94 -37
View File
@@ -1,6 +1,6 @@
'use client';
import React from 'react';
import React, { useEffect, useState } from 'react';
import { motion, Variants } from 'framer-motion';
export type AnimationType =
@@ -27,41 +27,13 @@ interface TextAnimateProps {
animation?: AnimationType;
}
const defaultItemVariants: Record<AnimationType, Variants> = {
fadeIn: {
hidden: { opacity: 0 },
show: { opacity: 1, transition: { duration: 0.5, ease: [0.16, 1, 0.3, 1] } },
},
blurIn: {
hidden: { opacity: 0, filter: 'blur(8px)', y: 8 },
show: { opacity: 1, filter: 'blur(0px)', y: 0, transition: { duration: 0.6, ease: [0.16, 1, 0.3, 1] } },
},
slideUp: {
hidden: { y: 24, opacity: 0 },
show: { y: 0, opacity: 1, transition: { duration: 0.5, ease: [0.16, 1, 0.3, 1] } },
},
slideDown: {
hidden: { y: -24, opacity: 0 },
show: { y: 0, opacity: 1, transition: { duration: 0.5, ease: [0.16, 1, 0.3, 1] } },
},
slideLeft: {
hidden: { x: 24, opacity: 0 },
show: { x: 0, opacity: 1, transition: { duration: 0.5, ease: [0.16, 1, 0.3, 1] } },
},
slideRight: {
hidden: { x: -24, opacity: 0 },
show: { x: 0, opacity: 1, transition: { duration: 0.5, ease: [0.16, 1, 0.3, 1] } },
},
scaleUp: {
hidden: { scale: 0.85, opacity: 0 },
show: { scale: 1, opacity: 1, transition: { duration: 0.5, ease: [0.16, 1, 0.3, 1] } },
},
};
// Ultra-smooth Apple-grade exponential easing
const easeOutApple = [0.16, 1, 0.3, 1];
export function TextAnimate({
children,
delay = 0,
duration = 0.4,
duration,
className = '',
as: Component = 'span',
by = 'word',
@@ -73,14 +45,99 @@ export function TextAnimate({
return <Component className={className}>{children}</Component>;
}
const itemVariant = defaultItemVariants[animation] || defaultItemVariants.slideLeft;
// Detect mobile device to ensure ultra-smooth 60-120fps animations with zero multi-second lag
const [isMobile, setIsMobile] = useState<boolean>(false);
useEffect(() => {
if (typeof window === 'undefined') return;
const checkMobile = () => {
setIsMobile(
window.innerWidth < 768 ||
(window.matchMedia && window.matchMedia('(pointer: coarse)').matches)
);
};
checkMobile();
window.addEventListener('resize', checkMobile, { passive: true });
return () => window.removeEventListener('resize', checkMobile);
}, []);
// On phones, character-by-character animations cause CPU thrashing and take 2-3 seconds.
// Grouping into words on mobile gives instant, silky 120fps motion.
const activeBy = isMobile ? 'word' : by;
const activeDuration = duration ?? (isMobile ? 0.26 : 0.34);
const activeDelay = isMobile ? Math.min(delay * 0.4, 0.08) : delay;
const activeStagger = isMobile
? 0.018
: (activeBy === 'character' ? 0.010 : 0.03);
const offset = isMobile ? 10 : 16;
const itemVariants: Record<AnimationType, Variants> = {
fadeIn: {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { duration: activeDuration, ease: easeOutApple }
},
},
blurIn: {
hidden: { opacity: 0, filter: isMobile ? 'none' : 'blur(5px)', y: offset },
show: {
opacity: 1,
filter: 'blur(0px)',
y: 0,
transition: { duration: activeDuration, ease: easeOutApple }
},
},
slideUp: {
hidden: { y: offset, opacity: 0 },
show: {
y: 0,
opacity: 1,
transition: { duration: activeDuration, ease: easeOutApple }
},
},
slideDown: {
hidden: { y: -offset, opacity: 0 },
show: {
y: 0,
opacity: 1,
transition: { duration: activeDuration, ease: easeOutApple }
},
},
slideLeft: {
hidden: { x: offset, opacity: 0 },
show: {
x: 0,
opacity: 1,
transition: { duration: activeDuration, ease: easeOutApple }
},
},
slideRight: {
hidden: { x: -offset, opacity: 0 },
show: {
x: 0,
opacity: 1,
transition: { duration: activeDuration, ease: easeOutApple }
},
},
scaleUp: {
hidden: { scale: 0.94, opacity: 0 },
show: {
scale: 1,
opacity: 1,
transition: { duration: activeDuration, ease: easeOutApple }
},
},
};
const itemVariant = itemVariants[animation] || itemVariants.slideLeft;
const containerVariants: Variants = {
hidden: {},
show: {
transition: {
staggerChildren: by === 'character' ? 0.015 : 0.05,
delayChildren: delay,
staggerChildren: activeStagger,
delayChildren: activeDelay,
},
},
};
@@ -95,13 +152,13 @@ export function TextAnimate({
initial="hidden"
whileInView={startOnView ? 'show' : undefined}
animate={!startOnView ? 'show' : undefined}
viewport={{ once, margin: '-20px' }}
viewport={{ once, margin: '20px' }}
className={`inline ${className}`}
>
{words.map((word, wIdx) => (
<React.Fragment key={wIdx}>
<span className="inline-block whitespace-nowrap">
{by === 'character' ? (
{activeBy === 'character' ? (
Array.from(word).map((char, cIdx) => (
<motion.span
key={`${char}-${cIdx}`}