Skip to content

refactor(editor): refactor zoom logic to use ViewBox - #188

Merged
Aarebecca merged 5 commits into
antvis:mainfrom
xxMudCloudxx:refactor/zoom-use-viewbox
Jan 29, 2026
Merged

Aarebecca merged 5 commits into
antvis:mainfrom
xxMudCloudxx:refactor/zoom-use-viewbox

Conversation

@xxMudCloudxx

Copy link
Copy Markdown
Contributor

Summary

Refactors the zoom interaction mechanism to directly manipulate the SVG viewBox instead of adjusting padding. This resolves critical stability issues where excessive negative padding caused SVG height collapse and provides a smoother, more mathematically correct zooming experience.

The Problems (Motivation)

The previous implementation used padding modifications to simulate zooming. This approach had several fundamental flaws:

  • Asymptotic Zero-Approaching Issue: The default padding provided by the template was 20px. As users zoomed in (padding approaching 0), the changes became infinitesimally small, effectively stopping the zoom before reaching the desired level.
  • Reset Bug: A logic error caused padding to reset to 10px when it hit 0, creating a jarring jump and preventing further zooming.
  • SVG Collapse: After attempting temporary fixes for the issues above, I discovered that if the padding reached a large negative value, it caused the SVG height to collapse, leading to severe rendering errors and total UI failure.

The Solution (Changes)

1. Direct ViewBox Manipulation (src/editor/interactions/zoom-wheel.ts)

  • Removed: All padding-based zoom logic.
  • Added: viewBox-based zoom.
    • Reads the current viewBox.
    • Calculates the new viewBox using a standard zoom factor ($1.1x$).
    • Updates the options.viewBox directly via UpdateOptionsCommand.
  • Features:
    • Standard Zoom: Hold shift to zoom in/out from the center.
    • Mouse-Centered Zoom: Hold Ctrl/Cmd to zoom towards the mouse pointer.
    • Reset Zoom: Ctrl + Shift + Scroll to reset.
    • Note: This shortcut is a temporary measure. A visible "Reset Zoom" button will be added in a future PR via a UI plugin, as strictly relying on keyboard shortcuts is not ideal for UX.
  • Boundary Checks: Prevents viewBox from becoming too small (min 20px) or too large (max 2000px).

2. ViewBox Utilities (src/utils/viewbox.ts)

  • Added calculateZoomedViewBox helper function.
  • This is a pure function that takes the current box, zoom factor, and pivot point to return the new box coordinates.
  • Ensures zoom math is consistent and easily testable.

3. Cleanup (src/utils/padding.ts)

  • setSVGPadding is now only used for legitimate padding settings, not for simulating zoom.

Future Plan

  • Implement Reset Zoom UI Button: To improve user experience, a visible "Reset Zoom" button will be created via a UI plugin in a subsequent PR. This will provide a more intuitive interaction and reduce the reliance on keyboard shortcuts.

- Refactor ZoomWheel to use direct viewBox manipulation instead of padding
- Eliminate logical issues with padding-based zoom (inverse scaling, 0-value traps)
- Update StateManager to prioritize viewBox updates over padding
- decouple zoom interaction from content BBox for consistent behavior
… checks

- **Interaction**:
  - Implement **Mouse-Point Zoom** (Ctrl/Meta + Wheel) using client-to-viewport projection.
  - Implement **Zoom Reset** (Ctrl + Shift + Wheel) to restore default view.
  - Fix **Center Zoom** (Shift + Wheel) to strictly adhere to viewBox center stability.

- **Tests**:
  - Add comprehensive unit tests (100% coverage) covering all zoom modes and edge cases.
  - Polyfill `DOMMatrix/DOMPoint` for JSDOM environment.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @xxMudCloudxx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly overhauls the SVG editor's zoom functionality. By transitioning from a problematic padding-based system to direct viewBox manipulation, the editor gains enhanced stability, precision, and a more intuitive user experience. The change addresses long-standing issues that led to rendering errors and inconsistent zoom behavior, laying a robust foundation for future interactive features.

Highlights

  • Refactored Zoom Mechanism: The zoom interaction now directly manipulates the SVG viewBox attribute instead of adjusting padding, resolving critical stability issues and providing a smoother experience.
  • Improved Zoom Stability and Accuracy: The previous padding-based zoom suffered from asymptotic zero-approaching, a reset bug, and SVG collapse with negative padding. The viewBox approach eliminates these fundamental flaws.
  • New Zoom Interaction Modes: Introduced standard center zoom (Shift + Wheel), mouse-centered zoom (Ctrl/Cmd + Wheel), and a temporary reset zoom (Ctrl + Shift + Scroll) functionality.
  • ViewBox Boundary Checks: Implemented checks to prevent the viewBox from becoming too small (min 20px) or too large (max 2000px), ensuring rendering stability.
  • Dedicated ViewBox Utility: A new pure helper function, calculateZoomedViewBox, was added to src/utils/viewbox.ts to ensure consistent and testable zoom calculations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request is an excellent refactor of the zoom functionality, moving from a problematic padding-based approach to a much more stable and correct implementation using the SVG viewBox. The code is well-organized, introducing pure functions for zoom calculations and comprehensive unit tests for the new logic.

I've identified a few areas for improvement:

  • There is a high-severity logic flaw in state.ts that prevents the new zoom reset functionality from working as intended.
  • The unit test for mouse-centered zoom could be strengthened to fully validate the behavior.
  • A confusing comment in zoom-wheel.ts should be removed to improve code clarity.

Overall, this is a high-quality contribution that significantly improves the editor's stability and user experience. Addressing the points above will make it even better.

Comment thread src/editor/interactions/zoom-wheel.ts Resolved
@codecov-commenter

codecov-commenter commented Jan 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 44.89%. Comparing base (ed4224a) to head (7a0a8c9).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #188      +/-   ##
==========================================
+ Coverage   44.81%   44.89%   +0.07%     
==========================================
  Files         332      332              
  Lines       26438    26473      +35     
  Branches     2072     2081       +9     
==========================================
+ Hits        11849    11884      +35     
  Misses      14579    14579              
  Partials       10       10              
Flag Coverage Δ
infographic 44.89% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/editor/interactions/zoom-wheel.ts 100.00% <100.00%> (ø)
src/editor/managers/state.ts 98.64% <100.00%> (+0.04%) ⬆️
src/utils/viewbox.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

I’ll address this in the next commit.

- **Logic Fixes**
  - `fix(state)`: Improve `updateOptions` to handle falsy `viewBox` values (enabling proper attribute removal).
- **Test Coverage**
  - `test(viewbox)`: Unit tests for `calculateZoomedViewBox` (zoom, pivot, identity).
  - `test(zoom-wheel)`: Verified precise coordinates for mouse-centered zooming.
  - `test(state-manager)`: Added tests for `viewBox` lifecycle (update/removal).
@xxMudCloudxx

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This is an excellent refactoring that replaces the previous padding-based zoom mechanism with a much more robust and mathematically sound viewBox manipulation approach. The code is clean, well-organized, and the accompanying tests are very comprehensive, covering numerous scenarios and edge cases. I've identified one high-severity bug related to boundary checks and have a couple of medium-severity suggestions to further improve maintainability and code organization. Overall, this is a high-quality contribution that significantly improves the stability and user experience of the zoom feature.

Comment thread src/editor/interactions/zoom-wheel.ts Resolved
Comment thread src/editor/interactions/zoom-wheel.ts Resolved
…SDOM.

- Created __tests__/setup/dom-polyfills.ts to house shared SVG-related polyfills.
- Replaced redundant inline implementations in coordinate.test.ts and zoom-wheel.test.ts.
- Improved consistency and maintainability of the test suite.
@Aarebecca

Copy link
Copy Markdown
Contributor

@xxMudCloudxx Thanks for your contribution 🎉

@Aarebecca
Aarebecca merged commit 9389e7a into antvis:main Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants