how separate the mouse click event and the courser

2019-06-06 12:38发布

问题:

I'm creating a VR Editor using AFRAME... I need to create an event when the user clicks on an element by mouse and to do deferent action when clicking by Aframe camera cursor.

I found the <a-scene cursor="rayOrigin: mouse"> but this would do the same click event on both mouse and cursor click. is that possible in AFRAME?

回答1:

Discriminate by the cursor element:

AFRAME.registerComponent('on-click', {
    init: function () {
      var self = this;
      this.el.addEventListener('click', function (evt) {
        if (self.el.sceneEl === evt.detail.cursorEl) {
          console.log("MOUSE");
        } else {
          console.log("CURSOR");
        }
      });
    }
  });

Example: https://glitch.com/edit/#!/winter-sagittarius



标签: aframe