Problem with rendering in Three.js: animation is causing vibration

I am facing a unique issue with the rotation animation of a sphere: it appears shaky and I'm unable to pinpoint the root cause of this problem.

Feel free to take a look at the example on this link

Below is the render function code snippet:

 function render() {

  controls.update();
  requestAnimationFrame(render);

  // Using a parametric parameter for camera rotation
  timer = Date.now()*0.0001;

  // Setting camera coordinates
  coordCamera.set(radiusCamera*Math.cos(timer), radiusCamera*Math.sin(timer), 0);

  // Call rotateCamera function
  rotateCamera();

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

  }

This includes functions like rotateCamera and computeRotation:

function computeRotation (objectRotation, coordObject) {

  // Applying rotation matrix 
  var rotationAll = new THREE.Matrix4();
  var rotationX = new THREE.Matrix4().makeRotationX(objectRotation.rotation.x);
  var rotationY = new THREE.Matrix4().makeRotationY(objectRotation.rotation.y);
  var rotationZ = new THREE.Matrix4().makeRotationZ(objectRotation.rotation.z);
  rotationAll.multiplyMatrices(rotationX, rotationY);
  rotationAll.multiply(rotationZ);

  // Compute world coordinates 
  coordObject.applyMatrix4(rotationAll);

  }

  function rotateCamera() {

  // Compute camera coordinates
  computeRotation(torus, coordCamera);

  // Set camera position for motion
  camera.position.set(coordCamera.x, coordCamera.y, coordCamera.z)

  }

If you can offer insights or solutions to address this issue, your assistance would be greatly appreciated.

Thank you for your help.

UPDATE :

I attempted integrating the solution below, yet unfortunately, the animation did not work as expected; nothing appeared on screen. You can view my efforts on jsfiddle:

https://jsfiddle.net/ysis81/uau3nw2q/5/

I also experimented with performance.now() but the animation continues to exhibit shaking. You can test this out on https://jsfiddle.net/ysis81/2Lok5agy/3/

To resolve this issue, I have initiated a bounty for potential solutions.

Answer №1

Utilize the requestAnimationFrame method to call your rendering function and receive a timestamp parameter. By leveraging this parameter, you can calculate the elapsed time since the previous frame, allowing for dynamic rotation calculations based on the difference.

var customRotationLogic = function (millisecondsElapsed) {
  // Implement camera rotation here
  // For instance, rotating 20 degrees per second
  var degPerSecond = 20;
  var degPerMS = degPerSecond / 1000;
  var rotationDegrees = millisecondsElapsed * degPerMS;
  // Invoke your rotation function
  // Note: Consider adding an option to disable basic rotation when moving the ball with the mouse

  // Set the desired camera rotation

  renderer.render(scene, camera);
};


var initiateRotation = function() {
    var lastFrameTime;
    var deltaTime;

    function loop( currentFrameTime ) {
        window.requestAnimationFrame(loop);
        lastFrameTime = lastFrameTime || currentFrameTime;
        deltaTime = currentFrameTime - lastFrameTime;

        customRotationLogic(deltaTime);

        lastFrameTime = currentFrameTime;
    }
  loop();
};

initiateRotation();

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

Assigning the href attribute dynamically

How can you dynamically set the href attribute of the <a> tag using jQuery? In addition, what is the method for retrieving the value of the href attribute from the <a> tag with jQuery? ...

How to Activate Child Component in Vue When Submitting a Form

This is my PhoneLineNumberComponent. Everything is working perfectly, but I'd like to have the child component form loaded when the user clicks the button on the parent component. The PhoneLineNumberComponent is the child component, and the Bu ...

Adjust the Container's gutters in MaterialUI according to screen sizes

I am trying to adjust the gutters for different screen sizes in my project. I want to turn off the gutters for xs, sm, and md, but have them enabled at xl and larger. Despite following the API documentation, it doesn't seem to be working as expected. ...

How can I retrieve the name of an HTTP status code using JavaScript and AngularJS?

My web application is built using AngularJS, JS, JQ, HTML5, and CSS3. It interacts with our projects' REST API by sending various HTTP methods and manipulating the data received. The functionality is similar to what can be achieved with DHC by Restlet ...

Tips for verifying a string date using the Ajax Control Toolkit (CalendarExtender) technology

