Skip to content

Image Viewer

A common use case: a single image that users can zoom and pan to inspect details.

Image Viewer
Landscape
100%
import { useRef } from "react"
import { useZoomPinch } from "use-zoom-pinch"
function ImageViewer({ src, alt }: { src: string; alt: string }) {
const containerRef = useRef<HTMLDivElement>(null)
const { view, zoomIn, zoomOut, resetView } = useZoomPinch({
containerRef,
minScale: 0.5,
maxScale: 10,
doubleTap: { mode: "toggle", step: 3 },
inertia: { friction: 0.95 },
})
return (
<div style={{ position: "relative", width: "100%", height: "80vh" }}>
<div
style={{
position: "absolute",
bottom: 16,
right: 16,
zIndex: 10,
display: "flex",
gap: 4,
}} >
<button onClick={() => zoomIn(1.5, { animate: true })}>+</button>
<button onClick={() => zoomOut(1.5, { animate: true })}>-</button>
<button onClick={() => resetView({ animate: true })}>Fit</button>
<span>{Math.round(view.zoom \* 100)}%</span>
</div>
<div
ref={containerRef}
style={{
width: "100%",
height: "100%",
overflow: "hidden",
touchAction: "none",
cursor: view.zoom > 1 ? "grab" : "zoom-in",
background: "#0f0f0f",
}}
>
<div
style={{
transform: `translate(${view.x}px, ${view.y}px) scale(${view.zoom})`,
transformOrigin: "0 0",
}}
>
<img
src={src}
alt={alt}
draggable={false}
style={{ display: "block", maxWidth: "100%", userSelect: "none" }}
/>
</div>
</div>
</div>
)
}