Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A interactive component that enables zooming into images, videos and other elements.
motion-sv
Your next camp
See our latest and best camp destinations all across the five continents of the globe.
components
<script lang="ts"> import { Lens } from "#lib/components/magic/lens/index.js"; import { Button } from "#lib/components/ui/button/index.js"; import * as Card from "#lib/components/ui/card/index.js";</script>
<Card.Root class="relative max-w-md shadow-none"> <Card.Header> <Lens zoomFactor={2} lensSize={150} isStatic={false} ariaLabel="Zoom Area"> <img src="https://images.unsplash.com/photo-1736606355698-5efdb410fe93?q=80&w=2071&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt="image_placeholder" width={500} height={500} /> </Lens> </Card.Header> <Card.Content> <Card.Title class="text-2xl">Your next camp</Card.Title> <Card.Description> See our latest and best camp destinations all across the five continents of the globe. </Card.Description> </Card.Content> <Card.Footer class="space-x-4"> <Button>Let's go</Button> <Button variant="secondary">Another time</Button> </Card.Footer></Card.Root>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/lens.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:
lens.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { motion, AnimatePresence } from "motion-sv"; import { cn } from "#lib/utils.js"; import type { Snippet } from "svelte";
interface Position { /** The x coordinate of the lens */ x: number; /** The y coordinate of the lens */ y: number; }
interface LensProps { children: Snippet; /** The zoom factor of the lens */ zoomFactor?: number; /** The size of the lens */ lensSize?: number; /** The position of the lens */ position?: Position; /** The default position of the lens */ defaultPosition?: Position; /** Whether the lens is static */ isStatic?: boolean; /** The duration of the animation */ duration?: number; /** The color of the lens */ lensColor?: string; /** The aria label of the lens */ ariaLabel?: string; class?: ClassValue; }
let { children, zoomFactor = 1.3, lensSize = 170, isStatic = false, position = { x: 0, y: 0 }, defaultPosition, duration = 0.1, lensColor = "black", ariaLabel = "Zoom Area", class: className, }: LensProps = $props();
// Validation $effect(() => { if (zoomFactor < 1) { throw new Error("zoomFactor must be greater than 1"); } if (lensSize < 0) { throw new Error("lensSize must be greater than 0"); } });
let isHovering = $state(false); let mousePosition: Position = $derived(position); let containerRef: HTMLDivElement | null = $state(null);
const currentPosition = $derived.by(() => { if (isStatic) return position; if (defaultPosition && !isHovering) return defaultPosition; return mousePosition; });
const handleMouseMove = (e: MouseEvent) => { if (!containerRef) return; const rect = containerRef.getBoundingClientRect(); mousePosition = { x: e.clientX - rect.left, y: e.clientY - rect.top, }; };
const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") isHovering = false; };
const maskImage = $derived( `radial-gradient(circle ${lensSize / 2}px at ${currentPosition.x}px ${currentPosition.y}px, ${lensColor} 100%, transparent 100%)` );
const transformOrigin = $derived(`${currentPosition.x}px ${currentPosition.y}px`);
const lensStyle = $derived({ maskImage, WebkitMaskImage: maskImage, transformOrigin, zIndex: 50, });
const zoomStyleString = $derived( `transform: scale(${zoomFactor}); transform-origin: ${transformOrigin};` );</script>
<!-- svelte-ignore a11y_no_noninteractive_tabindex --><!-- svelte-ignore a11y_no_noninteractive_element_interactions --><div bind:this={containerRef} class={cn("relative z-20 overflow-hidden rounded-xl", className)} onmouseenter={() => (isHovering = true)} onmouseleave={() => (isHovering = false)} onmousemove={handleMouseMove} onkeydown={handleKeyDown} role="region" aria-label={ariaLabel} tabindex="0"> {@render children()} {#if isStatic || defaultPosition} <motion.div initial={{ opacity: 0, scale: 0.58 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.8 }} transition={{ duration }} class="absolute inset-0 overflow-hidden" style={lensStyle} > <div class="absolute inset-0" style={zoomStyleString}> {@render children()} </div> </motion.div> {:else} <AnimatePresence mode="popLayout"> {#if isHovering} <motion.div initial={{ opacity: 0, scale: 0.58 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.8 }} transition={{ duration }} class="absolute inset-0 overflow-hidden" style={lensStyle} > <div class="absolute inset-0" style={zoomStyleString}> {@render children()} </div> </motion.div> {/if} </AnimatePresence> {/if}</div> index.ts
export { default as Lens } from "./lens.svelte";Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── lens/ ├── lens.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/lens/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 { Lens } from "#lib/components/magic/lens/index.js";</script>
<Lens> <img src="https://images.unsplash.com/photo-1736606355698-5efdb410fe93" alt="Mountain lake" /></Lens>Examples
Image Inspection
Inspect a high-resolution image with a larger lens and two-times zoom.
lens-image-detail.svelte
<script lang="ts"> import { Lens } from "$lib/components/magic/lens/index.js";</script>
<figure class="w-full max-w-lg"> <Lens zoomFactor={2} lensSize={180} ariaLabel="inspect mountain detail" class="rounded-lg"> <img src="https://images.unsplash.com/photo-1493246507139-91e8fad9978e?auto=format&fit=crop&w=1000&q=85" alt="mountains reflected in a lake" width="1000" height="667" class="aspect-3/2 w-full object-cover" /> </Lens> <figcaption class="text-muted-foreground mt-3 text-sm"> move over the image to inspect the detail </figcaption></figure>Diagram Labels
Inspect compact request labels on hover without obscuring the diagram at rest.
lens-diagram.svelte
<script lang="ts"> import { Lens } from "$lib/components/magic/lens/index.js";</script>
<figure class="w-full max-w-lg"> <Lens duration={0} zoomFactor={1.5} lensSize={100} ariaLabel="inspect request flow labels" class="rounded-lg border" > <svg viewBox="0 0 500 200" class="bg-background block h-auto w-full" role="img" aria-label="a browser sends a request to a router, which reads from the cache on a hit or the origin on a miss" > <g fill="none" stroke="var(--color-sky-500)" stroke-width="1"> <path d="M120 100H180 M270 100H315V60H380 M315 100V140H380" /> </g> <g fill="var(--background)" stroke="var(--border)"> <rect x="20" y="80" width="100" height="40" rx="6" /> <rect x="180" y="80" width="90" height="40" rx="6" /> <rect x="380" y="40" width="100" height="40" rx="6" /> <rect x="380" y="120" width="100" height="40" rx="6" /> </g> <g fill="var(--foreground)" text-anchor="middle" dominant-baseline="middle" font-size="12" font-family="monospace" > <text x="70" y="100">browser</text> <text x="225" y="100">router</text> <text x="430" y="60">cache</text> <text x="430" y="140">origin</text> </g> <g fill="var(--color-sky-600)" class="dark:fill-sky-400" text-anchor="middle" font-size="9" font-family="monospace" > <text x="150" y="90">request</text> <text x="347" y="50">hit</text> <text x="347" y="130">miss</text> </g> </svg> </Lens> <figcaption class="text-muted-foreground mt-3 text-sm"> hover to inspect the request path </figcaption></figure>Props
Lens
A component for Lens.
| Name | Type | Default | Description |
|---|---|---|---|
children | Snippet | | Content wrapped by the lens (image, video, etc.). |
zoomFactor | number | 1 | The zoom factor of the lens. |
lensSize | number | 100 | The size of the lens (diameter in pixels). |
position | { x: number; y: number } | { x: 0, y: 0 } | The position of the lens (x and y coordinates). |
defaultPosition | { x: number; y: number } | { x: 0, y: 0 } | The default position of the lens. |
isStatic | boolean | false | Whether the lens is static (doesn't follow pointer). |
duration | number | 0.3 | The duration of the animation (seconds). |
lensColor | string | "rgba(255,255,255,0.5)" | The color of the lens overlay. |
ariaLabel | string | "zoom lens" | The aria label of the lens for accessibility. |
class | ClassValue | "" | Additional CSS classes to apply to lens container. |