I have a dilemma with two formatted TextBoxes that receive their text value from a CalendarExtender. I need to validate that the first TextBox is greater than the second one, but the values are coming in as strings rather than dates. Ho ...

Navigate through a given value in the event that the property undergoes a name change with each

Exploring an example found on the JSON site, I am facing a situation where I am using an API with JSON data. The property name "glossary" changes for each request made. For instance, if you search for "glossary", the first property is glossary. However, if ...

Invoke the parent method within the child application

I have a checkbox on the child app controller. When the user clicks it, I need to call a method from the parent controller: Parent app controller: <div ng-controller="ParentCntr as parentCntr"> <child-app></child-app> </div> C ...

Using Angular to pass parameters to a function

When passing a parameter to the getActiveId function in the ng-click <span class="col ion-ios7-heart-outline center" ng- click="getActiveId({{activeSlide.selected}})"> The value turns to zero in the controller, but it is passed correctly after tes ...

Extracting information from a JSON file using React.js

I have a JSON file named data.json in my React.js project: [{"id": 1,"title": "Child Bride"}, {"id": 2, "title": "Last Time I Committed Suicide, The"}, {"id": 3, "title": "Jerry Seinfeld: 'I'm Telling You for the Last Time'"}, {"id": 4, ...

Despite containing elements, Array.length incorrectly reports as 0 in React, JavaScript, and TypeScript

I have been working on storing persistent data for my Electron app using electron-json-storage. Initially, I tried using neDB but encountered an error, so I switched to another method. However, it seems that the issue is not with neDB but rather with my ow ...

My React component is experiencing issues with the Console.log functionality

Utilizing React for a component, I have incorporated a button that triggers a function 'myFunction' upon clicking, which essentially executes a console.log command. https://i.sstatic.net/mc8wu.png Despite compiling the code without any errors in ...

Ways to incorporate React.js into HTML for showcasing a portfolio

Having difficulties rendering my code using React.js for my portfolio. I am attempting to showcase my projects with images and p tags containing project titles and my role. Additionally, I would like the React elements to be clickable, directing users to t ...

JavaScript still mentions a class that no longer exists

One of my elements has a class of .collapsed. When this element is clicked, a jQuery function is triggered to remove the .collapsed class and add the .expanded class instead. Even after the .collapsed class is removed, the function I have created continue ...

Guide on including the angular value (id) in conjunction with repeating data-target for executing a function

I have the following code snippet. Now, I am trying to pass a dynamic AngularJS variable, specifically the id of a repeating list. The button in question is used for deactivation purposes, and upon clicking it, a confirmation box pops up. Upon confirming ...

How can I replicate the X axis on both ends of a horizontal bar chart using only one dataset in Chart.js version 3.x?

Is there a way to duplicate the X axis ticks on both the bottom and top of a chart? I know there were solutions for older versions of Chartjs, but the options have changed in version 3.x. Previously, you could use something like this: xAxes: [ { ...

Switching an Enum from one type to another in JavaScript

UPDATE After reading a comment, I realized that Enum is not native to JavaScript but is actually part of TypeScript. I decided to keep the original title unchanged to help others who may also make the same mistake as me. I am faced with a scenario where ...

When the Popover appears in ShadCN, the input field unexpectedly takes focus

This unique component incorporates both Popover and Input functionality from shadcn. An issue I have encountered is that when I click on the Input to trigger the Popover, the Input loses focus and the cursor moves away from it. Expected outcome: Upon c ...

Leveraging AngularJS for parent-child communication through bindings in a unique and effective manner

I've come across the following code snippet: <div class="parent"> <div class="child"> and I have two directives, one for parent and one for child. When an event (such as a click) occurs on parent, I want something to happen on child ...

Images with fadeIn-Out effect on a slide restrict the movement of the div

I am currently working on a project where I have a div named "background" containing 4 images (400x300px) that fade in and out in a row every x seconds. The issue I am facing is that these images are displaying on the top-left of the browser, and I am tr ...

Having trouble with submitting an HTML form using JavaScript and PHP

My webpage has an HTML form that sends data to a PHP file for processing. One issue I'm facing is with a dynamically generated combo box that works fine when the page loads, but the selected value is not being passed when the form is submitted. The J ...