Disable the rotation animation on the Three.js cube

I've successfully created a 3D wireframe cube using Three.js examples, however, it's continuously rotating,

I'm looking to halt this animation.

When I remove the animate(); function at the end of the script, the Canvas fails to load.

// start animation
animate();
</script>

Trying to remove the animate(); function from :

    // request new frame
    requestAnimationFrame(function(){
        animate();
    });

The rotation stops but the shape distorts strangely, not resembling the cube. Moreover, with each refresh, the shape changes.

Below is my code:

Javascript:

    <script defer="defer">
  // revolutions per second
  var angularSpeed = 0.2; 
var lastTime = 0;

// this function runs on every animation frame
function animate(){
    // update
    var time = (new Date()).getTime();
    var timeDiff = time - lastTime;
    var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
    cube.rotation.y += angleChange;
    lastTime = time;

    // render
    renderer.render(scene, camera);

    // request new frame
    requestAnimationFrame(function(){
        animate();
    });
}

// renderer
var container = document.getElementById("container");
var renderer = new THREE.WebGLRenderer();
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);

// camera
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 700;

// scene
var scene = new THREE.Scene();

// cube Length, Height, Width
var cube = new THREE.Mesh(new THREE.CubeGeometry(400, 200, 200), new THREE.MeshBasicMaterial({
    wireframe: true,
    color: '#ff0000'
}));
cube.rotation.x = Math.PI * 0.1;
scene.add(cube);

// start animation
animate();
</script>

Fiddle link for reference: http://jsfiddle.net/UsEDt/1/

If you require additional information, feel free to ask.

Please advise on how to proceed.

Answer №1

If you want to maintain a constant rotation without it resetting when you refresh the page, simply disable the line of code that alters the angle. This specific line should be commented out:

//cube.rotation.y += angleChange;

As for adjusting the cube shape to make it a perfect unit cube, it is important to properly adjust its dimensions and aspect ratio of the window.

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

Executing Ionic function before application shutdown

Does anyone know of a function that can be triggered when the app is about to exit, close, or go into the background? Essentially any event that indicates "the user has stopped using the app"? In my app, I create a 'user log' that keeps track of ...

Guide to displaying a task within a table cell after a user submits the form

<?php $servername = "localhost"; $username = "u749668864_PandaHost"; $password = "Effy1234"; $dbname = "u749668864_PandaHost"; $q = $_REQUEST["task"]; $task = $q; echo $task; $conn->close(); ?> Exploring the world of ajax has left me scratching ...

Enhancing TypeScript Types with a custom method within an Angular 2 context

I am working on an Angular 2 project using the CLI. Currently, I am trying to add a custom method to the String type as shown below. interface String { customMethod(): number; } String.prototype.customMethod = function() { return 0; } Despite my ...

Using Javascript to dynamically change the background image url in CSS

I've been tinkering with this for a while now. I believed this code would do the trick as I'm fetching the value from the input and setting the background-image URL to that value. Appreciate any help! The following code is within the head tag. ...

Accessing variables from child controllers with populated select inputs in Angular

I'm currently facing an issue involving 3 controllers: Parent Controller: DocumentController Child Controller1: XdataController Child Controller2: CompanyController The child controllers are used to populate data in three Selector inputs on the fron ...

Spinning image on button click with seamless animation in Javascript

I'm trying to make an image rotate every second using the code below, but it's not working. Can you help me figure out why? <html> <head> <style> .rotated-image { -webkit-transform: rotate(2deg); transform: rotate(2deg); } & ...

The getElementByID function functions properly in Firefox but does encounter issues in Internet Explorer and Chrome

function switchVideo() { let selectedIndex = document.myvid1.vid_select.selectedIndex; let videoSelect = document.myvid1.vid_select.options[selectedIndex].value; document.getElementById("video").src = videoSelect; } <form name="myvid1"> <s ...

What is the best way to continuously add items to a div until their heights are equal?

In my layout, I have two divs positioned next to each other. Typically, the left div displays n+2 items while the right div displays n items. The value of n changes depending on the category and is already set. However, there are instances where certain ...

Utilizing a Material UI custom theme in React with Typescript: A step-by-step guide

Upon using the tool , I received a js file and a json file following the paths mentioned on the theme generator page: // src/ui/theme/index.js /* src/ui/theme/theme.json */ The files operate smoothly when left with the .js extension. However, when I attem ...

Steer clear of specifying product type when using AJAX to update cart count in Shopify

I have a unique scenario in my Shopify store where I need to update the cart count using Ajax, but exclude a specific product type called 'mw_product_option'. I found a workaround to exclude this product type from the count on a regular page refr ...

What methods can be used to broaden configuration variables within VSCode using an extension?

I attempted to develop an extension for vscode that requires reading the pasteImage.parth variable from the ./vscode/settings.json file { "pasteImage.path": "${workspaceRoot}/assets/images" } In my attempt to retrieve the variable us ...

What is preventing the function from successfully compiling text from various files that are uploaded using the HTML5 file reader into an array?

My current challenge involves attempting to upload two text files using the HTML 5 file reader. While I can successfully get the files into an array, encountering difficulty arises when trying to return that array from the function. One solution could be ...

Are you looking to load pictures using jQuery?

I am currently using a ul li menu setup as follows: <ul> <li><a href="#1">Image #1</a></li> <li><a href="#2">Image #2</a></li> <li><a href="#3">Image #3</a></li> ...

I am looking to incorporate a password recovery page into my login process, but I am facing difficulty navigating to it once the code has been modified

I'm looking to include a forgotten password option on my login page, but I'm facing an issue when trying to navigate to it after updating the code. The website is throwing the following error message: [vue-router] uncaught error during rout ...

The NPM command fails to execute the Webpack script, yet it successfully runs when manually entered into the terminal

Working on my project with Electron-React-Boilerplate, I encountered an issue while running npm run build-renderer. Despite the script appearing to finish, I receive an error. If I manually enter the code related to the build-renderer script in the termin ...

What is the best way to fix the Syntax error that reads "Unexpected token (1:13)"?

I can't seem to figure out why my code keeps showing errors in the browser. I'm still new to coding and learning slowly, with help from knowledgeable individuals on stackoverflow :) Card 1.jsx Syntax error:() Unexpected token (1:13) > 1 | i ...

An undefined value error occurred while attempting to save data to a MongoDB collection through a node/express server

Anticipated Outcome I am looking to successfully save a new page to mongo db after server validation of input, without encountering any errors in the console. The issue seems to be originating from the post method in the routes in admin_pages.js and I&apos ...

Creating interactive form fields using React

How can I update nested fields in react forms? First, create a new item using handleAddShareholder. Next, delete an existing item with handleRemoveShareholder. To change details of an item, use handleShareholderNameChange. You can then add a new array ...

Is there a way to use the onclick event to open a link in the same window and tab?

I am facing a challenge with a webpage containing multiple divs. I am trying to extract data from my database and display it within specific divs on the page. Additionally, I want this information to be presented when clicking on a link taking me back to t ...

React component refuses to re-render

I am facing an issue with my menu re-rendering in React. The handleToggleFunction only works for the first click, after which nothing happens and the state does not update. I am using Tailwind CSS in this component. Here is my code: I have attempted to us ...