-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
250 lines (198 loc) · 7.51 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
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
import React, { useEffect, useRef, useState, useMemo } from "react";
import { View, Text, TextInput, Button, Alert, Dimensions } from "react-native";
// import Canvas from "react-native-canvas"; // nah fuck this
import {
useSharedValue,
runOnUI,
runOnJS,
withTiming,
} from "react-native-reanimated";
import {
GestureDetector,
Gesture,
GestureHandlerRootView,
} from "react-native-gesture-handler";
import Svg, { Circle, Path } from "react-native-svg";
import Overlay from "./components/Overlay";
const App = () => {
const svgRef = useRef(null); // reference the currentstroke's svg so we can modify it
const xPosition = useSharedValue(0); // value for adding the positions to the svg's path
const yPosition = useSharedValue(0);
const xPositionPrev = useSharedValue(0); // value of the previous stroke, meant for calculating whether or not -
const yPositionPrev = useSharedValue(0); // another point should be added to the svg stroke line
const pathString = useSharedValue(""); // this is the literal string of the current stroke's path
// const combinedPathString = useSharedValue("");
let combinedPathString = "";
const strokePositionsArray = useSharedValue([]); // this is the array of svg paths that are tracked and added to when the user lifts the pencil
const pathsToBeCombined = useSharedValue([]); // this is the array of svg paths that are tracked and added to when the user lifts the pencil
const [pathStringState, setPathString] = useState(""); // these are the 'useState' function so I can rerender the function on command
const [allPathsState, setAllPathsState] = useState([]); // same as above, just for the whole canvas
const [penWidth, setPenWidth] = useState(5); // this is the width of the pen
const [penColor, setPenColor] = useState("black"); // this is the color of the pen
const addPath = (path) => {
const newPath = {
path: path,
color: penColor,
width: penWidth,
};
setAllPathsState([...allPathsState, newPath]);
};
const renderCurrentSVGPath = () => {
pathString.value = createSmoothPath(strokePositionsArray.value);
setPathString(pathString.value);
};
const combinePaths = async () => {
for(let i = 0; i < pathsToBeCombined.value.length; i++){
// console.log("path", i , pathsToBeCombined.value);
// console.log("combining ")
combinedPathString += pathsToBeCombined.value[i];
}
// setPenWidth(r30);
// console.log("combined path string", combinedPathString)
addPath(combinedPathString);
// deleteDuplicatePaths();
// pathsToBeCombined.value = [];
};
const startNewPath = async () => {
// console.log("end point of the previous stroke", xPosition.value, yPosition.value);
strokePositionsArray.value = strokePositionsArray.value.slice(-3);
// strokePositionsArray.value = [];
addPath(pathString.value);
pathsToBeCombined.value.push(pathString.value);
// console.log("paths to be combined had", pathsToBeCombined.value, "added to it");
pathString.value = "";
};
const deleteDuplicatePaths = () => {
for(let i = 1; i< pathsToBeCombined.value.length + 1; i++){
let cutAllPathsState = allPathsState.splice(allPathsState.length - i, 1);
setAllPathsState(cutAllPathsState);
}
};
function clearCanvas() {
console.log("clearing canvas");
setAllPathsState([]);
setPathString("");
// setPenColor("black");
// setPenWidth(5);
}
const renderAllSvgPaths = () => {};
const panGesture = Gesture.Pan()
.runOnJS(true) // makes it run on the js string so it doesnt continually crash
.onTouchesDown((e) => {
// console.log("touches down! -", e);
xPosition.value = e.allTouches[0].x;
yPosition.value = e.allTouches[0].y;
// console.log("finger down")
})
.onTouchesMove((e) => {
// console.log("finger moving")
//variables for checking if the positions are too close
const previousValue = { x: xPosition.value, y: yPosition.value };
const currentValue = { x: e.allTouches[0].x, y: e.allTouches[0].y };
//set previous values
xPositionPrev.value = xPosition.value;
yPositionPrev.value = yPosition.value;
//set current values
xPosition.value = e.allTouches[0].x;
yPosition.value = e.allTouches[0].y;
strokePositionsArray.value.push({
x: e.allTouches[0].x,
y: e.allTouches[0].y,
});
// if(strokePositionsArray.value.length <= 1){
// console.log("starting point of new line", strokePositionsArray.value[0]);
// }
// console.log(strokePositionsArray.value.length)
//add the action that renders the svg userStroke here
renderCurrentSVGPath();
// console.log(strokePositionsArray.value.length);
if(strokePositionsArray.value.length > 100){
startNewPath();
setPenColor(getRandomColor());
}
})
.onTouchesUp((e) => {
// // console.log("finger up");
// // console.log("touches up! -", e.allTouches[0].x, e.allTouches[0].y);
// xPosition.value = e.allTouches[0].x;
// yPosition.value = e.allTouches[0].y;
// strokePositionsArray.value.push({
// x: e.allTouches[0].x,
// y: e.allTouches[0].y,
// });
//start a new path so even if the array is less than 100, it still shows that segement
startNewPath();
//rerender it again with the latest point
renderCurrentSVGPath();
//empty positions array
addPath(pathString.value);
strokePositionsArray.value = [];
//paths get combined here
combinePaths();
// deleteDuplicatePaths();
pathsToBeCombined.value = [];
});
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<View className="w-full h-full bg-slate-200">
<Overlay clearCanvas={clearCanvas}/>
<GestureDetector gesture={panGesture}>
<View className="w-full h-full">
<Svg ref={svgRef} className="bg-transparentr w-full h-full">
<Path
stroke={penColor}
strokeWidth={penWidth}
strokeLinejoin="round" // Set line join to roundtha
strokeLinecap="round" // Set line cap to round
fill="none"
d={pathStringState}
/>
</Svg>
<Svg className="bg-transparent w-full h-full absolute">
{allPathsState.map((stroke, index) => {
return (
<Path
key={index}
stroke={stroke.color}
strokeWidth={stroke.width}
strokeLinejoin="round"
strokeLinecap="round"
fill="none"
d={stroke.path}
/>
);
})}
</Svg>
</View>
</GestureDetector>
</View>
</GestureHandlerRootView>
);
};
export default App;
//helpers
const createSmoothPath = (points) => {
if (points.length < 2) {
return "";
}
let path = `M${points[0].x} ${points[0].y}`;
// let path = ``;
for (let i = 0; i < points.length - 1; i++) {
const x1 = points[i].x;
const y1 = points[i].y;
const x2 = points[i + 1].x;
const y2 = points[i + 1].y;
const xMid = (x1 + x2) / 2;
const yMid = (y1 + y2) / 2;
path += ` Q${x1} ${y1}, ${xMid} ${yMid}`;
}
return path;
};
function getRandomColor() {
const letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return "black";
}