-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathTransformableImage.js
More file actions
218 lines (192 loc) · 6.13 KB
/
TransformableImage.js
File metadata and controls
218 lines (192 loc) · 6.13 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict';
import React, { Component, PropTypes } from 'react';
import { Image } from 'react-native';
import ViewTransformer from 'react-native-view-transformer';
let DEV = false;
export default class TransformableImage extends Component {
static enableDebug() {
DEV = true;
}
static propTypes = {
pixels: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number,
}),
enableTransform: PropTypes.bool,
enableScale: PropTypes.bool,
enableTranslate: PropTypes.bool,
/**
* Callback after a single tap (includes first and second tap of double-tap)
*/
onSingleTap: PropTypes.func,
/**
* Callback after a single tap which is not part of a double-tap
*/
onSingleTapConfirmed: PropTypes.func,
/**
* Callback on a double tap (if supplied, it overwrites the default scaling behavior)
*
* The first two arguments of the callback are the x- and y-coordinates of the
* finger while tapping, respectively.
*/
onDoubleTap: PropTypes.func,
onTransformGestureReleased: PropTypes.func,
onViewTransformed: PropTypes.func,
/**
* specify used Image-Component
*/
imageComponent: PropTypes.func,
};
static defaultProps = {
pixels: undefined,
enableTransform: true,
enableScale: true,
enableTranslate: true,
onSingleTap: undefined,
onSingleTapConfirmed: undefined,
onDoubleTap: undefined,
onTransformGestureReleased: undefined,
onViewTransformed: undefined,
imageComponent: Image,
};
constructor(props) {
super(props);
this.state = {
width: 0,
height: 0,
imageLoaded: false,
pixels: undefined,
keyAcumulator: 1
};
}
componentWillMount() {
if (!this.props.pixels) {
this.getImageSize(this.props.source);
}
}
componentWillReceiveProps(nextProps) {
if (!sameSource(this.props.source, nextProps.source)) {
//image source changed, clear last image's pixels info if any
this.setState({ pixels: undefined, keyAcumulator: this.state.keyAcumulator + 1 })
this.getImageSize(nextProps.source);
}
}
render() {
// jsx components must be uppercase
const { imageComponent: ImageComponent } = this.props;
let maxScale = 1;
let contentAspectRatio = undefined;
let width, height; //pixels
if (this.props.pixels) {
//if provided via props
width = this.props.pixels.width;
height = this.props.pixels.height;
} else if (this.state.pixels) {
//if got using Image.getSize()
width = this.state.pixels.width;
height = this.state.pixels.height;
}
if (width && height) {
contentAspectRatio = width / height;
if (this.state.width && this.state.height) {
maxScale = Math.max(width / this.state.width, height / this.state.height);
maxScale = Math.max(1, maxScale);
}
}
return (
<ViewTransformer
ref='viewTransformer'
key={'viewTransformer#' + this.state.keyAccumulator} //when image source changes, we should use a different node to avoid reusing previous transform state
enableTransform={this.props.enableTransform && this.state.imageLoaded} //disable transform until image is loaded
enableScale={this.props.enableScale}
enableTranslate={this.props.enableTranslate}
enableResistance={true}
onTransformGestureReleased={this.props.onTransformGestureReleased}
onViewTransformed={this.props.onViewTransformed}
onSingleTap={this.props.onSingleTap}
onSingleTapConfirmed={this.props.onSingleTapConfirmed}
onDoubleTap={this.props.onDoubleTap}
maxScale={maxScale}
contentAspectRatio={contentAspectRatio}
onLayout={this.onLayout.bind(this)}
style={this.props.style} >
<ImageComponent
{...this.props}
style={[this.props.style, { backgroundColor: 'transparent' }]}
resizeMode={'contain'}
onLoadStart={this.onLoadStart.bind(this)}
onLoad={this.onLoad.bind(this)}
capInsets={{
left: 0.1,
top: 0.1,
right: 0.1,
bottom: 0.1
}} //on iOS, use capInsets to avoid image downsampling
/>
</ViewTransformer>
);
}
onLoadStart(e) {
this.props.onLoadStart && this.props.onLoadStart(e);
this.setState({
imageLoaded: false
});
}
onLoad(e) {
this.props.onLoad && this.props.onLoad(e);
this.setState({
imageLoaded: true
});
}
onLayout(e) {
let { width, height } = e.nativeEvent.layout;
if (this.state.width !== width || this.state.height !== height) {
this.setState({
width: width,
height: height
});
}
}
getImageSize(source) {
if (!source) return;
const { imageComponent: ImageComponent } = this.props;
DEV && console.log('getImageSize...' + JSON.stringify(source));
if (typeof ImageComponent.getSize === 'function') {
if (source && source.uri) {
ImageComponent.getSize(
source.uri,
(width, height) => {
DEV && console.log('getImageSize...width=' + width + ', height=' + height);
if (width && height) {
if (this.state.pixels && this.state.pixels.width === width && this.state.pixels.height === height) {
//no need to update state
} else {
this.setState({ pixels: { width, height } });
}
}
},
(error) => {
console.error('getImageSize...error=' + JSON.stringify(error) + ', source=' + JSON.stringify(source));
})
} else {
console.warn('getImageSize...please provide pixels prop for local images');
}
} else {
console.warn('getImageSize...Image.getSize function not available before react-native v0.28');
}
}
getViewTransformerInstance() {
return this.refs['viewTransformer'];
}
}
function sameSource(source, nextSource) {
if (source === nextSource) {
return true;
}
if (source && nextSource) {
if (source.uri && nextSource.uri) {
return source.uri === nextSource.uri;
}
}
return false;
}