Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A customizable dot pattern background component with optional glow animations and mask effects.
motion-sv
dot-pattern.svelte
<script lang="ts"> import { DotPattern } from "$lib/components/magic/dot-pattern/index.js"; import { cn } from "$lib/utils.js";</script>
<div class="bg-background relative flex h-125 w-70 flex-col items-center justify-center overflow-hidden rounded-lg border md:min-w-2xl"> <DotPattern class={cn("mask-[radial-gradient(300px_circle_at_center,white,transparent)]")} /></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/dot-pattern.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:
dot-pattern.svelte
<script lang="ts"> import { onMount } from "svelte"; import { motion } from "motion-sv"; import { cn } from "#lib/utils.js"; import type { ClassValue, SVGAttributes } from "svelte/elements";
interface DotPatternProps extends SVGAttributes<SVGSVGElement> { width?: number; height?: number; x?: number; /** * The y-offset of the entire pattern */ y?: number; /** * The x-offset of individual dots */ cx?: number; /** * The y-offset of individual dots */ cy?: number; /** * The radius of each dot */ cr?: number; class?: ClassValue; /** * Whether dots should have a glowing animation effect */ glow?: boolean; }
let { width = 16, height = 16, x = 0, y = 0, cx = 1, cy = 1, cr = 1, class: className, glow = false, ...props }: DotPatternProps = $props();
const id = $props.id(); let containerRef: SVGSVGElement | null = $state(null); let dimensions = $state({ width: 0, height: 0 });
onMount(() => { const updateDimensions = () => { if (containerRef) { const rect = containerRef.getBoundingClientRect(); dimensions = { width: rect.width, height: rect.height }; } };
updateDimensions(); window.addEventListener("resize", updateDimensions); return () => window.removeEventListener("resize", updateDimensions); });
const dots = $derived( Array.from( { length: Math.ceil(dimensions.width / width) * Math.ceil(dimensions.height / height), }, (_, i) => { const col = i % Math.ceil(dimensions.width / width); const row = Math.floor(i / Math.ceil(dimensions.width / width)); return { x: col * width + cx, y: row * height + cy, delay: Math.random() * 5, duration: Math.random() * 3 + 2, }; } ) );</script>
<svg bind:this={containerRef} aria-hidden="true" class={cn("pointer-events-none absolute inset-0 h-full w-full text-neutral-400/80", className)} {...props}> <defs> <radialGradient id={`${id}-gradient`}> <stop offset="0%" stop-color="currentColor" stop-opacity="1" /> <stop offset="100%" stop-color="currentColor" stop-opacity="0" /> </radialGradient> </defs> {#each dots as dot (dot.x + "-" + dot.y)} <motion.circle cx={dot.x} cy={dot.y} r={cr} fill={glow ? `url(#${id}-gradient)` : "currentColor"} initial={glow ? { opacity: 0.4, scale: 1 } : {}} animate={glow ? { opacity: [0.4, 1, 0.4], scale: [1, 1.5, 1], } : {}} transition={glow ? { duration: dot.duration, repeat: Infinity, repeatType: "reverse", delay: dot.delay, ease: "easeInOut", } : {}} /> {/each}</svg> index.ts
import DotPattern from "./dot-pattern.svelte";export { DotPattern };Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── dot-pattern/ ├── dot-pattern.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/dot-pattern/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 { DotPattern } from "#lib/components/magic/dot-pattern/index.js";</script>
<div class="relative h-64 overflow-hidden"> <DotPattern class="absolute inset-0" /></div>Examples
Linear Gradient Mask
dot-pattern-linear-gradient.svelte
<script lang="ts"> import { DotPattern } from "$lib/components/magic/dot-pattern/index.js"; import { cn } from "$lib/utils.js";</script>
<div class="bg-background relative flex h-80 w-70 items-center justify-center overflow-hidden rounded-lg border p-20 md:min-w-2xl"> <DotPattern width={20} height={20} cx={1} cy={1} cr={1} class={cn("[mask-image:linear-gradient(to_bottom_right,white,transparent,transparent)]")} /></div>Glow Effect
dot-pattern-with-glow-effect.svelte
<script lang="ts"> import { DotPattern } from "$lib/components/magic/dot-pattern/index.js"; import { cn } from "$lib/utils.js";</script>
<div class="relative flex h-125 w-70 flex-col items-center justify-center overflow-hidden md:min-w-2xl"> <DotPattern glow={true} class={cn("mask-[radial-gradient(300px_circle_at_center,white,transparent)]")} /></div>Props
Dot Pattern
A customizable dot pattern background with optional animations.
| Name | Type | Default | Description |
|---|---|---|---|
width | number | 16 | Horizontal spacing between dots |
height | number | 16 | Vertical spacing between dots |
x | number | 0 | X-offset of the entire pattern |
y | number | 0 | Y-offset of the entire pattern |
cx | number | 1 | X-offset of individual dots |
cy | number | 1 | Y-offset of individual dots |
cr | number | 1 | Radius of each dot |
glow | boolean | false | Enable glowing animation effect |
class | ClassValue | "" | Additional CSS classes |