What could be causing my Three.js code to fail during testing?

Recently, I decided to delve into the world of Three.js by following a thorough tutorial. While everything seemed perfectly fine in my code editor of choice (Visual Studio Code 2019), I encountered a frustrating issue when I attempted to run the code and nothing appeared on the page.

The code editor I utilized stored my HTML file on the desktop, and upon running it, all I could see was the navbar that I had meticulously programmed - nothing else rendered on the page.

Below is the complete Three.js code snippet:
<script src="three.js-dev/build/three.min.js"> </script>
        <script>
            var scene = new THREE.scene();
            var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight);

            var renderer = new THREE.WebGLRenderer({antialias: true});
            renderer.setSize(window.innerWidth, window.innerHeight);
            $('body').append(renderer.domElement);

            var geometry = new THREE.BoxGeometry(1, 1, 1);
            var material = new THREE.MeshBasicMaterial ({color: 0xff0000});
            var cube = new THREE.Mesh(geometry, material);
            scene.add(cube);

            cube.position.z = -5;
            var animate = function () {
                cube.rotation.x += 0.01;
                renderer.render(scene, camera);
                requestAnimationFrame(animate);
            };
            animate();

Here is the code that precedes the Three.js implementation:

<html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script 
        src="https://code.jquery.com/jquery-2.1.4.js"
        integrity="sha256-siFczlgw4jULnUICcdm9gjQPZkw/YPDqhQ9+nAOScE4="
        crossorigin="anonymous"></script>
        </head>
        <body>
        <div id="navbar"><span>Three.js Tut</span></div>

Excitedly, I anticipated a mesmerizing sight of a rotating red cube, however, much to my dismay, nothing materialized. Could there be an error that I am overlooking?

Answer №1

There are a few mistakes in your code that need to be corrected:

  • THREE.scene should actually be written as THREE.Scene
  • {antialias = true} needs to be changed to {antialias: true}
  • { color = 0xff0000 } should be { color: 0xff0000 }

Check out a live demo using your corrected code: https://jsfiddle.net/so736vxj/

By the way, if you are using VSCode, it's strange that these errors are not being highlighted. Both the last two syntax errors should be flagged as they are not valid JavaScript.

three.js R105

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

What is the method for automatically scrolling down while hovering over an image?

How can I make it so that when an image is hovered over, it automatically scrolls to the bottom of the image? I have a couple of questions: How can I ensure the image scrolls to the end when hovered over? Currently, when I hover over the image, it doe ...

What is the correct method for adding a button to a popup in Leaflet Draw?

I need a button within a popup that can perform an action on the attached layer. L.marker(coors[i], { icon }) .addTo(this.drawnItem) .bindPopup(this._getCustomIcon(mix)) .openPopup(); Here is my _getCustomIcon() function: ...

Using JavaScript Object Constructor to alter text color

Seeking some guidance as a newbie here on how to deal with a problem I'm currently tackling. I understand that using an object constructor may not be the most efficient way to handle this, but I'm eager to enhance my knowledge of objects. My quer ...

"Incorporate multiple data entries into a table with the help of Jquery

How can I store multiple values in a table row using jQuery or JavaScript when the values come from a database via Ajax? <html> <head> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th ...

Issue with this.setState() not updating value despite not being related to asynchronous updates

Just a quick note, this specific question does not involve any asynchronous update problem (at least, as far as I can tell). I currently have a class component with the following code snippet (simplified for clarity on the main issue): constructor(props ...

How can I update information based on the chosen option in a select dropdown using Vue?

I have a chart displayed in my user interface that needs to be updated based on the selected time period. By default, the chart displays data from the past year, but when the user selects "Month," it should show data from the past month. <div cla ...

Having trouble with ng-click not correctly updating values within ng-repeat

Here is the code snippet: <div ng-repeat="data in products"> <div class=edit ng-click="dataUI.showEdit = true; content = data;"> </div> <div ng-repeat="renew in data.renewed"> <div class=edit ng-click="dataUI ...

Animating the camera's movement around the globe using three.js and the power of tween.js

Looking for a way to smoothly rotate a camera around a globe to a new position upon pressing a button. I have confirmed the coordinates are correct with the following code snippet: camera.position.set(posX, posY, posZ); camera.lookAt(new THREE.Vector3(0, ...

Launch a dialogue box by clicking on the buttons corresponding to the article ID

I am currently in the process of setting up an admin panel with some quick actions for managing my articles, such as deleting them. When I click on the trash can icon, a dialog box should open and prompt me to confirm the deletion. This dialog box should b ...

Bizarre Object notation with JavaScript

While browsing through a Library called WebApp.net, I stumbled upon this interesting piece of code: var $h = { get HEAD() { return 0 }, get BACK() { return 1 }, get HOME() { return 2 }, get LEFT() { return 3 }, get RIGHT() { return 4 } ...

Steps for resolving the "endless redirection loop" issue in Sharepoint

I am currently learning Javascript and working on setting up a multi-language Sharepoint site. I am trying to implement a code into each page that checks the user's email and the language in the URL (Portuguese or Spanish) and then redirects according ...

Are there advantages to incorporating d3.js through npm in an Angular 2 TypeScript project?

Summary: It is recommended to install d3.js via npm along with the typings. The accepted answer provides more details on this issue. This question was asked during my initial stages of learning Angular. The npm process is commonly used for tree-shaking, pa ...

How can I show a legend entry for every column in Amcharts?

Take a look at this code snippet: http://jsfiddle.net/ouLed1fp/ How can I create a legend entry for each column in the chart? Also, is there a way to display the column names next to them, providing a clear legend? <div id="chartdiv" style="width: 10 ...

What is the reason behind Chrome's automatic scrolling to ensure the clicked element is fully contained?

Recently, I have observed that when performing ajax page updates (specifically appends to a block, like in "Show more comments" scenarios) Chrome seems to automatically scroll in order to keep the clicked element in view. What is causing this behavior? Wh ...

Every time the Select box choice is changed, Jade Pug will undergo a new iteration

Recently, I began using a Pug/Jade template to go through an object of events sent from an express app. Everything is working smoothly, but now I've added a select box with a dropdown populated by venues in the event object. I'm facing a seemingl ...

Storing input values in the state using Typescript by default

Upon launching, my activeField state is initially empty. However, when a user focuses on the field, it gets added to the state. I am encountering a warning in Typescript because when I attempt to update the selectionEnd of that field, it tells me: Property ...

Adjust the background color of a specific list item when hovering over another list item

My dilemma lies in my inadequate knowledge to solve this issue: I have a dropdown menu embedded within a website. (View screenshot here: [SCREENSHOT]) The desired outcome is for the background color of an icon on the main list to change when I navigate to ...

Appending a forward slash at the end of a URL seamlessly directs the user to a serendipitous webpage experience, while

I'm currently developing a project on this website: Interestingly, when you append a forward slash to the end of the URL, all the images mysteriously disappear. Consequently, I am unable to include Google AdWords tracking code at the end of any URLs: ...

Linking this to a function that takes in arguments

I am currently working with an event handler that calls a fat arrow function to execute a specific method. import React, { Component } from 'react'; class App extends Component { sayHi = msg => { console.log(msg); }; render() { ...

Exploring MongoDB's Aggregation Framework: Finding the Mean

Is there a way to use the Aggregation Framework in MongoDB to calculate the average price for a specific Model within a given date range? Model var PriceSchema = new Schema({ price: { type: Number, required: true }, date: { ...