在这个例子中,我们将一个超链接绑定到一个坐标上,当相机到达该坐标时,这个超链接将会在场景中显示。这个超链接包括一个图像和链接,当用户点击图像时,将打开链接。
document.querySelector('a-scene').addEventListener('gps-entity-place-loaded', function () { console.log('GPS entity placed'); // Trigger hyperlink on position found window.addEventListener('deviceorientation', function (event) { if (!event.alpha) { return; } // Get coordinates var curLat = PositionService.getPositionLat(); var curLng = PositionService.getPositionLng(); // Check distance between current position and clickable position var metersFrom = DistanceService.getDistanceFromLatLonInMeters(curLat, curLng, latitude, longitude)
// Check if user is close enough and if content has already been loaded
if (metersFrom < range) {
// If the link has not already been clicked, add a class to show the link.
if (!hasClass(link, 'clicked')) {
link.classList.add('clicked');
}
}
else {
// If the user moves out of the range, remove the clicked class.
if (hasClass(link, 'clicked')) {
link.classList.remove('clicked');
}
}
});
});
在这个代码片段中,我们使用三个自定义服务(PositionService, DistanceService和hasClasServices)来执行距离计算、添加和删除CSS类。这个事件监听器在当我们的GPS实体在AR世界中加载后被触发,并且每当设
下一篇:AR.js标记训练不正确