在 AR.js 中显示到标记的距离

问题描述

我正在尝试在 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>

解决方法

不是一个完整的答案,但希望对您的代码的几个语法方面有所帮助 - 请参阅下面的内联注释。 请注意, distanceMsg 属性是由 AR.js 动态添加的(我不知道如何开始)所以如果你要求一个不存在的属性,你会得到空值。在下面更正的示例中,我暂时将其设置为固定字符串。

    <!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>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...