Skip to content

Minimap Pattern

A minimap shows a bird’s-eye view of the entire canvas with a viewport indicator. Since useZoomPinch gives you full access to ViewState, building a minimap is straightforward.

Minimap with click-to-navigate
import { useRef, useState } from "react"
import { useZoomPinch, type ViewState } from "use-zoom-pinch"
const CANVAS_SIZE = 2000
const MINIMAP_SIZE = 150
function CanvasWithMinimap() {
const containerRef = useRef<HTMLDivElement>(null)
const [viewState, setViewState] = useState<ViewState>({ x: 0, y: 0, zoom: 1 })
const { view, panTo } = useZoomPinch({
containerRef,
viewState,
onViewStateChange: setViewState,
minScale: 0.2,
maxScale: 5,
})
return (
<div style={{ position: "relative", width: "100%", height: "100vh" }}>
<div
ref={containerRef}
style={{
width: "100%",
height: "100%",
overflow: "hidden",
touchAction: "none",
cursor: "grab",
}} >
<div
style={{
transform: `translate(${view.x}px, ${view.y}px) scale(${view.zoom})`,
transformOrigin: "0 0",
position: "absolute",
}} >
<CanvasContent />
</div>
</div>
<Minimap view={view} onNavigate={panTo} containerRef={containerRef} />
</div>
)
}