Skip to content

fix(Stretch): Image aspect ratio resets to fit viewport when changing layout after calling setAspectRatio with isFitViewportAfterStretch disabled - #2832

Open
arul-trenser wants to merge 2 commits into
cornerstonejs:mainfrom
arul-trenser:fix-aspect-ratio-fit-viewport-issue
Open

fix(Stretch): Image aspect ratio resets to fit viewport when changing layout after calling setAspectRatio with isFitViewportAfterStretch disabled#2832
arul-trenser wants to merge 2 commits into
cornerstonejs:mainfrom
arul-trenser:fix-aspect-ratio-fit-viewport-issue

Conversation

@arul-trenser

@arul-trenser arul-trenser commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Context

Viewport.setAspectRatio(aspectRatio, isFitViewportAfterStretch) stretches the camera to a custom aspect ratio. When isFitViewportAfterStretch is false, it should keep that stretched ratio instead of fitting the image back to the viewport borders. But earlier, the isFitViewportAfterStretch flag was never saved anywhere. It was used only once inside setAspectRatioForVTKCamera to calculate the current math and then discarded. The VTK camera object had no memory of this flag.

Because of this, while run resetCamera(), did not know that fit-after-stretch was disabled. resetCamera() hardcoded isFitViewportAfterStretch as false when recreating the camera. Also, setViewPresentation() (used while restoring layout state) called setAspectRatio() without giving the second argument, which set it back to fit-to-viewport by default. So, your custom aspect ratio visually jumped back to fit-viewport behavior every time changed the layout.

The PR is incorporated by FlyWheel.io.

Fixes: #2830

Changes & Results

File changed:

  1. extendedVtkCamera.ts
  • Added getIsFitViewportAfterStretch() and setIsFitViewportAfterStretch() to the custom VTK camera interface and implementation. Now the flag is saved directly on the camera model itself as model.isFitViewportAfterStretch.
  1. vtkSlabCamera.ts
  • Added matching getIsFitViewportAfterStretch() and setIsFitViewportAfterStretch() type declarations to the vtkSlabCamera interface.
  1. RenderingEngine/Viewport.ts
  • Added getIsFitViewportAfterStretch() on Viewport, which reads the flag from getCamera() (defaults to false if not set).
  • getCameraNoRotation() now includes isFitViewportAfterStretch: vtkCamera.getIsFitViewportAfterStretch() in the returned camera state, so it stays available through getCamera().
  • setCamera() now calls vtkCamera.setIsFitViewportAfterStretch(isFitViewportAfterStretch) whenever aspect ratio is applied, saving the flag on the VTK camera.
  • resetCamera() now uses previousCamera.isFitViewportAfterStretch (instead of hardcoded false) when recreating the camera, keeping whichever stretch mode was active before.
  • setViewPresentation() now calls this.setAspectRatio(aspectRatio, this.getIsFitViewportAfterStretch()) instead of skipping the second argument, so layout restore also respects the saved flag.

Result:
The isFitViewportAfterStretch flag is now properly saved on the camera instead of being a one-time setting. When you set a viewport aspect ratio using setAspectRatio(aspectRatio, false), that stretched ratio stays saved across resetCamera() calls and layout restores, even when switching between common, custom, or MPR layouts. The image will no longer automatically snap back to fit-viewport behavior when you change the layout.

Before

2830-before.mp4

After

2830-after.mp4

Testing

  • Link with viewer
  • Add a function to do viewport.setAspectRatio([x, y], false)
  • Call that function
  • Switch the layout
  • Verify that the fit-to-viewport is working properly

Checklist

PR

  • 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: Windows 11
  • "Node version: 22.19.0
  • "Browser: 150.0.7871.187

Summary by CodeRabbit

Bug Fixes

  • Preserved viewport fitting behavior when stretching or resetting the camera.
  • Maintained the correct viewport presentation when restoring views or changing aspect ratios.

