Skip to content

Commit f988bf2

Browse files
feat: refactors unwanted variables and adds reset button
1 parent 06471a1 commit f988bf2

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/ScratchCard.tsx

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {useCallback, useMemo, useRef, useState} from 'react';
2-
import {LayoutChangeEvent, StyleSheet, View} from 'react-native';
2+
import {Button, LayoutChangeEvent, StyleSheet, View} from 'react-native';
33
import {
44
Canvas,
55
Group,
@@ -53,8 +53,7 @@ export const ScratchCard = () => {
5353
});
5454

5555
const STROKE_WIDTH = useRef<number>(40); // width of the scratch stroke
56-
const totalArea = useRef<number>(0); // Total area of the scratch card
57-
const pathArea = useRef<number>(0); // Total scratched area
56+
const totalAreaScratched = useRef<number>(0); // Total area scratched on the scratch card
5857
const [isScratched, setIsScratched] = useState(false); // is canvas scratched enough (> threshold)
5958
const [paths, setPaths] = useState<SkPath[]>([]); // user's scratch data in form of svg path
6059

@@ -78,25 +77,32 @@ export const ScratchCard = () => {
7877
paths[paths.length - 1].toSVGString(),
7978
);
8079

81-
pathArea.current +=
82-
pathProperties.getTotalLength() * STROKE_WIDTH.current;
83-
84-
const areaScratched = (pathArea.current / totalArea.current) * 100;
80+
const pathArea = pathProperties.getTotalLength() * STROKE_WIDTH.current;
81+
totalAreaScratched.current += pathArea;
82+
const {width, height} = canvasLayoutMeta;
83+
const areaScratched =
84+
(totalAreaScratched.current / (width * height)) * 100;
8585

8686
if (areaScratched > 70) {
8787
setIsScratched(true);
8888
// Do other stuff like provide a force feedback to the user (Vibration)
8989
// Disable the gesture handler to avoid registering more inputs (Saves computation and memory)
9090
}
9191
})
92-
.minDistance(1);
92+
.minDistance(1)
93+
.enabled(!isScratched);
9394

9495
const handleCanvasLayout = useCallback((e: LayoutChangeEvent) => {
9596
const {width, height} = e.nativeEvent.layout;
9697
setCanvasLayoutMeta({width, height});
97-
totalArea.current = width * height;
9898
}, []);
9999

100+
const handleReset = () => {
101+
setIsScratched(false);
102+
setPaths([]);
103+
totalAreaScratched.current = 0;
104+
};
105+
100106
const {width, height} = useMemo(() => canvasLayoutMeta, [canvasLayoutMeta]);
101107

102108
return (
@@ -137,6 +143,9 @@ export const ScratchCard = () => {
137143
<Offer width={width} height={height} />
138144
)}
139145
</Canvas>
146+
<View style={styles.buttonContainer}>
147+
<Button title="Reset" onPress={handleReset} />
148+
</View>
140149
</View>
141150
</GestureDetector>
142151
);
@@ -152,5 +161,8 @@ const styles = StyleSheet.create({
152161
width: '100%',
153162
height: '100%',
154163
},
164+
buttonContainer: {
165+
marginTop: 50,
166+
},
155167
});
156168
export default ScratchCard;

0 commit comments

Comments
 (0)