Skip to content

Fix zoom tool aspect ratio #2055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions packages/tools/src/tools/ZoomTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,50 @@ class ZoomTool extends BaseTool {
// Added spacing preset in case there is no imageData on viewport
const imageData = viewport.getImageData();
let spacing = [1, 1, 1];
let cappedParallelScale = parallelScaleToSet;
let thresholdExceeded = false;

if (imageData) {
spacing = imageData.spacing;
}

const { minZoomScale, maxZoomScale } = this.configuration;
const { dimensions } = imageData;
const imageWidth = dimensions[0] * spacing[0];
const imageHeight = dimensions[1] * spacing[1];

const t = element.clientHeight * spacing[1] * 0.5;
const scale = t / parallelScaleToSet;
const canvasAspect = size[0] / size[1];

let cappedParallelScale = parallelScaleToSet;
let thresholdExceeded = false;
// Get display area, if available
const displayArea = viewport.options?.displayArea;
const imageAreaScaleX = displayArea?.imageArea?.[0] ?? 1.1;
const imageAreaScaleY = displayArea?.imageArea?.[1] ?? 1.1;

if (imageData) {
if (scale < minZoomScale) {
cappedParallelScale = t / minZoomScale;
// Adjust image dimensions by display area scale
const scaledImageWidth = imageWidth * imageAreaScaleX;
const scaledImageHeight = imageHeight * imageAreaScaleY;
const scaledImageAspect = scaledImageWidth / scaledImageHeight;

// Determine the minimum parallel scale required to fully fit the image
let minParallelScaleRequired;
if (scaledImageAspect > canvasAspect) {
// Wider image, limit by width
minParallelScaleRequired = (scaledImageWidth / canvasAspect) * 0.5;
} else {
// Taller image, limit by height
minParallelScaleRequired = scaledImageHeight * 0.5;
}

const { minZoomScale, maxZoomScale } = this.configuration;

// Translate zoom scale limits to world-space scale
const minScaleInWorld = minParallelScaleRequired / maxZoomScale;
const maxScaleInWorld = minParallelScaleRequired / minZoomScale;

// Clamp zoom within allowed limits
if (parallelScaleToSet < minScaleInWorld) {
cappedParallelScale = minScaleInWorld;
thresholdExceeded = true;
} else if (scale >= maxZoomScale) {
cappedParallelScale = t / maxZoomScale;
} else if (parallelScaleToSet > maxScaleInWorld) {
cappedParallelScale = maxScaleInWorld;
thresholdExceeded = true;
}
}
Expand Down
Loading