'Display distance to marker in AR.js
I'm trying to display the distance to text markers (underneath the markers) in AR.js; according to the docs, distanceMsg
is a custom attribute from gps-entity-place
, so I thought it could be retrieved with getAttribute
and then set with setAttribute
, but I'm having no success; besides, I'm just beginning to learn JS.
The following code produces a text marker with the word "null":
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="https://aframe.io/releases/1.1.0/aframe.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/aframe-look-at-component.min.js"></script>
<script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar-nft.js"></script>
<script src="https://raw.githack.com/donmccurdy/aframe-extras/master/dist/aframe-extras.loaders.min.js"></script>
<script>
window.onload = () => {
const distanceMsg = document.querySelector('[gps-entity-place]').getAttribute('distanceMsg');
document.querySelector('a-text').setAttribute('value', distanceMsg);
};
</script> </head>
<body style="margin: 0; overflow: hidden;">
<a-scene
renderer="logarithmicDepthBuffer: true;"
embedded
loading-screen="enabled: false;"
arjs="sourceType: webcam; debugUIEnabled: false;"
>
<a-text
look-at="[gps-camera]"
scale="50 50 50"
gps-entity-place="latitude: 18.45030; longitude: -96.96152;"
></a-text>
<a-entity
text="value: place1;"
look-at="[gps-camera]"
scale="35 35 35"
gps-entity-place="latitude: 18.45045; longitude: -96.96160;"
></a-entity>
<a-camera gps-camera rotation-reader></a-camera>
</a-scene>
</body>
</html>
Solution 1:[1]
Not a complete answer, but hopefully helps with several syntax aspects of your code - see inline comments below. Note that the distanceMsg attribute is added dynamically by AR.js (I don't know how to kick that off) So if you ask for a non-existent attribute you get null back. In the corrected example below I just set it to a fixed string for now.
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="https://aframe.io/releases/1.1.0/aframe.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/aframe-look-at-component.min.js"></script>
<script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar-nft.js"></script>
<script src="https://raw.githack.com/donmccurdy/aframe-extras/master/dist/aframe-extras.loaders.min.js"></script>
<script>
window.onload = () => {
var n = document.querySelector('a-text'); // <-- find first node of that type
console.log(n);
n.flushToDOM(); // debug aid: this will show the AFrame attribute values
console.log(n); // by default they are not written back to DOM because expensive
var distanceMsg = n.getAttribute('distanceMsg'); // <-- returns null, so for show
distanceMsg = "so far away"; // just use something else
n.setAttribute('value', distanceMsg); // <-- this is how you change the text to display
};
</script>
</head>
<body style="margin: 0; overflow: hidden;">
<a-scene
renderer="logarithmicDepthBuffer: true;"
embedded
loading-screen="enabled: false;"
arjs="sourceType: webcam; debugUIEnabled: false;"
>
<a-text
look-at="[gps-camera]"
scale="50 50 50"
gps-entity-place="latitude: 18.45030; longitude: -96.96152;"
value= "place1" // <--- this is how you set up the text to display
></a-text>
<a-camera gps-camera rotation-reader></a-camera>
</a-scene>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Robert |