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'; 'use client';
import React from 'react'; import React, { useEffect, useState } from 'react';
import { motion, Variants } from 'framer-motion'; import { motion, Variants } from 'framer-motion';
export type AnimationType = export type AnimationType =
@@ -27,41 +27,13 @@ interface TextAnimateProps {
animation?: AnimationType; animation?: AnimationType;
} }
const defaultItemVariants: Record<AnimationType, Variants> = { // Ultra-smooth Apple-grade exponential easing
fadeIn: { const easeOutApple = [0.16, 1, 0.3, 1];
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] } },
},
};
export function TextAnimate({ export function TextAnimate({
children, children,
delay = 0, delay = 0,
duration = 0.4, duration,
className = '', className = '',
as: Component = 'span', as: Component = 'span',
by = 'word', by = 'word',
@@ -73,14 +45,99 @@ export function TextAnimate({
return <Component className={className}>{children}</Component>; 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 = { const containerVariants: Variants = {
hidden: {}, hidden: {},
show: { show: {
transition: { transition: {
staggerChildren: by === 'character' ? 0.015 : 0.05, staggerChildren: activeStagger,
delayChildren: delay, delayChildren: activeDelay,
}, },
}, },
}; };
@@ -95,13 +152,13 @@ export function TextAnimate({
initial="hidden" initial="hidden"
whileInView={startOnView ? 'show' : undefined} whileInView={startOnView ? 'show' : undefined}
animate={!startOnView ? 'show' : undefined} animate={!startOnView ? 'show' : undefined}
viewport={{ once, margin: '-20px' }} viewport={{ once, margin: '20px' }}
className={`inline ${className}`} className={`inline ${className}`}
> >
{words.map((word, wIdx) => ( {words.map((word, wIdx) => (
<React.Fragment key={wIdx}> <React.Fragment key={wIdx}>
<span className="inline-block whitespace-nowrap"> <span className="inline-block whitespace-nowrap">
{by === 'character' ? ( {activeBy === 'character' ? (
Array.from(word).map((char, cIdx) => ( Array.from(word).map((char, cIdx) => (
<motion.span <motion.span
key={`${char}-${cIdx}`} key={`${char}-${cIdx}`}
+2 -2
View File
@@ -60,13 +60,13 @@ export function FaqSection({
</div> </div>
<h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight text-balance"> <h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight text-balance">
<TextAnimate animation="slideLeft" by="character"> <TextAnimate animation="slideLeft" by="word">
{resolvedTitle} {resolvedTitle}
</TextAnimate> </TextAnimate>
</h2> </h2>
<p className="text-sm sm:text-base text-slate-600 font-sans text-pretty"> <p className="text-sm sm:text-base text-slate-600 font-sans text-pretty">
<TextAnimate animation="fadeIn" by="word" delay={0.15}> <TextAnimate animation="fadeIn" by="word" delay={0.06}>
Everything you need to know about our browser trial and privacy guarantees. Everything you need to know about our browser trial and privacy guarantees.
</TextAnimate> </TextAnimate>
</p> </p>
+4 -4
View File
@@ -317,12 +317,12 @@ export function HeroSection({
{/* Grand Headline: Masterpiece Proportions, Zero Awkward Wraps */} {/* Grand Headline: Masterpiece Proportions, Zero Awkward Wraps */}
<h1 className="text-3xl sm:text-5xl md:text-6xl lg:text-[5.2rem] xl:text-[5.8rem] font-display font-extrabold tracking-[-0.035em] leading-[1.08] max-w-6xl mx-auto text-balance"> <h1 className="text-3xl sm:text-5xl md:text-6xl lg:text-[5.2rem] xl:text-[5.8rem] font-display font-extrabold tracking-[-0.035em] leading-[1.08] max-w-6xl mx-auto text-balance">
<span className="block text-slate-950 pb-1 sm:pb-2"> <span className="block text-slate-950 pb-1 sm:pb-2">
<TextAnimate animation="slideLeft" by="character"> <TextAnimate animation="slideLeft" by="word">
Imagine. Transform. Imagine. Transform.
</TextAnimate> </TextAnimate>
</span> </span>
<span className="block mt-1 sm:mt-2 bg-gradient-to-r from-blue-600 via-indigo-600 to-cyan-500 bg-clip-text text-transparent pb-3 sm:whitespace-nowrap"> <span className="block mt-1 sm:mt-2 text-blue-600 pb-3 sm:whitespace-nowrap font-black">
<TextAnimate animation="slideLeft" by="character" delay={0.15}> <TextAnimate animation="slideLeft" by="word" delay={0.06}>
Create the Impossible. Create the Impossible.
</TextAnimate> </TextAnimate>
</span> </span>
@@ -330,7 +330,7 @@ export function HeroSection({
{/* Inspiring, Refined Editorial Subtitle */} {/* Inspiring, Refined Editorial Subtitle */}
<p className="mt-6 sm:mt-7 text-base sm:text-lg md:text-xl text-slate-600/90 max-w-2xl sm:max-w-3xl mx-auto font-sans leading-relaxed tracking-[-0.01em] text-balance"> <p className="mt-6 sm:mt-7 text-base sm:text-lg md:text-xl text-slate-600/90 max-w-2xl sm:max-w-3xl mx-auto font-sans leading-relaxed tracking-[-0.01em] text-balance">
<TextAnimate animation="fadeIn" by="word" delay={0.25}> <TextAnimate animation="fadeIn" by="word" delay={0.12}>
Turn ordinary moments into extraordinary visual art. Instant, effortless, and private photo magic right in your browser. Turn ordinary moments into extraordinary visual art. Instant, effortless, and private photo magic right in your browser.
</TextAnimate> </TextAnimate>
</p> </p>
@@ -46,13 +46,13 @@ export function HowItWorks3DSection({
</div> </div>
<h2 className="text-3xl sm:text-5xl font-display font-black text-slate-900 tracking-tight text-balance"> <h2 className="text-3xl sm:text-5xl font-display font-black text-slate-900 tracking-tight text-balance">
<TextAnimate animation="slideLeft" by="character"> <TextAnimate animation="slideLeft" by="word">
{title} {title}
</TextAnimate> </TextAnimate>
</h2> </h2>
<p className="text-base sm:text-lg text-slate-600 font-sans text-pretty"> <p className="text-base sm:text-lg text-slate-600 font-sans text-pretty">
<TextAnimate animation="fadeIn" by="word" delay={0.2}> <TextAnimate animation="fadeIn" by="word" delay={0.08}>
{subtitle} {subtitle}
</TextAnimate> </TextAnimate>
</p> </p>
@@ -96,13 +96,13 @@ export function RecommendedAppsSection({
</div> </div>
<h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight"> <h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight">
<TextAnimate animation="slideLeft" by="character"> <TextAnimate animation="slideLeft" by="word">
{title} {title}
</TextAnimate> </TextAnimate>
</h2> </h2>
<p className="text-sm sm:text-base text-slate-600 font-sans text-pretty"> <p className="text-sm sm:text-base text-slate-600 font-sans text-pretty">
<TextAnimate animation="fadeIn" by="word" delay={0.2}> <TextAnimate animation="fadeIn" by="word" delay={0.08}>
{subtitle} {subtitle}
</TextAnimate> </TextAnimate>
</p> </p>
@@ -112,13 +112,13 @@ export function ReviewsMarqueeSection({
</div> </div>
<h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight text-balance"> <h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight text-balance">
<TextAnimate animation="slideLeft" by="character"> <TextAnimate animation="slideLeft" by="word">
{title} {title}
</TextAnimate> </TextAnimate>
</h2> </h2>
<p className="text-sm sm:text-base text-slate-600 font-sans text-pretty"> <p className="text-sm sm:text-base text-slate-600 font-sans text-pretty">
<TextAnimate animation="fadeIn" by="word" delay={0.2}> <TextAnimate animation="fadeIn" by="word" delay={0.08}>
{subtitle} {subtitle}
</TextAnimate> </TextAnimate>
</p> </p>
@@ -122,13 +122,13 @@ export function ShiftableToolsGridSection({
</div> </div>
<h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight"> <h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight">
<TextAnimate animation="slideLeft" by="character"> <TextAnimate animation="slideLeft" by="word">
{title} {title}
</TextAnimate> </TextAnimate>
</h2> </h2>
<p className="text-sm sm:text-base text-slate-600 font-sans text-pretty"> <p className="text-sm sm:text-base text-slate-600 font-sans text-pretty">
<TextAnimate animation="fadeIn" by="word" delay={0.2}> <TextAnimate animation="fadeIn" by="word" delay={0.08}>
{subtitle} {subtitle}
</TextAnimate> </TextAnimate>
</p> </p>
@@ -207,13 +207,13 @@ export function TrendyBentoSection({
</div> </div>
<h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight text-balance"> <h2 className="text-3xl sm:text-5xl font-display font-extrabold text-slate-900 tracking-tight text-balance">
<TextAnimate animation="slideLeft" by="character"> <TextAnimate animation="slideLeft" by="word">
{title} {title}
</TextAnimate> </TextAnimate>
</h2> </h2>
<p className="text-sm sm:text-base text-slate-600 font-sans text-pretty"> <p className="text-sm sm:text-base text-slate-600 font-sans text-pretty">
<TextAnimate animation="fadeIn" by="word" delay={0.2}> <TextAnimate animation="fadeIn" by="word" delay={0.08}>
{subtitle} {subtitle}
</TextAnimate> </TextAnimate>
</p> </p>