-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
183 lines (173 loc) · 7.26 KB
/
index.js
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
import React from 'react';
import {
View,
PanResponder,
Animated,
Image,
StyleSheet,
} from 'react-native';
const ClipRect = require('@remobile/react-native-clip-rect');
module.exports = React.createClass({
getDefaultProps () {
return {
editRectWidth: 212,
editRectHeight: 212,
editRectRadius: 106,
overlayColor: 'rgba(0, 0, 0, 0.5)',
};
},
componentWillMount () {
const { editRectWidth, editRectHeight, imageWidth, imageHeight } = this.props;
// 上次/当前/动画 x 位移
this.lastGestureDx = null;
this.translateX = 0;
this.animatedTranslateX = new Animated.Value(this.translateX);
// 上次/当前/动画 y 位移
this.lastGestureDy = null;
this.translateY = 0;
this.animatedTranslateY = new Animated.Value(this.translateY);
// 缩放大小
this.scale = 1;
this.animatedScale = new Animated.Value(this.scale);
this.lastZoomDistance = null;
this.currentZoomDistance = 0;
// 图片大小
if (imageWidth / editRectWidth < imageHeight / editRectHeight) {
this.imageMinWidth = editRectWidth;
this.imageMinHeight = imageHeight / imageWidth * editRectWidth;
} else {
this.imageMinWidth = imageWidth / imageHeight * editRectHeight;
this.imageMinHeight = editRectHeight;
}
this.imageMinSize = Math.floor(Math.sqrt(this.imageMinWidth * this.imageMinWidth + this.imageMinHeight * this.imageMinHeight));
this.imagePanResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
onPanResponderTerminationRequest: (evt, gestureState) => false,
onPanResponderGrant: (evt, gestureState) => {
this.lastGestureDx = null;
this.lastGestureDy = null;
this.lastZoomDistance = null;
},
onPanResponderMove: (evt, gestureState) => {
const { changedTouches } = evt.nativeEvent;
if (changedTouches.length <= 1) {
this.translateX += (this.lastGestureDx === null) ? 0 : gestureState.dx - this.lastGestureDx;
this.translateY += (this.lastGestureDy === null) ? 0 : gestureState.dy - this.lastGestureDy;
this.lastGestureDx = gestureState.dx;
this.lastGestureDy = gestureState.dy;
this.updateTranslate();
} else {
const widthDistance = changedTouches[1].pageX - changedTouches[0].pageX;
const heightDistance = changedTouches[1].pageY - changedTouches[0].pageY;
this.currentZoomDistance = Math.floor(Math.sqrt(widthDistance * widthDistance + heightDistance * heightDistance));
if (this.lastZoomDistance !== null) {
let scale = this.scale + (this.currentZoomDistance - this.lastZoomDistance) * this.scale / this.imageMinSize;
if (scale < 1) {
scale = 1;
}
this.animatedScale.setValue(scale);
this.updateTranslate();
this.scale = scale;
}
this.lastZoomDistance = this.currentZoomDistance;
}
},
onPanResponderRelease: (evt, gestureState) => {},
onPanResponderTerminate: (evt, gestureState) => {},
});
},
updateTranslate () {
const { editRectWidth, editRectHeight } = this.props;
const xOffest = (this.imageMinWidth - editRectWidth / this.scale) / 2;
const yOffest = (this.imageMinHeight - editRectHeight / this.scale) / 2;
if (this.translateX > xOffest) {
this.translateX = xOffest;
}
if (this.translateX < -xOffest) {
this.translateX = -xOffest;
}
if (this.translateY > yOffest) {
this.translateY = yOffest;
}
if (this.translateY < -yOffest) {
this.translateY = -yOffest;
}
this.animatedTranslateX.setValue(this.translateX);
this.animatedTranslateY.setValue(this.translateY);
},
getCropData () {
const { editRectWidth, editRectHeight, imageWidth, imageHeight } = this.props;
const ratioX = imageWidth / this.imageMinWidth;
const ratioY = imageHeight / this.imageMinHeight;
const width = editRectWidth / this.scale;
const height = editRectHeight / this.scale;
const x = this.imageMinWidth / 2 - (width / 2 + this.translateX);
const y = this.imageMinHeight / 2 - (height / 2 + this.translateY);
return {
offset: { x: x * ratioX, y: y * ratioY },
size: { width: width * ratioX, height: height * ratioY },
};
},
render () {
const animatedStyle = {
transform: [{
scale: this.animatedScale,
}, {
translateX: this.animatedTranslateX,
}, {
translateY: this.animatedTranslateY,
}],
};
const { editRectWidth, editRectHeight, editRectRadius, source, style, overlayColor } = this.props;
return (
<View style={[styles.container, style]} {...this.imagePanResponder.panHandlers}>
<Animated.View style={animatedStyle}>
<Image resizeMode='contain' style={{ width:this.imageMinWidth, height:this.imageMinHeight }} source={source} />
</Animated.View>
<View style={styles.editboxContainer}>
<View style={{ flex: 1, backgroundColor: overlayColor }} />
<View style={styles.editboxMiddle} >
<View style={{ flex: 1, backgroundColor: overlayColor }} />
<View style={{ width: editRectWidth, height: editRectHeight }} >
<ClipRect style={{ width: editRectWidth, height: editRectHeight, borderRadius: editRectRadius, color: overlayColor }} />
<View style={[styles.clipRectBoder, { borderRadius: editRectRadius }]} />
</View>
<View style={{ flex: 1, backgroundColor: overlayColor }} />
</View>
<View style={{ flex: 1, backgroundColor: overlayColor }} />
</View>
</View>
);
},
});
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
overflow: 'hidden',
backgroundColor: 'black',
},
editboxContainer: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
},
clipRectBoder: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
borderColor: '#FFFFFF',
borderWidth: 2,
},
editboxMiddle: {
flexDirection: 'row',
},
});