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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
import {RouteProp} from '@react-navigation/core';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {useEffect, useRef, useState} from 'react';
import {Image, StyleSheet, TouchableOpacity, View} from 'react-native';
import {normalize} from 'react-native-elements';
import ImageZoom, {IOnMove} from 'react-native-image-pan-zoom';
import PhotoManipulator from 'react-native-photo-manipulator';
import CloseIcon from '../../assets/ionicons/close-outline.svg';
import {MainStackParams} from '../../routes';
import {
cropVideo,
HeaderHeight,
SCREEN_HEIGHT,
SCREEN_WIDTH,
} from '../../utils';
import {TaggSquareButton} from '../common';
import ReactNativeZoomableView from '@dudigital/react-native-zoomable-view/src/ReactNativeZoomableView';
import Video from 'react-native-video';
type ZoomInCropperRouteProps = RouteProp<MainStackParams, 'ZoomInCropper'>;
type ZoomInCropperNavigationProps = StackNavigationProp<
MainStackParams,
'ZoomInCropper'
>;
interface ZoomInCropperProps {
route: ZoomInCropperRouteProps;
navigation: ZoomInCropperNavigationProps;
}
export const ZoomInCropper: React.FC<ZoomInCropperProps> = ({
route,
navigation,
}) => {
const {screenType, media, selectedCategory} = route.params;
const [aspectRatio, setAspectRatio] = useState<number>(1);
// width and height of video, if video
const [origDimensions, setOrigDimensions] = useState<number[]>([0, 0]);
const vidRef = useRef(null);
// Stores the coordinates of the cropped image
const [x0, setX0] = useState<number>();
const [x1, setX1] = useState<number>();
const [y0, setY0] = useState<number>();
const [y1, setY1] = useState<number>();
// Stores crop information for video
const [videoCrop, setVideoCrop] = useState<{
cropWidth?: number;
cropHeight?: number;
cropOffsetX?: number;
cropOffsetY?: number;
}>({});
const checkIfUriImage = (uri: string) => {
return (
uri.endsWith('jpg') ||
uri.endsWith('JPG') ||
uri.endsWith('PNG') ||
uri.endsWith('png') ||
uri.endsWith('GIF') ||
uri.endsWith('gif')
);
};
// Setting original aspect ratio of image
useEffect(() => {
if (media.uri && checkIfUriImage(media.uri)) {
Image.getSize(
media.uri,
(w, h) => {
setAspectRatio(w / h);
},
(err) => console.log(err),
);
} else if (media.uri && !checkIfUriImage(media.uri)) {
const tempVideoCrop = {...videoCrop};
tempVideoCrop.cropWidth = origDimensions[0];
tempVideoCrop.cropHeight = origDimensions[1];
setVideoCrop(tempVideoCrop);
}
}, []);
// Possible need to delay setting aspect ratio of video until loaded
useEffect(() => {
if (media.uri && !checkIfUriImage(media.uri)) {
const tempVideoCrop = {...videoCrop};
tempVideoCrop.cropWidth = origDimensions[0];
tempVideoCrop.cropHeight = origDimensions[1];
setVideoCrop(tempVideoCrop);
}
}, [origDimensions]);
// Crops original image based of (x0, y0) and (x1, y1) coordinates
const handleNext = () => {
if (checkIfUriImage(media.uri)) {
if (
x0 !== undefined &&
x1 !== undefined &&
y0 !== undefined &&
y1 !== undefined
) {
PhotoManipulator.crop(media.uri, {
x: x0,
y: y1,
width: Math.abs(x0 - x1),
height: Math.abs(y0 - y1),
})
.then((croppedURL) => {
navigation.navigate('CaptionScreen', {
screenType,
title: title,
media: {
uri: croppedURL,
isVideo: false,
},
});
})
.catch((err) => console.log('err: ', err));
} else if (
x0 === undefined &&
x1 === undefined &&
y0 === undefined &&
y1 === undefined
) {
navigation.navigate('CaptionScreen', {
screenType,
title: title,
media,
});
}
} else {
if (!videoCrop.cropHeight || !videoCrop.cropWidth) {
const tempVideoCrop = {...videoCrop};
tempVideoCrop.cropWidth = origDimensions[0];
tempVideoCrop.cropHeight = origDimensions[1];
setVideoCrop(tempVideoCrop);
}
cropVideo(
media.uri,
(croppedURL: string) => {
navigation.navigate('CaptionScreen', {
screenType,
media: {
uri: croppedURL,
isVideo: true,
},
selectedCategory,
});
},
videoCrop,
);
}
};
// for whenever the video is altered by the user
const onVideoMove = (zoomableEvent: any) => {
const {originalHeight, originalWidth} = zoomableEvent;
let cropWidth = 0;
let cropHeight = 0;
let cropOffsetX = 0;
let cropOffsetY = 0;
if (vidRef !== null && vidRef.current !== null) {
vidRef.current.measure(
(
_x: number,
_y: number,
width: number,
height: number,
pageX: number,
pageY: number,
) => {
// width
cropWidth = origDimensions[0] * (originalWidth / width);
// offsetX
cropOffsetX = -1 * origDimensions[0] * (pageX / width);
if (cropOffsetX < 0) {
cropOffsetX = 0;
} else if (cropOffsetX + cropWidth > origDimensions[0] - 1) {
cropOffsetX = origDimensions[0] - cropWidth - 1;
}
// height
if (
height * (SCREEN_WIDTH / aspectRatio / originalHeight) >
SCREEN_HEIGHT
) {
const superHeight = width / aspectRatio;
cropHeight = origDimensions[1] * (originalHeight / superHeight);
// offsetY
const topDeadZone = (height - superHeight) / 2;
const offsetY = topDeadZone + pageY;
cropOffsetY = -1 * origDimensions[1] * (offsetY / superHeight);
if (cropOffsetY < 0) {
cropOffsetY = 0;
} else if (cropOffsetY + cropHeight > origDimensions[1]) {
cropOffsetY = origDimensions[1] - cropHeight - 1;
}
} else {
cropHeight = origDimensions[1];
}
const tempVideoCrop = {...videoCrop};
tempVideoCrop.cropWidth = cropWidth;
tempVideoCrop.cropHeight = cropHeight;
tempVideoCrop.cropOffsetX = cropOffsetX;
tempVideoCrop.cropOffsetY = cropOffsetY;
setVideoCrop(tempVideoCrop);
},
);
}
};
/* Records (x0, y0) and (x1, y1) coordinates used later for cropping,
* based on(x, y) - the center of the image and scale of zoom
*/
const onMove = (position: IOnMove) => {
Image.getSize(
media.uri,
(w, h) => {
const x = position.positionX;
const y = position.positionY;
const scale = position.scale;
const screen_ratio = SCREEN_HEIGHT / SCREEN_WIDTH;
let tempx0 = w / 2 - x * (w / SCREEN_WIDTH) - w / 2 / scale;
let tempx1 = w / 2 - x * (w / SCREEN_WIDTH) + w / 2 / scale;
if (tempx0 < 0) {
tempx0 = 0;
}
if (tempx1 > w) {
tempx1 = w;
}
const x_distance = Math.abs(tempx1 - tempx0);
const y_distance = screen_ratio * x_distance;
let tempy0 = h / 2 - y * (h / SCREEN_HEIGHT) + y_distance / 2;
let tempy1 = h / 2 - y * (h / SCREEN_HEIGHT) - y_distance / 2;
if (tempy0 > h) {
tempy0 = h;
}
if (tempy1 < 0) {
tempy1 = 0;
}
setX0(tempx0);
setX1(tempx1);
setY0(tempy0);
setY1(tempy1);
},
(err) => console.log(err),
);
};
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.closeButton}
onPress={() => navigation.goBack()}>
<CloseIcon height={25} width={25} color={'white'} />
</TouchableOpacity>
{checkIfUriImage(media.uri) ? (
<ImageZoom
style={styles.zoomView}
cropWidth={SCREEN_WIDTH}
cropHeight={SCREEN_HEIGHT}
imageWidth={SCREEN_WIDTH}
imageHeight={SCREEN_WIDTH / aspectRatio}
onMove={onMove}>
<Image
style={{width: SCREEN_WIDTH, height: SCREEN_WIDTH / aspectRatio}}
source={{
uri: media.uri,
}}
/>
</ImageZoom>
) : (
<ReactNativeZoomableView
maxZoom={10}
minZoom={1}
zoomStep={0.5}
initialZoom={1}
bindToBorders={true}
// onZoomAfter={this.logOutZoomState}
onDoubleTapAfter={(
_1: any,
_2: any,
zoomableViewEventObject: any,
) => {
onVideoMove(zoomableViewEventObject);
}}
onShiftingAfter={(_1: any, _2: any, zoomableViewEventObject: any) => {
onVideoMove(zoomableViewEventObject);
}}
onShiftingEnd={(_1: any, _2: any, zoomableViewEventObject: any) => {
onVideoMove(zoomableViewEventObject);
}}
onZoomAfter={(_1: any, _2: any, zoomableViewEventObject: any) => {
onVideoMove(zoomableViewEventObject);
}}
onZoomEnd={(_1: any, _2: any, zoomableViewEventObject: any) => {
onVideoMove(zoomableViewEventObject);
}}
style={styles.zoomView}>
<View style={styles.videoParent} ref={vidRef}>
<Video
source={{uri: media.uri}}
volume={1}
style={[
styles.media,
{
height: SCREEN_WIDTH / aspectRatio,
},
]}
repeat={true}
resizeMode={'contain'}
onLoad={(response) => {
const {width, height} = response.naturalSize;
setOrigDimensions([width, height]);
setAspectRatio(width / height);
}}
/>
</View>
</ReactNativeZoomableView>
)}
<TaggSquareButton
onPress={handleNext}
title={'Next'}
buttonStyle={'normal'}
buttonColor={'blue'}
labelColor={'white'}
style={styles.button}
labelStyle={styles.buttonLabel}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: 'black',
height: SCREEN_HEIGHT,
width: SCREEN_WIDTH,
// flexDirection: 'column',
// justifyContent: 'center',
},
closeButton: {
position: 'absolute',
top: 0,
paddingTop: HeaderHeight,
zIndex: 1,
marginLeft: '5%',
},
button: {
zIndex: 1,
position: 'absolute',
bottom: normalize(20),
right: normalize(15),
width: normalize(108),
height: normalize(25),
borderRadius: 10,
},
buttonLabel: {
fontWeight: '700',
fontSize: normalize(15),
lineHeight: normalize(17.8),
letterSpacing: normalize(1.3),
textAlign: 'center',
},
media: {
zIndex: 0,
flex: 1,
},
videoParent: {
flex: 1,
},
zoomView: {
backgroundColor: 'black',
flex: 1,
// borderColor: 'pink',
// borderWidth: 2,
},
});
|