No Description

Canvas.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. const dimension = { width: 300, height: 300 };
  9. const CanvasRendering = ({ onDrawn }) => {
  10. const ref = useRef();
  11. useEffect(() => {
  12. const ctx = ref.current.getContext('2d');
  13. ctx.fillStyle = '#eee';
  14. ctx.fillRect(0, 0, ref.current.width, ref.current.height);
  15. ctx.fillStyle = 'red';
  16. ctx.fillRect(120, 30, 60, 60);
  17. ctx.fillStyle = 'blue';
  18. ctx.fillRect(140, 50, 60, 60);
  19. const timeout = setTimeout(onDrawn, 500); // hack. react-native-canvas have no way to tell when it's executed
  20. return () => clearTimeout(timeout);
  21. }, []);
  22. return <Canvas ref={ref} style={dimension} />;
  23. };
  24. const CanvasExample = () => {
  25. const [source, setSource] = useState(null);
  26. const viewShotRef = useRef();
  27. const onCapture = useCallback(() => {
  28. viewShotRef.current.capture().then(uri => setSource({ uri }));
  29. }, []);
  30. return (
  31. <SafeAreaView>
  32. <ViewShot ref={viewShotRef} style={dimension}>
  33. <CanvasRendering onDrawn={onCapture} />
  34. </ViewShot>
  35. <Image source={source} style={dimension} />
  36. </SafeAreaView>
  37. );
  38. };
  39. CanvasExample.navigationOptions = {
  40. title: 'react-native-canvas',
  41. };
  42. export default CanvasExample;