Dia Text Reveal
svelte-animations/dia-text-revealFreeBlock
A sweeping gradient text reveal with viewport triggering and rotating text support.
Install it
npx shadcn@latest add https://sv-animations.vercel.app/r/dia-text-reveal.jsonSource
1<script lang="ts">2 import { cn } from "$UTILS$";3 import {4 animate,5 motion,6 useInView,7 useMotionValue,8 useReducedMotion,9 useTransform,10 type AnimationPlaybackControls,11 type MotionHTMLAttributes,12 type MotionProps,13 } from "motion-sv";14 import { untrack } from "svelte";1516 const DEFAULT_COLORS = ["#c679c4", "#fa3d1d", "#ffb005", "#e1e1fe", "#0358f7"];17 const BAND_HALF = 17;18 const SWEEP_START = -BAND_HALF;19 const SWEEP_END = 100 + BAND_HALF;2021 type DiaTextRevealProps = Omit<22 MotionProps<"span"> & MotionHTMLAttributes<"span">,23 "children" | "style" | "animate" | "transition" | "color"24 > & {25 text: string | string[];26 colors?: string[];27 textColor?: string;28 duration?: number;29 delay?: number;30 repeat?: boolean;31 repeatDelay?: number;32 triggerOnView?: boolean;33 once?: boolean;34 class?: string;35 fixedWidth?: boolean;36 };3738 const sweepEase = (t: number) => (t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2);3940 function buildGradient(pos: number, colors: string[], textColor: string) {41 const bandStart = pos - BAND_HALF;42 const bandEnd = pos + BAND_HALF;4344 if (bandStart >= 100) {45 return `linear-gradient(90deg, ${textColor}, ${textColor})`;46 }4748 const count = colors.length;49 const parts: string[] = [];5051 if (bandStart > 0) {52 parts.push(`${textColor} 0%`, `${textColor} ${bandStart.toFixed(2)}%`);53 }5455 colors.forEach((color, index) => {56 const pct = count === 1 ? pos : bandStart + (index / (count - 1)) * BAND_HALF * 2;57 parts.push(`${color} ${pct.toFixed(2)}%`);58 });5960 if (bandEnd < 100) {61 parts.push(`transparent ${bandEnd.toFixed(2)}%`, "transparent 100%");62 }6364 return `linear-gradient(90deg, ${parts.join(", ")})`;65 }6667 function measureWidths(element: HTMLElement, texts: string[]) {68 const ghost = element.cloneNode() as HTMLElement;6970 Object.assign(ghost.style, {71 position: "absolute",72 visibility: "hidden",73 pointerEvents: "none",74 width: "auto",75 whiteSpace: "nowrap",76 });7778 element.parentElement?.appendChild(ghost);7980 const widths = texts.map((text) => {81 ghost.textContent = text;82 return ghost.getBoundingClientRect().width;83 });8485 ghost.remove();86 return widths;87 }8889 let {90 text,91 colors = DEFAULT_COLORS,92 textColor = "var(--foreground)",93 duration = 1.5,94 delay = 0,95 repeat = false,96 repeatDelay = 0.5,97 triggerOnView = true,98 once = true,99 class: className,100 fixedWidth = false,101 ...props102 }: DiaTextRevealProps = $props();103104// … truncated
What it pulls in
npm packages
Files it writes
More from Svelte Animations
All 66 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Beamanimated-beam | Svelte Animations | Blocks | Free | 1 dep · 6 files | |
| Animated Circular Progress Baranimated-circular-progress-bar | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Gradient Textanimated-gradient-text | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Animated Grid Patternanimated-grid-pattern | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Listanimated-list | Svelte Animations | Blocks | Free | 1 dep · 2 files | |
| Animated Shiny Textanimated-shiny-text | Svelte Animations | Blocks | Free | no deps | |
| Animated Theme Toggleranimated-theme-toggler | Svelte Animations | Blocks | Free | no deps · 2 files | |
| Arc Timelinearc-timeline | Svelte Animations | Blocks | Free | no deps · 3 files |
