Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A description for Warp Background component.
motion-sv
Congratulations on Your Promotion!
Your hard work and dedication have paid off. We're thrilled to see you take this next step in your career. Keep up the fantastic work!
warp-background.svelte
<script lang="ts"> import { WarpBackground } from "$lib/components/magic/warp-background/index.js"; import * as Card from "$lib/components/ui/card/index.js";</script>
<WarpBackground> <Card.Root class="w-80"> <Card.Content class="flex flex-col gap-2 p-4"> <Card.Title>Congratulations on Your Promotion!</Card.Title> <Card.Description> Your hard work and dedication have paid off. We're thrilled to see you take this next step in your career. Keep up the fantastic work! </Card.Description> </Card.Content> </Card.Root></WarpBackground>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/warp-background.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:
warp-background.svelte
<script lang="ts"> import { motion } from "motion-sv"; import { cn } from "#lib/utils.js"; import type { Snippet } from "svelte"; import type { ClassValue, HTMLAttributes } from "svelte/elements";
interface BeamProps { width: string | number; x: string | number; delay: number; duration: number; }
interface WarpBackgroundProps extends HTMLAttributes<HTMLDivElement> { children: Snippet; class?: ClassValue; perspective?: number; beamsPerSide?: number; beamSize?: number; beamDelayMax?: number; beamDelayMin?: number; beamDuration?: number; gridColor?: string; }
let { children, perspective = 100, class: className, beamsPerSide = 3, beamSize = 5, beamDelayMax = 3, beamDelayMin = 0, beamDuration = 3, gridColor = "var(--border)", ...props }: WarpBackgroundProps = $props();
const generateBeams = () => { const beams = []; const cellsPerSide = Math.floor(100 / beamSize); const step = cellsPerSide / beamsPerSide;
for (let i = 0; i < beamsPerSide; i++) { const x = Math.floor(i * step); const delay = Math.random() * (beamDelayMax - beamDelayMin) + beamDelayMin; beams.push({ x, delay }); } return beams; };
const topBeams = generateBeams(); const rightBeams = generateBeams(); const bottomBeams = generateBeams(); const leftBeams = generateBeams();
const containerStyle = $derived(` --perspective: ${perspective}px; --grid-color: ${gridColor}; --beam-size: ${beamSize}%; `);</script>
<div class={cn("relative rounded border p-20", className)} {...props}> <div style={containerStyle} class="@container-[size] pointer-events-none absolute top-0 left-0 size-full overflow-hidden [clipPath:inset(0)] perspective-(--perspective) transform-3d" > <!-- top side --> <div class="@container absolute z-20 h-[100cqmax] w-[100cqi] origin-[50%_0%] transform-[rotateX(-90deg)] bg-size-[var(--beam-size)_var(--beam-size)] [background:linear-gradient(var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_-0.5px_/var(--beam-size)_var(--beam-size),linear-gradient(90deg,var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_50%_/var(--beam-size)_var(--beam-size)] transform-3d" > {#each topBeams as beam, index} {@const hue = Math.floor(Math.random() * 360)} {@const ar = Math.floor(Math.random() * 10) + 1} <motion.div style={{ "--x": `${beam.x * beamSize}%`, "--width": `${beamSize}%`, "--aspect-ratio": `${ar}`, "--background": `linear-gradient(hsl(${hue} 80% 60%), transparent)`, }} class="absolute top-0 left-(--x) aspect-[1/var(--aspect-ratio)] w-(--width) [background:var(--background)]" initial={{ y: "100cqmax", x: "-50%" }} animate={{ y: "-100%", x: "-50%" }} transition={{ duration: beamDuration, delay: beam.delay, repeat: Infinity, ease: "linear", }} /> {/each} </div> <!-- bottom side --> <div class="@container absolute top-full h-[100cqmax] w-[100cqi] origin-[50%_0%] transform-[rotateX(-90deg)] bg-size-[var(--beam-size)_var(--beam-size)] [background:linear-gradient(var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_-0.5px_/var(--beam-size)_var(--beam-size),linear-gradient(90deg,var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_50%_/var(--beam-size)_var(--beam-size)] transform-3d" > {#each bottomBeams as beam, index} {@const hue = Math.floor(Math.random() * 360)} {@const ar = Math.floor(Math.random() * 10) + 1} <motion.div style={{ "--x": `${beam.x * beamSize}%`, "--width": `${beamSize}%`, "--aspect-ratio": `${ar}`, "--background": `linear-gradient(hsl(${hue} 80% 60%), transparent)`, }} class="absolute top-0 left-(--x) aspect-[1/var(--aspect-ratio)] w-(--width) [background:var(--background)]" initial={{ y: "100cqmax", x: "-50%" }} animate={{ y: "-100%", x: "-50%" }} transition={{ duration: beamDuration, delay: beam.delay, repeat: Infinity, ease: "linear", }} /> {/each} </div> <!-- left side --> <div class="@container absolute top-0 left-0 h-[100cqmax] w-[100cqh] origin-[0%_0%] transform-[rotate(90deg)_rotateX(-90deg)] bg-size-[var(--beam-size)_var(--beam-size)] [background:linear-gradient(var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_-0.5px_/var(--beam-size)_var(--beam-size),linear-gradient(90deg,var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_50%_/var(--beam-size)_var(--beam-size)] transform-3d" > {#each leftBeams as beam, index} {@const hue = Math.floor(Math.random() * 360)} {@const ar = Math.floor(Math.random() * 10) + 1} <motion.div style={{ "--x": `${beam.x * beamSize}%`, "--width": `${beamSize}%`, "--aspect-ratio": `${ar}`, "--background": `linear-gradient(hsl(${hue} 80% 60%), transparent)`, }} class="absolute top-0 left-(--x) aspect-[1/var(--aspect-ratio)] w-(--width) [background:var(--background)]" initial={{ y: "100cqmax", x: "-50%" }} animate={{ y: "-100%", x: "-50%" }} transition={{ duration: beamDuration, delay: beam.delay, repeat: Infinity, ease: "linear", }} /> {/each} </div> <!-- right side --> <div class="@container absolute top-0 right-0 h-[100cqmax] w-[100cqh] origin-[100%_0%] transform-[rotate(-90deg)_rotateX(-90deg)] bg-size-[var(--beam-size)_var(--beam-size)] [background:linear-gradient(var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_-0.5px_/var(--beam-size)_var(--beam-size),linear-gradient(90deg,var(--grid-color)_0_1px,transparent_1px_var(--beam-size))_50%_50%_/var(--beam-size)_var(--beam-size)] transform-3d" > {#each rightBeams as beam, index} {@const hue = Math.floor(Math.random() * 360)} {@const ar = Math.floor(Math.random() * 10) + 1} <motion.div style={{ "--x": `${beam.x * beamSize}%`, "--width": `${beamSize}%`, "--aspect-ratio": `${ar}`, "--background": `linear-gradient(hsl(${hue} 80% 60%), transparent)`, }} class="absolute top-0 left-(--x) aspect-[1/var(--aspect-ratio)] w-(--width) [background:var(--background)]" initial={{ y: "100cqmax", x: "-50%" }} animate={{ y: "-100%", x: "-50%" }} transition={{ duration: beamDuration, delay: beam.delay, repeat: Infinity, ease: "linear", }} /> {/each} </div> </div> <div class="relative"> {@render children()} </div></div> index.ts
export { default as WarpBackground } from "./warp-background.svelte";Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── warp-background/ ├── warp-background.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/warp-background/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 { WarpBackground } from "#lib/components/magic/warp-background/index.js";</script>
<WarpBackground> <div class="rounded-xl bg-background p-8">Warped background</div></WarpBackground>Props
Warp Background
A component for Warp Background.
| Name | Type | Default | Description |
|---|---|---|---|
class | ClassValue | "" | Additional CSS classes to apply |
perspective | number | 100 | Perspective value for the 3D effect |
beamsPerSide | number | 3 | Number of beams per side |
beamSize | number | 5 | Size of each beam |
beamDelayMax | number | 3 | Maximum delay for each beam |
beamDelayMin | number | 0 | Minimum delay for each beam |
beamDuration | number | 3 | Duration of each beam animation |
gridColor | string | "var(--border)" | Color of the grid |
children | Snippet | required | The content to wrap |