javascript Three.js - 未捕获的类型错误:未定义不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15471879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Three.js - Uncaught TypeError: undefined is not a function
提问by rahules
I'm getting the Uncaught TypeError: undefined is not a function
while using Three.js.
The error is being shown for the line where I'm creating a THREE.PerspectiveCamera
.
The script is
我正在Uncaught TypeError: undefined is not a function
使用 Three.js。错误显示在我创建THREE.PerspectiveCamera
. 脚本是
window.requestAnimFrame = (function(callback){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 60);
};
})();
function animate(lastTime, angularSpeed, three){
// update
var date = new Date();
var time = date.getTime();
lastTime = time;
// render
three.renderer.render(three.scene, three.camera);
// request new frame
requestAnimFrame(function(){
animate(lastTime, angularSpeed, three);
});
}
$(window).bind('load',function(){
var angularSpeed = 0.2; // revolutions per second
var lastTime = 0;
$container = $("#container");
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
$container.append(renderer.domElement);
// camera - Uncaught Type Error on the below line
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.y = -450;
camera.position.z = 400;
camera.rotation.x = 45 * (Math.PI / 180);
// scene
var scene = new THREE.Scene();
var material = new THREE.LineBasicMaterial({
color: 0x0000ff,
});
var geometry = new THREE.Geometry();
for(var i=0;i<100;i++){
geometry.vertices.push(new THREE.Vector3(i-100,i-100,i-100));
geometry.vertices.push(new THREE.Vector3(i+100,i+100,i+100));
var line = new Three.Line(geometry,material);
scene.add(line);
geometry=new THREE.Geometry();
}
// create wrapper object that contains three.js objects
var three = {
renderer: renderer,
camera: camera,
scene: scene,
};
animate(lastTime, angularSpeed, three);
});
Is this because the way I'm declaring the camera is wrong? I checked the three.js documentation and the example give there is basically the same. So I'm stuck on what to do.
这是因为我声明相机的方式是错误的吗?我查看了three.js 文档,那里给出的示例基本相同。所以我被困在做什么上。
UPDATE:
I was using a local copy of Three.js when I encountered the last error. I switched it with the link http://www.html5canvastutorials.com/libraries/Three.js.
Now, the PerspectiveCamera error is gone, but it produces a new error inside the Three.js script. The error is Uncaught TypeError: Cannot read property 'x' of undefined
on line 337 of the Three.js script
更新:当我遇到最后一个错误时,我正在使用 Three.js 的本地副本。我用链接http://www.html5canvastutorials.com/libraries/Three.js切换了它。现在,PerspectiveCamera 错误消失了,但它在 Three.js 脚本中产生了一个新错误。错误Uncaught TypeError: Cannot read property 'x' of undefined
在 Three.js 脚本的第 337 行
Thanks.
谢谢。
回答by Nashenas
The problem with the local copy is you used the unbuilt Three.js file (that would be src/Three.js). The one you want is either build/three.js or build/three.min.js. I copied that into my project, and changed the reference to it in the script tag's src attribute, and it all started working.
本地副本的问题在于您使用了未构建的 Three.js 文件(即 src/Three.js)。您想要的是 build/three.js 或 build/three.min.js。我将它复制到我的项目中,并在脚本标记的 src 属性中更改了对它的引用,然后一切都开始工作了。
In short:
简而言之:
mrdoob-three.js-2524525(or some other number here)
|
+----build
| |
| +--three.js <-- YES, CORRECT FILE
| +--three.min.js <-- YES
| ...
|
+----src
... |
+--Three.js <-- NO, PREBUILT FILE THAT ONLY CONTAINS CONSTANTS
...