gabor aron
gabor aron

Reputation: 410

How to get click event with javascript from an A-frame element

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?

Upvotes: 5

Views: 19516

Answers (2)

bartburkhardt
bartburkhardt

Reputation: 7768

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

Upvotes: 1

ngokevin
ngokevin

Reputation: 13233

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

Upvotes: 3

Related Questions