如何在VueJS中使用ThreeJS

时间:2019-03-18 14:20:46

标签: vue.js three.js

我想使用threeJS,所以我用npm我的三个--save安装它。我遵循threeJS文档中的基本教程,但出现错误。

  mounted () {
    this.initThree();
  },
  methods: {
    initThree() {
      this.scene = new THREE.Scene();
      this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

      this.renderer = new THREE.WebGLRenderer( { canvas: document.getElementById( "background" ), alpha: true, antialias: true } );
      this.renderer.setSize( window.innerWidth, window.innerHeight );

      let geometry = new THREE.BoxGeometry( 1, 1, 1 );
      let material = new THREE.MeshBasicMaterial( { color: 0x0000ff } );
      this.cube = new THREE.Mesh( geometry, material );
      this.scene.add( this.cube );

      this.camera.position.z = 5;
      this.animate()

    },

    animate() {

      this.cube.rotation.x += 0.01;
      this.renderer.render( this.scene, this.camera );
      requestAnimationFrame( this.animate() );

    }
  }
}

Error

1 个答案:

答案 0 :(得分:4)

您的问题与VueJS / ThreeJs无关,您只需修复此行:

requestAnimationFrame( this.animate() );

应该是:

requestAnimationFrame(this.animate)

请详细了解回调:https://developer.mozilla.org/en-US/docs/Glossary/Callback_function