terra vidhal

Image Zoom

Component

About

An accessible image viewer with pointer-following zoom, wheel zoom, on-image controls, drag or keyboard panning, a circular magnifier lens, a picture-in-picture preview, or a draggable before/after compare divider with a split callback, plus loading, error and reset states.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Image Zoom to your project and fits it to your theme.

Code

How Image Zoom is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

/**
 * PrequelUI Animate — MIT — https://prequeluianimate.terravidhal.me
 */

import { ImageZoom } from "@/components/ui/image-zoom";

const ART =
  "data:image/svg+xml;charset=utf-8," +
  encodeURIComponent(
    `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 750">
      <defs>
        <linearGradient id="space" x1="0" y1="0" x2="0.9" y2="1">
          <stop offset="0%" stop-color="#02040f"/>
          <stop offset="52%" stop-color="#071028"/>
          <stop offset="100%" stop-color="#0b1c3c"/>
        </linearGradient>
        <radialGradient id="nebulaA" cx="50%" cy="50%" r="50%">
          <stop offset="0%" stop-color="#7c3aed" stop-opacity="0.45"/>
          <stop offset="100%" stop-color="#7c3aed" stop-opacity="0"/>
        </radialGradient>
        <radialGradient id="nebulaB" cx="50%" cy="50%" r="50%">
          <stop offset="0%" stop-color="#22d3ee" stop-opacity="0.35"/>
          <stop offset="100%" stop-color="#22d3ee" stop-opacity="0"/>
        </radialGradient>
        <radialGradient id="planet" cx="34%" cy="28%" r="82%">
          <stop offset="0%" stop-color="#e0fbff"/>
          <stop offset="24%" stop-color="#7dd3fc"/>
          <stop offset="58%" stop-color="#0ea5b7"/>
          <stop offset="86%" stop-color="#0b4a63"/>
          <stop offset="100%" stop-color="#052033"/>
        </radialGradient>
        <radialGradient id="crater" cx="38%" cy="34%" r="72%">
          <stop offset="0%" stop-color="#04202e" stop-opacity="0.85"/>
          <stop offset="72%" stop-color="#062a3c" stop-opacity="0.55"/>
          <stop offset="100%" stop-color="#67e8f9" stop-opacity="0.28"/>
        </radialGradient>
        <linearGradient id="ring" x1="0" y1="0" x2="1" y2="0">
          <stop offset="0%" stop-color="#f0abfc" stop-opacity="0"/>
          <stop offset="24%" stop-color="#f0abfc" stop-opacity="0.55"/>
          <stop offset="50%" stop-color="#a5f3fc" stop-opacity="0.85"/>
          <stop offset="76%" stop-color="#67e8f9" stop-opacity="0.45"/>
          <stop offset="100%" stop-color="#67e8f9" stop-opacity="0"/>
        </linearGradient>
        <linearGradient id="moon" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stop-color="#fef3c7"/>
          <stop offset="100%" stop-color="#b45309"/>
        </linearGradient>
        <pattern id="grid" width="40" height="40" patternUnits="userSpaceOnUse">
          <path d="M40 0H0V40" fill="none" stroke="#ffffff" stroke-opacity="0.045"/>
        </pattern>
        <filter id="softGlow" x="-60%" y="-60%" width="220%" height="220%">
          <feGaussianBlur stdDeviation="10" result="blur"/>
          <feMerge>
            <feMergeNode in="blur"/>
            <feMergeNode in="SourceGraphic"/>
          </feMerge>
        </filter>
      </defs>

      <rect width="1200" height="750" fill="url(#space)"/>
      <rect width="1200" height="750" fill="url(#grid)"/>
      <ellipse cx="180" cy="150" rx="420" ry="330" fill="url(#nebulaA)"/>
      <ellipse cx="1040" cy="640" rx="460" ry="340" fill="url(#nebulaB)"/>

      <g fill="#ffffff">
        <circle cx="118" cy="86" r="1.7" fill-opacity="0.7"/>
        <circle cx="262" cy="196" r="1.2" fill-opacity="0.55"/>
        <circle cx="352" cy="92" r="2" fill-opacity="0.65"/>
        <circle cx="474" cy="214" r="1.1" fill-opacity="0.45"/>
        <circle cx="558" cy="74" r="1.6" fill-opacity="0.6"/>
        <circle cx="1016" cy="104" r="2.2" fill-opacity="0.75"/>
        <circle cx="1104" cy="228" r="1.3" fill-opacity="0.5"/>
        <circle cx="962" cy="282" r="1.5" fill-opacity="0.55"/>
        <circle cx="884" cy="84" r="1.1" fill-opacity="0.4"/>
        <circle cx="180" cy="620" r="1.8" fill-opacity="0.6"/>
        <circle cx="312" cy="690" r="1.2" fill-opacity="0.45"/>
        <circle cx="420" cy="606" r="2.4" fill-opacity="0.7"/>
        <circle cx="508" cy="678" r="1.4" fill-opacity="0.5"/>
        <circle cx="640" cy="632" r="1.1" fill-opacity="0.35"/>
        <circle cx="1128" cy="540" r="1.9" fill-opacity="0.6"/>
        <circle cx="1060" cy="690" r="1.2" fill-opacity="0.45"/>
      </g>

      <g fill="none" stroke="#f0abfc" stroke-opacity="0.22" stroke-width="1.5">
        <path d="M86 466C228 398 334 508 486 448s282-120 438-46"/>
        <path d="M60 524C210 462 328 566 500 506s294-112 452-44" stroke-opacity="0.12"/>
      </g>

      <g transform="translate(744 352)">
        <ellipse rx="252" ry="74" fill="none" stroke="url(#ring)" stroke-width="14" transform="rotate(-18)"/>
        <ellipse rx="208" ry="58" fill="none" stroke="#a5f3fc" stroke-opacity="0.28" stroke-width="3" transform="rotate(-18)"/>
        <ellipse rx="286" ry="88" fill="none" stroke="#f0abfc" stroke-opacity="0.16" stroke-width="2" transform="rotate(-18)"/>

        <circle r="132" fill="#0e7490" fill-opacity="0.25" filter="url(#softGlow)"/>
        <circle r="120" fill="url(#planet)"/>

        <g fill="url(#crater)">
          <ellipse cx="-38" cy="-24" rx="26" ry="22" transform="rotate(-14)"/>
          <ellipse cx="34" cy="26" rx="30" ry="24" transform="rotate(12)"/>
          <ellipse cx="-20" cy="54" rx="16" ry="13" transform="rotate(-6)"/>
          <ellipse cx="58" cy="-38" rx="14" ry="11" transform="rotate(18)"/>
          <ellipse cx="-62" cy="24" rx="11" ry="9" transform="rotate(-20)"/>
          <ellipse cx="12" cy="-62" rx="9" ry="7" transform="rotate(8)"/>
          <ellipse cx="66" cy="4" rx="7" ry="6"/>
          <ellipse cx="-8" cy="4" rx="6" ry="5"/>
          <ellipse cx="44" cy="62" rx="5" ry="4"/>
          <ellipse cx="-50" cy="-58" rx="5" ry="4"/>
        </g>

        <g fill="none" stroke="#e0fbff" stroke-opacity="0.26" stroke-width="1.6">
          <path d="M-92-38C-46-64 22-70 78-44"/>
          <path d="M-98 12C-52-18 26-22 90 6" stroke-opacity="0.16"/>
          <path d="M-74 66C-24 40 44 38 96 60" stroke-opacity="0.12"/>
          <path d="M-44 92C-6 74 48 74 80 88" stroke-opacity="0.1"/>
        </g>

        <circle cx="-16" cy="-18" r="122" fill="none" stroke="#ffffff" stroke-opacity="0.1"/>
        <circle cx="-40" cy="-44" r="118" fill="none" stroke="#ffffff" stroke-opacity="0.06"/>
      </g>

      <g>
        <circle cx="332" cy="258" r="20" fill="url(#moon)"/>
        <circle cx="326" cy="252" r="5" fill="#78350f" fill-opacity="0.4"/>
        <circle cx="340" cy="266" r="3" fill="#78350f" fill-opacity="0.34"/>
        <circle cx="332" cy="258" r="34" fill="none" stroke="#fde68a" stroke-opacity="0.18"/>
      </g>

      <g stroke="#ffffff" stroke-linecap="round">
        <path d="M186 112v16M178 120h16" stroke-opacity="0.5" stroke-width="1.6"/>
        <path d="M1092 132v10M1087 137h10" stroke-opacity="0.38" stroke-width="1.4"/>
        <path d="M534 660v8M530 664h8" stroke-opacity="0.3" stroke-width="1.2"/>
      </g>

      <path d="M906 156c26-18 62-14 78 10" fill="none" stroke="#a5f3fc" stroke-opacity="0.4" stroke-width="2" stroke-linecap="round"/>
      <circle cx="990" cy="168" r="5" fill="#e0fbff" fill-opacity="0.9"/>
    </svg>`,
  );

export default function ImageZoomDemo() {
  return (
    <main className="flex min-h-screen w-full items-center justify-center bg-background p-4 sm:p-8">
      <ImageZoom
        src={ART}
        alt="A ringed planet with craters, orbital lines and stars in deep space"
        variant="compare"
        defaultSplit={56}
        defaultOrigin={{ x: 56, y: 50 }}
        lensZoom={2.6}
        minZoom={1.4}
        maxZoom={5}
        showZoomBadge
        showControls
        scrollZoom
        zoomStep={0.3}
        controlsPosition="bottom-right"
        doubleClickToReset
        loading="eager"
        className="aspect-[16/10] w-full max-w-4xl overflow-hidden rounded-2xl shadow-2xl shadow-foreground/10"
      />
    </main>
  );
}

Dependencies

More by terra vidhal

View all
  • Topographic Lines

    Topographic Lines

    4
  • Gravity Stars Background

    Gravity Stars Background

    4
  • Spotlight Card

    Spotlight Card

    3
  • Marquee

    Marquee

    2
  • Firework Burst

    Firework Burst

    1
  • Aurora Bubbles

    Aurora Bubbles

    1
  • Glyph Field

    Glyph Field

  • Shimmering Text

    Shimmering Text

Explore more

Part of PrequelUI Animate — browse the full library on 21st.dev.