是否可以将 Mutation Observer 与三个 js 元素 - A 框架一起使用?

Is it possible to use Mutation Observer with a Three js element - A-Frame?

export class ColliderComponent {

  constructor() {
    this.observer = this.mutationObserver();      
    this.aframe(); 
  }


  //Registers the AFRAME component.
  aframe(){
    const __this = this; 
    AFRAME.registerComponent('collider', {
      schema: {

      },
      init: function () {
          console.log("The element to be observed is:",this.el);

          __this.observer.observe(this.el, {characterData:true, subtree:true, attributes: true, attributeFilter: ['position'], childList : true}); 
      },

      tick : function(){
        console.log(this.el.getObject3D('mesh').position);
      }
    });

  }

  private tick(){

  }

  private mutationObserver() : MutationObserver{
    return new MutationObserver(mutations => {
      mutations.forEach(mutation => {
        console.log("Changed position");
      });
    });
  }

}

我正在努力创建一个简单的对撞机。我将跟踪具有 "collider" 分量的元素并使用 intersectsBox 检查它们是否相交。不幸的是,我似乎无法让 MutationObserver 工作。我宁愿使用这种方法而不是 tick,因为它将开始每帧执行它,而不是在元素移动时执行。

有什么建议吗?

您可以使用

el.addEventListener('componentchanged', function (evt) {
  if (evt.detail.name === 'position') {

  }
});

但是 polling/ticking 通过 tick 是同步的,可能仍然是一个不错的方法。