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}`}
+2 -2
View File
@@ -60,13 +60,13 @@ export function FaqSection({
</div>
<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}
</TextAnimate>
</h2>
<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.
</TextAnimate>
</p>
+4 -4
View File
@@ -317,12 +317,12 @@ export function HeroSection({
{/* 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">
<span className="block text-slate-950 pb-1 sm:pb-2">
<TextAnimate animation="slideLeft" by="character">
<TextAnimate animation="slideLeft" by="word">
Imagine. Transform.
</TextAnimate>
</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">
<TextAnimate animation="slideLeft" by="character" delay={0.15}>
<span className="block mt-1 sm:mt-2 text-blue-600 pb-3 sm:whitespace-nowrap font-black">
<TextAnimate animation="slideLeft" by="word" delay={0.06}>
Create the Impossible.
</TextAnimate>
</span>
@@ -330,7 +330,7 @@ export function HeroSection({
{/* 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">
<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.
</TextAnimate>
</p>
@@ -46,13 +46,13 @@ export function HowItWorks3DSection({
</div>
<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}
</TextAnimate>
</h2>
<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}
</TextAnimate>
</p>
@@ -96,13 +96,13 @@ export function RecommendedAppsSection({
</div>
<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}
</TextAnimate>
</h2>
<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}
</TextAnimate>
</p>
@@ -112,13 +112,13 @@ export function ReviewsMarqueeSection({
</div>
<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}
</TextAnimate>
</h2>
<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}
</TextAnimate>
</p>
@@ -122,13 +122,13 @@ export function ShiftableToolsGridSection({
</div>
<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}
</TextAnimate>
</h2>
<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}
</TextAnimate>
</p>
@@ -207,13 +207,13 @@ export function TrendyBentoSection({
</div>
<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}
</TextAnimate>
</h2>
<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}
</TextAnimate>
</p>