What could be causing my canvas element to only display a blank black screen?

Setting up a 3d asset viewer in Three.js can be quite challenging, especially for someone who is new to JavaScript like myself. After following the advice to wrap my code within an 'init();' function, I encountered a new issue - a black screen instead of the expected 3d model.

I spent a significant amount of time troubleshooting various problems, such as the absence of the 'canvas' inside the 'container' div and the issues with the 'onWindowResize' function. Despite resolving all these issues and ensuring there are no errors in the code, the problem persists.

Although I understand the importance of keeping the code concise, I'm unable to pinpoint the exact source of the problem. Hence, I've included most of the code on the page:

<div id="container" ></div>
    <script>
    ...
    ...
    // Code where the issue might be originating
    ...
    ...
    </script>

Prior to encapsulating the code within the 'init();' function, everything was working perfectly fine. The expected outcome was to see a simple ship model floating in water, accompanied by a cloud skybox. The interactive controls were functioning, including auto-rotation.

However, the current state of the project is far from this ideal scenario. Despite successful obj loading, as indicated in the console log, the screen remains black with no visible content.

If anyone experienced with Three.js can assist me in resolving this issue, I would greatly appreciate it!

Answer №1

one line

start;

must be a function call

start();

Also, you may need to adjust the code below where you define the start function from

var start = function(){

Change it to

function start(){

The reason being that named functions are defined when the code is loaded, while variables var are created when the code is executed. So, in code like this

init();
start();

var start = function(){ ...

start does not actually exist at the point the code tries to call it. However, with this code

init();
start();

function start(){ ...

it does exist

You could also rearrange the code so, for example, define start before you use it, which should work.

var start = function(){ 
   ...
};

init();
start();

It also seems that some variables are declared inside init, which means they are not available to start. For example,

var renderer = new THREE.WebGLRenderer({antialias : true});

declares a new variable renderer that only init can see. If you want to set the renderer variable that is outside of init, change the code to

 renderer = new THREE.WebGLRenderer({antialias : true});

controls is never defined, so you may need to define it or comment it out

 controls.update();

to

 // controls.update();

Note: you might find these tutorials helpful. If you are new to JavaScript, it is recommended to spend time learning JavaScript.

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

How can I remove the back button that the Ionic framework adds when using $state.go('app.home') to navigate to a page?

I have an app with a sidebar menu. Currently, I am on the second page and I am calling a controller function that redirects me to the first page using: $state.go('app.home'); The issue I am facing is that on this page, a back button is displayed ...

Eliminate unnecessary CSS classes from libraries such as bootstrap when working on a React project

Our team is currently in the process of developing a React project that involves webpack and babel. Our goal is to automatically remove any unused CSS classes from CSS frameworks Bootstrap and AdminLTE 2, which are integral parts of our project. For this ...

How to emphasize a portion of an expression in AngularJS using bold formatting

I have a specific goal in mind: to emphasize part of an angular expression by making it bold. To achieve this, I am working with an object obj which needs to be converted into a string str. obj = $scope.gridOptions1.api.getFilterModel(); for (var pr ...

Troublesome CSS conflicts arise when using minified assets with AngularJS and Webpack

After transitioning my Angular project to the Webpack build system, I encountered issues with Angular Dependency Injection in the JS source code. Surprisingly, now I am facing JS errors that are targeting CSS files, leaving me completely bewildered about w ...

utilizing a Bootstrap modal element throughout multiple HTML documents

I have a bootstrap modal dialog div that I want to use in all 4 html pages of my web application without repeating the code. I have a common JavaScript file for this purpose. What is the best way to achieve this? <!-- Modal --> <div class="modal ...

Ways to determine the types of props received by a function when the arguments vary for each scenario?

I have a specialized component that handles the majority of tasks for a specific operation. This component needs to invoke the onSubmit function received through props, depending on the type of the calling component. Below is an example code snippet show ...

HTML5 database error: Uncaught TypeError due to illegal invocation

When I test the code below in Chrome, I encounter an error: var db = openDatabase('foo', 1, 'foo', 1111); var sql = function(callback){ db.transaction(function(tx){ callback(tx.executeSql); }); }; sql(function(query){ ...

The Threejs collada 3D model is experiencing rendering issues at certain viewing angles

My 3D product preview using a Collada model with canvas as a texture is generally working well, but I am facing rendering issues with the corner parts from certain angles. I have included screenshots of the canvas, 3D model, and the DAE model. Could the ...

The absence of the iframe in ie8 is causing problems that cannot be fixed with relative positioning

On a website, I am integrating an external 2-factor authentication solution called Duo Web using their PHP and Javascript. It works smoothly on all browsers except for IE8. When the user passes the initial login screen, the 2FA login page loads, but the if ...

Unable to fetch Rails response through Ajax

Whenever I make a post request from my phonegap app (using ajax in javascript) to my rails server, the post goes through successfully. However, I do not receive any response from the server which ultimately leads to failure. It's puzzling why I'm ...

The challenges encountered with JSONP/Ajax Script - displaying 'Undefined'

I need help retrieving data from a webserver that I don't have control over. I've searched online but haven't found a solution yet. I've tried various codes, with and without DataTables. If anyone could provide guidance on where to go ...

Error: Axios header not refreshing automatically in React. User must manually refresh the page

After logging in, I want to update the JWT token in the header before redirecting to the home page. Login.tsx ... const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { event.preventDefault(); const data = new FormData(event.curr ...

When clicking on an element, all elements will change, not just the specific one

I have been experimenting with basic Jquery and noticed something peculiar. On my page, I have an h1 heading and a generic link. When I click the link, I expect the text to change to "This text has now changed". However, what actually happens is either the ...

Javascript enables dynamic field addition to tables

I have JSON data that needs to be displayed in an HTML table. To input the values, I have individual fields for firstname, lastname, email, and phone number, along with an "Add Row" button. When I click the "Add Row" button, I want the entered values to b ...

Rotate an object upwards in three.js in order to achieve dynamic movement and enhance

I'm struggling with 3D calculations and could really use some assistance. In my scene, I have a sphere representing the earth and I'm using OrbitControl to "rotate" it (although in reality, OrbitControl rotates the camera). I need a function, s ...

Extracting JavaScript variable data to PHP using Ajax technology

I've been trying to save latitude and longitude values in PHP variables, but I'm having trouble. Here is the code I'm using. Can anyone please help me figure out why these values are not being stored in PHP variables: Code: <!DOCTYPE h ...

Rotation of a point cloud geometry within three.js

After creating a geometry using specific x, y, z positions, I need to rotate this geometry around its own axis similar to how the earth rotates. I want the geometry to move, not the camera. Can anyone provide assistance? Here is the current code snippet: ...

Adjusting the timing of a scheduled meeting

Is there a way for me to update the time of a Subject within my service? I'm considering abstracting this function into a service: date: Date; setTime(hours: number, mins: number, secs: number): void { this.date.setHours(hours); this.date.s ...

The BufferGeometry fails to refresh its data even when the needsupdate flag is toggled to true

I am facing difficulties with updating the position of point geometry in Three JS. I attempted to move individual points and eventually reach a specific location. To maintain organization, I constructed a class constructor. It renders and rotates perfectl ...

Delete auto-generated list using handlebars JS

I have successfully created a dynamic list using Handlebars.js and its template. However, I am now facing confusion on how to remove or delete items from the list using a function or specific code. As I am new to Handlebars, I would appreciate any help. ...