I keep encountering the
Uncaught TypeError: undefined is not a function
error while working with Three.js.
This error is specifically being shown at the line where I'm creating a THREE.PerspectiveCamera
.
Here's the script:
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);
});
Could the issue be related to how I am declaring the camera? I've gone through the documentation provided by three.js and my code seems quite similar to their example. Any suggestions would be greatly appreciated.
UPDATE: Initially, I was using a local copy of Three.js which led to the PerspectiveCamera error. Upon switching it with the external link , the PerspectiveCamera error disappeared but now a new error has surfaced within the Three.js script. The error message reads
Uncaught TypeError: Cannot read property 'x' of undefined
on line 337 of the Three.js script.
Thank you.