Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A text animation that scrambles letters before revealing the final text.
motion-sv
hyper-text.svelte
<script lang="ts"> import { HyperText } from "$lib/components/magic/hyper-text/index.js";</script>
<HyperText text="Hover Me!"></HyperText>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/hyper-text.json
Install dependencies
Install the required packages for this component:
pnpm add motion-sv
Copy the Source Code
Copy and paste the following code into your project:
hyper-text.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { onMount } from "svelte"; import { motion, AnimatePresence } from "motion-sv"; import { cn } from "#lib/utils.js";
type CharacterSet = string[] | readonly string[];
type ElementType = "div" | "span" | "p" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
interface HyperTextProps { text: string; class?: ClassValue; duration?: number; delay?: number; as?: ElementType; startOnView?: boolean; animateOnHover?: boolean; characterSet?: CharacterSet; }
let { text, class: className, duration = 800, delay = 0, as = "div", startOnView = false, animateOnHover = true, characterSet = Object.freeze("ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")) as readonly string[], }: HyperTextProps = $props();
let MotionComponent = $derived(motion[as]);
let displayText = $state<string[]>([]); let isAnimating = $state(false); let iterationCount = $state(0); let elementRef: any = $state(null);
// Initialize displayText when text changes $effect(() => { displayText = text.split(""); });
const getRandomInt = (max: number): number => Math.floor(Math.random() * max);
const handleAnimationTrigger = () => { if (animateOnHover && !isAnimating) { iterationCount = 0; isAnimating = true; } };
// Handle animation start based on view or delay onMount(() => { if (!startOnView) { const startTimeout = setTimeout(() => { isAnimating = true; }, delay); return () => clearTimeout(startTimeout); }
const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { setTimeout(() => { isAnimating = true; }, delay); observer.disconnect(); } }, { threshold: 0.1, rootMargin: "-30% 0px -30% 0px" } );
if (elementRef) { observer.observe(elementRef); }
return () => observer.disconnect(); });
// Handle scramble animation $effect(() => { if (!isAnimating) return;
const maxIterations = text.length; const startTime = performance.now(); let animationFrameId: number;
const animate = (currentTime: number) => { const elapsed = currentTime - startTime; const progress = Math.min(elapsed / duration, 1);
iterationCount = progress * maxIterations;
displayText = displayText.map((letter, index) => letter === " " ? letter : index <= iterationCount ? text[index] : characterSet[getRandomInt(characterSet.length)] );
if (progress < 1) { animationFrameId = requestAnimationFrame(animate); } else { isAnimating = false; } };
animationFrameId = requestAnimationFrame(animate);
return () => cancelAnimationFrame(animationFrameId); });</script>
<MotionComponent bind:this={elementRef} class={cn("overflow-hidden py-2 text-4xl font-medium", className)} onmouseenter={handleAnimationTrigger}> <AnimatePresence> {#each displayText as letter, index (index)} <motion.span class={cn("font-mono", letter === " " ? "w-3" : "")}> {letter.toUpperCase()} </motion.span> {/each} </AnimatePresence></MotionComponent> index.ts
import HyperText from "./hyper-text.svelte";export { HyperText };Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── hyper-text/ ├── hyper-text.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/hyper-text/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 { HyperText } from "#lib/components/magic/hyper-text/index.js";</script>
<HyperText text="Hover me" />Examples
Hex Character Set
Scramble a revision identifier using only hexadecimal characters.
Revision
hyper-text-hex.svelte
<script lang="ts"> import { HyperText } from "$lib/components/magic/hyper-text/index.js";
const characterSet = "0123456789ABCDEF".split("");</script>
<div class="text-center"> <p class="text-muted-foreground mb-2 text-sm">Revision</p> <HyperText text="A7F03C" {characterSet} duration={600} class="text-3xl text-emerald-700 sm:text-5xl dark:text-emerald-300" /></div>Delayed Reveal
Reveal a section heading after a short delay, with hover replay disabled.
Changes, fixes, and migration guides.
hyper-text-delayed.svelte
<script lang="ts"> import { HyperText } from "$lib/components/magic/hyper-text/index.js";</script>
<div class="text-center"> <HyperText as="h2" text="Release notes" delay={300} duration={1200} animateOnHover={false} class="text-2xl sm:text-4xl" /> <p class="text-muted-foreground mt-2 text-sm">Changes, fixes, and migration guides.</p></div>Numeric Characters
Reveal a build number using only digits in the scramble.
build number
hyper-text-numeric.svelte
<script lang="ts"> import { MediaQuery } from "svelte/reactivity"; import { HyperText } from "$lib/components/magic/hyper-text/index.js";
const reducedMotion = new MediaQuery("(prefers-reduced-motion: reduce)"); const digits = "0123456789".split("");</script>
<div class="text-center"> <p class="text-muted-foreground mb-2 text-sm">build number</p> {#if reducedMotion.current} <p class="font-mono text-3xl text-emerald-700 tabular-nums sm:text-5xl dark:text-emerald-300" > 20261113 </p> {:else} <HyperText text="20261113" characterSet={digits} duration={900} class="text-3xl text-emerald-700 tabular-nums sm:text-5xl dark:text-emerald-300" /> {/if}</div>Props
Hyper Text
A component for Hyper Text.
| Name | Type | Default | Description |
|---|---|---|---|
text | string | undefined | The text content to be animated |
class | ClassValue | "" | Additional CSS classes to apply |
duration | number | 800 | Duration of the animation in milliseconds |
delay | number | 0 | Delay before animation starts in milliseconds |
as | "div" | "span" | "p" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "div" | Component to render as |
startOnView | boolean | false | Whether to start animation when element comes into view |
animateOnHover | boolean | true | Whether to trigger animation on hover |
characterSet | string[] | readonly string[] | ABCDEFGHIJKLMNOPQRSTUVWXYZ | Custom character set for scramble effect |