react-pan-and-zoom-hoc
React HOC that helps with panning and zooming elements
Installation
Node
npm install --save react-pan-and-zoom-hoc
Browser
Include lib/panAndZoomHoc.js
with RequireJS.
Building
git clone https://github.com/woutervh-/react-pan-and-zoom-hoc.gitnpm installnpm run build
Running example locally
git clone https://github.com/woutervh-/react-pan-and-zoom-hoc.gitnpm installnpm run buildnpm run examples# Now you can open http://localhost:8888/ to view the example.
Options
x
(number, default:0.5
): initial x-coordinate that represents the horizontal center of the viewport.y
(number, default0.5
): initial y-coordinate that represents the vertical center of the viewport.scale
(number, default1
): initial scale of the viewport.scaleFactor
(number, default√2
): when zooming in or out, use this factor to multiply the scale by.minScale
(number, default:Number.EPSILON
): minimal allowed value for the scale.maxScale
(number, default:Number.POSITIVE_INFINITY
): maximal allowed value for the scale.renderOnChange
(boolean, default:false
): iftrue
, when panning or zooming, it will force a re-render of the component.passOnProps
(boolean, default:false
): iftrue
, will pass on thex
,y
, andscale
props to the wrapped component. IfrenderOnChange
is also set totrue
this will cause the props (with updated values) to be passed on every time a pan or zoom event occurs.ignorePanOutside
(boolean, default:false
): iftrue
, when the mouse exits the element the panning will stop. When the mouse re-enters the element, and the mouse button is still down, then panning will resume.disableZoomToMouse
(boolean, default:false
): iftrue
, the scroll wheel zooming will always scroll to the center regardless of the mouse position.disableScrollZoom
(boolean, default:false
): iftrue
, the scroll wheel zooming functionality will be disabled.zoomEndTimeout
(number, default:500
): after the user has used the scroll wheel to pan/zoom, the component will waitzoomEndTimeout
milliseconds and then fireonZoomEnd
. If the user uses the scroll wheel before this time has passed, the timeout will reset.onPanStart
(function, optional): invoked when the component starts to pan. Receives the following arguments:event
(MouseEvent): original event which triggered the panning to start.
onPanMove
(function, optional): invoked when the component pans in the x or y direction. Receives the following arguments:x
(number): new x-coordinate.y
(number): new y-coordinate.event
(MouseEvent): original event which triggered the panning movement.
onPanEnd
(function, optional): invoked when the component stop panning. Receives the following arguments:x
(number): new x-coordinate.y
(number): new y-coordinate.event
(MouseEvent): original event which triggered the panning to stop.
onZoom
(function, optional): invoked when the user zooms using the scroll wheel. Receives the following arguments:x
(number): old x-coordinate.y
(number): old y-coordinate.scale
(number): old scale value.event
(WheelEvent): original event which triggered the pan/zoom event.
onZoomEnd
(function, optional): invoked whenzoomEndTimeout
amount of milliseconds has passed since the last scroll event.onPanAndZoom
(function, optional): invoked when the component pans and zooms (for example when the mouse wheel is used). Receives the following arguments:x
(number): new x-coordinate.y
(number): new y-coordinate.scale
(number): new scale value.event
(WheelEvent): original event which triggered the pan/zoom event.
Unmanaged example
;; Component { const x y scale width height ...other = thisprops; return <div style=width height overflow: 'hidden' border: '1px solid black'> <img style=transform: `scale(, ) translate(px, px` width=width height=height ...other/> </div>; } const PannableAndZoomableFigure = ; Component { return <PannableAndZoomableFigure renderOnChange=true passOnProps=true src="http://lorempixel.com/400/200/" width=400 height=200 />; }
Managed example
;; const InteractiveDiv = ; Component state = x: 05 y: 05 scale: 1 ; { this; } { this; } { const x y scale = thisstate; return <InteractiveDiv x=x y=y scale=scale scaleFactor=Math minScale=1 maxScale=2 ** 18 onPanAndZoom= this style=width: 250 height: 250 border: '1px solid black' position: 'relative' onPanMove= this > <div style=position: 'absolute' top: `%` left: `%` width: 1 height: 1 backgroundColor: 'black'/> </InteractiveDiv>; }