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

Zoom & Pan on GeoChart (or potentially any chart type) #399

Open
brunolm opened this issue Jun 3, 2021 · 1 comment
Open

Zoom & Pan on GeoChart (or potentially any chart type) #399

brunolm opened this issue Jun 3, 2021 · 1 comment

Comments

@brunolm
Copy link

brunolm commented Jun 3, 2021

Here's my workaround, I believe something similar could be integrated with the package to provide zoom/pan as a built-in feature.

const DomMatrix = window.DOMMatrix || window.WebKitCSSMatrix || window.MSCSSMatrix;

const preventDefault = (e) => e.preventDefault();

const pan = ({ element, scale, offsetX, offsetY }) => {
  const matrix = new DomMatrix(element.style.transform);
  element.style.transform = `matrix(${scale}, 0, 0, ${scale}, ${matrix.e - offsetX}, ${matrix.f - offsetY})`;
};

const onMouseDownSymbol = Symbol('onMouseDown');

// map: `g` tag inside svg
const mapElement = container.querySelector('svg g[clip-path]');

  if (window[onMouseDownSymbol]) {
    container.removeEventListener('mousedown', window[onMouseDownSymbol]);
  }

  window[onMouseDownSymbol] = (event) => {
    if (event.button !== 0 && event.button !== 2) {
      return;
    }

    let previousClientX = event.clientX;
    let previousClientY = event.clientY;

    const onMouseMove = (moveEvent) => {
      pan({
        element: mapElement,
        scale: zoom,
        offsetX: previousClientX - moveEvent.clientX,
        offsetY: previousClientY - moveEvent.clientY,
      });
      previousClientX = moveEvent.clientX;
      previousClientY = moveEvent.clientY;
      moveEvent.preventDefault();
    };

    const onMouseUp = () => {
      container.removeEventListener('mouseup', onMouseUp);
      container.removeEventListener('mousemove', onMouseMove);
      container.removeEventListener('contextmenu', preventDefault);
    };

    container.addEventListener('mouseup', onMouseUp);
    container.addEventListener('mousemove', onMouseMove);
    container.addEventListener('contextmenu', preventDefault);
  };

  container.addEventListener('mousedown', window[onMouseDownSymbol], false);
@brunolm brunolm changed the title Zoom & Pan on GeoChart (or any chart) Zoom & Pan on GeoChart (or potentially any chart type) Jun 3, 2021
@innv-imtinan
Copy link

where have you defined container

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