From a6cd642c59476d6eca938fd1b2c4da761edeb344 Mon Sep 17 00:00:00 2001 From: etosphere Date: Sun, 4 Oct 2026 23:50:00 -0400 Subject: [PATCH] Fixed: Preserve horizontal pan and zoom when scrolling The wheel handler and the top and bottom limit clamps recentered the viewport horizontally, so a zoomed-in canvas that had been dragged sideways jumped back to the middle as soon as it was scrolled. The scrollbar refit the layout width instead, which also recentered the viewport and, when zoomed in past the page width, reset the zoom. Keep the horizontal position when scrolling with the wheel or the scrollbar. The clamps keep the target x because OpenSeadragon raises the pan event for an animated drag step before the view moves; the current x would drop that step's horizontal movement at the limits. Refs #569 --- browser-tests/public-api.spec.ts | 50 ++++++++++++++++++++++++++++++++ src/viewer-element.ts | 13 +++++---- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/browser-tests/public-api.spec.ts b/browser-tests/public-api.spec.ts index 8f8ae63c..b49603b0 100644 --- a/browser-tests/public-api.spec.ts +++ b/browser-tests/public-api.spec.ts @@ -557,6 +557,56 @@ test("navigates by openings and frames an image region", async ({page}) => { await expect.poll(() => page.evaluate(() => (window as any).diva.getState().zoom)).not.toBeNull(); }); +test("keeps the zoom and horizontal position while scrolling", async ({page}) => { + const states = await page.evaluate(async () => { + const viewer = (document.getElementById("main-viewer") as any).viewer; + const viewport = viewer.viewport; + const Point = (window as any).OpenSeadragon.Point; + const state = () => ({...viewport.getCenter(true), zoom : viewport.getZoom(true)}); + const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve)); + const wheel = (deltaY: number) => + viewer.canvas.dispatchEvent(new WheelEvent("wheel", {deltaY, bubbles : true, cancelable : true})); + + const zoomed = new Promise((resolve) => viewer.addOnceHandler("animation-finish", resolve)); + viewport.zoomTo(1.5, null, true); + await zoomed; + viewport.panBy(new Point(0.1, 0), true); + const panned = state(); + + wheel(120); + const wheeled = state(); + + const thumb = document.querySelector(".diva-scrollbar-thumb")!; + const thumbY = thumb.getBoundingClientRect().top; + thumb.dispatchEvent(new MouseEvent("mousedown", {clientY : thumbY})); + document.dispatchEvent(new MouseEvent("mousemove", {clientY : thumbY + 40})); + document.dispatchEvent(new MouseEvent("mouseup")); + const thumbDragged = state(); + + wheel(-100000); + const top = state(); + viewport.panBy(new Point(0.05, -0.05), false); + await nextFrame(); + await nextFrame(); + viewport.panBy(new Point(0.05, -0.05), false); + while (!viewport.getCenter(true).equals(viewport.getCenter(false))) + { + await nextFrame(); + } + const dragged = state(); + + return {panned, wheeled, thumbDragged, top, dragged}; + }); + + expect(states.wheeled.y).toBeGreaterThan(states.panned.y); + expect(states.wheeled.x).toBeCloseTo(states.panned.x, 6); + expect(states.thumbDragged.y).toBeGreaterThan(states.wheeled.y); + expect(states.thumbDragged.x).toBeCloseTo(states.panned.x, 6); + expect(states.thumbDragged.zoom).toBeCloseTo(states.panned.zoom, 6); + expect(states.dragged.y).toBeCloseTo(states.top.y, 6); + expect(states.dragged.x).toBeCloseTo(states.panned.x + 0.1, 6); +}); + test("bolds ranges containing the current page in the contents index", async ({page}) => { const name = "ranged"; await page.route(`${origin}/api/${name}/manifest`, (route) => route.fulfill({json : { diff --git a/src/viewer-element.ts b/src/viewer-element.ts index 9f38fd17..0ceae76c 100644 --- a/src/viewer-element.ts +++ b/src/viewer-element.ts @@ -1968,10 +1968,9 @@ class OsdViewer extends HTMLElement const panY = rect.height > 0 ? (deltaY / rect.height) * speed : 0; const center = viewport.getCenter(true); - const nextCenter = new OpenSeadragon.Point(this.getCenterX(), center.y + panY); + const nextCenter = new OpenSeadragon.Point(center.x, center.y + panY); viewport.panTo(nextCenter, true); - this.recenterBoundsX(); this.clampTop(); this.clampBottom(); this.maybeEmitPageChange(); @@ -2057,7 +2056,7 @@ class OsdViewer extends HTMLElement const clampedCenterY = (bounds.height / 2) + minTop; this.isClamping = true; - vp.panTo(new OpenSeadragon.Point(this.getCenterX(), clampedCenterY), true); + vp.panTo(new OpenSeadragon.Point(vp.getCenter(false).x, clampedCenterY), true); vp.applyConstraints(); this.isClamping = false; } @@ -2091,7 +2090,7 @@ class OsdViewer extends HTMLElement const clampedCenterY = maxBottom - bounds.height / 2; this.isClamping = true; - vp.panTo(new OpenSeadragon.Point(this.getCenterX(), clampedCenterY), true); + vp.panTo(new OpenSeadragon.Point(vp.getCenter(false).x, clampedCenterY), true); vp.applyConstraints(); this.isClamping = false; } @@ -2475,7 +2474,8 @@ class OsdViewer extends HTMLElement const maxScroll = totalHeight - viewportHeight; const newScrollY = scrollProgress * maxScroll; - this.scrollToOffset(newScrollY); + const center = this.viewer.viewport.getCenter(true); + this.viewer.viewport.panTo(new OpenSeadragon.Point(center.x, newScrollY + (viewportHeight / 2)), true); }; const onMouseUp = (): void => { @@ -2523,7 +2523,8 @@ class OsdViewer extends HTMLElement const maxScroll = totalHeight - viewportHeight; const newScrollY = scrollProgress * maxScroll; - this.scrollToOffset(newScrollY); + const center = this.viewer.viewport.getCenter(true); + this.viewer.viewport.panTo(new OpenSeadragon.Point(center.x, newScrollY + (viewportHeight / 2)), true); }); } }