Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Created November 3, 2020 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcdnl/ff72e4b7403d266c693471de005cb349 to your computer and use it in GitHub Desktop.
Save amcdnl/ff72e4b7403d266c693471de005cb349 to your computer and use it in GitHub Desktop.
import panzoom, { PanZoom } from 'panzoom';
import { useEffect, useRef, useState } from 'react';
export interface PanZoomProps {
maxZoom?: number;
minZoom?: number;
}
export const usePanZoom = (props?: PanZoomProps) => {
const elmRef = useRef<any | null>(null);
// Some good ideas in these:
// - https://github.com/graphql-editor/graphql-editor/blob/master/src/Graf/Graf.tsx#L77
// - https://github.com/aoshea/react-svg-viewer/blob/master/src/index.tsx
// - https://www.npmjs.com/package/use-pan-zoom
const [instance, setInstance] = useState<PanZoom | null>(null);
useEffect(() => {
if (elmRef.current) {
const cur = panzoom(elmRef.current, {
maxZoom: props?.maxZoom || 5,
minZoom: props?.minZoom || 1,
enableTextSelection: false,
zoomDoubleClickSpeed: 1,
bounds: true,
boundsPadding: 0.1,
beforeMouseDown: (e: any) => {
if (e.target.tagName === 'rect') {
return true;
}
return false;
}
});
setInstance(cur);
}
return () => {
instance?.dispose();
};
}, [elmRef]);
return [elmRef, instance] as any[];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment