UNPKG

2.08 kBMarkdownView Raw
1# react-pinch-zoom-pan
2
3A react component that lets you add pinch-zoom and pan sub components. On touch you can pinch-zoom and pan the zoomed image. On desktop you can 'pinch' by holding down your *ALT-key* and do a mousedown from center of inner content onto the edges.
4
5## Install
6
7`npm install react-pinch-zoom-pan`
8
9## Usage
10
11Take a look at demo/App.js for usage, you can also run it in your local enviroment by
12
13`npm install & npm start`
14
15and open [localhost:3001](http://localhost:3001)
16
17```
18import React, {Component} from 'react'
19import s from 'react-prefixr'
20import PinchPanZoom from 'react-pinch-zoom-pan'
21
22export default class App extends Component {
23
24 /* Use the css padding-top to make the container as high as inner content */
25 getContainerStyle(ratio) {
26 return {
27 paddingTop: ratio.toFixed(2) + '%',
28 overflow: 'hidden',
29 position: 'relative'
30 }
31 }
32
33 /* Position inner content absolute */
34 getInnerStyle() {
35 return {
36 position: 'absolute',
37 top: 0,
38 left: 0,
39 right: 0,
40 bottom: 0
41 }
42 }
43
44 render() {
45 const {height,width} = this.props
46 const ratio = (height / width) * 100
47 return (
48 <PinchPanZoom maxScale={2} render={obj => {
49 return (
50 <div style={this.getContainerStyle(ratio)}>
51 <div style={this.getInnerStyle()}>
52 <img
53 src={`http://lorempixel.com/${width}/${height}/nature/`}
54 style={s({
55 width: '100%',
56 height: 'auto',
57 transform: `scale(${obj.scale}) translateY(${obj.y}px) translateX(${obj.x}px)`,
58 transition: '.3s ease'
59 })} />
60 </div>
61 </div>
62 )
63 }} />
64 )
65 }
66}
67```
68
69## Discussion
70
71* My experience with rxjs, see `src/ReactPinchPanZoom.js` if you have any suggestions and submit a pull request.
72
73Thanks to [Hugo Bessaa](https://github.com/hugobessaa) and [rx-react-pinch](https://github.com/hugobessaa/rx-react-pinch) for inital idea, but it had no support for panning and desktop.
\No newline at end of file