Pan and zoom for images
Try to use gestures with this image ๐
Introduction
@beoe/pan-zoom
โ is a small client-side script that adds pan and zoom capabilities to SVG images (or any DOM node, actually).
There are many similar scripts, but the main difference is that this one supports gestures for all types of devices:
Intention | Mouse | Trackpad/Touchpad | Touchscreen |
---|---|---|---|
Pan | Click + move | Click + move | Two-finger drag |
Zoom | Ctrl + wheel | Pinch | Pinch |
Reset | Double click | Double click | Double tap |
Scroll | Wheel | Two-finger drag | One-finger drag |
Pay attention to the following:
- Gestures are intentionally selected to avoid interfering with the systemโs default scroll gestures, to prevent โscroll traps.โ
- Cmd + click - zoom in.
- Alt + click - zoom out.
- The first double click (or tap) - zooms in by 2x.
Installation
-
Install dependencies
-
Add
svgpanzoom.ts
-
Use
svgpanzoom.ts
in the base layout
Starlight specific code
-
Use
svgpanzoom.ts
in thePageFrame
-
Override
PageFrame
in Astro config
Further Improvements
- Create a Rehype plugin to wrap images in a container (
<figure class="beoe"></figure>
) to avoid creating it on the client side. - Do not stretch images if they are smaller than viewport
- Prevent clicks on drag or pan
- minimap and full-screen mode, like in reactflow โ