-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
163 lines (152 loc) · 6.45 KB
/
App.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
import React, { useEffect, useRef, useState } from 'react';
import { View, StyleSheet, PanResponder, Dimensions ,Text, Animated, TouchableOpacity } from 'react-native';
import Svg, { Path } from 'react-native-svg';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { faTrash , faCircle , faPencil , faClose , faT , faBold , faArrowsRotate} from '@fortawesome/free-solid-svg-icons';
const { width, height } = Dimensions.get('window');
const DrawingApp = () => {
const colorRef = useRef();
const pencilRef = useRef()
const [color , setColor] = useState('black');
const [sStrokeWidth , setStrokeWidth] = useState(3)
const viewAnimated = new Animated.Value(0)
const colorChangeRef = useRef(color);
const strokeWithRef = useRef(sStrokeWidth)
const [pathData, setPathData] = React.useState([]);
const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderStart : (evt) =>{
const { nativeEvent } = evt;
const { locationX, locationY } = nativeEvent;
setPathData(pathData => [...pathData , { path : ` M ${locationX} ${locationY} ` , color_ : colorChangeRef.current , strokeW_ : strokeWithRef.current}])
},
onPanResponderMove: (event) => {
const { nativeEvent } = event;
const { locationX, locationY } = nativeEvent;
if (locationX > 0 && locationX < width && locationY > 0 && locationY < height) {
setPathData((pathData) => {
const newArr = [...pathData];
newArr[newArr.length - 1].path += ` L${locationX},${locationY} `;
return newArr
});
}
},
onPanResponderRelease : (evt) => {
const { locationX, locationY } = evt.nativeEvent;
setPathData((pathData) => {
const newArr = [...pathData];
newArr[newArr.length - 1].path += ` L${locationX},${locationY} M${locationX},${locationY} `;
return newArr
});
}
} )).current;
const startAnimation = () => {
Animated.timing(viewAnimated , {toValue : 1 , useNativeDriver : false})
.start()
}
const closeAnimation = () => {
Animated.timing(viewAnimated , {toValue : 0 , useNativeDriver : false})
.start()
}
const changeColor = (colorName) => {
setColor(colorName)
colorChangeRef.current = colorName;
}
const openColorView =(value) => {
colorRef.current.setNativeProps({display : value})
}
const openPencilView =(value) => {
pencilRef.current.setNativeProps({display : value})
}
useEffect(() => {
console.log(pathData)
} , [pathData])
return (
<>
<View {...panResponder.panHandlers} style={styles.container}>
{pathData && (
<Svg height={height} width={width}>
{pathData.map((item, index) => (
<Path
key={index}
d={item.path}
stroke={item.color_}
strokeWidth={item.strokeW_}
fill="none"
/>
))}
</Svg>
)}
</View>
<View style={styles.drawSetWrapper}>
<Animated.View style={[styles.drawSetView ,
{
width : viewAnimated.interpolate({
inputRange : [0,1] ,
outputRange : [0 , 300]
}),
transform : [{scale : viewAnimated}]
}]}>
<View ref={colorRef} style={[styles.set , {display : 1}]} >
<TouchableOpacity onPress={() => { changeColor('black') }}><FontAwesomeIcon icon={faCircle} color={'black'} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { changeColor('blue')}}><FontAwesomeIcon icon={faCircle} color={'blue'} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { changeColor('yellow')}}><FontAwesomeIcon icon={faCircle} color={'yellow'} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { changeColor('purple')}}><FontAwesomeIcon icon={faCircle} color={'purple'} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { changeColor('red')}}><FontAwesomeIcon icon={faCircle} color={'red'} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { openColorView(1);closeAnimation()}}><FontAwesomeIcon icon={faClose} size={25} /></TouchableOpacity>
</View>
<View ref={pencilRef} style={[styles.set , {display : 1}]}>
<TouchableOpacity onPress={() => { setStrokeWidth(1) ; strokeWithRef.current = 3}}><FontAwesomeIcon icon={faArrowsRotate} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { setStrokeWidth(1) ; strokeWithRef.current = 1}}><FontAwesomeIcon icon={faT} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { setStrokeWidth(10) ; strokeWithRef.current = 8}}><FontAwesomeIcon icon={faBold} size={23} /></TouchableOpacity>
<TouchableOpacity onPress={() => { openPencilView(1) ;closeAnimation()}}><FontAwesomeIcon icon={faClose} size={25} /></TouchableOpacity>
</View>
</Animated.View>
<View style={styles.drawSet}>
<TouchableOpacity onPress={() => { openColorView(0) ; startAnimation()}}><FontAwesomeIcon icon={faCircle} size={20} /></TouchableOpacity>
<TouchableOpacity onPress={() => { openPencilView(0); startAnimation()}}><FontAwesomeIcon icon={faPencil} size={20} /></TouchableOpacity>
<TouchableOpacity onPress={() => setPathData([])}><FontAwesomeIcon icon={faTrash} size={20} /></TouchableOpacity>
</View>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
drawSetWrapper : {
width : '100%',
flexDirection : 'row' ,
justifyContent : 'center' ,
bottom : '7%' ,
position : 'absolute'
},
drawSet : {
backgroundColor : '#EFF2F7',
height : 60 ,
width : 300 ,
borderRadius : 30 ,
alignItems : 'center' ,
flexDirection : 'row' ,
justifyContent : 'space-around' ,
position : 'relative',
overflow : 'hidden'
} ,
drawSetView : {
position : 'absolute' ,
height : 60,
backgroundColor : '#C4E1F6' ,
zIndex : 99 ,
borderRadius : 30 ,
},
set : {
flexDirection : 'row',
justifyContent : 'space-around' ,
height : 60,
alignItems : 'center'
}
});
export default DrawingApp;