|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
import React, {useEffect, useRef, useState } from 'react';
|
|
|
|
|
import { SketchCanvas, SketchCanvasRef } from 'rn-perfect-sketch-canvas';
|
|
|
|
|
import { StyleSheet, Button, View, Text } from 'react-native';
|
|
|
|
|
import { SvgUri } from 'react-native-svg';
|
|
|
|
|
import { StyleSheet, Button, View, Text, useColorScheme } from 'react-native';
|
|
|
|
|
import { SvgXml } from 'react-native-svg';
|
|
|
|
|
import Slider from '@react-native-community/slider'
|
|
|
|
|
|
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
@ -13,43 +13,54 @@ type DrawingCanvaProps = {
|
|
|
|
|
|
|
|
|
|
const DrawingCanva = (props: DrawingCanvaProps) => {
|
|
|
|
|
|
|
|
|
|
const style = useColorScheme() == 'light' ? style_light : style_dark;
|
|
|
|
|
|
|
|
|
|
const canvasRef = useRef<SketchCanvasRef>(null);
|
|
|
|
|
const [strokeWidth, setStroke] = useState(5);
|
|
|
|
|
const [isCanvasReady, setIsCanvasReady] = useState(false);
|
|
|
|
|
const [imgXml, setImgXml] = useState('<svg></svg>');
|
|
|
|
|
|
|
|
|
|
const selectedKanji = KanjiMapper.SerializedObjectToKanji(useSelector(state => state.kanjiReducer.selectedKanji));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (canvasRef.current) {
|
|
|
|
|
fetchXml();
|
|
|
|
|
setIsCanvasReady(true);
|
|
|
|
|
}
|
|
|
|
|
}, [canvasRef.current]);
|
|
|
|
|
|
|
|
|
|
const fetchXml = async () => {
|
|
|
|
|
const xml = await (await fetch(selectedKanji.image)).text();
|
|
|
|
|
setImgXml(xml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<View style={styles.container}>
|
|
|
|
|
{selectedKanji && (<SvgUri
|
|
|
|
|
width="75%"
|
|
|
|
|
height="75%"
|
|
|
|
|
uri={selectedKanji.image}
|
|
|
|
|
style={styles.back}
|
|
|
|
|
opacity={0.1}
|
|
|
|
|
/>)}
|
|
|
|
|
<View style={style.container}>
|
|
|
|
|
{selectedKanji && (
|
|
|
|
|
<SvgXml
|
|
|
|
|
xml={imgXml
|
|
|
|
|
.replace(/fill="#[0-9a-f]{6}"/g, `fill=${style.svg.color}`)}
|
|
|
|
|
width="75%"
|
|
|
|
|
height="75%"
|
|
|
|
|
opacity={0.1}
|
|
|
|
|
style={style.back}
|
|
|
|
|
/>)}
|
|
|
|
|
<SketchCanvas
|
|
|
|
|
ref={canvasRef}
|
|
|
|
|
strokeColor={'black'}
|
|
|
|
|
strokeColor={style.canvas.strokeColor}
|
|
|
|
|
strokeWidth={strokeWidth}
|
|
|
|
|
containerStyle={styles.canvas}
|
|
|
|
|
containerStyle={style.canvas}
|
|
|
|
|
/>
|
|
|
|
|
<Slider
|
|
|
|
|
style={styles.slider}
|
|
|
|
|
style={style.slider}
|
|
|
|
|
onValueChange={(val) => setStroke(val)}
|
|
|
|
|
minimumValue={5}
|
|
|
|
|
maximumValue={10}
|
|
|
|
|
minimumTrackTintColor={"#FF5C5C"}
|
|
|
|
|
/>
|
|
|
|
|
{isCanvasReady && (<View style={styles.menu}>
|
|
|
|
|
{isCanvasReady && (<View style={style.menu}>
|
|
|
|
|
<Button color="#FF5C5C" onPress={canvasRef.current?.reset} title="Reset" />
|
|
|
|
|
<Button color="#FF5C5C" onPress={canvasRef.current?.undo} title="Undo" />
|
|
|
|
|
</View>)}
|
|
|
|
@ -57,7 +68,42 @@ const DrawingCanva = (props: DrawingCanvaProps) => {
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
|
const style_light = StyleSheet.create({
|
|
|
|
|
svg: {
|
|
|
|
|
color: "black"
|
|
|
|
|
},
|
|
|
|
|
container: {
|
|
|
|
|
flex: 1,
|
|
|
|
|
width: "100%",
|
|
|
|
|
},
|
|
|
|
|
back: {
|
|
|
|
|
alignSelf: "center",
|
|
|
|
|
width: "75%",
|
|
|
|
|
height: "75%",
|
|
|
|
|
position: "absolute"
|
|
|
|
|
},
|
|
|
|
|
canvas: {
|
|
|
|
|
alignSelf: "center",
|
|
|
|
|
height: "75%",
|
|
|
|
|
width: "75%",
|
|
|
|
|
borderWidth: 2,
|
|
|
|
|
borderColor: "black",
|
|
|
|
|
strokeColor: "black"
|
|
|
|
|
},
|
|
|
|
|
menu: {
|
|
|
|
|
flexDirection: "row",
|
|
|
|
|
justifyContent: "space-evenly",
|
|
|
|
|
},
|
|
|
|
|
slider: {
|
|
|
|
|
width: "75%",
|
|
|
|
|
alignSelf: "center",
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const style_dark = StyleSheet.create({
|
|
|
|
|
svg: {
|
|
|
|
|
color: "white"
|
|
|
|
|
},
|
|
|
|
|
container: {
|
|
|
|
|
flex: 1,
|
|
|
|
|
width: "100%",
|
|
|
|
@ -73,7 +119,9 @@ const styles = StyleSheet.create({
|
|
|
|
|
height: "75%",
|
|
|
|
|
width: "75%",
|
|
|
|
|
borderWidth: 2,
|
|
|
|
|
borderColor: "black"
|
|
|
|
|
borderColor: "white",
|
|
|
|
|
strokeColor: "white"
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
menu: {
|
|
|
|
|
flexDirection: "row",
|
|
|
|
@ -85,4 +133,6 @@ const styles = StyleSheet.create({
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default DrawingCanva;
|
|
|
|
|