A hook to handle pinch-to-zoom gestures on touch devices.
Made by raouf.codesOverview
The usePinchZoom hook provides pinch-to-zoom gesture handling for touch devices. It tracks two-finger touch gestures and calculates zoom levels with configurable min/max bounds.
Installation
Usage
Basic
With Custom Bounds
With Reset Button
API Reference
usePinchZoom
UsePinchZoomOptions
| Prop | Type | Default |
|---|---|---|
elementRef | React.RefObject<HTMLElement | null> | - |
onZoom | function | - |
minZoom? | number | 0.5 |
maxZoom? | number | 3 |
initialZoom? | number | 1 |
Touch Events
The hook handles the following touch events:
- touchstart — Records initial pinch distance when two fingers touch
- touchmove — Calculates new zoom based on finger distance change
- touchend/touchcancel — Resets pinch tracking when gesture ends
Styling Tips
For smooth zooming, consider these CSS properties:
Features
- Bounded zoom — Configurable min and max zoom levels
- Smooth gestures — Calculates zoom based on pinch distance ratio
- Prevents conflicts — Calls
preventDefault()to stop page zooming - Clean cleanup — Removes all touch listeners on unmount
Browser Support
Pinch zoom works on all modern touch-enabled browsers:
- iOS Safari
- Android Chrome
- Touch-enabled laptops
Credits
- Based on standard touch gesture handling patterns
Built by raouf.codes. The source code is available on GitHub.
Last updated: 2/27/2026