Skip to content

Commit

Permalink
Only preserve the viewport if the preserved viewport 'makes sense'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 28, 2025
1 parent 8e1a0c9 commit fc3ce9a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
8 changes: 1 addition & 7 deletions __tests__/integration/mirador/mirador-configs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ export default {
transitions: {},
},
windows: [{
canvasId: 'https://iiif.harvardartmuseums.org/manifests/object/299843/canvas/canvas-47174892',
manifestId: 'https://iiif.harvardartmuseums.org/manifests/object/299843',
},
{
canvasId: 'https://iiif.bodleian.ox.ac.uk/iiif/canvas/e58b8c60-005c-4c41-a22f-07d49cb25ede.json',
manifestId: 'https://iiif.bodleian.ox.ac.uk/iiif/manifest/e32a277e-91e2-4a6d-8ba6-cc4bad230410.json',
thumbnailNavigationPosition: 'far-bottom',
manifestId: 'https://wellcomelibrary.org/iiif/b18035723/manifest',
}],
};
33 changes: 32 additions & 1 deletion src/components/OpenSeadragonComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ function OpenSeadragonComponent({
bounds: viewport.getBounds(),
flip: viewport.getFlip(),
rotation: viewport.getRotation(),
worldBounds: (() => {
const homeBounds = viewport.viewer?.world?.getHomeBounds();

if (!homeBounds) return undefined;

return [homeBounds.x, homeBounds.y, homeBounds.width, homeBounds.height];

Check warning on line 41 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L41

Added line #L41 was not covered by tests
})(),
x: Math.round(viewport.centerSpringX.target.value),
y: Math.round(viewport.centerSpringY.target.value),
zoom: viewport.zoomSpring.target.value,
Expand Down Expand Up @@ -105,17 +112,41 @@ function OpenSeadragonComponent({
const bounds = viewerConfig.bounds || worldBounds;
if (bounds && !viewerConfig.x && !viewerConfig.y && !viewerConfig.zoom) {
const rect = new Openseadragon.Rect(...bounds);
if (rect.equals(viewport.getBounds())) {
if (!rect.equals(viewport.getBounds())) {
viewport.fitBounds(rect, false);
}
}
}, [initialViewportSet, setInitialBounds, viewerConfig, viewerRef, worldBounds]);

useEffect(() => {
if (!osdConfig.preserveViewport) return;
if (!viewerConfig?.worldBounds || !worldBounds) return;

const viewer = viewerRef.current;
if (!viewer) return;
const { viewport } = viewer;

Check warning on line 127 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L125-L127

Added lines #L125 - L127 were not covered by tests

const [_x, _y, width, height] = viewerConfig.worldBounds;
const [_x1, _y1, width1, height1] = worldBounds;

Check warning on line 130 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L129-L130

Added lines #L129 - L130 were not covered by tests

const previousAspectRatio = (1.0 * width) / height;
const newAspectRatio = (1.0 * width1) / height1;

Check warning on line 133 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L132-L133

Added lines #L132 - L133 were not covered by tests

if ((previousAspectRatio < (1 - osdConfig.resetViewportAfterAspectRatioDelta) * newAspectRatio)

Check warning on line 135 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L135

Added line #L135 was not covered by tests
|| (previousAspectRatio > (1 + osdConfig.resetViewportAfterAspectRatioDelta) * newAspectRatio)) {
const rect = new Openseadragon.Rect(...worldBounds);
if (!rect.equals(viewport.getBounds())) {

Check warning on line 138 in src/components/OpenSeadragonComponent.js

View check run for this annotation

Codecov / codecov/patch

src/components/OpenSeadragonComponent.js#L137-L138

Added lines #L137 - L138 were not covered by tests
viewport.fitBounds(rect, false);
}
}
}, [osdConfig, viewerConfig, worldBounds, viewerRef]);

// initialize OSD stuff when this component is mounted
useEffect(() => {
const viewer = Openseadragon({
element: ref.current,
...osdConfig,
preserveViewportAspectRatio: undefined,
});

viewer.addHandler('canvas-drag', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/OpenSeadragonViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ export function OpenSeadragonViewer({
const apiRef = useRef();
const [viewer, setViewer] = useState(null);
const onViewportChange = useCallback(({
flip, rotation, x, y, zoom,
flip, rotation, worldBounds, x, y, zoom,
}) => {
updateViewport(windowId, {
flip,
rotation,
worldBounds,
x,
y,
zoom,
Expand Down
1 change: 1 addition & 0 deletions src/config/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ export default {
blendTime: 0.1,
preserveImageSizeOnResize: true,
preserveViewport: true,
resetViewportAfterAspectRatioDelta: 0.25,
showNavigationControl: false,
zoomPerClick: 1, // disable zoom-to-click
zoomPerDoubleClick: 2.0
Expand Down

0 comments on commit fc3ce9a

Please sign in to comment.