related-ui-components 1.6.1 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/components/ScratchCard/ScratchCard.js +55 -92
- package/lib/commonjs/components/ScratchCard/ScratchCard.js.map +1 -1
- package/lib/module/components/ScratchCard/ScratchCard.js +59 -103
- package/lib/module/components/ScratchCard/ScratchCard.js.map +1 -1
- package/lib/typescript/commonjs/components/ScratchCard/ScratchCard.d.ts.map +1 -1
- package/lib/typescript/module/components/ScratchCard/ScratchCard.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/ScratchCard/ScratchCard.tsx +100 -151
|
@@ -12,15 +12,10 @@ var _reactNativeGestureHandler = require("react-native-gesture-handler");
|
|
|
12
12
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
13
13
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
14
14
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
15
|
-
// Import runOnJS and useSharedValue
|
|
16
|
-
|
|
17
|
-
// Ignore specific warning if it appears, related to path mutation - use cautiously
|
|
18
|
-
// LogBox.ignoreLogs(['Skia: SkPath.Make()']);
|
|
19
|
-
|
|
20
15
|
const ScratchCard = ({
|
|
21
16
|
style,
|
|
22
17
|
children,
|
|
23
|
-
image,
|
|
18
|
+
image = null,
|
|
24
19
|
brushStrokeWidth = 50,
|
|
25
20
|
revealThreshold = 0.8,
|
|
26
21
|
width = 300,
|
|
@@ -32,89 +27,57 @@ const ScratchCard = ({
|
|
|
32
27
|
textFontSize = 16,
|
|
33
28
|
onScratched
|
|
34
29
|
}) => {
|
|
35
|
-
const
|
|
36
|
-
const
|
|
30
|
+
const img = (0, _reactNativeSkia.useImage)(image);
|
|
31
|
+
const font = (0, _reactNativeSkia.useFont)(textFont, textFontSize);
|
|
37
32
|
const [[areaWidth, areaHeight], setSize] = (0, _react.useState)([0, 0]);
|
|
38
33
|
const [isScratched, setScratched] = (0, _react.useState)(false);
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
height: newHeight
|
|
51
|
-
} = event.nativeEvent.layout;
|
|
52
|
-
if (newWidth > 0 && newHeight > 0) {
|
|
53
|
-
if (newWidth !== areaWidth || newHeight !== areaHeight) {
|
|
54
|
-
setSize([newWidth, newHeight]);
|
|
55
|
-
}
|
|
56
|
-
if (!isLayoutReady) {
|
|
57
|
-
setLayoutReady(true);
|
|
34
|
+
const path = (0, _reactNativeReanimated.useSharedValue)(_reactNativeSkia.Skia.Path.Make().moveTo(0, 0));
|
|
35
|
+
|
|
36
|
+
//Check scratch progress
|
|
37
|
+
const checkScratchProgress = () => {
|
|
38
|
+
const bounds = path.value.getBounds();
|
|
39
|
+
const scratchedArea = bounds.height * bounds.width;
|
|
40
|
+
const totalArea = areaWidth * areaHeight;
|
|
41
|
+
if (scratchedArea / totalArea > revealThreshold && !isScratched) {
|
|
42
|
+
setScratched(true);
|
|
43
|
+
if (onScratched) {
|
|
44
|
+
onScratched();
|
|
58
45
|
}
|
|
59
|
-
} else {
|
|
60
|
-
setLayoutReady(false);
|
|
61
46
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}, [onScratched]);
|
|
47
|
+
};
|
|
48
|
+
(0, _react.useEffect)(() => {
|
|
49
|
+
console.log("HI");
|
|
50
|
+
}, [path]);
|
|
67
51
|
const pan = _reactNativeGestureHandler.Gesture.Pan().averageTouches(true).maxPointers(1).onBegin(e => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
newPath.moveTo(e.x, e.y);
|
|
72
|
-
newPath.lineTo(e.x + 0.001, e.y + 0.001);
|
|
73
|
-
path.value = newPath;
|
|
74
|
-
(0, _reactNativeSkia.notifyChange)(path);
|
|
75
|
-
} catch (error) {
|
|
76
|
-
console.error("ScratchCard: Error in onBegin:", error);
|
|
77
|
-
}
|
|
52
|
+
path.value.moveTo(e.x, e.y);
|
|
53
|
+
path.value.lineTo(e.x, e.y);
|
|
54
|
+
(0, _reactNativeSkia.notifyChange)(path);
|
|
78
55
|
}).onChange(e => {
|
|
79
|
-
if (!isLayoutReady || isThresholdReached.value) return;
|
|
80
56
|
try {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
path.value = newPath;
|
|
57
|
+
// checkScratchProgress();
|
|
58
|
+
path.value.lineTo(e.x, e.y);
|
|
84
59
|
(0, _reactNativeSkia.notifyChange)(path);
|
|
85
|
-
const bounds = path.value.getBounds();
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
60
|
+
// const bounds = path.value.getBounds();
|
|
61
|
+
// const scratchedArea = bounds.height * bounds.width;
|
|
62
|
+
// const totalArea = areaWidth * areaHeight;
|
|
63
|
+
|
|
64
|
+
// if (scratchedArea / totalArea > revealThreshold && !isScratched) {
|
|
65
|
+
// console.log("SCRATCHED")
|
|
66
|
+
// // setScratched(true);
|
|
67
|
+
// // if (onScratched) {
|
|
68
|
+
// // onScratched();
|
|
69
|
+
// // }
|
|
70
|
+
// }
|
|
97
71
|
} catch (error) {
|
|
98
|
-
console.
|
|
72
|
+
console.log(error);
|
|
99
73
|
}
|
|
74
|
+
|
|
75
|
+
// checkScratchProgress();
|
|
100
76
|
});
|
|
101
|
-
const textMetrics = _react.default.useMemo(() => {
|
|
102
|
-
if (loadedFont && text && areaWidth > 0 && areaHeight > 0) {
|
|
103
|
-
const metrics = loadedFont.measureText(text);
|
|
104
|
-
const textX = areaWidth / 2 - metrics.width / 2;
|
|
105
|
-
const textY = areaHeight / 2 + metrics.height / 3;
|
|
106
|
-
return {
|
|
107
|
-
x: textX,
|
|
108
|
-
y: textY,
|
|
109
|
-
width: metrics.width,
|
|
110
|
-
height: metrics.height
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
return null;
|
|
114
|
-
}, [loadedFont, text, areaWidth, areaHeight]);
|
|
115
|
-
const canRenderCanvas = isLayoutReady && areaWidth > 0 && areaHeight > 0;
|
|
116
77
|
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
|
|
117
|
-
onLayout:
|
|
78
|
+
onLayout: e => {
|
|
79
|
+
setSize([e.nativeEvent.layout.width, e.nativeEvent.layout.height]);
|
|
80
|
+
},
|
|
118
81
|
style: [styles.container, style, {
|
|
119
82
|
width,
|
|
120
83
|
height
|
|
@@ -122,7 +85,7 @@ const ScratchCard = ({
|
|
|
122
85
|
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
|
|
123
86
|
style: styles.content,
|
|
124
87
|
children: children
|
|
125
|
-
}), !isScratched &&
|
|
88
|
+
}), !isScratched && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeGestureHandler.GestureDetector, {
|
|
126
89
|
gesture: pan,
|
|
127
90
|
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeSkia.Canvas, {
|
|
128
91
|
style: styles.canvas,
|
|
@@ -132,8 +95,8 @@ const ScratchCard = ({
|
|
|
132
95
|
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeSkia.Rect, {
|
|
133
96
|
x: 0,
|
|
134
97
|
y: 0,
|
|
135
|
-
width:
|
|
136
|
-
height:
|
|
98
|
+
width: 1000,
|
|
99
|
+
height: 1000,
|
|
137
100
|
color: "white"
|
|
138
101
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeSkia.Path, {
|
|
139
102
|
path: path,
|
|
@@ -144,8 +107,8 @@ const ScratchCard = ({
|
|
|
144
107
|
strokeWidth: brushStrokeWidth
|
|
145
108
|
})]
|
|
146
109
|
}),
|
|
147
|
-
children:
|
|
148
|
-
image:
|
|
110
|
+
children: img ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeSkia.Image, {
|
|
111
|
+
image: img,
|
|
149
112
|
fit: "cover",
|
|
150
113
|
x: 0,
|
|
151
114
|
y: 0,
|
|
@@ -158,13 +121,13 @@ const ScratchCard = ({
|
|
|
158
121
|
width: areaWidth,
|
|
159
122
|
height: areaHeight,
|
|
160
123
|
color: backgroundColor
|
|
161
|
-
}),
|
|
162
|
-
x:
|
|
163
|
-
y:
|
|
124
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeSkia.Text, {
|
|
125
|
+
x: areaWidth / 2 - (font?.measureText(text).width || 0) / 2,
|
|
126
|
+
y: areaHeight / 2 + (font?.measureText(text).height || 0) / 2,
|
|
164
127
|
text: text,
|
|
165
128
|
color: textFontColor,
|
|
166
|
-
font:
|
|
167
|
-
})
|
|
129
|
+
font: font
|
|
130
|
+
})]
|
|
168
131
|
})
|
|
169
132
|
})
|
|
170
133
|
})
|
|
@@ -174,23 +137,23 @@ const ScratchCard = ({
|
|
|
174
137
|
const styles = _reactNative.StyleSheet.create({
|
|
175
138
|
container: {
|
|
176
139
|
position: "relative",
|
|
177
|
-
overflow: "hidden"
|
|
140
|
+
overflow: "hidden",
|
|
141
|
+
width: "100%",
|
|
142
|
+
height: "100%"
|
|
178
143
|
},
|
|
179
144
|
content: {
|
|
180
145
|
position: "absolute",
|
|
181
146
|
top: 0,
|
|
182
147
|
left: 0,
|
|
183
148
|
width: "100%",
|
|
184
|
-
height: "100%"
|
|
185
|
-
zIndex: 1
|
|
149
|
+
height: "100%"
|
|
186
150
|
},
|
|
187
151
|
canvas: {
|
|
188
152
|
position: "absolute",
|
|
189
153
|
top: 0,
|
|
190
154
|
left: 0,
|
|
191
155
|
width: "100%",
|
|
192
|
-
height: "100%"
|
|
193
|
-
zIndex: 2
|
|
156
|
+
height: "100%"
|
|
194
157
|
}
|
|
195
158
|
});
|
|
196
159
|
var _default = exports.default = ScratchCard;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","_interopRequireWildcard","require","_reactNativeSkia","_reactNative","_reactNativeReanimated","_reactNativeGestureHandler","_jsxRuntime","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","ScratchCard","style","children","image","brushStrokeWidth","revealThreshold","width","height","backgroundColor","text","textFont","textFontColor","textFontSize","onScratched","
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_reactNativeSkia","_reactNative","_reactNativeReanimated","_reactNativeGestureHandler","_jsxRuntime","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","ScratchCard","style","children","image","brushStrokeWidth","revealThreshold","width","height","backgroundColor","text","textFont","textFontColor","textFontSize","onScratched","img","useImage","font","useFont","areaWidth","areaHeight","setSize","useState","isScratched","setScratched","path","useSharedValue","Skia","Path","Make","moveTo","checkScratchProgress","bounds","value","getBounds","scratchedArea","totalArea","useEffect","console","log","pan","Gesture","Pan","averageTouches","maxPointers","onBegin","x","y","lineTo","notifyChange","onChange","error","jsxs","View","onLayout","nativeEvent","layout","styles","container","jsx","content","GestureDetector","gesture","Canvas","canvas","Mask","mode","mask","Group","Rect","color","strokeJoin","strokeCap","strokeWidth","Image","fit","Text","measureText","StyleSheet","create","position","overflow","top","left","_default","exports"],"sourceRoot":"..\\..\\..\\..\\src","sources":["components/ScratchCard/ScratchCard.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,gBAAA,GAAAD,OAAA;AAeA,IAAAE,YAAA,GAAAF,OAAA;AAQA,IAAAG,sBAAA,GAAAH,OAAA;AACA,IAAAI,0BAAA,GAAAJ,OAAA;AAAwE,IAAAK,WAAA,GAAAL,OAAA;AAAA,SAAAM,yBAAAC,CAAA,6BAAAC,OAAA,mBAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,CAAA,WAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA,KAAAF,CAAA;AAAA,SAAAR,wBAAAQ,CAAA,EAAAE,CAAA,SAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA,eAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,WAAAK,OAAA,EAAAL,CAAA,QAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA,OAAAC,CAAA,IAAAA,CAAA,CAAAG,GAAA,CAAAN,CAAA,UAAAG,CAAA,CAAAI,GAAA,CAAAP,CAAA,OAAAQ,CAAA,KAAAC,SAAA,UAAAC,CAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,CAAA,IAAAd,CAAA,oBAAAc,CAAA,OAAAC,cAAA,CAAAC,IAAA,CAAAhB,CAAA,EAAAc,CAAA,SAAAG,CAAA,GAAAP,CAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAb,CAAA,EAAAc,CAAA,UAAAG,CAAA,KAAAA,CAAA,CAAAV,GAAA,IAAAU,CAAA,CAAAC,GAAA,IAAAP,MAAA,CAAAC,cAAA,CAAAJ,CAAA,EAAAM,CAAA,EAAA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QAAQ,EAAE,UAAU;IACpBE,GAAG,EAAE,CAAC;IACNC,IAAI,EAAE,CAAC;IACP5E,KAAK,EAAE,MAAM;IACbC,MAAM,EAAE;EACV,CAAC;EACDwD,MAAM,EAAE;IACNgB,QAAQ,EAAE,UAAU;IACpBE,GAAG,EAAE,CAAC;IACNC,IAAI,EAAE,CAAC;IACP5E,KAAK,EAAE,MAAM;IACbC,MAAM,EAAE;EACV;AACF,CAAC,CAAC;AAAC,IAAA4E,QAAA,GAAAC,OAAA,CAAAlG,OAAA,GAEYc,WAAW","ignoreList":[]}
|
|
@@ -1,27 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import React, {
|
|
4
|
-
} from "react";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
// Import SkFont type
|
|
8
|
-
// Import SkImage type
|
|
9
|
-
} from "@shopify/react-native-skia";
|
|
10
|
-
import { View, StyleSheet
|
|
11
|
-
|
|
12
|
-
// Optional: for ignoring specific logs if needed
|
|
13
|
-
} from "react-native";
|
|
14
|
-
// Import runOnJS and useSharedValue
|
|
15
|
-
import { runOnJS, useSharedValue } from "react-native-reanimated";
|
|
3
|
+
import React, { useEffect, useState } from "react";
|
|
4
|
+
import { Canvas, Group, Image, Mask, Path, Rect, Skia, useImage, Text, useFont, notifyChange } from "@shopify/react-native-skia";
|
|
5
|
+
import { View, StyleSheet } from "react-native";
|
|
6
|
+
import { useSharedValue } from "react-native-reanimated";
|
|
16
7
|
import { Gesture, GestureDetector } from "react-native-gesture-handler";
|
|
17
|
-
|
|
18
|
-
// Ignore specific warning if it appears, related to path mutation - use cautiously
|
|
19
|
-
// LogBox.ignoreLogs(['Skia: SkPath.Make()']);
|
|
20
8
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
21
9
|
const ScratchCard = ({
|
|
22
10
|
style,
|
|
23
11
|
children,
|
|
24
|
-
image,
|
|
12
|
+
image = null,
|
|
25
13
|
brushStrokeWidth = 50,
|
|
26
14
|
revealThreshold = 0.8,
|
|
27
15
|
width = 300,
|
|
@@ -33,89 +21,57 @@ const ScratchCard = ({
|
|
|
33
21
|
textFontSize = 16,
|
|
34
22
|
onScratched
|
|
35
23
|
}) => {
|
|
36
|
-
const
|
|
37
|
-
const
|
|
24
|
+
const img = useImage(image);
|
|
25
|
+
const font = useFont(textFont, textFontSize);
|
|
38
26
|
const [[areaWidth, areaHeight], setSize] = useState([0, 0]);
|
|
39
27
|
const [isScratched, setScratched] = useState(false);
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
height: newHeight
|
|
52
|
-
} = event.nativeEvent.layout;
|
|
53
|
-
if (newWidth > 0 && newHeight > 0) {
|
|
54
|
-
if (newWidth !== areaWidth || newHeight !== areaHeight) {
|
|
55
|
-
setSize([newWidth, newHeight]);
|
|
56
|
-
}
|
|
57
|
-
if (!isLayoutReady) {
|
|
58
|
-
setLayoutReady(true);
|
|
28
|
+
const path = useSharedValue(Skia.Path.Make().moveTo(0, 0));
|
|
29
|
+
|
|
30
|
+
//Check scratch progress
|
|
31
|
+
const checkScratchProgress = () => {
|
|
32
|
+
const bounds = path.value.getBounds();
|
|
33
|
+
const scratchedArea = bounds.height * bounds.width;
|
|
34
|
+
const totalArea = areaWidth * areaHeight;
|
|
35
|
+
if (scratchedArea / totalArea > revealThreshold && !isScratched) {
|
|
36
|
+
setScratched(true);
|
|
37
|
+
if (onScratched) {
|
|
38
|
+
onScratched();
|
|
59
39
|
}
|
|
60
|
-
} else {
|
|
61
|
-
setLayoutReady(false);
|
|
62
40
|
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}, [onScratched]);
|
|
41
|
+
};
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
console.log("HI");
|
|
44
|
+
}, [path]);
|
|
68
45
|
const pan = Gesture.Pan().averageTouches(true).maxPointers(1).onBegin(e => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
newPath.moveTo(e.x, e.y);
|
|
73
|
-
newPath.lineTo(e.x + 0.001, e.y + 0.001);
|
|
74
|
-
path.value = newPath;
|
|
75
|
-
notifyChange(path);
|
|
76
|
-
} catch (error) {
|
|
77
|
-
console.error("ScratchCard: Error in onBegin:", error);
|
|
78
|
-
}
|
|
46
|
+
path.value.moveTo(e.x, e.y);
|
|
47
|
+
path.value.lineTo(e.x, e.y);
|
|
48
|
+
notifyChange(path);
|
|
79
49
|
}).onChange(e => {
|
|
80
|
-
if (!isLayoutReady || isThresholdReached.value) return;
|
|
81
50
|
try {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
path.value = newPath;
|
|
51
|
+
// checkScratchProgress();
|
|
52
|
+
path.value.lineTo(e.x, e.y);
|
|
85
53
|
notifyChange(path);
|
|
86
|
-
const bounds = path.value.getBounds();
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
54
|
+
// const bounds = path.value.getBounds();
|
|
55
|
+
// const scratchedArea = bounds.height * bounds.width;
|
|
56
|
+
// const totalArea = areaWidth * areaHeight;
|
|
57
|
+
|
|
58
|
+
// if (scratchedArea / totalArea > revealThreshold && !isScratched) {
|
|
59
|
+
// console.log("SCRATCHED")
|
|
60
|
+
// // setScratched(true);
|
|
61
|
+
// // if (onScratched) {
|
|
62
|
+
// // onScratched();
|
|
63
|
+
// // }
|
|
64
|
+
// }
|
|
98
65
|
} catch (error) {
|
|
99
|
-
console.
|
|
66
|
+
console.log(error);
|
|
100
67
|
}
|
|
68
|
+
|
|
69
|
+
// checkScratchProgress();
|
|
101
70
|
});
|
|
102
|
-
const textMetrics = React.useMemo(() => {
|
|
103
|
-
if (loadedFont && text && areaWidth > 0 && areaHeight > 0) {
|
|
104
|
-
const metrics = loadedFont.measureText(text);
|
|
105
|
-
const textX = areaWidth / 2 - metrics.width / 2;
|
|
106
|
-
const textY = areaHeight / 2 + metrics.height / 3;
|
|
107
|
-
return {
|
|
108
|
-
x: textX,
|
|
109
|
-
y: textY,
|
|
110
|
-
width: metrics.width,
|
|
111
|
-
height: metrics.height
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
return null;
|
|
115
|
-
}, [loadedFont, text, areaWidth, areaHeight]);
|
|
116
|
-
const canRenderCanvas = isLayoutReady && areaWidth > 0 && areaHeight > 0;
|
|
117
71
|
return /*#__PURE__*/_jsxs(View, {
|
|
118
|
-
onLayout:
|
|
72
|
+
onLayout: e => {
|
|
73
|
+
setSize([e.nativeEvent.layout.width, e.nativeEvent.layout.height]);
|
|
74
|
+
},
|
|
119
75
|
style: [styles.container, style, {
|
|
120
76
|
width,
|
|
121
77
|
height
|
|
@@ -123,7 +79,7 @@ const ScratchCard = ({
|
|
|
123
79
|
children: [/*#__PURE__*/_jsx(View, {
|
|
124
80
|
style: styles.content,
|
|
125
81
|
children: children
|
|
126
|
-
}), !isScratched &&
|
|
82
|
+
}), !isScratched && /*#__PURE__*/_jsx(GestureDetector, {
|
|
127
83
|
gesture: pan,
|
|
128
84
|
children: /*#__PURE__*/_jsx(Canvas, {
|
|
129
85
|
style: styles.canvas,
|
|
@@ -133,8 +89,8 @@ const ScratchCard = ({
|
|
|
133
89
|
children: [/*#__PURE__*/_jsx(Rect, {
|
|
134
90
|
x: 0,
|
|
135
91
|
y: 0,
|
|
136
|
-
width:
|
|
137
|
-
height:
|
|
92
|
+
width: 1000,
|
|
93
|
+
height: 1000,
|
|
138
94
|
color: "white"
|
|
139
95
|
}), /*#__PURE__*/_jsx(Path, {
|
|
140
96
|
path: path,
|
|
@@ -145,8 +101,8 @@ const ScratchCard = ({
|
|
|
145
101
|
strokeWidth: brushStrokeWidth
|
|
146
102
|
})]
|
|
147
103
|
}),
|
|
148
|
-
children:
|
|
149
|
-
image:
|
|
104
|
+
children: img ? /*#__PURE__*/_jsx(Image, {
|
|
105
|
+
image: img,
|
|
150
106
|
fit: "cover",
|
|
151
107
|
x: 0,
|
|
152
108
|
y: 0,
|
|
@@ -159,13 +115,13 @@ const ScratchCard = ({
|
|
|
159
115
|
width: areaWidth,
|
|
160
116
|
height: areaHeight,
|
|
161
117
|
color: backgroundColor
|
|
162
|
-
}),
|
|
163
|
-
x:
|
|
164
|
-
y:
|
|
118
|
+
}), /*#__PURE__*/_jsx(Text, {
|
|
119
|
+
x: areaWidth / 2 - (font?.measureText(text).width || 0) / 2,
|
|
120
|
+
y: areaHeight / 2 + (font?.measureText(text).height || 0) / 2,
|
|
165
121
|
text: text,
|
|
166
122
|
color: textFontColor,
|
|
167
|
-
font:
|
|
168
|
-
})
|
|
123
|
+
font: font
|
|
124
|
+
})]
|
|
169
125
|
})
|
|
170
126
|
})
|
|
171
127
|
})
|
|
@@ -175,23 +131,23 @@ const ScratchCard = ({
|
|
|
175
131
|
const styles = StyleSheet.create({
|
|
176
132
|
container: {
|
|
177
133
|
position: "relative",
|
|
178
|
-
overflow: "hidden"
|
|
134
|
+
overflow: "hidden",
|
|
135
|
+
width: "100%",
|
|
136
|
+
height: "100%"
|
|
179
137
|
},
|
|
180
138
|
content: {
|
|
181
139
|
position: "absolute",
|
|
182
140
|
top: 0,
|
|
183
141
|
left: 0,
|
|
184
142
|
width: "100%",
|
|
185
|
-
height: "100%"
|
|
186
|
-
zIndex: 1
|
|
143
|
+
height: "100%"
|
|
187
144
|
},
|
|
188
145
|
canvas: {
|
|
189
146
|
position: "absolute",
|
|
190
147
|
top: 0,
|
|
191
148
|
left: 0,
|
|
192
149
|
width: "100%",
|
|
193
|
-
height: "100%"
|
|
194
|
-
zIndex: 2
|
|
150
|
+
height: "100%"
|
|
195
151
|
}
|
|
196
152
|
});
|
|
197
153
|
export default ScratchCard;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["React","
|
|
1
|
+
{"version":3,"names":["React","useEffect","useState","Canvas","Group","Image","Mask","Path","Rect","Skia","useImage","Text","useFont","notifyChange","View","StyleSheet","useSharedValue","Gesture","GestureDetector","jsx","_jsx","jsxs","_jsxs","ScratchCard","style","children","image","brushStrokeWidth","revealThreshold","width","height","backgroundColor","text","textFont","textFontColor","textFontSize","onScratched","img","font","areaWidth","areaHeight","setSize","isScratched","setScratched","path","Make","moveTo","checkScratchProgress","bounds","value","getBounds","scratchedArea","totalArea","console","log","pan","Pan","averageTouches","maxPointers","onBegin","e","x","y","lineTo","onChange","error","onLayout","nativeEvent","layout","styles","container","content","gesture","canvas","mode","mask","color","strokeJoin","strokeCap","strokeWidth","fit","measureText","create","position","overflow","top","left"],"sourceRoot":"..\\..\\..\\..\\src","sources":["components/ScratchCard/ScratchCard.tsx"],"mappings":";;AAAA,OAAOA,KAAK,IAAIC,SAAS,EAAUC,QAAQ,QAAQ,OAAO;AAC1D,SACEC,MAAM,EACNC,KAAK,EACLC,KAAK,EACLC,IAAI,EACJC,IAAI,EACJC,IAAI,EACJC,IAAI,EAGJC,QAAQ,EACRC,IAAI,EACJC,OAAO,EACPC,YAAY,QACP,4BAA4B;AACnC,SAEEC,IAAI,EAEJC,UAAU,QAGL,cAAc;AACrB,SAA0BC,cAAc,QAAQ,yBAAyB;AACzE,SAASC,OAAO,EAAEC,eAAe,QAAQ,8BAA8B;AAAC,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAkBxE,MAAMC,WAAuC,GAAGA,CAAC;EAC/CC,KAAK;EACLC,QAAQ;EACRC,KAAK,GAAG,IAAI;EACZC,gBAAgB,GAAG,EAAE;EACrBC,eAAe,GAAG,GAAG;EACrBC,KAAK,GAAG,GAAG;EACXC,MAAM,GAAG,GAAG;EACZC,eAAe,GAAG,SAAS;EAC3BC,IAAI,GAAG,EAAE;EACTC,QAAQ;EACRC,aAAa,GAAG,SAAS;EAC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IAAC,CACQ,CAClB;EAAA,CACG,CAAC;AAEX,CAAC;AAED,MAAM+B,MAAM,GAAGtD,UAAU,CAACmE,MAAM,CAAC;EAC/BZ,SAAS,EAAE;IACTa,QAAQ,EAAE,UAAU;IACpBC,QAAQ,EAAE,QAAQ;IAClBvD,KAAK,EAAE,MAAM;IACbC,MAAM,EAAE;EACV,CAAC;EACDyC,OAAO,EAAE;IACPY,QAAQ,EAAE,UAAU;IACpBE,GAAG,EAAE,CAAC;IACNC,IAAI,EAAE,CAAC;IACPzD,KAAK,EAAE,MAAM;IACbC,MAAM,EAAE;EACV,CAAC;EACD2C,MAAM,EAAE;IACNU,QAAQ,EAAE,UAAU;IACpBE,GAAG,EAAE,CAAC;IACNC,IAAI,EAAE,CAAC;IACPzD,KAAK,EAAE,MAAM;IACbC,MAAM,EAAE;EACV;AACF,CAAC,CAAC;AAEF,eAAeP,WAAW","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ScratchCard.d.ts","sourceRoot":"","sources":["../../../../../src/components/ScratchCard/ScratchCard.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ScratchCard.d.ts","sourceRoot":"","sources":["../../../../../src/components/ScratchCard/ScratchCard.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAsC,MAAM,OAAO,CAAC;AAgB3D,OAAO,EACL,SAAS,EAET,SAAS,EAET,kBAAkB,EAEnB,MAAM,cAAc,CAAC;AAItB,KAAK,gBAAgB,GAAG;IACtB,KAAK,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,kBAAkB,CAAC;IAC3B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;CAC1B,CAAC;AAEF,QAAA,MAAM,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,gBAAgB,CAqI3C,CAAC;AAyBF,eAAe,WAAW,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ScratchCard.d.ts","sourceRoot":"","sources":["../../../../../src/components/ScratchCard/ScratchCard.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ScratchCard.d.ts","sourceRoot":"","sources":["../../../../../src/components/ScratchCard/ScratchCard.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAsC,MAAM,OAAO,CAAC;AAgB3D,OAAO,EACL,SAAS,EAET,SAAS,EAET,kBAAkB,EAEnB,MAAM,cAAc,CAAC;AAItB,KAAK,gBAAgB,GAAG;IACtB,KAAK,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IAC7B,KAAK,CAAC,EAAE,kBAAkB,CAAC;IAC3B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;CAC1B,CAAC;AAEF,QAAA,MAAM,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,gBAAgB,CAqI3C,CAAC;AAyBF,eAAe,WAAW,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
useRef,
|
|
3
|
-
useState,
|
|
4
|
-
useEffect,
|
|
5
|
-
useCallback, // Import useCallback
|
|
6
|
-
} from "react";
|
|
1
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
7
2
|
import {
|
|
8
3
|
Canvas,
|
|
9
4
|
Group,
|
|
@@ -12,13 +7,12 @@ import {
|
|
|
12
7
|
Path,
|
|
13
8
|
Rect,
|
|
14
9
|
Skia,
|
|
10
|
+
LinearGradient,
|
|
11
|
+
vec,
|
|
15
12
|
useImage,
|
|
16
13
|
Text,
|
|
17
14
|
useFont,
|
|
18
15
|
notifyChange,
|
|
19
|
-
SkPath, // Import SkPath type
|
|
20
|
-
SkFont, // Import SkFont type
|
|
21
|
-
SkImage, // Import SkImage type
|
|
22
16
|
} from "@shopify/react-native-skia";
|
|
23
17
|
import {
|
|
24
18
|
StyleProp,
|
|
@@ -26,21 +20,17 @@ import {
|
|
|
26
20
|
ViewStyle,
|
|
27
21
|
StyleSheet,
|
|
28
22
|
ImageRequireSource,
|
|
29
|
-
|
|
23
|
+
NativeTouchEvent,
|
|
30
24
|
} from "react-native";
|
|
31
|
-
|
|
32
|
-
import { runOnJS, useSharedValue } from "react-native-reanimated";
|
|
25
|
+
import { useDerivedValue, useSharedValue } from "react-native-reanimated";
|
|
33
26
|
import { Gesture, GestureDetector } from "react-native-gesture-handler";
|
|
34
27
|
|
|
35
|
-
// Ignore specific warning if it appears, related to path mutation - use cautiously
|
|
36
|
-
// LogBox.ignoreLogs(['Skia: SkPath.Make()']);
|
|
37
|
-
|
|
38
28
|
type ScratchCardProps = {
|
|
39
29
|
style?: StyleProp<ViewStyle>;
|
|
40
30
|
image?: ImageRequireSource;
|
|
41
31
|
children?: React.ReactNode;
|
|
42
32
|
brushStrokeWidth?: number;
|
|
43
|
-
revealThreshold?: number;
|
|
33
|
+
revealThreshold?: number;
|
|
44
34
|
width?: number;
|
|
45
35
|
height?: number;
|
|
46
36
|
backgroundColor?: string;
|
|
@@ -48,15 +38,15 @@ type ScratchCardProps = {
|
|
|
48
38
|
textFont?: ImageRequireSource;
|
|
49
39
|
textFontSize?: number;
|
|
50
40
|
textFontColor?: string;
|
|
51
|
-
onScratched?: () => void;
|
|
41
|
+
onScratched?: () => void;
|
|
52
42
|
};
|
|
53
43
|
|
|
54
44
|
const ScratchCard: React.FC<ScratchCardProps> = ({
|
|
55
45
|
style,
|
|
56
46
|
children,
|
|
57
|
-
image,
|
|
47
|
+
image = null,
|
|
58
48
|
brushStrokeWidth = 50,
|
|
59
|
-
revealThreshold = 0.8,
|
|
49
|
+
revealThreshold = 0.8,
|
|
60
50
|
width = 300,
|
|
61
51
|
height = 300,
|
|
62
52
|
backgroundColor = "#CCCCCC",
|
|
@@ -66,161 +56,120 @@ const ScratchCard: React.FC<ScratchCardProps> = ({
|
|
|
66
56
|
textFontSize = 16,
|
|
67
57
|
onScratched,
|
|
68
58
|
}) => {
|
|
69
|
-
const
|
|
70
|
-
const
|
|
59
|
+
const img = useImage(image);
|
|
60
|
+
const font = useFont(textFont, textFontSize);
|
|
71
61
|
|
|
72
62
|
const [[areaWidth, areaHeight], setSize] = useState([0, 0]);
|
|
73
63
|
const [isScratched, setScratched] = useState(false);
|
|
74
|
-
const [isLayoutReady, setLayoutReady] = useState(false);
|
|
75
|
-
|
|
76
|
-
const isThresholdReached = useSharedValue(false);
|
|
77
64
|
|
|
78
|
-
const path = useSharedValue
|
|
65
|
+
const path = useSharedValue(Skia.Path.Make().moveTo(0,0));
|
|
79
66
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
67
|
+
//Check scratch progress
|
|
68
|
+
const checkScratchProgress = () => {
|
|
69
|
+
const bounds = path.value.getBounds();
|
|
70
|
+
const scratchedArea = bounds.height * bounds.width;
|
|
71
|
+
const totalArea = areaWidth * areaHeight;
|
|
85
72
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
setSize([newWidth, newHeight]);
|
|
73
|
+
if (scratchedArea / totalArea > revealThreshold && !isScratched) {
|
|
74
|
+
setScratched(true);
|
|
75
|
+
if (onScratched) {
|
|
76
|
+
onScratched();
|
|
91
77
|
}
|
|
92
|
-
if (!isLayoutReady) {
|
|
93
|
-
setLayoutReady(true);
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
setLayoutReady(false);
|
|
97
78
|
}
|
|
98
|
-
}
|
|
79
|
+
};
|
|
99
80
|
|
|
100
|
-
|
|
101
|
-
setScratched(true);
|
|
102
|
-
onScratched?.();
|
|
103
|
-
}, [onScratched]);
|
|
81
|
+
useEffect(() => {console.log("HI")}, [path])
|
|
104
82
|
|
|
105
83
|
const pan = Gesture.Pan()
|
|
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
|
-
if (!bounds || areaWidth <= 0 || areaHeight <= 0) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const scratchedArea = bounds.width * bounds.height;
|
|
136
|
-
const totalArea = areaWidth * areaHeight;
|
|
137
|
-
|
|
138
|
-
if (totalArea > 0 && scratchedArea / totalArea > revealThreshold) {
|
|
139
|
-
if (!isThresholdReached.value) {
|
|
140
|
-
isThresholdReached.value = true;
|
|
141
|
-
runOnJS(revealCardOnJS)();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.error("ScratchCard: Error in onChange (UI Thread):", error);
|
|
146
|
-
}
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
const textMetrics = React.useMemo(() => {
|
|
150
|
-
if (loadedFont && text && areaWidth > 0 && areaHeight > 0) {
|
|
151
|
-
const metrics = loadedFont.measureText(text);
|
|
152
|
-
const textX = areaWidth / 2 - metrics.width / 2;
|
|
153
|
-
const textY = areaHeight / 2 + metrics.height / 3;
|
|
154
|
-
return { x: textX, y: textY, width: metrics.width, height: metrics.height };
|
|
84
|
+
.averageTouches(true)
|
|
85
|
+
.maxPointers(1)
|
|
86
|
+
.onBegin(e => {
|
|
87
|
+
path.value.moveTo(e.x, e.y);
|
|
88
|
+
path.value.lineTo(e.x, e.y);
|
|
89
|
+
notifyChange(path as any);
|
|
90
|
+
})
|
|
91
|
+
.onChange(e => {
|
|
92
|
+
try {
|
|
93
|
+
// checkScratchProgress();
|
|
94
|
+
path.value.lineTo(e.x, e.y);
|
|
95
|
+
notifyChange(path as any);
|
|
96
|
+
// const bounds = path.value.getBounds();
|
|
97
|
+
// const scratchedArea = bounds.height * bounds.width;
|
|
98
|
+
// const totalArea = areaWidth * areaHeight;
|
|
99
|
+
|
|
100
|
+
// if (scratchedArea / totalArea > revealThreshold && !isScratched) {
|
|
101
|
+
// console.log("SCRATCHED")
|
|
102
|
+
// // setScratched(true);
|
|
103
|
+
// // if (onScratched) {
|
|
104
|
+
// // onScratched();
|
|
105
|
+
// // }
|
|
106
|
+
// }
|
|
107
|
+
} catch (error) {
|
|
108
|
+
console.log(error)
|
|
155
109
|
}
|
|
156
|
-
return null;
|
|
157
|
-
}, [loadedFont, text, areaWidth, areaHeight]);
|
|
158
110
|
|
|
159
|
-
|
|
111
|
+
// checkScratchProgress();
|
|
112
|
+
})
|
|
160
113
|
|
|
161
114
|
return (
|
|
162
115
|
<View
|
|
163
|
-
onLayout={
|
|
116
|
+
onLayout={(e) => {
|
|
117
|
+
setSize([e.nativeEvent.layout.width, e.nativeEvent.layout.height]);
|
|
118
|
+
}}
|
|
164
119
|
style={[styles.container, style, { width, height }]}
|
|
165
120
|
>
|
|
166
121
|
<View style={styles.content}>{children}</View>
|
|
167
122
|
|
|
168
|
-
{!isScratched &&
|
|
123
|
+
{!isScratched && (
|
|
169
124
|
<GestureDetector gesture={pan}>
|
|
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
|
-
|
|
125
|
+
<Canvas
|
|
126
|
+
style={styles.canvas}
|
|
127
|
+
>
|
|
128
|
+
<Mask
|
|
129
|
+
mode="luminance"
|
|
130
|
+
mask={
|
|
131
|
+
<Group>
|
|
132
|
+
<Rect x={0} y={0} width={1000} height={1000} color="white" />
|
|
133
|
+
<Path
|
|
134
|
+
path={path}
|
|
135
|
+
color="black"
|
|
136
|
+
style="stroke"
|
|
137
|
+
strokeJoin="round"
|
|
138
|
+
strokeCap="round"
|
|
139
|
+
strokeWidth={brushStrokeWidth}
|
|
140
|
+
/>
|
|
141
|
+
</Group>
|
|
142
|
+
}
|
|
143
|
+
>
|
|
144
|
+
{img ? (
|
|
145
|
+
<Image
|
|
146
|
+
image={img}
|
|
147
|
+
fit="cover"
|
|
148
|
+
x={0}
|
|
149
|
+
y={0}
|
|
150
|
+
width={areaWidth}
|
|
151
|
+
height={areaHeight}
|
|
152
|
+
/>
|
|
153
|
+
) : (
|
|
154
|
+
<Group>
|
|
155
|
+
<Rect
|
|
197
156
|
x={0}
|
|
198
157
|
y={0}
|
|
199
158
|
width={areaWidth}
|
|
200
159
|
height={areaHeight}
|
|
160
|
+
color={backgroundColor}
|
|
201
161
|
/>
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
x={textMetrics.x}
|
|
214
|
-
y={textMetrics.y}
|
|
215
|
-
text={text}
|
|
216
|
-
color={textFontColor}
|
|
217
|
-
font={loadedFont}
|
|
218
|
-
/>
|
|
219
|
-
) : null}
|
|
220
|
-
</Group>
|
|
221
|
-
)}
|
|
222
|
-
</Mask>
|
|
223
|
-
</Canvas>
|
|
162
|
+
<Text
|
|
163
|
+
x={areaWidth / 2 - (font?.measureText(text).width || 0) / 2}
|
|
164
|
+
y={areaHeight / 2 + (font?.measureText(text).height || 0) / 2}
|
|
165
|
+
text={text}
|
|
166
|
+
color={textFontColor}
|
|
167
|
+
font={font}
|
|
168
|
+
/>
|
|
169
|
+
</Group>
|
|
170
|
+
)}
|
|
171
|
+
</Mask>
|
|
172
|
+
</Canvas>
|
|
224
173
|
</GestureDetector>
|
|
225
174
|
)}
|
|
226
175
|
</View>
|
|
@@ -231,6 +180,8 @@ const styles = StyleSheet.create({
|
|
|
231
180
|
container: {
|
|
232
181
|
position: "relative",
|
|
233
182
|
overflow: "hidden",
|
|
183
|
+
width: "100%",
|
|
184
|
+
height: "100%",
|
|
234
185
|
},
|
|
235
186
|
content: {
|
|
236
187
|
position: "absolute",
|
|
@@ -238,7 +189,6 @@ const styles = StyleSheet.create({
|
|
|
238
189
|
left: 0,
|
|
239
190
|
width: "100%",
|
|
240
191
|
height: "100%",
|
|
241
|
-
zIndex: 1,
|
|
242
192
|
},
|
|
243
193
|
canvas: {
|
|
244
194
|
position: "absolute",
|
|
@@ -246,8 +196,7 @@ const styles = StyleSheet.create({
|
|
|
246
196
|
left: 0,
|
|
247
197
|
width: "100%",
|
|
248
198
|
height: "100%",
|
|
249
|
-
zIndex: 2,
|
|
250
199
|
},
|
|
251
200
|
});
|
|
252
201
|
|
|
253
|
-
export default ScratchCard;
|
|
202
|
+
export default ScratchCard;
|