ember js中的three.js动画

时间:2016-09-12 20:47:26

标签: ember.js three.js ember-controllers

我应该如何为ember js中的动画创建一个three.js的渲染循环。我到现在为止的代码是

import Ember from 'ember';

export default Ember.Controller.extend({
  scene: new THREE.Scene(),
  camera: new THREE.PerspectiveCamera(75,
    window.innerWidth/window.innerHeight,1,500),
  renderer: new THREE.WebGLRenderer(),
  init: function() {
    this.renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(this.renderer.domElement);
    this.renderPreloader();
  },
  renderPreloader:function() {
    var geometry = new THREE.BoxGeometry( 1, 1, 1 );
    var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
    var cube = new THREE.Mesh(geometry,material);
    this.scene.add(cube);
    this.camera.position.set(2,2,2);
    this.camera.lookAt(cube.position);
    this.renderer.render(this.scene,this.camera);
    this.renderLoop();
  },
  renderLoop: function() {
    requestAnimationFrame(this.renderLoop);
    this.camera.position.x = this.camera.position.x+0.01;
    this.renderer.render(this.scene,this.camera);
  }
});

我得到的错误this未在requestAnimationFrame定义。通过这个方法的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

要正确传递范围,请执行以下操作:

requestAnimationFrame(this.renderLoop.bind(this));

requestAnimationFrame(() => {this.renderLoop()});