Skip to content

Instantly share code, notes, and snippets.

@Iamsheye
Created May 6, 2024 12:57
Show Gist options
  • Save Iamsheye/7c464b85ab92fdf231b7dae017beb97c to your computer and use it in GitHub Desktop.
Save Iamsheye/7c464b85ab92fdf231b7dae017beb97c to your computer and use it in GitHub Desktop.
Image Magnifier
import { useState } from 'react';
const ImageMagnifier = ({
src,
className,
width,
height,
alt,
magnifierHeight = 150,
magnifierWidth = 150,
zoomLevel = 3
}) => {
const [showMagnifier, setShowMagnifier] = useState(false);
const [[imgWidth, imgHeight], setSize] = useState([0, 0]);
const [[x, y], setXY] = useState([0, 0]);
const mouseEnter = (e) => {
const el = e.currentTarget;
const { width, height } = el.getBoundingClientRect();
setSize([width, height]);
setShowMagnifier(true);
}
const mouseLeave = (e) => {
e.preventDefault();
setShowMagnifier(false);
}
const mouseMove = (e) => {
const el = e.currentTarget;
const { top, left } = el.getBoundingClientRect();
const x = e.pageX - left - window.scrollX;
const y = e.pageY - top - window.scrollY;
setXY([x, y]);
};
return <div className="relative inline-block">
<img
src={src}
className={className}
width={width}
height={height}
alt={alt}
onMouseEnter={(e) => mouseEnter(e)}
onMouseLeave={(e) => mouseLeave(e)}
onMouseMove={(e) => mouseMove(e)}
/>
<div
style={{
display: showMagnifier ? '' : 'none',
position: 'absolute',
pointerEvents: 'none',
height: `${magnifierHeight}px`,
width: `${magnifierWidth}px`,
opacity: '1',
border: '1px solid lightgrey',
backgroundColor: 'white',
borderRadius: '5px',
backgroundImage: `url('${src}')`,
backgroundRepeat: 'no-repeat',
top: `${y - magnifierHeight / 2}px`,
left: `${x - magnifierWidth / 2}px`,
backgroundSize: `${imgWidth * zoomLevel}px ${imgHeight * zoomLevel}px`,
backgroundPositionX: `${-x * zoomLevel + magnifierWidth / 2}px`,
backgroundPositionY: `${-y * zoomLevel + magnifierHeight / 2}px`,
}}
/>
</div>
};
export default ImageMagnifier;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment