【问题标题】:Display distance to marker in AR.js在 AR.js 中显示到标记的距离
【发布时间】:2023-12-24 20:44:01
【问题描述】:

我正在尝试在 AR.js 中显示到文本标记(在标记下方)的距离;根据文档,distanceMsg 是来自gps-entity-place 的自定义属性,所以我认为可以使用getAttribute 检索它,然后使用setAttribute 进行设置,但我没有成功;另外,我刚开始学JS。

以下代码生成一个带有单词“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/aframe-look-at-component@0.8.0/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>

【问题讨论】:

    标签: javascript gps ar.js


    【解决方案1】:

    不是一个完整的答案,但希望对您代码的几个语法方面有所帮助 - 请参阅下面的内联 cmets。 请注意,distanceMsg 属性是由 AR.js 动态添加的(我不知道如何启动它)所以如果你要求一个不存在的属性,你会得到 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/aframe-look-at-component@0.8.0/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>
    

    【讨论】: