Skip to content
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

zoomSpeed is overwritten after first mousewheel event #181

Open
z1ndabad opened this issue Sep 4, 2024 · 1 comment
Open

zoomSpeed is overwritten after first mousewheel event #181

z1ndabad opened this issue Sep 4, 2024 · 1 comment

Comments

@z1ndabad
Copy link

z1ndabad commented Sep 4, 2024

Describe the bug
After changing the value of controls().zoomSpeed, the initial mousewheel event zooms at the specified speed, but future events received by the underlying globe DOM node zoom at the default speed.

useEffect(() => {
    const controls = globeRef.current?.controls();
    if (controls) {
      controls.zoomSpeed = 10;
    }
  }, [globeRef.current]);

Reproduction
https://codesandbox.io/p/sandbox/react-globe-gl-forked-f89hnq?workspaceId=ff4eac7f-570c-4bb7-aa7a-be79c7cbdde4

  • Zoom in on the globe once -- zoom speed is fast
  • Zoom in on the globe again -- zoom is slow
  • Reload page, zoom in on the globe -- zoom speed is fast again

Expected behavior
Setting controls().zoomSpeed should override the zoom speed for the lifetime of the DOM node.

Desktop

  • OS: Windows
  • Browser: Chrome 128.0.6613.114

Additional context & Workaround
The zoom speed scaling based on altitude may be responsible: https://github.com/vasturiano/globe.gl/blob/2a8a6f39717a9fd213782f943de2ce557045f699/src/globe.js#L392

I worked around this by adding a new event listener, but I'm not sure how to exactly reproduce the zoomSpeed variation based on altitude (i.e. properly setting POV from outside the package). Happy to attempt a PR if you have feedback on where to put the user-supplied speed in the zoom-adjustment function.

  useEffect(() => {
    const controls = globeRef.current.controls();
    const ZOOM_SPEED = 2;
    if (globeRef.current && controls) {
      controls.zoomSpeed = ZOOM_SPEED;

      // Workaround
      controls.addEventListener("change", () => {
        let pov = globeRef.current.pointOfView();
        controls.rotateSpeed = pov.altitude * 0.2;
        controls.zoomSpeed = (pov.altitude + 1) * ZOOM_SPEED;
      });
    }
  }, [globeRef]);
@vasturiano
Copy link
Owner

@z1ndabad thanks for reaching out.

You can override that built-in behaviour of the progressive zoom speed by tapping into the onZoom event handler.

For example:

<Globe
  ref={globeRef}
  ...
  onZoom=(useCallback(() => globeRef.current.controls().zoomSpeed = 10, [globeRef]))
/>

That will override the default behaviour with a constant zoom speed of 10.

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

No branches or pull requests

2 participants