Animated Beam
svelte-animations/animated-beamFreeBlock
A component for creating animated beam effects between two elements with customizable gradients and paths.
Install it
npx shadcn@latest add https://sv-animations.vercel.app/r/animated-beam.jsonSource
1<script lang="ts">2 import { motion } from "motion-sv";3 import { cn } from "$UTILS$";4 import type { AnimatedBeamProps } from "./types";5 import { usePathCalculator } from "./use-path-calculator.svelte";6 import { useGradientCoordinates } from "./use-gradient-coordinates.svelte";7 import { useResizeObserver } from "./use-resize-observer.svelte";89 let {10 class: className,11 containerRef,12 fromRef,13 toRef,14 curvature = 0,15 reverse = false,16 duration = Math.random() * 3 + 4,17 delay = 0,18 pathColor = "gray",19 pathWidth = 2,20 pathOpacity = 0.2,21 gradientStartColor = "#ffaa40",22 gradientStopColor = "#9c40ff",23 startXOffset = 0,24 startYOffset = 0,25 endXOffset = 0,26 endYOffset = 0,27 }: AnimatedBeamProps = $props();2829 // Generate unique ID for gradient30 // let id = $state(`gradient-${Math.random().toString(36).substring(2, 11)}`);31 let id = $props.id();3233 // Use path calculator34 const pathCalculator = usePathCalculator();3536 // Calculate gradient coordinates based on reverse prop37 const gradientCoordinates = $derived(useGradientCoordinates(reverse));3839 // Update path function40 const updatePath = () => {41 pathCalculator.calculatePath(42 containerRef,43 fromRef,44 toRef,45 curvature,46 startXOffset,47 startYOffset,48 endXOffset,49 endYOffset50 );51 };5253 // Setup resize observer54 useResizeObserver(() => containerRef, updatePath);5556 // Watch for changes in refs and offsets57 $effect(() => {58 updatePath();59 });60</script>6162<svg63 fill="none"64 width={pathCalculator.svgDimensions.width}65 height={pathCalculator.svgDimensions.height}66 xmlns="http://www.w3.org/2000/svg"67 class={cn("pointer-events-none absolute top-0 left-0 transform-gpu stroke-2", className)}68 viewBox={`0 0 ${pathCalculator.svgDimensions.width} ${pathCalculator.svgDimensions.height}`}69>70 <path71 d={pathCalculator.pathD}72 stroke={pathColor}73 stroke-width={pathWidth}74 stroke-opacity={pathOpacity}75 stroke-linecap="round"76 />77 <path78 d={pathCalculator.pathD}79 stroke-width={pathWidth}80 stroke={`url(#${id})`}81 stroke-opacity="1"82 stroke-linecap="round"83 />84 <defs>85 <motion.linearGradient86 class="transform-gpu"87 {id}88 gradientUnits="userSpaceOnUse"89 initial={{90 x1: "0%",91 x2: "0%",92 y1: "0%",93 y2: "0%",94 }}95 animate={{96 x1: gradientCoordinates.x1,97 x2: gradientCoordinates.x2,98 y1: gradientCoordinates.y1,99 y2: gradientCoordinates.y2,100 }}101 transition={{102 delay,103 duration,104 ease: [0.16, 1, 0.3, 1],105 repeat: Infinity,106// … truncated
What it pulls in
npm packages
Files it writes
More from Svelte Animations
All 66 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 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 | |
| Aurora Textaurora-text | Svelte Animations | Blocks | Free | no deps · 2 files |
