|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+//@flow
|
|
|
2
|
+import React, { useRef, useState, useEffect, useCallback } from 'react';
|
|
|
3
|
+import { SafeAreaView, Image, View } from 'react-native';
|
|
|
4
|
+import ViewShot from 'react-native-view-shot';
|
|
|
5
|
+import { Surface, Group, Shape } from '@react-native-community/art';
|
|
|
6
|
+import Desc from './Desc';
|
|
|
7
|
+import Canvas from 'react-native-canvas';
|
|
|
8
|
+
|
|
|
9
|
+const dimension = { width: 300, height: 300 };
|
|
|
10
|
+
|
|
|
11
|
+const CanvasRendering = ({ onDrawn }) => {
|
|
|
12
|
+ const ref = useRef();
|
|
|
13
|
+ useEffect(() => {
|
|
|
14
|
+ const ctx = ref.current.getContext('2d');
|
|
|
15
|
+ ctx.fillStyle = '#eee';
|
|
|
16
|
+ ctx.fillRect(0, 0, ref.current.width, ref.current.height);
|
|
|
17
|
+ ctx.fillStyle = 'red';
|
|
|
18
|
+ ctx.fillRect(120, 30, 60, 60);
|
|
|
19
|
+ ctx.fillStyle = 'blue';
|
|
|
20
|
+ ctx.fillRect(140, 50, 60, 60);
|
|
|
21
|
+ const timeout = setTimeout(onDrawn, 500); // hack. react-native-canvas have no way to tell when it's executed
|
|
|
22
|
+ return () => clearTimeout(timeout);
|
|
|
23
|
+ }, []);
|
|
|
24
|
+ return <Canvas ref={ref} style={dimension} />;
|
|
|
25
|
+};
|
|
|
26
|
+
|
|
|
27
|
+const CanvasExample = () => {
|
|
|
28
|
+ const [source, setSource] = useState(null);
|
|
|
29
|
+ const viewShotRef = useRef();
|
|
|
30
|
+ const onCapture = useCallback(() => {
|
|
|
31
|
+ viewShotRef.current.capture().then(uri => setSource({ uri }));
|
|
|
32
|
+ }, []);
|
|
|
33
|
+ return (
|
|
|
34
|
+ <SafeAreaView>
|
|
|
35
|
+ <ViewShot ref={viewShotRef} style={dimension}>
|
|
|
36
|
+ <CanvasRendering onDrawn={onCapture} />
|
|
|
37
|
+ </ViewShot>
|
|
|
38
|
+
|
|
|
39
|
+ <Image source={source} style={dimension} />
|
|
|
40
|
+ </SafeAreaView>
|
|
|
41
|
+ );
|
|
|
42
|
+};
|
|
|
43
|
+
|
|
|
44
|
+CanvasExample.navigationOptions = {
|
|
|
45
|
+ title: 'react-native-canvas',
|
|
|
46
|
+};
|
|
|
47
|
+
|
|
|
48
|
+export default CanvasExample;
|