diff options
author | Shravya Ramesh <shravs1208@gmail.com> | 2021-06-18 10:51:38 -0700 |
---|---|---|
committer | Shravya Ramesh <shravs1208@gmail.com> | 2021-06-18 10:51:38 -0700 |
commit | 5449a11f4c404e505ba4369bbdbefd73eeda0be3 (patch) | |
tree | 231045489b5bb326eb8a34a5556d016654433e2b /src | |
parent | ea562eff372995cf8f2cfc389d979452a3885c52 (diff) |
Add image manipulation for cropping
Diffstat (limited to 'src')
-rw-r--r-- | src/components/comments/ZoomInCropper.tsx | 85 | ||||
-rw-r--r-- | src/components/moments/MomentPost.tsx | 2 | ||||
-rw-r--r-- | src/components/profile/PublicProfile.tsx | 4 |
3 files changed, 83 insertions, 8 deletions
diff --git a/src/components/comments/ZoomInCropper.tsx b/src/components/comments/ZoomInCropper.tsx index 54acc26e..79047b41 100644 --- a/src/components/comments/ZoomInCropper.tsx +++ b/src/components/comments/ZoomInCropper.tsx @@ -2,13 +2,21 @@ import {RouteProp} from '@react-navigation/core'; import {useFocusEffect} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import React, {useCallback, useEffect, useState} from 'react'; -import {Dimensions, Image, StyleSheet, TouchableOpacity} from 'react-native'; +import { + Dimensions, + Image, + StyleSheet, + TouchableOpacity, + View, +} from 'react-native'; import {normalize} from 'react-native-elements'; -import ImageZoom from 'react-native-image-pan-zoom'; +import ImageZoom, {IOnMove} from 'react-native-image-pan-zoom'; import CloseIcon from '../../assets/ionicons/close-outline.svg'; import {MainStackParams} from '../../routes'; -import {HeaderHeight, SCREEN_WIDTH} from '../../utils'; -import {TaggSquareButton} from '../common'; +import {HeaderHeight, SCREEN_HEIGHT, SCREEN_WIDTH} from '../../utils'; +import {LoadingIndicator, TaggSquareButton} from '../common'; +import PhotoManipulator from 'react-native-photo-manipulator'; +import {trackPromise} from 'react-promise-tracker'; type ZoomInCropperRouteProps = RouteProp<MainStackParams, 'ZoomInCropper'>; type ZoomInCropperNavigationProps = StackNavigationProp< @@ -51,6 +59,70 @@ export const ZoomInCropper: React.FC<ZoomInCropperProps> = ({ ); } }, []); + + const handleNext = () => { + console.log('x0: ', x0, 'x1: ', x1, 'y0: ', y0, 'y1: ', y1); + if ( + x0 !== undefined && + x1 !== undefined && + y0 !== undefined && + y1 !== undefined && + image.sourceURL + ) { + console.log('x0x1y0y1'); + PhotoManipulator.crop(image.sourceURL, { + x: x0, + y: y1, + width: Math.abs(x0 - x1), + height: Math.abs(y0 - y1), + }) + .then((croppedURL) => console.log('croppedURL: ', croppedURL)) + .catch((err) => console.log('err: ', err)); + } + console.log('crop photo and send to caption screen'); + }; + + const [x0, setX0] = useState<number>(); + const [x1, setX1] = useState<number>(); + const [y0, setY0] = useState<number>(); + const [y1, setY1] = useState<number>(); + + const onMove = (position: IOnMove) => { + Image.getSize( + image.path, + (w, h) => { + const x = position.positionX; + const y = position.positionY; + const scale = position.scale; + const screen_ratio = SCREEN_HEIGHT / SCREEN_WIDTH; + let x0 = w / 2 - x * (w / SCREEN_WIDTH) - w / 2 / scale; + let x1 = w / 2 - x * (w / SCREEN_WIDTH) + w / 2 / scale; + if (x0 < 0) { + x0 = 0; + } + if (x1 > w) { + x1 = w; + } + const x_distance = Math.abs(x1 - x0); + const y_distance = screen_ratio * x_distance; + let y0 = h / 2 - y * (h / SCREEN_HEIGHT) + y_distance / 2; + let y1 = h / 2 - y * (h / SCREEN_HEIGHT) - y_distance / 2; + if (y0 > h) { + y0 = h; + } + if (y1 < 0) { + y1 = 0; + } + console.log('onMove x0: ', x0, 'x1: ', x1, 'y0: ', y0, 'y1: ', y1); + setX0(x0); + setX1(x1); + setY0(y0); + setY1(y1); + }, + (err) => console.log(err), + ); + }; + return ( <> <TouchableOpacity @@ -63,7 +135,8 @@ export const ZoomInCropper: React.FC<ZoomInCropperProps> = ({ cropWidth={Dimensions.get('window').width} cropHeight={Dimensions.get('window').height} imageWidth={SCREEN_WIDTH} - imageHeight={SCREEN_WIDTH / aspectRatio}> + imageHeight={SCREEN_WIDTH / aspectRatio} + onMove={onMove}> <Image style={{width: SCREEN_WIDTH, height: SCREEN_WIDTH / aspectRatio}} source={{ @@ -72,7 +145,7 @@ export const ZoomInCropper: React.FC<ZoomInCropperProps> = ({ /> </ImageZoom> <TaggSquareButton - onPress={() => console.log('Navigate to caption screen')} + onPress={handleNext} title={'Next'} buttonStyle={'normal'} buttonColor={'blue'} diff --git a/src/components/moments/MomentPost.tsx b/src/components/moments/MomentPost.tsx index 3840cb08..0f105c0d 100644 --- a/src/components/moments/MomentPost.tsx +++ b/src/components/moments/MomentPost.tsx @@ -115,7 +115,7 @@ const MomentPost: React.FC<MomentPostProps> = ({ ); const [commentPreview, setCommentPreview] = useState<MomentCommentPreviewType | null>(moment.comment_preview); - const {keyboardVisible, scrollTo} = useContext(MomentContext); + const {keyboardVisible} = useContext(MomentContext); const [hideText, setHideText] = useState(false); const [isFullScreen, setIsFullScreen] = useState<boolean>(false); diff --git a/src/components/profile/PublicProfile.tsx b/src/components/profile/PublicProfile.tsx index 8a80c56f..6b991d7c 100644 --- a/src/components/profile/PublicProfile.tsx +++ b/src/components/profile/PublicProfile.tsx @@ -87,7 +87,9 @@ const PublicProfile: React.FC<ContentProps> = ({ scrollViewRef.current ) { setScrollEnabled(false); - scrollViewRef.current.scrollTo({y: 0}); + if (scrollViewRef && scrollViewRef.current) { + scrollViewRef.current.scrollTo({y: 0}); + } navigation.navigate('MomentUploadPrompt', { screenType, momentCategory: momentCategories[0], |