简体   繁体   English

是否可以将Mutation Observer与Three js元素-A-Frame一起使用?

[英]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");
      });
    });
  }

}

I'm working on creating a simple collider. 我正在创建一个简单的对撞机。 I'm going to track the elements that have the "collider" component and check if they're intersecting using intersectsBox . 我将跟踪具有“对撞机”组件的元素,并使用intersectsBox检查它们是否intersectsBox Unfortunately, I can't seem to make the MutationObserver to work. 不幸的是,我似乎无法使MutationObserver正常工作。 I'd rather use this approach instead of the tick, because it will start executing it per frame, instead when the elements move. 我宁愿使用这种方法而不是滴答声,因为它将开始每帧执行一次,而不是在元素移动时开始执行。

Any suggestions? 有什么建议么?

You can use 您可以使用

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

  }
});

But polling/ticking via tick is synchronous and probably still not a bad way to go. 但是通过刻度线进行轮询/计时是同步的,可能仍然不是一个坏方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM