All components
Lens
effectsAceternity UI component.
responsive · 560px
Install
Same command in any shadcn project — React (Vite/CRA), Next.js, Remix, Astro, and more:
$
npx shadcn@latest add https://your-domain/r/lens.jsonUsage
"use client";
import { Lens } from "@/registry/aceternity-ui/lens";
export default function Demo() {
return (
<div className="flex h-full w-full items-center justify-center bg-background p-8">
<div className="w-[480px] max-w-full">
<Lens zoomFactor={1.8} lensSize={160}>
<img
src="https://images.unsplash.com/photo-1441974231531-c6227db76b6e?q=80&w=1200&auto=format&fit=crop"
alt="Forest landscape"
width={480}
height={320}
className="block rounded-xl w-full object-cover"
style={{ aspectRatio: "3/2" }}
/>
</Lens>
<p className="mt-3 text-sm text-muted-foreground text-center">
Move cursor over the image to magnify
</p>
</div>
</div>
);
}Component source
"use client";
import React, { useRef, useState } from "react";
import { AnimatePresence, motion } from "motion/react";
interface LensProps {
children: React.ReactNode;
zoomFactor?: number;
lensSize?: number;
position?: {
x: number;
y: number;
};
isStatic?: boolean;
isFocusing?: () => void;
hovering?: boolean;
setHovering?: (hovering: boolean) => void;
}
export const Lens: React.FC<LensProps> = ({
children,
zoomFactor = 1.5,
lensSize = 170,
isStatic = false,
position = { x: 200, y: 150 },
hovering,
setHovering,
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const [localIsHovering, setLocalIsHovering] = useState(false);
const isHovering = hovering !== undefined ? hovering : localIsHovering;
const setIsHovering = setHovering || setLocalIsHovering;
// const [isHovering, setIsHovering] = useState(false);
const [mousePosition, setMousePosition] = useState({ x: 100, y: 100 });
const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
const rect = e.currentTarget.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
setMousePosition({ x, y });
};
return (
<div
ref={containerRef}
className="relative overflow-hidden rounded-lg z-20"
onMouseEnter={() => {
setIsHovering(true);
}}
onMouseLeave={() => setIsHovering(false)}
onMouseMove={handleMouseMove}
>
{children}
{isStatic ? (
<div>
<motion.div
initial={{ opacity: 0, scale: 0.58 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
transition={{ duration: 0.3, ease: "easeOut" }}
className="absolute inset-0 overflow-hidden"
style={{
maskImage: `radial-gradient(circle ${lensSize / 2}px at ${
position.x
}px ${position.y}px, black 100%, transparent 100%)`,
WebkitMaskImage: `radial-gradient(circle ${lensSize / 2}px at ${
position.x
}px ${position.y}px, black 100%, transparent 100%)`,
transformOrigin: `${position.x}px ${position.y}px`,
}}
>
<div
className="absolute inset-0"
style={{
transform: `scale(${zoomFactor})`,
transformOrigin: `${position.x}px ${position.y}px`,
}}
>
{children}
</div>
</motion.div>
</div>
) : (
<AnimatePresence>
{isHovering && (
<div>
<motion.div
initial={{ opacity: 0, scale: 0.58 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
transition={{ duration: 0.3, ease: "easeOut" }}
className="absolute inset-0 overflow-hidden"
style={{
maskImage: `radial-gradient(circle ${lensSize / 2}px at ${
mousePosition.x
}px ${mousePosition.y}px, black 100%, transparent 100%)`,
WebkitMaskImage: `radial-gradient(circle ${
lensSize / 2
}px at ${mousePosition.x}px ${
mousePosition.y
}px, black 100%, transparent 100%)`,
transformOrigin: `${mousePosition.x}px ${mousePosition.y}px`,
zIndex: 50,
}}
>
<div
className="absolute inset-0"
style={{
transform: `scale(${zoomFactor})`,
transformOrigin: `${mousePosition.x}px ${mousePosition.y}px`,
}}
>
{children}
</div>
</motion.div>
</div>
)}
</AnimatePresence>
)}
</div>
);
};Dependencies
motion
Source: Aceternity UI