/* eslint-disable react-native/no-inline-styles */
import React, { Fragment, useState, useRef, useCallback } from 'react';
import {
SafeAreaView,
StyleSheet,
Switch,
TextInput,
Picker,
ScrollView,
View,
Text,
StatusBar,
Image,
} from 'react-native';
import { Buffer } from 'buffer';
import * as ART from '@react-native-community/art';
import Slider from '@react-native-community/slider';
import omit from 'lodash/omit';
import { captureRef, captureScreen } from 'react-native-view-shot';
import { Surface } from 'gl-react-native';
import GL from 'gl-react';
import MapView from 'react-native-maps';
import WebView from 'react-native-webview';
import Video from 'react-native-video';
import SvgUri from 'react-native-svg-uri';
import Btn from './Btn';
const catsSource = {
uri: 'https://i.imgur.com/5EOyTDQ.jpg',
};
const shaders = GL.Shaders.create({
helloGL: {
frag: `
precision highp float;
varying vec2 uv;
uniform float blue;
void main () {
gl_FragColor = vec4(uv.x, uv.y, blue, 1.0);
}`,
},
});
const HelloGL = GL.createComponent(
({ blue }) => ,
{ displayName: 'HelloGL' },
);
const App = () => {
const fullRef = useRef();
const headerRef = useRef();
const formRef = useRef();
const emptyRef = useRef();
const complexRef = useRef();
const svgRef = useRef();
const glRef = useRef();
const mapViewRef = useRef();
const webviewRef = useRef();
const videoRef = useRef();
const transformParentRef = useRef();
const transformRef = useRef();
const surfaceRef = useRef();
const [previewSource, setPreviewSource] = useState(catsSource);
const [result, setResult] = useState({ error: null, res: null });
const [config, setConfig] = useState({
format: 'png',
quality: 0.9,
result: 'tmpfile',
snapshotContentContainer: false,
});
const onCapture = useCallback(
res => {
if (config.result === 'base64') {
const b = Buffer.from(res, 'base64');
console.log('buffer of length ' + b.length);
}
setPreviewSource({
uri: config.result === 'base64' ? 'data:image/' + config.format + ';base64,' + res : res,
});
setResult({
error: null,
res,
});
},
[config],
);
const onCaptureFailure = useCallback(error => {
console.warn(error);
setPreviewSource(null);
setResult({
error,
res: null,
});
}, []);
const capture = useCallback(
ref => {
(ref ? captureRef(ref, config) : captureScreen(config))
.then(res =>
config.result !== 'tmpfile'
? res
: new Promise((success, failure) =>
// just a test to ensure res can be used in Image.getSize
Image.getSize(res, (width, height) => success(res), failure),
),
)
.then(onCapture, onCaptureFailure);
},
[config, onCapture, onCaptureFailure],
);
return (
😃 ViewShot Example 😜
This is a
react-native-view-shot
showcase.
{result.error ? (
{'' + (result.error.message || result.error)}
) : (
)}
{result.res ? result.res.slice(0, 200) : ''}
setPreviewSource(catsSource)} />
capture(headerRef)} />
capture(formRef)} />
capture(complexRef)} />
capture(fullRef)} />
capture(svgRef)} />
capture(transformParentRef)} />
capture(transformRef)} />
capture(glRef)} />
capture(mapViewRef)} />
capture(webviewRef)} />
capture(videoRef)} />
capture()} />
capture(emptyRef)} />
Format
setConfig({ ...config, format })}
>
Quality
setConfig({ ...config, quality })}
/>
{(config.quality * 100).toFixed(0)}%
Size
setConfig(
omit(
{
...config,
width: 300,
height: 300,
},
checked ? [] : ['width', 'height'],
),
)
}
/>
{config.width !== undefined ? (
!isNaN(txt) && setConfig({ ...config, width: parseInt(txt, 10) })
}
/>
) : (
(auto)
)}
x
{config.height !== undefined ? (
!isNaN(txt) && setConfig({ ...config, height: parseInt(txt, 10) })
}
/>
) : (
(auto)
)}
Result
setConfig({ ...config, result: r })}
>
snapshotContentContainer
setConfig({ ...config, snapshotContentContainer })
}
/>
Experimental Stuff
Transform
Sample Text
);
};
const styles = StyleSheet.create({
root: {
backgroundColor: '#f6f6f6',
},
container: {
paddingVertical: 20,
},
title: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
experimental: {
padding: 10,
flexDirection: 'column',
alignItems: 'center',
},
experimentalTitle: {
fontSize: 16,
margin: 10,
},
experimentalTransform: {
transform: [{ rotate: '180deg' }],
backgroundColor: 'powderblue',
},
experimentalTransformV2: {
// transform: [{ rotate: '180deg' }],
backgroundColor: 'skyblue',
},
p1: {
marginBottom: 10,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
alignItems: 'center',
},
text: {
color: '#333',
},
code: {
fontWeight: 'bold',
color: '#000',
},
field: {
flexDirection: 'row',
alignItems: 'center',
paddingVertical: 4,
paddingHorizontal: 10,
},
label: {
minWidth: 80,
fontStyle: 'italic',
color: '#888',
},
switch: {
marginRight: 50,
},
input: {
flex: 1,
marginHorizontal: 5,
},
inputText: {
flex: 1,
marginHorizontal: 5,
color: 'red',
textAlign: 'center',
},
preview: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-around',
},
previewImage: {
width: 375,
height: 300,
},
previewUriText: {
fontSize: 12,
fontStyle: 'italic',
color: '#666',
textAlign: 'center',
padding: 10,
paddingBottom: 0,
},
previewError: {
width: 375,
height: 300,
paddingTop: 20,
textAlign: 'center',
fontSize: 20,
fontWeight: 'bold',
color: '#fff',
backgroundColor: '#c00',
},
header: {
backgroundColor: '#f6f6f6',
borderColor: '#000',
borderWidth: 1,
paddingBottom: 20,
},
form: {
backgroundColor: '#fff',
},
btns: {
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
justifyContent: 'center',
paddingVertical: 10,
margin: 4,
},
experimentalRoot: {
flex: 1,
flexDirection: 'row',
},
});
App.navigationOptions = {
title: 'Full Example',
};
export default App;