This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
forked from gre/react-native-view-shot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
454 lines (447 loc) · 14.5 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
ScrollView,
Image,
Switch,
TextInput,
Picker,
Slider,
WebView,
ART
} from "react-native";
import SvgUri from "react-native-svg-uri";
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 Video from "react-native-video";
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 }) => <GL.Node shader={shaders.helloGL} uniforms={{ blue }} />,
{ displayName: "HelloGL" }
);
export default class App extends Component {
state = {
previewSource: catsSource,
error: null,
res: null,
value: {
format: "png",
quality: 0.9,
result: "tmpfile",
snapshotContentContainer: false
}
};
snapshot = refname => () =>
(refname
? captureRef(this.refs[refname], this.state.value)
: captureScreen(this.state.value)
)
.then(
res =>
this.state.value.result !== "tmpfile"
? res
: new Promise((success, failure) =>
// just a test to ensure res can be used in Image.getSize
Image.getSize(
res,
(width, height) => (
console.log(res, width, height), success(res)
),
failure
)
)
)
.then(res =>
this.setState({
error: null,
res,
previewSource: {
uri:
this.state.value.result === "base64"
? "data:image/" + this.state.value.format + ";base64," + res
: res
}
})
)
.catch(
error => (
console.warn(error),
this.setState({ error, res: null, previewSource: null })
)
);
render() {
const { value, previewSource, error, res } = this.state;
const {
format,
quality,
width,
height,
result,
snapshotContentContainer
} = value;
return (
<ScrollView
ref="full"
style={styles.root}
contentContainerStyle={styles.container}
>
<View ref="header" style={styles.header}>
<Text style={styles.title}>😃 ViewShot Example 😜</Text>
<View style={styles.p1}>
<Text style={styles.text}>This is a </Text>
<Text style={styles.code}>react-native-view-shot</Text>
<Text style={styles.text}> showcase.</Text>
</View>
<View style={styles.preview}>
{error ? (
<Text style={styles.previewError}>
{"" + (error.message || error)}
</Text>
) : (
<Image
fadeDuration={0}
resizeMode="contain"
style={styles.previewImage}
source={previewSource}
/>
)}
</View>
<Text numberOfLines={1} style={styles.previewUriText}>
{res ? res.slice(0, 200) : ""}
</Text>
</View>
<View ref="form" style={styles.form}>
<View style={styles.btns}>
<Btn
label="😻 Reset"
onPress={() => this.setState({ previewSource: catsSource })}
/>
<Btn label="📷 Head Section" onPress={this.snapshot("header")} />
<Btn label="📷 Form" onPress={this.snapshot("form")} />
<Btn
label="📷 Experimental Section"
onPress={this.snapshot("complex")}
/>
<Btn label="📷 All (ScrollView)" onPress={this.snapshot("full")} />
<Btn label="📷 SVG" onPress={this.snapshot("svg")} />
<Btn label="📷 Transform" onPress={this.snapshot("transformParent")} />
<Btn label="📷 Transform Child" onPress={this.snapshot("transform")} />
<Btn label="📷 GL React" onPress={this.snapshot("gl")} />
<Btn label="📷 MapView" onPress={this.snapshot("mapview")} />
<Btn label="📷 WebView" onPress={this.snapshot("webview")} />
<Btn label="📷 Video" onPress={this.snapshot("video")} />
<Btn label="📷 Native Screenshot" onPress={this.snapshot()} />
<Btn
label="📷 Empty View (should crash)"
onPress={this.snapshot("empty")}
/>
</View>
<View style={styles.field}>
<Text style={styles.label}>Format</Text>
<Picker
style={styles.input}
selectedValue={format}
onValueChange={format =>
this.setState({ value: { ...value, format } })}
>
<Picker.Item label="PNG" value="png" />
<Picker.Item label="JPEG" value="jpeg" />
<Picker.Item label="WEBM (android only)" value="webm" />
<Picker.Item label="RAW (android only)" value="raw" />
<Picker.Item label="INVALID" value="_invalid_" />
</Picker>
</View>
<View style={styles.field}>
<Text style={styles.label}>Quality</Text>
<Slider
style={styles.input}
value={quality}
onValueChange={quality =>
this.setState({ value: { ...value, quality } })}
/>
<Text>{(quality * 100).toFixed(0)}%</Text>
</View>
<View style={styles.field}>
<Text style={styles.label}>Size</Text>
<Switch
style={styles.switch}
value={width !== undefined}
onValueChange={checked =>
this.setState({
value: omit(
{
...value,
width: 300,
height: 300
},
checked ? [] : ["width", "height"]
)
})}
/>
{width !== undefined ? (
<TextInput
style={styles.inputText}
value={"" + width}
keyboardType="number-pad"
onChangeText={txt =>
!isNaN(txt) &&
this.setState({
value: { ...value, width: parseInt(txt, 10) }
})}
/>
) : (
<Text style={styles.inputText}>(auto)</Text>
)}
<Text>x</Text>
{height !== undefined ? (
<TextInput
style={styles.inputText}
value={"" + height}
keyboardType="number-pad"
onChangeText={txt =>
!isNaN(txt) &&
this.setState({
value: { ...value, height: parseInt(txt, 10) }
})}
/>
) : (
<Text style={styles.inputText}>(auto)</Text>
)}
</View>
<View style={styles.field}>
<Text style={styles.label}>Result</Text>
<Picker
style={styles.input}
selectedValue={result}
onValueChange={result =>
this.setState({ value: { ...value, result } })}
>
<Picker.Item label="tmpfile" value="tmpfile" />
<Picker.Item label="base64" value="base64" />
<Picker.Item label="zip-base64 (Android Only)" value="zip-base64" />
<Picker.Item label="data URI" value="data-uri" />
<Picker.Item label="INVALID" value="_invalid_" />
</Picker>
</View>
<View style={styles.field}>
<Text style={styles.label}>snapshotContentContainer</Text>
<Switch
style={styles.switch}
value={snapshotContentContainer}
onValueChange={snapshotContentContainer =>
this.setState({
value: { ...value, snapshotContentContainer }
})}
/>
</View>
</View>
<View ref="empty" collapsable={false} />
<View style={styles.experimental} ref="complex" collapsable={false}>
<Text style={styles.experimentalTitle}>Experimental Stuff</Text>
<View ref="transformParent" collapsable={false} style={{flex: 1, flexDirection: 'row'}}>
<View ref="transformInner" collapsable={false} style={styles.experimentalTransform}>
<Text ref="transform" >Transform</Text>
<ART.Surface ref="surface" width={20} height={20}>
<ART.Text
fill="#000000"
font={{fontFamily:'Arial',fontSize: 6}}
>Sample Text</ART.Text>
<ART.Shape
d='M2.876,10.6499757 L16.375,18.3966817 C16.715,18.5915989 17.011,18.4606545 17.125,18.3956822 C17.237,18.3307098 17.499,18.1367923 17.499,17.746958 L17.499,2.25254636 C17.499,1.86271212 17.237,1.66879457 17.125,1.6038222 C17.011,1.53884983 16.715,1.4079055 16.375,1.60282262 L2.876,9.34952866 C2.537,9.54544536 2.5,9.86930765 2.5,10.000252 C2.5,10.1301967 2.537,10.4550586 2.876,10.6499757 M16.749,20 C16.364,20 15.98,19.8990429 15.629,19.6971288 L2.13,11.9504227 L2.129,11.9504227 C1.422,11.5445953 1,10.8149056 1,10.000252 C1,9.18459879 1.422,8.45590864 2.129,8.04908162 L15.629,0.302375584 C16.332,-0.10245228 17.173,-0.10045313 17.876,0.306373884 C18.579,0.713200898 18.999,1.44089148 18.999,2.25254636 L18.999,17.746958 C18.999,18.5586129 18.579,19.2863035 17.876,19.6931305 C17.523,19.8970438 17.136,20 16.749,20'
fill="blue"
stroke="black"
strokeWidth={0}
>
</ART.Shape>
</ART.Surface>
</View>
<View ref="right" style={styles.experimentalTransformV2}>
<ART.Surface ref="surface2" width={20} height={20}>
<ART.Shape
x={0}
y={10}
d='M2.876,10.6499757 L16.375,18.3966817 C16.715,18.5915989 17.011,18.4606545 17.125,18.3956822 C17.237,18.3307098 17.499,18.1367923 17.499,17.746958 L17.499,2.25254636 C17.499,1.86271212 17.237,1.66879457 17.125,1.6038222 C17.011,1.53884983 16.715,1.4079055 16.375,1.60282262 L2.876,9.34952866 C2.537,9.54544536 2.5,9.86930765 2.5,10.000252 C2.5,10.1301967 2.537,10.4550586 2.876,10.6499757 M16.749,20 C16.364,20 15.98,19.8990429 15.629,19.6971288 L2.13,11.9504227 L2.129,11.9504227 C1.422,11.5445953 1,10.8149056 1,10.000252 C1,9.18459879 1.422,8.45590864 2.129,8.04908162 L15.629,0.302375584 C16.332,-0.10245228 17.173,-0.10045313 17.876,0.306373884 C18.579,0.713200898 18.999,1.44089148 18.999,2.25254636 L18.999,17.746958 C18.999,18.5586129 18.579,19.2863035 17.876,19.6931305 C17.523,19.8970438 17.136,20 16.749,20'
fill="red"
>
</ART.Shape>
</ART.Surface>
</View>
</View>
<View ref="svg" collapsable={false}>
<SvgUri
width={200}
height={200}
source={require("./homer-simpson.svg")}
/>
</View>
<View ref="gl" collapsable={false}>
<Surface width={300} height={300}>
<HelloGL blue={0.5} />
</Surface>
</View>
<MapView
ref="mapview"
initialRegion={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421
}}
style={{ width: 300, height: 300 }}
/>
<View
ref="webview"
collapsable={false}
style={{ width: 300, height: 300 }}
>
<WebView
source={{
uri: "https://github.com/gre/react-native-view-shot"
}}
/>
</View>
<Video
ref="video"
style={{ width: 300, height: 300 }}
source={require("./broadchurch.mp4")}
volume={0}
repeat
/>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
root: {
flex: 1,
backgroundColor: "#f6f6f6"
},
container: {
paddingVertical: 20,
backgroundColor: "#f6f6f6"
},
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
}
});