-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsvg-p2.js
111 lines (101 loc) · 3.21 KB
/
svg-p2.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
import React, { useState } from 'react';
import { View, StyleSheet, Dimensions, TouchableOpacity, Text } from 'react-native';
import { Svg, Path, Circle } from 'react-native-svg';
const { height, width } = Dimensions.get('window');
// You can donate at https://www.buymeacoffee.com/mmshilleh if I saved you time
// Subscribe https://www.youtube.com/@mmshilleh/videos
export default () => {
const windowWidth = Dimensions.get('window').width;
const [polygonVertices, setPolygonVertices] = useState([
{ x: windowWidth / 4, y: windowWidth / 4 },
{ x: windowWidth * 3 / 4, y: windowWidth / 4 },
{ x: windowWidth * 3 / 4, y: windowWidth * 3 / 4 },
{ x: windowWidth / 4, y: windowWidth * 3 / 4 },
]);
const [draggedVertexIndex, setDraggedVertexIndex] = useState(null);
const polygonPath = polygonVertices
.map((vertex) => `${vertex.x},${vertex.y}`)
.join(' ');
const getActiveVertex = (x, y) => {
const threshold = 20;
for (let i = 0; i < polygonVertices.length; i++) {
const vertex = polygonVertices[i];
const distance = Math.sqrt((x - vertex.x) ** 2 + (y - vertex.y) ** 2);
if (distance <= threshold) {
return i;
}
}
return null;
};
const onTouchEndPolygon = () => {
setDraggedVertexIndex(null);
};
const onTouchMovePolygon = (event) => {
const locationX = event.nativeEvent.locationX;
const locationY = event.nativeEvent.locationY;
let activeVertex;
if (draggedVertexIndex === null) {
activeVertex = getActiveVertex(locationX, locationY);
setDraggedVertexIndex(activeVertex);
}
const updatedVertices = [...polygonVertices];
updatedVertices[draggedVertexIndex] = {
x: event.nativeEvent.locationX,
y: event.nativeEvent.locationY,
};
setPolygonVertices(updatedVertices);
};
const handleReset = () => { setPolygonVertices([
{ x: windowWidth / 4, y: windowWidth / 4 },
{ x: windowWidth * 3 / 4, y: windowWidth / 4 },
{ x: windowWidth * 3 / 4, y: windowWidth * 3 / 4 },
{ x: windowWidth / 4, y: windowWidth * 3 / 4 },
])}
return (
<View style={styles.container}>
<View style={styles.svgContainer} onTouchMove={onTouchMovePolygon} onTouchEnd={onTouchEndPolygon}>
<Svg>
<Path d={`M${polygonPath}Z`} fill="grey" stroke="grey" strokeWidth={2} />
{polygonVertices.map((vertex, index) => (
<Circle
key={index}
cx={vertex.x}
cy={vertex.y}
r={4}
fill="black"
/>
))}
</Svg>
</View>
<TouchableOpacity style={styles.clearButton} onPress={handleReset}>
<Text style={styles.clearButtonText}>Reset</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
svgContainer: {
height: height * 0.7,
width,
borderColor: 'black',
backgroundColor: 'white',
borderWidth: 1,
},
clearButton: {
marginTop: 10,
backgroundColor: 'black',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 5,
},
clearButtonText: {
color: 'white',
fontSize: 16,
fontWeight: 'bold',
},
});