Animated Grid Pattern
svelte-animations/animated-grid-patternFreeBlock
An animated grid pattern component that creates a dynamic background with moving squares that fade in and out.
Install it
npx shadcn@latest add https://sv-animations.vercel.app/r/animated-grid-pattern.jsonSource
1<script lang="ts">2 import { onMount } from "svelte";3 import { motion } from "motion-sv";4 import { cn } from "$UTILS$";5 import type { HTMLAttributes } from "svelte/elements";67 interface AnimatedGridPatternProps extends HTMLAttributes<SVGElement> {8 width?: number;9 height?: number;10 x?: number;11 y?: number;12 strokeDasharray?: number;13 numSquares?: number;14 maxOpacity?: number;15 duration?: number;16 repeatDelay?: number;17 class?: string;18 }1920 type Square = {21 id: number;22 pos: [number, number];23 iteration: number;24 };2526 let {27 width = 40,28 height = 40,29 x = -1,30 y = -1,31 strokeDasharray = 0,32 numSquares = 50,33 class: className,34 maxOpacity = 0.5,35 duration = 4,36 repeatDelay = 0.5,37 ...props38 }: AnimatedGridPatternProps = $props();3940 const id = $props.id();41 let containerRef: SVGSVGElement | null = $state(null);42 let dimensions = $state({ width: 0, height: 0 });43 let squares = $state<Array<Square>>([]);4445 const getPos = (): [number, number] => {46 return [47 Math.floor((Math.random() * dimensions.width) / width),48 Math.floor((Math.random() * dimensions.height) / height),49 ];50 };5152 const generateSquares = (count: number): Array<Square> => {53 return Array.from({ length: count }, (_, i) => ({54 id: i,55 pos: getPos(),56 iteration: 0,57 }));58 };5960 const updateSquarePosition = (squareId: number) => {61 const current = squares[squareId];62 if (!current || current.id !== squareId) return;6364 const nextSquares = squares.slice();65 nextSquares[squareId] = {66 ...current,67 pos: getPos(),68 iteration: current.iteration + 1,69 };7071 squares = nextSquares;72 };7374 $effect(() => {75 if (dimensions.width && dimensions.height) {76 squares = generateSquares(numSquares);77 }78 });7980 onMount(() => {81 const element = containerRef;82 if (!element) return;8384 const resizeObserver = new ResizeObserver((entries) => {85 for (const entry of entries) {86 const nextWidth = entry.contentRect.width;87 const nextHeight = entry.contentRect.height;88 if (dimensions.width !== nextWidth || dimensions.height !== nextHeight) {89 dimensions = { width: nextWidth, height: nextHeight };90 }91 }92 });9394 resizeObserver.observe(element);9596 return () => {97 resizeObserver.disconnect();98 };99 });100</script>101102<svg103 bind:this={containerRef}104 aria-hidden="true"105 class={cn(106 "pointer-events-none absolute inset-0 h-full w-full fill-gray-400/30 stroke-gray-400/30",107 className108 )}109 {...props}110>111 <defs>112// … 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 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 | |
| Aurora Textaurora-text | Svelte Animations | Blocks | Free | no deps · 2 files |
