import React from "react"; import { useState } from "react"; // import "./styles.css"; interface ImageMagnifierProps { src: string; id: string; // ref: any; width?: string; height?: string; magnifierHeight?: number; magnifieWidth?: number; zoomLevel?: number; style?: any; onLoad?: () => void; innerRef: any } const ImageMagnifier = (props: ImageMagnifierProps) => { // console.log(style) const [[x, y], setXY] = useState([0, 0]); const [[imgWidth, imgHeight], setSize] = useState([0, 0]); const [showMagnifier, setShowMagnifier] = useState(false); const { src, id, width, height, onLoad, magnifierHeight = 100, magnifieWidth = 100, zoomLevel = 1.5, innerRef } = props; return (
{ // update image size and turn-on magnifier const elem = e.currentTarget; const { width, height } = elem.getBoundingClientRect(); setSize([width, height]); setShowMagnifier(true); }} onMouseMove={(e) => { // update cursor position const elem = e.currentTarget; const { top, left } = elem.getBoundingClientRect(); // calculate cursor position on the image const x = e.pageX - left - window.pageXOffset; const y = e.pageY - top - window.pageYOffset; setXY([x, y]); }} onMouseLeave={() => { // close magnifier setShowMagnifier(false); }} onLoad={onLoad} alt={"img"} />
); }; export default ImageMagnifier;