admin管理员组

文章数量:1296302

UPDATE: You can use onclick= function();in the HTML.

For example: <a-box onclick="myFunction()"></a-box>

I want to get click event of cursor with javascript from an A-frame element, like a box for example, how can I do it?

UPDATE: You can use onclick= function();in the HTML.

For example: <a-box onclick="myFunction()"></a-box>

I want to get click event of cursor with javascript from an A-frame element, like a box for example, how can I do it?

Share Improve this question edited Nov 15, 2016 at 9:43 gabor aron asked Nov 14, 2016 at 14:10 gabor arongabor aron 4102 gold badges4 silver badges19 bronze badges 1
  • Check aframe.io/docs/0.8.0/introduction/… – Jaider Commented Jan 20, 2019 at 11:15
Add a ment  | 

2 Answers 2

Reset to default 3

If you are using the cursor ponent:

box.addEventListener('click', function (evt) { // ... });

If you want to use the mouse cursor, try https://www.npmjs./package/aframe-mouse-cursor-ponent

You could create a custom ponent like this;

<script>
AFRAME.registerComponent('clickhandler', {
        schema: {
          txt: {default:'default'}
        },        
        init: function () {
          var data = this.data;
          var el = this.el;        
          el.addEventListener('click', function () {            
           console.log(data.txt);
          });        
        }
      });
</script>

<a-image src="img1.png" clickhandler="txt:image1"></a-image>
<a-box clickhandler="txt:box1"></a-box>

<a-entity cursor="rayOrigin:mouse"></a-entity>

More info here https://aframe.io/docs/1.2.0/core/ponent.html

本文标签: aframeHow to get click event with javascript from an Aframe elementStack Overflow