Event listeners in ar.js

China☆狼群 提交于 2021-01-19 10:33:18

问题


I am developing a project which involves ar.js that is displaying 3d objects and text to teach children the alphabet on both mobile devices and laptops. I was trying to add an event listener as an extra to make the children interact more. My target is to click/touch on the model displayed and it will enlarge or change color or rotation. Attached find my code. Hopefully you could solve my issue.

HTML Code

<!DOCTYPE html>
<html>
    <head>
<script src = "https://aframe.io/releases/1.0.3/aframe.min.js"></script>   
        <script src = "https://cdn.rawgit.com/jeromeetienne/AR.js/1.6.0/aframe/build/aframe-ar.js"></script> 
        <script src = "event.js"></script>
        <script src="https://rawgit.com/donmccurdy/aframe-extras/master/dist/aframe-extras.loaders.min.js"></script>
     </head>   
    <body>
<a-scene embedded arjs = 'sourceType: webcam; debugUIEnabled:false;'>
<a-assets>
                <a-asset-item id = "apple" src = "apple/scene.gltf"></a-asset-item>
        </a-assets>
 <a-marker id = "appleM" type = "pattern" url = "Asset/pattern-apple.patt"
        markerhandler emitevents = "true" cursor="rayOrigin: mouse">

            <a-entity id = "animatedApple" gltf-model = "#apple" position = "0 -1 0" scale = ".05, .05, .05"></a-entity>

            <a-text value="A for Apple"  color = "purple" position = "-1.3 1 0" scale = '2, 2, 2'></a-text>
</a-marker>
<a-entity camera></a-entity>
      </a-scene>
    </body>
</html>

event.js (file for the event handlers)

init: function() {
        const animatedMarker = document.querySelector('#appleM');
        const aEntity = document.querySelector('#animatedApple');

        // every click, we make our model grow in size :)
        animatedMarker.addEventListener('click', function(ev, target){
            const intersectedElement = ev && ev.detail && ev.detail.intersectedEl;
            if (aEntity && intersectedElement === aEntity) {
                const scale = aEntity.getAttribute('scale');
                Object.keys(scale).forEach((key) => scale[key] = scale[key] + 1);
                aEntity.setAttribute('scale', scale);
            }
        });
}});


回答1:


I think the main issue is something with a-frame version 1.0.X. For some reason the addEventListerner('click') won't work if I use that.

Below is my code, you click your model and it should increase the scale. Note: there is issue about the click event not at the expected spot.

<!DOCTYPE html>
<html>
<head>
    <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>
    <!-- IDK Why If I Use This, The addEventListener('click') won't work-->
    <!--     <script src = "https://aframe.io/releases/1.0.3/aframe.min.js"></script>    -->
    <!--     <script src = "https://cdn.rawgit.com/jeromeetienne/AR.js/1.6.0/aframe/build/aframe-ar.js"></script>  -->
    <script>

        AFRAME.registerComponent('button', {
            init: function() {

                const gltf = document.querySelector('#animatedApple');
                var x = gltf.getAttribute('scale').x;
                var y = gltf.getAttribute('scale').y;
                var z = gltf.getAttribute('scale').z;

                // every click, we make our model grow in size :)
                gltf.addEventListener('click', function(ev, target){
                    console.log(gltf.getAttribute('scale'));
                    gltf.setAttribute('scale', x + " " + y + " "+ z);
                    x += 0.1;
                    y += 0.1;
                    z += 0.1;
                });
            }
        });
    </script>
    <script src="https://rawgit.com/donmccurdy/aframe-extras/master/dist/aframe-extras.loaders.min.js"></script>
</head>   
<body>
    <a-scene embedded arjs = 'sourceType: webcam; debugUIEnabled:false;'>
    <a-assets>
        <a-asset-item id = "apple" src = "apple/scene.gltf"></a-asset-item> <!-- change to your assets -->
    </a-assets>
    <a-marker id = "appleM" preset="hiro" emitevents="true" button> <!-- change to your marker, i use default hiro.jpg -->
        <a-entity cursor="rayOrigin: mouse" raycaster="objects: .clickable; useWorldCoordinates: true;"></a-entity> <!-- important for addEventListener('click'). Notice the '.clickable'-->
        <a-entity class="clickable" id ="animatedApple" gltf-model = "#apple" position = "0 0 0" rotation="270 0 0" scale = "0.5 0.5 0.5"></a-entity> <!-- Notice the '.clickable'-->

        <a-text id="aText" value="A for Apple"  color = "purple" position = "0 0 0" rotation="270 0 0" scale = "2 2 2"></a-text>
    </a-marker>
    <a-entity camera></a-entity>
    </a-scene>
</body>
</html>


来源:https://stackoverflow.com/questions/60935284/event-listeners-in-ar-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!