Skip to content

Commit 00e4e07

Browse files
committed
fix: every zoom step is 1.6x, panning at one step
1 parent a909859 commit 00e4e07

2 files changed

Lines changed: 74 additions & 44 deletions

File tree

‎src/components/place/area-map.astro‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const svg = toSvg(
5151
<slot name="inset" />
5252
</div>
5353
)}
54-
<div class="area-map-shape mx-auto max-w-xl" set:html={svg} />
54+
<div data-zoom-map class="area-map-shape mx-auto max-w-xl" set:html={svg} />
5555
{note && <figcaption class="mt-5 text-center text-slate text-sm">{note}</figcaption>}
5656
</figure>
5757

‎src/lib/map-zoom.ts‎

Lines changed: 73 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const MAX = 8;
22
const STEP = 1.6;
3+
const WHEEL = 1.2;
34
/** Pointer travel in px after which a press is a drag, not a click on a link. */
45
const DRAG = 4;
56

@@ -21,23 +22,30 @@ function button(label: string, text: string): HTMLButtonElement {
2122
* Adds zoom and pan to one map. It changes the SVG viewBox, so lines stay sharp and stroke widths
2223
* stay the same. The wheel zooms only with Ctrl or ⌘, so the page still scrolls over the map.
2324
*/
25+
/** The map of a frame. A frame can hold a small locator map too, which does not zoom. */
26+
function mapIn(frame: HTMLElement): SVGSVGElement | null {
27+
return frame.querySelector('[data-zoom-map] svg') ?? frame.querySelector('svg');
28+
}
29+
2430
function attach(frame: HTMLElement): void {
25-
const svg = frame.querySelector('svg');
31+
const svg = mapIn(frame);
2632
if (!svg || frame.dataset.zoomReady) return;
2733
frame.dataset.zoomReady = 'true';
2834
const [x0 = 0, y0 = 0, w0 = 1, h0 = 1] = (svg.getAttribute('viewBox') ?? '')
2935
.split(' ')
3036
.map(Number);
3137
const full: View = { x: x0, y: y0, w: w0, h: h0 };
3238
let view = { ...full };
39+
/** Zoom level: 1 is the map as the page shows it, each step multiplies it by STEP. */
40+
let level = 1;
41+
/** Screen px per map unit at level 1, read when the first zoom starts. */
42+
let base = 0;
3343

3444
// Zoomed in, the map fills the whole frame instead of its own narrow box, so it is not cut
35-
// into a rectangle. The viewBox then takes the frame's shape, not the map's.
45+
// into a rectangle, and runs to the rounded edge of the frame.
3646
const holder = svg.parentElement ?? frame;
37-
let aspect = full.h / full.w;
3847
const expand = (on: boolean) => {
3948
if (on === svg.hasAttribute('data-zoomed')) return;
40-
// Zoomed in, the map runs to the rounded edge of the frame, with no straight cut inside it.
4149
const caption = frame.querySelector('figcaption')?.getBoundingClientRect().height ?? 0;
4250
const height = frame.getBoundingClientRect().height - caption;
4351
svg.toggleAttribute('data-zoomed', on);
@@ -47,39 +55,56 @@ function attach(frame: HTMLElement): void {
4755
svg.style.width = on ? '100%' : '';
4856
svg.style.height = on ? `${height}px` : '';
4957
svg.style.maxHeight = on ? 'none' : '';
50-
const box = svg.getBoundingClientRect();
51-
aspect = on ? box.height / box.width : full.h / full.w;
5258
};
53-
/** The widest view: the whole map fits, at the frame's shape. */
54-
const widest = () => Math.max(full.w, full.h / aspect);
55-
const clamp = (v: View): View => {
56-
const w = Math.min(widest(), Math.max(full.w / MAX, v.w));
57-
const h = w * aspect;
58-
// Keep the middle of the view over the map, so it cannot be dragged away.
59-
const cx = Math.min(full.x + full.w, Math.max(full.x, v.x + v.w / 2));
60-
const cy = Math.min(full.y + full.h, Math.max(full.y, v.y + v.h / 2));
61-
return { w, h, x: cx - w / 2, y: cy - h / 2 };
62-
};
63-
const apply = (v: View) => {
64-
const zoomedIn = v.w < widest() - 0.5;
65-
expand(zoomedIn);
66-
view = zoomedIn ? clamp(v) : { ...full };
59+
const isZoomed = () => level > 1;
60+
61+
/** Shows the map at a level, with a map point at a screen point. Level 1 goes back to normal. */
62+
const show = (next: number, map: { x: number; y: number }, screen: { x: number; y: number }) => {
63+
level = Math.min(MAX, next);
64+
if (level <= 1.0001) {
65+
level = 1;
66+
expand(false);
67+
view = { ...full };
68+
} else {
69+
expand(true);
70+
const box = svg.getBoundingClientRect();
71+
const scale = base * level;
72+
const w = box.width / scale;
73+
const h = box.height / scale;
74+
// Keep the middle of the view over the map, so it cannot be dragged away.
75+
const cx = Math.min(
76+
full.x + full.w,
77+
Math.max(full.x, map.x - (screen.x - box.left) / scale + w / 2),
78+
);
79+
const cy = Math.min(
80+
full.y + full.h,
81+
Math.max(full.y, map.y - (screen.y - box.top) / scale + h / 2),
82+
);
83+
view = { w, h, x: cx - w / 2, y: cy - h / 2 };
84+
}
6785
svg.setAttribute('viewBox', `${view.x} ${view.y} ${view.w} ${view.h}`);
68-
svg.style.cursor = zoomedIn ? 'grab' : '';
69-
reset.hidden = !zoomedIn;
86+
svg.style.cursor = isZoomed() ? 'grab' : '';
87+
reset.hidden = !isZoomed();
7088
};
71-
/** Zooms by a factor around a point given in screen px. */
89+
90+
/** Zooms by a factor around a screen point, or the middle of the map. */
7291
const zoom = (factor: number, cx?: number, cy?: number) => {
73-
// The map point under the pointer, before the frame changes shape.
92+
if (!isZoomed() && factor <= 1) return;
7493
const box = svg.getBoundingClientRect();
75-
const fx = cx === undefined ? 0.5 : (cx - box.left) / box.width;
76-
const fy = cy === undefined ? 0.5 : (cy - box.top) / box.height;
77-
const px = view.x + view.w * fx;
78-
const py = view.y + view.h * fy;
79-
const w = Math.min(view.w, widest()) / factor;
80-
expand(w < widest() - 0.5);
81-
const h = w * aspect;
82-
apply({ w, h, x: px - w * fx, y: py - h * fy });
94+
if (!isZoomed()) base = box.width / full.w;
95+
const screen = { x: cx ?? box.left + box.width / 2, y: cy ?? box.top + box.height / 2 };
96+
const map = {
97+
x: view.x + ((screen.x - box.left) / box.width) * view.w,
98+
y: view.y + ((screen.y - box.top) / box.height) * view.h,
99+
};
100+
show(level * factor, map, screen);
101+
};
102+
/** Moves the view by screen px. */
103+
const pan = (dx: number, dy: number) => {
104+
const box = svg.getBoundingClientRect();
105+
const scale = base * level;
106+
const screen = { x: box.left, y: box.top };
107+
show(level, { x: view.x - dx / scale, y: view.y - dy / scale }, screen);
83108
};
84109

85110
const controls = document.createElement('div');
@@ -91,7 +116,7 @@ function attach(frame: HTMLElement): void {
91116
reset.hidden = true;
92117
zoomIn.addEventListener('click', () => zoom(STEP));
93118
zoomOut.addEventListener('click', () => zoom(1 / STEP));
94-
reset.addEventListener('click', () => apply({ ...full, w: Number.POSITIVE_INFINITY }));
119+
reset.addEventListener('click', () => show(1, full, { x: 0, y: 0 }));
95120
controls.append(reset, zoomOut, zoomIn);
96121
frame.classList.add('relative');
97122
frame.append(controls);
@@ -101,7 +126,7 @@ function attach(frame: HTMLElement): void {
101126
(e) => {
102127
if (!e.ctrlKey && !e.metaKey) return;
103128
e.preventDefault();
104-
zoom(e.deltaY < 0 ? 1.2 : 1 / 1.2, e.clientX, e.clientY);
129+
zoom(e.deltaY < 0 ? WHEEL : 1 / WHEEL, e.clientX, e.clientY);
105130
},
106131
{ passive: false },
107132
);
@@ -134,23 +159,18 @@ function attach(frame: HTMLElement): void {
134159
moved += DRAG;
135160
return;
136161
}
137-
if (view.w >= full.w) return;
138-
const box = svg.getBoundingClientRect();
162+
if (!isZoomed()) return;
139163
const dx = next.x - last.x;
140164
const dy = next.y - last.y;
141165
moved += Math.abs(dx) + Math.abs(dy);
142166
if (moved > DRAG) svg.setPointerCapture(e.pointerId);
143-
apply({
144-
...view,
145-
x: view.x - (dx / box.width) * view.w,
146-
y: view.y - (dy / box.height) * view.h,
147-
});
167+
pan(dx, dy);
148168
});
149169
const end = (e: PointerEvent) => {
150170
pointers.delete(e.pointerId);
151171
if (pointers.size < 2) {
152172
pinch = 0;
153-
svg.style.touchAction = view.w < full.w ? 'none' : 'pan-y';
173+
svg.style.touchAction = isZoomed() ? 'none' : 'pan-y';
154174
}
155175
};
156176
svg.addEventListener('pointerup', end);
@@ -172,10 +192,20 @@ function attach(frame: HTMLElement): void {
172192
export function initMapZoom(): void {
173193
const scan = () => {
174194
for (const frame of document.querySelectorAll<HTMLElement>('[data-zoom]')) {
175-
const svg = frame.querySelector('svg');
195+
const svg = mapIn(frame);
176196
// A new <svg> means the map was drawn again: drop the old controls and attach once more.
177197
if (svg && svg !== zoomed.get(frame)) {
178198
frame.querySelector(':scope > [data-zoom-controls]')?.remove();
199+
if (svg.hasAttribute('data-zoomed')) {
200+
svg.removeAttribute('data-zoomed');
201+
frame.style.padding = '';
202+
frame.style.overflow = '';
203+
const holder = svg.parentElement;
204+
if (holder) holder.style.maxWidth = '';
205+
svg.style.width = '';
206+
svg.style.height = '';
207+
svg.style.maxHeight = '';
208+
}
179209
delete frame.dataset.zoomReady;
180210
zoomed.set(frame, svg);
181211
attach(frame);

0 commit comments

Comments
 (0)