StealThis .dev

Pinch Zoom

Pinch-to-zoom and pan image viewer. Two-finger spread zooms in; single-finger drag pans when zoomed. Double-tap resets. No libraries.

Open in Lab
vanilla-js css
Targets: JS HTML

Code

Pinch Zoom

A full pinch-to-zoom image viewer. Use two fingers to zoom in and out, one finger to pan while zoomed, and double-tap to reset to the original size.

How it works

  1. touchstart with two touches records the initial distance between fingers
  2. touchmove with two touches computes the new distance and sets scale via transform
  3. Scale is clamped between 1× and 5×
  4. Single-finger touchmove when scale > 1 pans the image by updating translateX / translateY
  5. Pan boundaries are constrained so the image cannot be dragged fully off screen
  6. Double-tap (touchend within 300ms of previous tap) resets scale and position

Accessibility

  • Add role="img" and a descriptive aria-label on the container for screen readers
  • Keyboard zoom via + / - keys is included as a fallback

When to use it

  • Product image detail pages
  • Mobile photo viewers and galleries
  • Map or diagram viewers