Skip to content

fix/wsi-viewport-preserve-zoom-on-resize#2807

Open
katyayni1999000 wants to merge 1 commit into
cornerstonejs:mainfrom
katyayni1999000:fix/wsi-viewport-preserve-zoom-on-resize
Open

fix/wsi-viewport-preserve-zoom-on-resize#2807
katyayni1999000 wants to merge 1 commit into
cornerstonejs:mainfrom
katyayni1999000:fix/wsi-viewport-preserve-zoom-on-resize

Conversation

@katyayni1999000

@katyayni1999000 katyayni1999000 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Context

  • [] My Pull Request title is descriptive, accurate and follows the
    semantic-release format and guidelines.

Code

  • [] My code has been well-documented (function documentation, inline comments,
    etc.)

Public Documentation Updates

  • [] The documentation page has been updated as necessary for any public API
    additions or removals.

Tested Environment

  • [] "OS:
  • [] "Node version:
  • [] "Browser:

Summary by CodeRabbit

  • Bug Fixes
    • Preserved the slide’s zoom level when resizing the viewer, including when entering or exiting full-screen mode.
    • Maintained consistent rendering while adapting the viewport to its new size.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

WSIViewport.resize() now preserves relative slide zoom across container size changes by recalculating and reapplying the OpenLayers view resolution after updating the map size.

Changes

WSI viewport resize

Layer / File(s) Summary
Preserve zoom during viewport resize
packages/core/src/RenderingEngine/WSIViewport.ts
WSIViewport.resize() calculates a resolution ratio from the previous and current map sizes, updates the map size, reapplies the adjusted view resolution, and retains canvas synchronization and render-value refresh behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: wayfarer3130

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title is specific, but it does not follow the repository's semantic-release format required by the checklist. Use semantic-release style, e.g. fix(WSIViewport): preserve zoom on resize.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The PR description includes the required Context, Changes & Results, Testing, and Checklist sections with substantive details.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/RenderingEngine/WSIViewport.ts`:
- Around line 459-473: Update the viewport resize logic around `relativeZoom`,
`oldSize`, and `newSize` to require a non-null projection extent and strictly
positive width and height for both map sizes before calling
`getResolutionForExtent`. Validate that the computed `relativeZoom` is finite
before applying `view.setResolution`, while preserving the existing behavior for
valid dimensions and extents.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 144b37b3-f24b-473f-aa4d-61159f6d81a0

📥 Commits

Reviewing files that changed from the base of the PR and between af97420 and 70e70d1.

📒 Files selected for processing (1)
  • packages/core/src/RenderingEngine/WSIViewport.ts

Comment on lines +459 to +473
const extent = view.getProjection().getExtent();
const oldSize = map.getSize();
const oldResolution = view.getResolution();
let relativeZoom;
if (oldSize && oldResolution) {
relativeZoom =
view.getResolutionForExtent(extent, oldSize) / oldResolution;
}
map.updateSize();
const newSize = map.getSize();
if (relativeZoom && newSize) {
view.setResolution(
view.getResolutionForExtent(extent, newSize) / relativeZoom
);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

Guard against zero dimensions and invalid extents to prevent OpenLayers crashes.

When the viewport container is temporarily hidden (e.g., inside a collapsed panel or display: none) or initialized with zero dimensions, map.getSize() returns [0, 0]. Since arrays are truthy in JavaScript, the code proceeds and computes relativeZoom as Infinity (due to division by zero in getResolutionForExtent).

When the container is later shown and resized, dividing by Infinity leads to view.setResolution(0), which corrupts the OpenLayers view state. Additionally, view.getProjection().getExtent() can return null, which causes getResolutionForExtent to throw an error.

Ensure the sizes strictly have width and height > 0 and that the resulting relativeZoom is finite before applying it.

🔒️ Proposed fix to add robust boundary checks
-      const extent = view.getProjection().getExtent();
+      const extent = view.getProjection()?.getExtent();
       const oldSize = map.getSize();
       const oldResolution = view.getResolution();
       let relativeZoom;
-      if (oldSize && oldResolution) {
+      if (extent && oldSize && oldSize[0] > 0 && oldSize[1] > 0 && oldResolution > 0) {
         relativeZoom =
           view.getResolutionForExtent(extent, oldSize) / oldResolution;
       }
       map.updateSize();
       const newSize = map.getSize();
-      if (relativeZoom && newSize) {
+      if (relativeZoom > 0 && Number.isFinite(relativeZoom) && newSize && newSize[0] > 0 && newSize[1] > 0) {
         view.setResolution(
           view.getResolutionForExtent(extent, newSize) / relativeZoom
         );
       }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const extent = view.getProjection().getExtent();
const oldSize = map.getSize();
const oldResolution = view.getResolution();
let relativeZoom;
if (oldSize && oldResolution) {
relativeZoom =
view.getResolutionForExtent(extent, oldSize) / oldResolution;
}
map.updateSize();
const newSize = map.getSize();
if (relativeZoom && newSize) {
view.setResolution(
view.getResolutionForExtent(extent, newSize) / relativeZoom
);
}
const extent = view.getProjection()?.getExtent();
const oldSize = map.getSize();
const oldResolution = view.getResolution();
let relativeZoom;
if (extent && oldSize && oldSize[0] > 0 && oldSize[1] > 0 && oldResolution > 0) {
relativeZoom =
view.getResolutionForExtent(extent, oldSize) / oldResolution;
}
map.updateSize();
const newSize = map.getSize();
if (relativeZoom > 0 && Number.isFinite(relativeZoom) && newSize && newSize[0] > 0 && newSize[1] > 0) {
view.setResolution(
view.getResolutionForExtent(extent, newSize) / relativeZoom
);
}
🧰 Tools
🪛 GitHub Actions: Docusaurus Build / 0_docusaurus-build.txt

[error] 461-461: TypeScript (tsc) failed with TS2339: Property 'getSize' does not exist on type 'WSIMapLike'.

🪛 GitHub Actions: Docusaurus Build / docusaurus-build

[error] 461-461: TypeScript (tsc) failed: TS2339: Property 'getSize' does not exist on type 'WSIMapLike'.

🪛 GitHub Actions: Validate ESM Packaging / 1_build-packages.txt

[error] 461-461: TypeScript (tsc) failed with TS2339: Property 'getSize' does not exist on type 'WSIMapLike'.

🪛 GitHub Actions: Validate ESM Packaging / build-packages

[error] 461-461: TypeScript compilation failed (tsc). TS2339: Property 'getSize' does not exist on type 'WSIMapLike'.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/RenderingEngine/WSIViewport.ts` around lines 459 - 473,
Update the viewport resize logic around `relativeZoom`, `oldSize`, and `newSize`
to require a non-null projection extent and strictly positive width and height
for both map sizes before calling `getResolutionForExtent`. Validate that the
computed `relativeZoom` is finite before applying `view.setResolution`, while
preserving the existing behavior for valid dimensions and extents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant