Skip to content

[bug] Zoom in/out broken after selecting "Zoom to Fit" #2069

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

Closed
spartan-uyen opened this issue Jun 5, 2025 · 1 comment · Fixed by #2079
Closed

[bug] Zoom in/out broken after selecting "Zoom to Fit" #2069

spartan-uyen opened this issue Jun 5, 2025 · 1 comment · Fixed by #2079
Labels
bug Something isn't working

Comments

@spartan-uyen
Copy link

Summary:
After selecting the "Zoom to Fit" option, the zoom value becomes NaN%, and subsequent zoom in or zoom out actions do not work.

Steps to reproduce:

  1. Select the "Zoom to Fit" option from the zoom controls.
  2. Attempt to zoom in or zoom out.

Actual result:
The zoom display shows NaN%.
Zooming in or out does not have any effect.
https://github.com/user-attachments/assets/d6a3ec78-b421-4c23-9d14-cfe66f04c742

Expected result:
The zoom level should display a valid percentage after selecting "Zoom to Fit".
Zoom in and zoom out functions should work correctly afterward.

@spartan-uyen spartan-uyen added the bug Something isn't working label Jun 5, 2025
@Rish-it
Copy link
Contributor

Rish-it commented Jun 6, 2025

@Kitenite what causing the bug

const newScale = Math.min(scaleX, scaleY) * DefaultSettings.SCALE;

This fallback position logic:

const newPosition = {
            x: DefaultSettings.PAN_POSITION.x,
            y: DefaultSettings.PAN_POSITION.y, 
        };

bypasses any dynamic calculation and leads to misaligned centering, particularly when dimensions change or content is rendered in an iframe.

  • Double scale multiplication creates invalid values
  • Hardcoded positioning ignores calculated dimensions

Temp Solution : We can try dynamic centering calculations

        const contentHeight = contentRect.height * newScale;
        const newPosition = {
            x: (containerRect.width - contentWidth) / 2,
            y: (containerRect.height - contentHeight) / 2,
        };

issue: Still results in NaN% when contentRect or containerRect have invalid or uninitialized dimensions.

Better solution: with Viewport + iFrame-Aware Scaling

  • Iframe-based content rendering
  • Parent viewport constraints
  • Dynamic scaling and centering
  • Guards against invalid dimensions

Dropping my PR for this.

Rish-it added a commit to Rish-it/onlook that referenced this issue Jun 6, 2025
…er dimension validation and error handling - Improve scale calculation with finite number checks - Ensure zoom in/out functionality works correctly after zoom to fit - Fixes onlook-dev#2069
Rish-it added a commit to Rish-it/onlook that referenced this issue Jun 6, 2025
…er dimension validation and error handling - Improve scale calculation with finite number checks - Fixes onlook-dev#2069
Kitenite pushed a commit that referenced this issue Jun 6, 2025
* fix: resolve NaN% zoom issue after 'Zoom to Fit' selection - Add proper dimension validation and error handling - Improve scale calculation with finite number checks - Fixes #2069
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants