js中的事件监听器

js中的事件监听器

本文介绍了ar.js中的事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个涉及ar.js的项目,该项目显示3d对象和文本,以在移动设备和笔记本电脑上教孩子们学习字母表.我试图添加一个事件监听器作为额外的功能,以使孩子们更多地进行互动.我的目标是单击/触摸显示的模型,它将放大或更改颜色或旋转.附件中找到我的代码.希望你能解决我的问题.

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代码

<!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(事件处理程序的文件)

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);
            }
        });
}});

推荐答案

我认为主要问题是 a-frame版本1.0.X 的问题.出于某些原因,如果我使用 addEventListerner('click')无效.

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>

这篇关于ar.js中的事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:50