Enhancements

  • Added support for viewing and updating the camera’s stretch-fit setting.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8316c9a4-38bb-49ea-ad78-acd4784ca94d

📥 Commits

Reviewing files that changed from the base of the PR and between ab8a356 and abc7362.

📒 Files selected for processing (4)
  • packages/core/src/RenderingEngine/Viewport.ts
  • packages/core/src/RenderingEngine/vtkClasses/extendedVtkCamera.ts
  • packages/core/src/RenderingEngine/vtkClasses/vtkSlabCamera.ts
  • packages/core/src/types/IViewport.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/core/src/RenderingEngine/vtkClasses/vtkSlabCamera.ts
  • packages/core/src/RenderingEngine/vtkClasses/extendedVtkCamera.ts
  • packages/core/src/RenderingEngine/Viewport.ts

📝 Walkthrough

Walkthrough

The camera model now stores and exposes isFitViewportAfterStretch. Viewport preserves this value during camera resets, camera extraction, aspect-ratio updates, and view-presentation restoration.

Changes

Viewport stretch-fit state

Layer / File(s) Summary
Camera stretch-fit state API
packages/core/src/RenderingEngine/vtkClasses/extendedVtkCamera.ts, packages/core/src/RenderingEngine/vtkClasses/vtkSlabCamera.ts, packages/core/src/types/IViewport.ts
The camera APIs and ViewPresentation define and expose isFitViewportAfterStretch.
Viewport camera state preservation
packages/core/src/RenderingEngine/Viewport.ts
Viewport camera operations preserve, apply, and restore the stretch-fit setting. The viewport also exposes a getter for the setting.

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

Suggested reviewers: wayfarer3130

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the aspect-ratio reset bug and follows the semantic-release format.
Description check ✅ Passed The description explains the bug, changes, results, testing steps, and checklist with all required items completed.
Linked Issues check ✅ Passed The changes preserve isFitViewportAfterStretch across camera resets and layout restoration, meeting issue #2830 objectives.
Out of Scope Changes check ✅ Passed All reported changes directly support preserving custom stretched aspect ratios across viewport and camera updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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: 2

🤖 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/Viewport.ts`:
- Line 1797: Update the camera update flow around setAspectRatioForVTKCamera and
vtkCamera.setIsFitViewportAfterStretch to resolve isFitViewportAfterStretch once
from cameraInterface, previousCamera, and the default before any aspect-ratio
handling. Apply that resolved value independently of aspectRatio, pass it to
setAspectRatioForVTKCamera, use it in the VTK setter, and persist the resolved
value in updatedCamera.
- Line 2269: Extend the ViewPresentation type to include
isFitViewportAfterStretch, persist the value from getViewPresentation(), and
update setViewPresentation() to pass the stored flag to setAspectRatio(). Use
the target viewport’s current getIsFitViewportAfterStretch() value only when
applying presentations created without the new property.
🪄 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: 68505726-8373-43b7-b1d9-df1514129974

📥 Commits

Reviewing files that changed from the base of the PR and between a35a2b5 and a17207e.

📒 Files selected for processing (3)
  • packages/core/src/RenderingEngine/Viewport.ts
  • packages/core/src/RenderingEngine/vtkClasses/extendedVtkCamera.ts
  • packages/core/src/RenderingEngine/vtkClasses/vtkSlabCamera.ts

Comment thread packages/core/src/RenderingEngine/Viewport.ts
Comment thread packages/core/src/RenderingEngine/Viewport.ts Outdated
…fter calling setAspectRatio with isFitViewportAfterStretch disabled
@arul-trenser
arul-trenser force-pushed the fix-aspect-ratio-fit-viewport-issue branch from a17207e to abc7362 Compare August 5, 2026 07:05
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@sen-trenser

Copy link
Copy Markdown

@wayfarer3130 Could you please take a look at this PR?
Thanks!

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.

[Bug] Image aspect ratio resets to fit viewport when changing layout after calling setAspectRatio with isFitViewportAfterStretch disabled

2 participants