Pan & Zoom Viewer
Zoom a map-like image with the wheel, buttons or a two-finger pinch and drag it to pan, with zoom limits and a reset.
AI prompt
Clean UI mockup of an image viewer showing a detailed illustrated city map zoomed to 250 percent, the map filling a rounded frame, a small vertical stack of round buttons plus, minus and reset at the top right, a zoom percentage label at the bottom left, a thin mini-map outline in the corner showing the visible area, light neutral theme, flat modern style. Negative: heavy shadows, clutter, text errors, device frames. Reduced motion: static design.About this pattern
Definition
A pan and zoom viewer lets people inspect detail in a large image or map. It combines three inputs: wheel or pinch to scale around the pointer, drag to move, and buttons for people who cannot use gestures. Limits stop the image being lost off screen.
Purpose & UX principles
- Zoom around the pointer or the pinch centre, not the image corner.
- Clamp the scale, for example one to four times, and clamp the pan so the image always covers the frame.
- Track two pointers for pinch with pointer events; set touch-action: none.
- Always offer plus, minus and reset buttons.
Interaction states
- Wheel, drag, pinch and Zoom in, Zoom out and Reset buttons.
Motion specification
- Transforms follow input directly.
- Reduced motion: no easing is used at all.
Responsive behaviour
- Keep the buttons outside the gesture area or clearly marked so they remain usable at any zoom.
- Support keyboard: arrows pan, plus and minus zoom.
Code
<div class="bx"><div class="vp" id="vp" tabindex="0" aria-label="Map viewer. Arrow keys pan, plus and minus zoom."><div class="mp" id="mp"></div><span class="zl" id="zl">100%</span></div><div class="two3"><button class="mini" id="zi" type="button">Zoom in</button><button class="mini" id="zo" type="button">Zoom out</button><button class="mini" id="zr" type="button">Reset</button></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.vp{position:relative;height:150px;border-radius:12px;overflow:hidden;border:1px solid var(--line);touch-action:none;cursor:grab;background:#e8ecdf}.vp:active{cursor:grabbing}.mp{position:absolute;left:0;top:0;width:330px;height:150px;transform-origin:0 0;background:radial-gradient(circle at 18% 30%,#9cc6e6 0 16px,transparent 17px),radial-gradient(circle at 70% 64%,#9cc6e6 0 22px,transparent 23px),repeating-linear-gradient(0deg,#fff 0 4px,transparent 4px 30px),repeating-linear-gradient(90deg,#fff 0 4px,transparent 4px 44px),#dfe6d2}.zl{position:absolute;left:8px;bottom:6px;background:rgba(255,255,255,.9);padding:1px 7px;border-radius:99px;font-size:11px;font-weight:600}.two3{display:grid;grid-template-columns:repeat(3,1fr);gap:6px}.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const vp=document.getElementById('vp'),mp=document.getElementById('mp'),W=330,H=150;let s=1,tx=0,ty=0;const P=new Map();let d0=0,s0=1;
function clamp(){s=Math.max(1,Math.min(4,s));tx=Math.min(0,Math.max(W-W*s,tx));ty=Math.min(0,Math.max(H-H*s,ty));}
function draw(){clamp();mp.style.transform='translate('+tx+'px,'+ty+'px) scale('+s+')';document.getElementById('zl').textContent=Math.round(s*100)+'%';}
function zoomAt(cx,cy,ns){const k=Math.max(1,Math.min(4,ns))/s;tx=cx-(cx-tx)*k;ty=cy-(cy-ty)*k;s=s*k;draw();}
function loc(e){const r=vp.getBoundingClientRect();return [(e.clientX-r.left)*W/r.width,(e.clientY-r.top)*H/r.height];}
vp.addEventListener('wheel',e=>{e.preventDefault();const l=loc(e);zoomAt(l[0],l[1],s*(e.deltaY<0?1.15:1/1.15));},{passive:false});
vp.addEventListener('pointerdown',e=>{vp.setPointerCapture(e.pointerId);P.set(e.pointerId,[e.clientX,e.clientY]);if(P.size===2){const a=[...P.values()];d0=Math.hypot(a[0][0]-a[1][0],a[0][1]-a[1][1]);s0=s;}});
vp.addEventListener('pointermove',e=>{if(!P.has(e.pointerId))return;const o=P.get(e.pointerId);P.set(e.pointerId,[e.clientX,e.clientY]);const r=vp.getBoundingClientRect(),k=W/r.width;if(P.size===1){tx+=(e.clientX-o[0])*k;ty+=(e.clientY-o[1])*k;draw();}else if(P.size===2){const a=[...P.values()],d=Math.hypot(a[0][0]-a[1][0],a[0][1]-a[1][1]),cx=((a[0][0]+a[1][0])/2-r.left)*k,cy=((a[0][1]+a[1][1])/2-r.top)*k;zoomAt(cx,cy,s0*d/d0);}});
['pointerup','pointercancel'].forEach(t=>vp.addEventListener(t,e=>{P.delete(e.pointerId);}));
document.getElementById('zi').addEventListener('click',()=>zoomAt(W/2,H/2,s*1.4));document.getElementById('zo').addEventListener('click',()=>zoomAt(W/2,H/2,s/1.4));document.getElementById('zr').addEventListener('click',()=>{s=1;tx=0;ty=0;draw();});
vp.addEventListener('keydown',e=>{const m={ArrowLeft:[30,0],ArrowRight:[-30,0],ArrowUp:[0,30],ArrowDown:[0,-30]}[e.key];if(m){e.preventDefault();tx+=m[0];ty+=m[1];draw();}else if(e.key==='+'||e.key==='='){zoomAt(W/2,H/2,s*1.4);}else if(e.key==='-'){zoomAt(W/2,H/2,s/1.4);}});draw();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Pan & Zoom Viewer — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.vp{position:relative;height:150px;border-radius:12px;overflow:hidden;border:1px solid var(--line);touch-action:none;cursor:grab;background:#e8ecdf}.vp:active{cursor:grabbing}.mp{position:absolute;left:0;top:0;width:330px;height:150px;transform-origin:0 0;background:radial-gradient(circle at 18% 30%,#9cc6e6 0 16px,transparent 17px),radial-gradient(circle at 70% 64%,#9cc6e6 0 22px,transparent 23px),repeating-linear-gradient(0deg,#fff 0 4px,transparent 4px 30px),repeating-linear-gradient(90deg,#fff 0 4px,transparent 4px 44px),#dfe6d2}.zl{position:absolute;left:8px;bottom:6px;background:rgba(255,255,255,.9);padding:1px 7px;border-radius:99px;font-size:11px;font-weight:600}.two3{display:grid;grid-template-columns:repeat(3,1fr);gap:6px}.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="vp" id="vp" tabindex="0" aria-label="Map viewer. Arrow keys pan, plus and minus zoom."><div class="mp" id="mp"></div><span class="zl" id="zl">100%</span></div><div class="two3"><button class="mini" id="zi" type="button">Zoom in</button><button class="mini" id="zo" type="button">Zoom out</button><button class="mini" id="zr" type="button">Reset</button></div></div>
</div>
<script>
const vp=document.getElementById('vp'),mp=document.getElementById('mp'),W=330,H=150;let s=1,tx=0,ty=0;const P=new Map();let d0=0,s0=1;
function clamp(){s=Math.max(1,Math.min(4,s));tx=Math.min(0,Math.max(W-W*s,tx));ty=Math.min(0,Math.max(H-H*s,ty));}
function draw(){clamp();mp.style.transform='translate('+tx+'px,'+ty+'px) scale('+s+')';document.getElementById('zl').textContent=Math.round(s*100)+'%';}
function zoomAt(cx,cy,ns){const k=Math.max(1,Math.min(4,ns))/s;tx=cx-(cx-tx)*k;ty=cy-(cy-ty)*k;s=s*k;draw();}
function loc(e){const r=vp.getBoundingClientRect();return [(e.clientX-r.left)*W/r.width,(e.clientY-r.top)*H/r.height];}
vp.addEventListener('wheel',e=>{e.preventDefault();const l=loc(e);zoomAt(l[0],l[1],s*(e.deltaY<0?1.15:1/1.15));},{passive:false});
vp.addEventListener('pointerdown',e=>{vp.setPointerCapture(e.pointerId);P.set(e.pointerId,[e.clientX,e.clientY]);if(P.size===2){const a=[...P.values()];d0=Math.hypot(a[0][0]-a[1][0],a[0][1]-a[1][1]);s0=s;}});
vp.addEventListener('pointermove',e=>{if(!P.has(e.pointerId))return;const o=P.get(e.pointerId);P.set(e.pointerId,[e.clientX,e.clientY]);const r=vp.getBoundingClientRect(),k=W/r.width;if(P.size===1){tx+=(e.clientX-o[0])*k;ty+=(e.clientY-o[1])*k;draw();}else if(P.size===2){const a=[...P.values()],d=Math.hypot(a[0][0]-a[1][0],a[0][1]-a[1][1]),cx=((a[0][0]+a[1][0])/2-r.left)*k,cy=((a[0][1]+a[1][1])/2-r.top)*k;zoomAt(cx,cy,s0*d/d0);}});
['pointerup','pointercancel'].forEach(t=>vp.addEventListener(t,e=>{P.delete(e.pointerId);}));
document.getElementById('zi').addEventListener('click',()=>zoomAt(W/2,H/2,s*1.4));document.getElementById('zo').addEventListener('click',()=>zoomAt(W/2,H/2,s/1.4));document.getElementById('zr').addEventListener('click',()=>{s=1;tx=0;ty=0;draw();});
vp.addEventListener('keydown',e=>{const m={ArrowLeft:[30,0],ArrowRight:[-30,0],ArrowUp:[0,30],ArrowDown:[0,-30]}[e.key];if(m){e.preventDefault();tx+=m[0];ty+=m[1];draw();}else if(e.key==='+'||e.key==='='){zoomAt(W/2,H/2,s*1.4);}else if(e.key==='-'){zoomAt(W/2,H/2,s/1.4);}});draw();
</script>
</body></html>