Skip to content

Commit 21cf7d5

Browse files
committed
refactor, start tracking with button
1 parent da4cc98 commit 21cf7d5

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

public/three.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@
9191
</style>
9292
</head>
9393
<body>
94-
<div id="container"></div>
94+
<div id="container" style='position: relative'>
95+
</div>
96+
<input type="button" value="Start!" style="position: absolute; top: 10px; left: 10px; width:100px; height:50px; z-index: 10" id="start" />
9597
<!--
9698
<script type="module" src="index.js"></script>
9799
-->
98-
<script src="dist/three/bundle.js?c"></script>
100+
<script src="dist/three/bundle.js?d"></script>
99101
</body>
100102
</html>

three/index.js

+56-34
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,43 @@ function main() {
2323

2424
const origLon = -0.72, origLat = 51.05;
2525

26+
let alva, arjs, arCamView, ctx, video;
27+
let gotFirstGps = false;
28+
2629
Camera.Initialize(config).then( async(media) => {
27-
const video = media.el;
30+
video = media.el;
2831
const size = resize2cover(video.videoWidth, video.videoHeight, container.clientWidth, container.clientHeight);
2932

3033
canvas.width = container.clientWidth;
3134
canvas.height = container.clientHeight;
3235
video.style.width = size.width + 'px';
3336
video.style.height = size.height + 'px';
3437

35-
const ctx = canvas.getContext('2d', {
38+
ctx = canvas.getContext('2d', {
3639
alpha: false,
3740
desynchronized: true
3841
});
42+
43+
44+
container.appendChild(canvas);
45+
container.appendChild(view);
46+
47+
initAlva().then(initArjs);
3948

40-
const alva = await AlvaAR.Initialize(canvas.width, canvas.height);
41-
const arCamView = new ARCamView(view, canvas.width, canvas.height);
49+
}).catch( error => alert(error) );
50+
51+
document.getElementById("start").addEventListener("click", e => {
52+
setupFrameHandler();
53+
});
54+
55+
56+
async function initAlva() {
57+
alva = await AlvaAR.Initialize(canvas.width, canvas.height);
58+
arCamView = new ARCamView(view, canvas.width, canvas.height);
59+
}
4260

43-
const arjs = new THREEx.LocationBased(arCamView.scene, arCamView.camera, { initialPositionAsOrigin: true});
44-
console.log('sending fakeGps');
61+
function initArjs() {
62+
arjs = new THREEx.LocationBased(arCamView.scene, arCamView.camera, { initialPositionAsOrigin: true});
4563

4664
const geom = new THREE.BoxGeometry(20,20,20);
4765
const props = [{
@@ -76,47 +94,51 @@ function main() {
7694
yDis:-100
7795
}
7896
];
79-
let objectsAdded = false;
8097

81-
arjs.on("gpsupdate", e => {
82-
console.log('camera position now:');
98+
arjs.on("gpsupdate", pos => {
99+
alert(`Got GPS position: ${pos.coords.longitude} ${pos.coords.latitude}`);
100+
console.log(`camera position now:`);
83101
console.log(arCamView.camera.position);
84-
if(!objectsAdded) {
102+
if(!gotFirstGps) {
85103
for(let i=0; i<props.length; i++) {
86104
const object = new THREE.Mesh(geom, props[i].mtl);
87105
object.visible = false;
88106
const pos = arjs.lonLatToWorldCoords(origLon + props[i].lonDis, origLat + props[i].latDis);
89107
console.log(pos);
90108
arCamView.addObject(object, pos[0], arCamView.camera.position.y + props[i].yDis, pos[1]);
91109
}
92-
objectsAdded = true;
93-
onFrame( () => {
94-
ctx.clearRect(0, 0, canvas.width, canvas.height);
95-
ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
96-
const frame = ctx.getImageData(0, 0, canvas.width, canvas.height);
97-
const pose = alva.findCameraPose(frame);
98-
if(pose) {
99-
arCamView.updateCameraPose(pose);
100-
} else {
101-
arCamView.lostCamera();
102-
const dots = alva.getFramePoints();
103-
for(const p of dots) {
104-
ctx.fillStyle = 'white';
105-
ctx.fillRect(p.x, p.y,2, 2);
106-
}
107-
}
108-
return true;
109-
}, 30);
110+
gotFirstGps = true;
111+
//setupFrameHandler();
110112
}
111113
});
114+
// arjs.startGps();
115+
arjs.fakeGps(-0.72, 51.05);
116+
}
112117

113-
container.appendChild(canvas);
114-
container.appendChild(view);
115-
arjs.fakeGps(origLon, origLat);
118+
function setupFrameHandler() {
119+
if(gotFirstGps) {
120+
onFrame( () => {
116121

117-
118-
119-
}).catch( error => alert(error) );
122+
ctx.clearRect(0, 0, canvas.width, canvas.height);
123+
ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
124+
const frame = ctx.getImageData(0, 0, canvas.width, canvas.height);
125+
const pose = alva.findCameraPose(frame);
126+
if(pose) {
127+
arCamView.updateCameraPose(pose);
128+
} else {
129+
arCamView.lostCamera();
130+
const dots = alva.getFramePoints();
131+
for(const p of dots) {
132+
ctx.fillStyle = 'white';
133+
ctx.fillRect(p.x, p.y,2, 2);
134+
}
135+
}
136+
return true;
137+
}, 30);
138+
} else {
139+
alert('Cannot start frame processing as no GPS location yet');
140+
}
141+
}
120142
}
121143

122144
main();

0 commit comments

Comments
 (0)