Error Message in Three.js: "Function is not defined in Perspective Camera"

My program was functioning well, but now I'm encountering the "undefined is not a function" error within the three.js file. The problematic line is 10466 in the uncompressed library, specifically within the declaration of THREE.PerspectiveCamera. Where could the issue lie? Appreciate any help!

var assets = {};

var modelLoader = new THREE.JSONLoader(true);
modelLoader.load("assets/horse.js", onLoadedAssets);

var loading = setTimeout(function() {
    console.log("loading...");
}, 1000);

var scene, camera, renderer, controls;
var horse, box;

function init() {
    scene = new THREE.Scene();

    var w = window.innerWidth,
        h = window.innerHeight;

    camera = THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.y = 0;
    camera.target = new THREE.Vector3(0, 0, 0);

    var light1 = new THREE.DirectionalLight(0xefefff, 2);
    light1.position.set(1, 1, 1).normalize();
    scene.add(light1);

    var light2 = new THREE.DirectionalLight(0xffefef, 2);
    light2.position.set(-1, -1, -1).normalize();
    scene.add(light2);

    renderer = new THREE.WebGLRenderer({
        antialias: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    window.addEventListener('resize', onWindowResize, false);

    box = new Box(500, 500, 500);
    horse = new Horse();
}

function animate() {
    requestAnimationFrame(animate);
    render();
}


var prevTime = Date.now();

function render() {

    camera.position.x = 560 + Math.random() * 40;
    camera.position.z = 560 + Math.random() * 40;

    camera.lookAt(camera.target);

    if (horse.animation) {

        var time = Date.now();

        horse.animation.update(time - prevTime);

        prevTime = time;

    }

    renderer.render(scene, camera);

}

function onLoadedAssets(geometry) {

    clearTimeout(loading);
    console.log("loaded assets.")

    geometry.computeFaceNormals();
    geometry.computeVertexNormals();

    var material = new THREE.MeshLambertMaterial({
        color: 0x606060,
        morphTargets: true
    });

    var mesh = new THREE.Mesh(geometry, material);
    mesh.scale.set(1, 1, 1);

    var animation = new THREE.MorphAnimation(mesh);
    animation.play();
    animation.isPlaying = false;

    assets["horse"] = {
        mesh: mesh,
        animation: animation
    };

    init();
    animate();
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;

    camera.updateProjectionMatrix();

    renderer.setSize(w, h);
}

function Horse() {

    var self;

    self = assets["horse"];
    scene.add(self.mesh);

    self.startMoving = function() {
        self.animation.isPlaying = true;
    };

    self.stopMoving = function() {
        self.animation.isPlaying = false;
    };

    return self;
}

function Box(xidth, yidth, zidth) {
    var self = this;

    var geometry = new THREE.BoxGeometry(xidth, yidth, zidth);
    var material = new THREE.MeshBasicMaterial({
        color: 0x666666,
        opacity: 0.4,
        transparent: true
    });
    var cube = new THREE.Mesh(geometry, material);
    scene.add(cube);

    self.geometry = geometry;
    self.cube = cube;
}

Answer №1

It seems like the issue lies in this specific line of code

camera = THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);

Have you tried adding 'new' before THREE...?

camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn how to calculate the unique value count of a field across two MongoDB collections

In my case, I am dealing with two MongoDB collections: A and B. Each of them contains the field 'user'. I am interested in finding the number of unique users in A, B, as well as the combined collection (A + B). Here is a pseudo code example: A.u ...

What is the best way to retrieve data based on a condition using JavaScript within a React application?

I am struggling to load only a specific number of rows that meet a certain condition. Unfortunately, the current code is fetching all 25 rows and the condition is not being applied correctly. If anyone could provide assistance, it would be greatly apprec ...

What steps can be taken to prompt the layout to transition?

I have an element that sticks to the top based on the current scroll offset. The issue is that the layout doesn't update when there is space available after the stuck element. This creates a ghost gap where the stuck element used to be... http://fidd ...

Utilizing React for handling data exchange between child and parent components

I am still learning about React and material-ui, and I am exploring how to pass data from a child component to a parent component to update the parent state. Currently, when I try to update the state with a new date, it is being logged in the console but t ...

Retrieving JSON data for a Material-UI dialog box

Currently, I am working on processing a JSON file that contains detailed information. To enhance the appearance and functionality of the data, I am utilizing React with Material-UI. The JSON structure I am dealing with looks like this: [{"item":123456", ...

What could be causing the Angular router outlet to not route properly?

Check out this demo showcasing 2 outlets (Defined in app.module.ts): <router-outlet></router-outlet> <router-outlet name="b"></router-outlet> The specified routes are: const routes: Routes = [ { path: 'a', com ...

Retrieving an Element that Triggered an HTTP GET Request in Node.js with Express

In my Express front-end development project, I have a table with rows containing links that trigger GET requests. These requests are sent to the back-end, which is created using node.js, in order to retrieve files corresponding to each row. All links follo ...

Connect Angular Elements with Polymer 1.0

I have a unique custom element: <my-element options='{{myOptions}}'></my-element> When using this, the ready callback in the web component only receives {{myOptions}} for the options property. Despite trying various tools, none seem ...

Error: The function cannot be performed on _nextProps.children

I'm having trouble implementing react context with nextJS and I keep encountering this error: Server Error TypeError: _nextProps.children is not a function This is my code for _App.js: import Head from "next/head"; import Router from &q ...

Connecting a JavaScript script from my HTML file to Django's static files - here's how

I recently started a Django project with frontend code that wasn't initially written for Django. I am having trouble connecting this script: <script> document.body.appendChild(document.createElement('script')). src='js/ma ...

The concept of JavaScript context within unidentified functions

I am trying to assign a function as a property of each element in an array and then call it with different arguments. My initial approach was to use an anonymous function like this: for ( var i = 0; i < object_count; i++ ) { objects[i].callback = f ...

Extract particular information from the JSON reply

When working with a JSON response in JavaScript, I use the following code to convert it to a string: var myObject = JSON.stringify(data); Although this code successfully prints out the results, I am having trouble extracting specific data such as myObjec ...

Embedding external javascript within another javascript file

I have two pieces of code - code 1 and code 2. Code 1 is login.html which I am saving as login.js, and code 2 consists of some jQuery and JavaScript codes. My problem is how to insert the first code into the second code. Code 1: login.html (saved as logi ...

Preventing default form submission in jQuery: How to cancel it when a certain condition is met

I have a contact form where I validate the input values upon clicking on the submit button. If there is at least one empty input, I trigger an alert and prevent the form submission by using preventDefault. However, if all inputs are filled and submitted, t ...

pop-up window that shows chosen choices using javascript or ajax

I have a specific HTML code that allows users to select multiple options. I would like these selected options to be displayed in a popup or a div in real-time as the user makes their selections. I have attempted using a selectbox with checkboxes, but extra ...

Tips for utilizing nested filters to identify when there is a single list item and its child is specifically a <strong> tag

I am seeking to designate a specific class to the <ul> element only under certain conditions: it must have a single <li> which contains a solitary child, namely <strong>, without any additional text nodes or elements preceding or followin ...

Exploring elements with Watir WebDriver and ExtJS

Currently, I am performing an acceptance test using watir-webdriver with Ruby. I have a question regarding ExtJs support with Watir webdriver. Is it possible to locate elements that are dynamically generated by ExtJS? I have attempted the following: @brow ...

Switching columns to rows using JavaScript

I came across a solution on Stack Overflow about swapping rows with columns in a matrix using JavaScript. However, it didn't work for me (probably because I'm still learning). The data is structured in arrays like: id [1, 2, 3] caption [one, tw ...

Having trouble with CubeTextureLoader()? Perhaps I'm missing a step somewhere

Below is the code I am working with: var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); var renderer = new THREE.WebGLRenderer({antialias:true}); renderer.setSize(window.innerWid ...

Can the caller function's arguments be altered using Function.prototype.apply()?

function modifyValues(a,b){ console.log(arguments); //["oldValue","oldValue"] var newArguments = updateValues.apply(this,arguments); for (var i=0;i<arguments.length;i++){ arguments[i] = newArguments[i]; } console.log(arguments); // ...