简体   繁体   中英

How to add a finger touch event listener to a text (without circular cursor) in AR.js/ AFrame?

I am building an AR App and I want to navigate to a URL once the user touches a 'a-text' (Hello World text) on phone display.

How can I achieve this?

Code is here https://glitch.com/edit/#!/tide-jelly-jackal

First of all, you should update your a-frame version. Took me like a while, why my code doesn't work on yours.

anyway, this is the working code

<!DOCTYPE html>
<script src="https://aframe.io/releases/0.9.2/aframe.min.js"></script>
<script src="https://cdn.rawgit.com/jeromeetienne/AR.js/1.7.2/aframe/build/aframe-ar.js"></script>
<script>
AFRAME.registerComponent('for_a_marker', {
    init: function () {
        var text = document.querySelector('#text');
        text.addEventListener("click", function (evt) { //or you could do "mouseenter" [event](https://aframe.io/docs/0.9.0/components/cursor.html#events)
            alert("do your stuff");
        });
    }
});
</script>
<body style='margin : 0px; overflow: hidden; font-family: Monospace;'>
    <a-scene embedded arjs='debugUIEnabled: false; sourceType: webcam' vr-mode-ui='enabled: false'>
        <a-entity cursor="rayOrigin: mouse" raycaster="objects: .intersectable; useWorldCoordinates: true;"></a-entity>
        <a-marker id="marker" preset="hiro" emitevents="true" for_a_marker> 
            <a-text id="text" class="intersectable" scale="2 2 2" position="0 0 0" rotation="270 0 0" value="text" color="red"></a-text>   
        </a-marker>
        <a-entity camera></a-entity>
    </a-scene>
</body>
</html>

i am not really an expert and still learning too. You could experiment yourself too. But take a note to this:

  1. if you don't use AFRAME.registerComponent part, var text will null
  2. <a-entity cursor ... .intersectable ...> make sure to put the .intersectable to your <a-text class="intersectable"> . This is the one who do the magic, for more detail check this documentation: a-frame 0.9.0 raycaster

Note: there is issue about the click event not at the expected spot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM