useGestureScale.ts 919 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { useEffect, useState } from 'react';
  2. import { usePinch } from 'react-use-gesture';
  3. const useGestureScale = (domTarget: RefObject<HTMLDivElement> | null) => {
  4. const [zoom, set] = useState(0);
  5. const bind = usePinch(
  6. ({ offset: [d] }) => {
  7. set(d / 200);
  8. },
  9. {
  10. domTarget,
  11. eventOptions: { passive: false },
  12. distanceBounds: { min: -100, max: 150 },
  13. }
  14. );
  15. const notAllowOriginalEvent = (e: any) => e.preventDefault();
  16. useEffect(() => {
  17. document.addEventListener('gesturestart', notAllowOriginalEvent);
  18. document.addEventListener('gesturechange', notAllowOriginalEvent);
  19. return () => {
  20. document.removeEventListener('gesturestart', notAllowOriginalEvent);
  21. document.removeEventListener('gesturechange', notAllowOriginalEvent);
  22. };
  23. }, []);
  24. useEffect(() => {
  25. bind();
  26. }, [bind]);
  27. return [zoom];
  28. };
  29. export default useGestureScale;