Animate text with various effects like blur, slide, scale, and fade with granular control over words, characters, or lines.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="Hello, Svelte Developers!" animation="blurInUp" by="character" once /></div>Installation
Install dependencies
Install the required packages for this component:
Copy the Source Code
Copy and paste the following code into your project:
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { cn } from "#lib/utils.js"; import type { ElementType, MotionProps, Variants } from "motion-sv"; import { motion, AnimatePresence } from "motion-sv";
type AnimationType = "text" | "word" | "character" | "line"; type AnimationVariant = | "fadeIn" | "blurIn" | "blurInUp" | "blurInDown" | "slideUp" | "slideDown" | "slideLeft" | "slideRight" | "scaleUp" | "scaleDown";
const staggerTimings: Record<AnimationType, number> = { text: 0.06, word: 0.05, character: 0.03, line: 0.06, };
const defaultContainerVariants = { hidden: { opacity: 1 }, show: { opacity: 1, transition: { delayChildren: 0, staggerChildren: 0.05, }, }, exit: { opacity: 0, transition: { staggerChildren: 0.05, staggerDirection: -1, }, }, }; const defaultItemVariants: Variants = { hidden: { opacity: 0 }, show: { opacity: 1, }, exit: { opacity: 0, }, };
const defaultItemAnimationVariants: Record< AnimationVariant, { container: Variants; item: Variants } > = { fadeIn: { container: defaultContainerVariants, item: { hidden: { opacity: 0, y: 20 }, show: { opacity: 1, y: 0, transition: { duration: 0.3, }, }, exit: { opacity: 0, y: 20, transition: { duration: 0.3 }, }, }, }, blurIn: { container: defaultContainerVariants, item: { hidden: { opacity: 0, filter: "blur(10px)" }, show: { opacity: 1, filter: "blur(0px)", transition: { duration: 0.3, }, }, exit: { opacity: 0, filter: "blur(10px)", transition: { duration: 0.3 }, }, }, }, blurInUp: { container: defaultContainerVariants, item: { hidden: { opacity: 0, filter: "blur(10px)", y: 20 }, show: { opacity: 1, filter: "blur(0px)", y: 0, transition: { y: { duration: 0.3 }, opacity: { duration: 0.4 }, filter: { duration: 0.3 }, }, }, exit: { opacity: 0, filter: "blur(10px)", y: 20, transition: { y: { duration: 0.3 }, opacity: { duration: 0.4 }, filter: { duration: 0.3 }, }, }, }, }, blurInDown: { container: defaultContainerVariants, item: { hidden: { opacity: 0, filter: "blur(10px)", y: -20 }, show: { opacity: 1, filter: "blur(0px)", y: 0, transition: { y: { duration: 0.3 }, opacity: { duration: 0.4 }, filter: { duration: 0.3 }, }, }, }, }, slideUp: { container: defaultContainerVariants, item: { hidden: { y: 20, opacity: 0 }, show: { y: 0, opacity: 1, transition: { duration: 0.3, }, }, exit: { y: -20, opacity: 0, transition: { duration: 0.3, }, }, }, }, slideDown: { container: defaultContainerVariants, item: { hidden: { y: -20, opacity: 0 }, show: { y: 0, opacity: 1, transition: { duration: 0.3 }, }, exit: { y: 20, opacity: 0, transition: { duration: 0.3 }, }, }, }, slideLeft: { container: defaultContainerVariants, item: { hidden: { x: 20, opacity: 0 }, show: { x: 0, opacity: 1, transition: { duration: 0.3 }, }, exit: { x: -20, opacity: 0, transition: { duration: 0.3 }, }, }, }, slideRight: { container: defaultContainerVariants, item: { hidden: { x: -20, opacity: 0 }, show: { x: 0, opacity: 1, transition: { duration: 0.3 }, }, exit: { x: 20, opacity: 0, transition: { duration: 0.3 }, }, }, }, scaleUp: { container: defaultContainerVariants, item: { hidden: { scale: 0.5, opacity: 0 }, show: { scale: 1, opacity: 1, transition: { duration: 0.3, scale: { type: "spring", damping: 15, stiffness: 300, }, }, }, exit: { scale: 0.5, opacity: 0, transition: { duration: 0.3 }, }, }, }, scaleDown: { container: defaultContainerVariants, item: { hidden: { scale: 1.5, opacity: 0 }, show: { scale: 1, opacity: 1, transition: { duration: 0.3, scale: { type: "spring", damping: 15, stiffness: 300, }, }, }, exit: { scale: 1.5, opacity: 0, transition: { duration: 0.3 }, }, }, }, };
interface TextAnimateProps extends MotionProps { /** * The text content to animate */ content: string; /** * The class name to be applied to the component */ class?: ClassValue; /** * The class name to be applied to each segment */ segmentClass?: string; /** * The delay before the animation starts */ delay?: number; /** * The duration of the animation */ duration?: number; /** * Custom motion variants for the animation */ variants?: Variants; /** * The element type to render */ // as?: ElementType; /** * How to split the text ("text", "word", "character") */ by?: AnimationType; /** * Whether to start animation when component enters viewport */ startOnView?: boolean; /** * Whether to animate only once */ once?: boolean; /** * The animation preset to use */ animation?: AnimationVariant; /** * Whether to enable accessibility features (default: true) */ accessible?: boolean; }
// as = "span", let { content, class: className = "", segmentClass = "", delay = 0, duration = 0.3, variants, by = "word", startOnView = true, once = false, animation = "fadeIn", accessible = true, }: TextAnimateProps = $props();
let segments: string[] = $derived.by(() => { if (!content) return []; let text = content; switch (by) { case "word": return text.split(/(\s+)/); break; case "character": return text.split(""); break; case "line": // split by \n or \r\n or \\n // trim each line return ( text.split("\\n").map((line) => line.trim()) || text.split("\n").map((line) => line.trim()) ); break; case "text": default: return [text]; break; } });
let finalVariants = $derived( variants ? { container: { hidden: { opacity: 0 }, show: { opacity: 1, transition: { opacity: { duration: 0.01, delay }, delayChildren: delay, staggerChildren: duration / segments.length, }, }, exit: { opacity: 0, transition: { staggerChildren: duration / segments.length, staggerDirection: -1, }, }, }, item: variants, } : animation ? { container: { ...defaultItemAnimationVariants[animation].container, show: { ...defaultItemAnimationVariants[animation].container.show, transition: { delayChildren: delay, staggerChildren: duration / segments.length, }, }, exit: { ...defaultItemAnimationVariants[animation].container.exit, transition: { staggerChildren: duration / segments.length, staggerDirection: -1, }, }, }, item: defaultItemAnimationVariants[animation].item, } : { container: defaultContainerVariants, item: defaultItemVariants } );
// $inspect(segments, "Segments"); // $inspect(content, "Content");</script>
<AnimatePresence mode="popLayout"> <motion.div variants={finalVariants.container as Variants} initial="hidden" whileInView={startOnView ? "show" : undefined} animate={startOnView ? undefined : "show"} exit="exit" inViewOptions={{ once }} aria-label={accessible ? segments.join(" ") : undefined} class={cn("whitespace-pre-wrap", className)} > {#if accessible} <span class="sr-only">{content}</span> {/if} {#each segments as segment, i} <motion.span variants={finalVariants.item} custom={i * staggerTimings[by]} class={cn( by === "line" ? "block" : "inline-block whitespace-pre", by === "character" && "", segmentClass )} aria-hidden={accessible ? true : undefined} > {segment} </motion.span> {/each} </motion.div></AnimatePresence> Folder Structure
src/└── lib/ └── components/ └── magic/ └── text-animate/ ├── text-animate.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/text-animate/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.Usage
<script lang="ts"> import { TextAnimate } from "#lib/components/magic/text-animate/index.js";</script>
<TextAnimate content="Animated text" animation="blurInUp" />Examples
Blur In by Text
Animate entire text with a blur-in effect.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="Blur In Text" animation="blurIn" /></div>Slide Up by Word
Animate each word with a slide-up effect.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="Slide up by word" animation="slideUp" by="word" /></div>Scale Up by Text
Animate entire text with a scale-up spring effect.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="Scale up by text" animation="scaleUp" by="text" /></div>Fade In by Line
Animate text line by line with a fade-in effect.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="Fade in by line as paragraph\n\nFade in by line as paragraph\n\nFade in by line as paragraph" animation="fadeIn" by="line" /></div>Slide Left by Character
Animate each character with a slide-left effect.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="Slide left by character" animation="slideLeft" by="character" /></div>With Delay
Add a delay before the animation starts.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="This animation starts after a delay" animation="blurInUp" by="character" delay={2} /></div>With Duration
Control the total duration of the animation.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js";</script>
<div class="flex items-center justify-center"> <TextAnimate content="Slower animation with custom duration" animation="slideUp" by="word" duration={3} /></div>Custom Motion Variants
Use custom motion variants for complete control over the animation.
<script lang="ts"> import { TextAnimate } from "$lib/components/magic/text-animate/index.js"; import type { Variants } from "motion-sv";
const customVariants: Variants = { hidden: { opacity: 0, y: 30, rotate: 45, scale: 0.5, }, show: (i) => ({ opacity: 1, y: 0, rotate: 0, scale: 1, transition: { delay: i * 0.1, duration: 0.4, y: { type: "spring", damping: 12, stiffness: 200, mass: 0.8, }, rotate: { type: "spring", damping: 8, stiffness: 150, }, scale: { type: "spring", damping: 10, stiffness: 300, }, }, }), exit: (i) => ({ opacity: 0, y: 30, rotate: 45, scale: 0.5, transition: { delay: i * 0.1, duration: 0.4, }, }), };</script>
<div class="flex items-center justify-center"> <TextAnimate content="Svelte is Amazing!" variants={customVariants} by="character" /></div>Props
Text Animate
A component for animating text with various effects and granular control.
| Name | Type | Default | Description |
|---|---|---|---|
content | string | "" | The text content to animate (required) |
animation | "fadeIn" | "blurIn" | "blurInUp" | "blurInDown" | "slideUp" | "slideDown" | "slideLeft" | "slideRight" | "scaleUp" | "scaleDown" | "fadeIn" | The animation preset to use |
by | "text" | "word" | "character" | "line" | "word" | How to split the text for animation |
delay | number | 0 | Delay before the animation starts (in seconds) |
duration | number | 0.3 | Total duration of the animation (in seconds) |
variants | Variants | undefined | Custom motion variants for the item animation |
startOnView | boolean | true | Start animation when component enters viewport |
once | boolean | false | Animate only once when in view |
accessible | boolean | true | Enable accessibility features (screen reader support) |
class | ClassValue | "" | Additional CSS classes to apply to the container |
segmentClass | string | "" | Additional CSS classes to apply to each segment |
Animation Presets
The component comes with the following animation presets:
- fadeIn - Simple fade in animation
- blurIn - Blur from blurred to clear
- blurInUp - Blur in with upward motion
- blurInDown - Blur in with downward motion
- slideUp - Slide in from bottom
- slideDown - Slide in from top
- slideLeft - Slide in from right
- slideRight - Slide in from left
- scaleUp - Scale from small to normal with spring
- scaleDown - Scale from large to normal with spring
Text Segmentation
Control how the text is split for animation using the by prop:
- text - Animate the entire text as one unit
- word - Animate each word separately (default)
- character - Animate each character separately
- line - Animate each line separately (split by \n)