Created
May 6, 2024 12:57
-
-
Save Iamsheye/7c464b85ab92fdf231b7dae017beb97c to your computer and use it in GitHub Desktop.
Image Magnifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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