App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import React, { useEffect, useRef, useState } from "react"; import { INITIAL_VALUE, ReactSVGPanZoom, TOOL_NONE, fitSelection, zoomOnViewerCenter, fitToViewer, } from "react-svg-pan-zoom"; function App() { const Viewer = useRef(null); const [tool, setTool] = useState(TOOL_NONE); const [value, setValue] = useState(INITIAL_VALUE); /* Read all the available methods in the documentation */ const _zoomOnViewerCenter1 = () => Viewer.current.zoomOnViewerCenter(1.1) const _fitSelection1 = () => Viewer.current.fitSelection(40, 40, 200, 200) const _fitToViewer1 = () => Viewer.current.fitToViewer() /* keep attention! handling the state in the following way doesn't fire onZoom and onPam hooks */ const _zoomOnViewerCenter2 = () => setValue(zoomOnViewerCenter(value, 1.1)) const _fitSelection2 = () => setValue(fitSelection(value, 40, 40, 200, 200)) const _fitToViewer2 = () => setValue(fitToViewer(value)) useEffect(() => { Viewer.current.fitToViewer(); }, []); return ( <div> <h1>ReactSGPanZoom</h1> <hr /> <button className="btn" onClick={() => _zoomOnViewerCenter1()}> Zoom on center (mode 1) </button> <button className="btn" onClick={() => _fitSelection1()}> Zoom area 200x200 (mode 1) </button> <button className="btn" onClick={() => _fitToViewer1()}> Fit (mode 1) </button> <button className="btn" onClick={() => _zoomOnViewerCenter2()}> Zoom on center (mode 2) </button> <button className="btn" onClick={() => _fitSelection2()}> Zoom area 200x200 (mode 2) </button> <button className="btn" onClick={() => _fitToViewer2()}> Fit (mode 2) </button> <hr /> <ReactSVGPanZoom ref={Viewer} width={500} height={500} tool={tool} onChangeTool={setTool} value={value} onChangeValue={setValue} onZoom={(e) => console.log("zoom")} onPan={(e) => console.log("pan")} onClick={(event) => console.log("click", event.x, event.y, event.originalEvent) } > <svg width={617} height={316}> <g fillOpacity=".5" strokeWidth="4"> <rect x="400" y="40" width="100" height="200" fill="#4286f4" stroke="#f4f142" /> <circle cx="108" cy="108.5" r="100" fill="#0ff" stroke="#0ff" /> <circle cx="180" cy="209.5" r="100" fill="#ff0" stroke="#ff0" /> <circle cx="220" cy="109.5" r="100" fill="#f0f" stroke="#f0f" /> </g> </svg> </ReactSVGPanZoom> <hr /> </div> ); } export default App; |