【问题标题】:ThreeJS as AngularJS Scope Object - requestAnimationFrame doesn't recognize callbackThreeJS 作为 AngularJS 范围对象 - requestAnimationFrame 无法识别回调
【发布时间】:2015-04-17 08:18:34
【问题描述】:

早上,

我正在做一些实验来修改我的 JS 知识,弄清楚 AngularJS 可以做什么/是为了什么,并掌握 ThreeJS - 通常我用 C++ 编写 OpenGL/MFC,但我写了一点 php/ js/html5 过去。

我正在尝试将一些 ThreeJS 包装在 AngularJS 控制器的 $scope 对象中。

Chrome JS 调试器中的错误是

'Uncaught TypeError: Failed to execute 'requestAnimationFrame' on 'Window': The callback provided as parameter 1 is not a function'

这是一些代码。

<script type="text/javascript">
    var appMain = angular.module("myApp", []);

    appMain.controller("myCtrl", function($scope) {

        // WebGL via THREEJS
        $scope.threejs = {
            width:640,
            height:480,
            scene: null,
            camera: null,
            renderer: null,
            box:null,
            initialise: function()
            {
                this.scene = new THREE.Scene();
                this.camera = new THREE.PerspectiveCamera(75, this.width/this.height, 0.1, 100);
                this.renderer = new THREE.WebGLRenderer();

                this.renderer.setSize(this.width, this.height);

                document.body.appendChild(this.renderer.domElement);

                // Create a Spinning Cube
                var geom = new THREE.BoxGeometry(1,1,1);
                var material = new THREE.MeshLambertMaterial({color:'blue'});

                this.box = new THREE.Mesh(geom, material);

                this.scene.add(this.box);

                this.camera.position.z = 5;
            },
            render: function()
            {
                requestAnimationFrame(this.render);

                this.box.rotation.x += 0.1;
                this.box.rotation.y += 0.1;

                this.renderer.render(this.scene, this.camera);
            }
        };

        $scope.threejs.initialise();
        $scope.threejs.render();
    });



</script>

..我得到一个静态的、非旋转的立方体 - 即它只渲染一次。

我认为这个错误更多地与我对 Javascript 工作原理的误解有关,而不是与其他任何事情有关。

请帮忙!

【问题讨论】:

    标签: javascript angularjs three.js angularjs-scope requestanimationframe


    【解决方案1】:

    One of the related questions on the panel on the right suggested:

    requestAnimationFrame(this.render.bind(this));

    现在我的立方体旋转了。我很高兴。现在我将围绕bind()做一些研究。

    【讨论】:

    • 如果您使用的是ES2015,那么只需使用箭头语法包装您的渲染/动画/whateveryoucallit 函数:() =&gt; animate
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多