5

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?

gabor aron
  • 390
  • 2
  • 3
  • 15

2 Answers2

3

If you are using the cursor component:

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

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

ngokevin
  • 12,980
  • 2
  • 38
  • 84
1

You could create a custom component 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/component.html

bartburkhardt
  • 7,498
  • 1
  • 18
  • 14