Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A text component with a video background.
runed
OCEAN
video-text.svelte
<script lang="ts"> import { VideoText } from "$lib/components/magic/video-text/index.js";</script>
<div class="relative h-50 w-full overflow-hidden"> <VideoText src="https://cdn.magicui.design/ocean-small.webm" content="OCEAN"></VideoText></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/video-text.json
Install dependencies
Install the required packages for this component:
pnpm add runed
Copy the Source Code
Copy and paste the following code into your project:
video-text.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { onMount } from "svelte"; import { cn } from "#lib/utils.js"; import { watch } from "runed";
interface VideoTextProps { src: string; content: string; class?: ClassValue; autoPlay?: boolean; muted?: boolean; loop?: boolean; preload?: "auto" | "metadata" | "none"; fontSize?: string | number; fontWeight?: string | number; textAnchor?: string; dominantBaseline?: string; fontFamily?: string; }
let { src, content, class: className = "", autoPlay = true, muted = true, loop = true, preload = "auto", fontSize = 20, fontWeight = "bold", textAnchor = "middle", dominantBaseline = "middle", fontFamily = "sans-serif", }: VideoTextProps = $props();
let svgMask = $state("");
let updateSvgMask = () => { const responsiveFontSize = typeof fontSize === "number" ? `${fontSize}vw` : fontSize; const newSvgMask = `<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%'><text x='50%' y='50%' font-size='${responsiveFontSize}' font-weight='${fontWeight}' text-anchor='${textAnchor}' dominant-baseline='${dominantBaseline}' font-family='${fontFamily}'>${content}</text></svg>`; svgMask = newSvgMask; }; // Handle window resize onMount(() => { window.addEventListener("resize", updateSvgMask); // return () => window.removeEventListener("resize", updateSvgMask); });
let dataUrlMask = $derived(`url("data:image/svg+xml,${encodeURIComponent(svgMask)}")`);
// Update SVG mask when content or settings change watch( [ () => content, () => fontSize, () => fontWeight, () => textAnchor, () => dominantBaseline, () => fontFamily, ], () => { updateSvgMask(); } );</script>
<div class={cn("relative size-full", className)}> <!-- Create a container that masks the video to only show within text --> <div class="absolute inset-0 flex items-center justify-center" style=" mask-image: {dataUrlMask}; -webkit-mask-image: {dataUrlMask}; mask-size: contain; -webkit-mask-size: contain; mask-repeat: no-repeat; -webkit-mask-repeat: no-repeat; mask-position: center; -webkit-mask-position: center; " > <video class="h-full w-full object-cover" autoplay={autoPlay} {muted} {loop} {preload} playsinline > <source {src} /> Your browser does not support the video tag. </video> </div>
<!-- Add a backup text element for SEO/accessibility --> <span class="sr-only">{content}</span></div> index.ts
import VideoText from "./video-text.svelte";export { VideoText };Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── video-text/ ├── video-text.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/video-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 { VideoText } from "#lib/components/magic/video-text/index.js";</script>
<div class="h-48 overflow-hidden"> <VideoText src="https://cdn.magicui.design/ocean-small.webm" content="OCEAN" /></div>Props
Video Text
A component for Video Text.
| Name | Type | Default | Description |
|---|---|---|---|
class | ClassValue | "" | Additional CSS classes to apply |
src | string | | Source URL of the video |
content | string | | Text content to display |
autoPlay | boolean | true | Whether to autoplay the video |
muted | boolean | true | Whether to mute the video |
loop | boolean | true | Whether to loop the video |
preload | "auto" | "metadata" | "none" | "auto" | Preload behavior of the video |
fontSize | string | number | 20 | Font size of the text |
fontWeight | string | number | "bold" | Font weight of the text |
textAnchor | string | "middle" | Text anchor alignment |
dominantBaseline | string | "middle" | Dominant baseline alignment |
fontFamily | string | "sans-serif" | Font family of the text |