What could be preventing the spinning cube from rendering in three.js?

While there are no errors showing up, the result is just a black screen. Due to the small size of the HTML and CSS code snippets, I suspect the issue lies within the JavaScript.

// Creating a three.js scene: a 3D environment for objects
const scene = new THREE.Scene();

const camera = new THREE.PerspectiveCamera(
  60,
  window.innerWidth / window.innerHeight,
  1,
 10000
);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xaaaaaa, 1);

document.body.appendChild(renderer.domElement);

const cube = {
 geometry: new THREE.BoxGeometry(1, 1, 1),
 material: new THREE.MeshBasicMaterial({ color: 0x00ff00 })
};

cube.mesh = new THREE.Mesh(cube.geometry, cube.material);

scene.add(cube.mesh);

camera.position.z = 5;

function render() {
 renderer.render(scene, camera);

 cube.mesh.rotation.x += 0.08;
 cube.mesh.rotation.y -= 0.05;

 requestAnimationFrame(render);
}
html, body {
 overflow: hidden; 
 user-select: none; 
 padding: 0; 
 margin: 0; 
}

canvas { 
 width: 100%; 
 height: 100%; 
 padding: 0; 
}
<!DOCTYPE html> <html> 
 <head> 
 
 <meta charset="utf-8"> 
 <meta name="viewport" content="width=device-width"> 
 
 <title>Three.js app</title> 
 <link href="style.css" rel="stylesheet" type="text/css" /> 
 </head> 
  
 <body> 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/104/three.min.js"></script> 
 <script src="script.js"></script> 
 </body> 
 </html>

Answer №1

Initiate the animation by executing the render function:

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
  60,
  window.innerWidth / window.innerHeight,
  1,
  10000
);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xaaaaaa, 1);

document.body.appendChild(renderer.domElement);

const cube = {
  geometry: new THREE.BoxGeometry(1, 1, 1),
  material: new THREE.MeshBasicMaterial({ color: 0x00ff00 })
};

cube.mesh = new THREE.Mesh(cube.geometry, cube.material);
scene.add(cube.mesh);
camera.position.z = 5;

function render() {
    renderer.render(scene, camera);
    cube.mesh.rotation.x += 0.08;
    cube.mesh.rotation.y -= 0.05;
    requestAnimationFrame(render);
}
render();                             // <--- 
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/104/three.min.js"></script>

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

Expanding the Reach of HTML Documents

I'm currently working on a project using HTML files (no JSP or any other client-side technology) and utilizing JQuery for scripting. Within my tables, columns, and fields, I have a lot of text content. How can I implement internationalization on my we ...

What causes a ReferenceError when attempting to retrieve the class name of a React component?

Take a look at my React component located in index.js: import React from 'react' import ReactDOM from 'react-dom' class App extends React.Component { render() { return ( <div className="App"> <h1>Hello, ...

Conceal the pseudo element when clicked

Running a WordPress site, I have a button on the home page that is set to load more posts when clicked. I wanted to add a touch of design by including an upside-down caret next to the text "load more posts" using pseudo-elements. While I successfully achie ...

javascript array push not functioning as expected

I have written some JavaScript code that is supposed to add another element with a value of 1 when I click a button. However, currently it is resetting the array so only one element gets added each time. How can I fix this issue? var x = document.getElem ...

Showing particular URL text upon opening a new window using JavaScript

I've encountered an intriguing scenario. In my application, there's a feature that triggers a new window/tab to open when a button is clicked. Upon opening, a predefined HTML page is shown to the user with a specific URL set. I'm curious abo ...

What steps can be taken to resolve this issue?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ut ...

Preventing text wrapping in Boostrap 4 tabs - what's the solution?

Currently utilizing Boostrap 4 for a simple 4 tab horizontal navigation. The desired display on big screens is: "Some text [font-awesome-icon]" x 4 tabs. For smaller screens, it should show as: "[font-awesome-icon]". While the display works, the font-aweso ...

Steer clear of using self=this within JavaScript functions

Is there a way to eliminate the need for declaring const self = this in JavaScript/Node.js for each type of function? function Server(config) { const self = this; this.config = config; setTimeout(function() { console.log(self.config); ...

Is it necessary to have height:0 and width:0 when display:none is already applied?

Is it necessary to include height:0 and width:0 when applying display:none to a class? I've noticed these properties being used in certain instances, could they potentially resolve any issues with compatibility in older browsers? ...

Tips for managing a stored data reservoir with react-query

I am looking to develop a unique custom hook that can effectively manage a cache and fetch only new items. Here is the expected behavior: Upon initial request for [1, 2, 3, 4, 5], it should fetch all [1, 2, 3, 4, 5] as the cache is empty. If a request is ...

How can I extract data from the 'ngx-quill' editor when integrating it with a FormBuilder in Angular?

After implementing the 'ngx-quill' editor package in my Angular 15 project, I encountered an issue where the value of the content form control was returning 'null' upon form submission using FormBuilder. Despite entering text such as he ...

What is the best way to use jQuery to emphasize specific choices within an HTML select element?

Seeking help with jQuery and RegEx in JavaScript for selecting specific options in an HTML select list. var ddl = $($get('<%= someddl.ClientID %>')); Is there a way to utilize the .each() function for this task? For Instance: <select i ...

Reverse the impact of the preceding CSS directive

In this screenshot example, the padding at the bottom in Material-UI has been increased from 16px to 24px using the CSS rule below: .MuiCardContent-root:last-child { padding-bottom: 24px; } https://i.sstatic.net/IWaIu.png I want to revert it back to ...

Fixed css footer with full height content container

For my website's sticky footer, I followed this technique: I wanted to add a border around the entire site that also encompasses the footer, but ran into an issue where it did not extend around the page properly. Here is what happened: https://i.ssta ...

Utilizing lazy evaluation, multiple functions are triggered by ng-click in succession

Successfully disabled ngClick on an element when the scope variable (notInProgress) is set to true as shown below: <a data-ng-click="notInProgress || $ctrl.setTab('renewal');</a> Now, I want to include additional functions to be execut ...

Text in Bootstrap Carousel shifting during transition to active item

I'm encountering a problem similar to the one discussed in this thread about Bootstrap Carousel Caption movement after transition. Despite reading and attempting the solutions provided, I still haven't been able to resolve my issue... In my case ...

Editing the object retrieved from JSON is not possible once it has been fetched

Project. Input text in the field and it appears on the shirt. When you click "see back," there is an issue where new text overlaps old text. Clicking on "see front" allows you to enter new text, with the previous text saved underneath. Question: How can ...

How can I troubleshoot Ajax not loading my additional external JavaScript files?

$(document).ready(function () { $("#livesearch").on("keyup",function(){ var search_term = $(this).val(); $.ajax({ url:"ajax-live-search.php", type:"POST", d ...

Quirks in TailwindCSS template literals behavior

Looking at this specific component: const SaleBadge = ({ textContent, badgeColor }) => { return ( <Badge className={`bg-${badgeColor}-200 hover:bg-${badgeColor}-300 animate-pulse align-middle ml-2`} variant="secondary"><Pe ...

Clicking on only one element should trigger it

My HTML structure looks like this: <article> <overlay> <IMG> </overlay> <div class="wrapper"> </div> </article> <article> <overlay> <IMG> </overlay> <di ...