1.0.6 • Published 5 years ago
use-pan-zoomable-svg v1.0.6
use-pan-zoomable-svg
Create pan & zoomable svg for react easily.
Use it to implement SVG based features like Org chart.
Get Started!
npm install use-pan-zoomable-svg
or
yarn add use-pan-zoomable-svg
How to use (View Demo)
import React, { useRef } from 'react';
import usePanZoomableSVG from 'use-pan-zoomable-svg';
const initialViewBox = {
x: -100,
y: -100,
width: 200,
height: 200,
scale: 1
};
const App = () => {
const svg = useRef();
const { width, height } = initialViewBox;
const {
viewBox,
handleZoom,
handleMouseDown,
handleTouchStart,
handleWheel,
reset,
} = usePanZoomableSVG({initialViewBox, svg});
return (
<div>
<svg
ref={svg}
onMouseDown={handleMouseDown}
onTouchStart={handleTouchStart}
onWheel={handleWheel}
viewBox={viewBox}
width={width}
height={height}
>
<circle cx="0" cy="0" r="20"></circle>
</svg>
<button onClick={() => handleZoom(1 / 1.3)}>+</button>
<button onClick={() => handleZoom(1.3)}>-</button>
<button onClick={reset}>reset</button>
</div>
);
};
Props
All props below are required.
x
: set minimum x of the viewBoxy
: set minimum y of the viewBoxwidth
: set width of the viewBoxheight
: set height of the viewBoxscale
: default scale of the SVG
returned values
viewBox
: set it to your svg's propviewBox
.handleZoom
: you can manually zoom your svg component by setting a number.reset
: reset your svg to initial view.handleMouseDown
: set it to your svg's proponMouseDown
.handleTouchStart
: set it to your svg's proponTouchStart
.handleWheel
: set it to your svg's proponWheel
.