-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
97 lines (88 loc) · 2.74 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native';
import { ARKit, DetectionImage } from 'react-native-arkit';
const apiKey = 'YOUR-API-KEY-HERE';
const getImageURL = async () => {
const pages = 19000; // Cached number of pages to avoid two API hits (misses some pages)
const totalrecordsperquery = 5;
const randomPage = Math.floor(Math.random()*pages) + 1;
const url = `https://api.harvardartmuseums.org/object?q=imagepermissionlevel:0&page=${randomPage}&fields=primaryimageurl&apikey=${apiKey}`;
try {
const res = await fetch(url, {
method: 'GET'
});
const json = await res.json();
const records = json.records;
for (let i = 0; i < records.length; i++) {
const record = records[i];
if (record.primaryimageurl) {
return record.primaryimageurl;
}
}
} catch (err) {
alert(JSON.stringify(err));
throw new Error(`An error occurred: ${JSON.stringify(err)}.`);
}
throw new Error('No valid image URLs in the selected page.');
};
export default class App extends Component {
constructor() {
super();
this.state = {
cards: {}
}
}
async componentDidMount() {
const url = await getImageURL();
this.setState({
url
});
}
render() {
const cards = this.state.cards;
const url = this.state.url;
const children = Object.keys(cards).map((name) => {
const anchor = cards[name];
const { position, eulerAngles, positionAbsolute } = anchor;
return (
<ARKit.Box
position={ position }
shape={{ width: 0.0635, height: 0.001, length: 0.0889, chamfer: 0 }}
eulerAngles={ eulerAngles }
material={{
diffuse: { path: url, intensity: 1 }
}}
/>
);
});
return (
<View style={{ flex: 1 }}>
<ARKit
// debug // debug mode will show feature points detected and 3D axis
style={{ flex: 1 }}
planeDetection // turn on plane detection
lightEstimation // turn on light estimation
detectionImages={[
{ resourceGroupName: 'AR Resources' }
]}
onAnchorDetected={ (anchor) => {}}
onAnchorUpdated={ (anchor) => {
const { position, eulerAngles, positionAbsolute, type } = anchor;
if (type === 'image') {
const { image: { name } } = anchor;
this.setState({ cards: { [name]: { position, eulerAngles, positionAbsolute }}})
}
}}
>
{ children }
</ARKit>
</View>
);
}
}
AppRegistry.registerComponent('MyFirstARKitApp', () => App);