What is the best way to manually decrease the speed of an object's rotation using javascript?

Can anyone assist me with slowing down the mouse-controlled rotation of a 3D object in javascript? The current rotation is too sensitive and difficult to control manually. Below is the code I am using:


    <html>
<head>
<script src="js/three.js"></script>
<script src="js/ColladaLoader.js"></script>
<script src="js/OrbitControls.js"></script></head>
<body>
<script>
    // Setting up the global variables for scene, camera, and renderer.
  var scene, camera, renderer, mesh;

  init();
  animate();

// Scene initialization function.
  function init() {

    // Creating the scene and defining its size.
    scene = new THREE.Scene();
    scene.add( new THREE.AmbientLight( 0x999999 ) );
    var WIDTH = window.innerWidth,
        HEIGHT = window.innerHeight;

// Creating a renderer and appending it to the DOM.
    renderer = new THREE.WebGLRenderer({antialias:true});
    renderer.setSize(WIDTH, HEIGHT);
    document.body.appendChild(renderer.domElement);


 // Creating a camera, positioning it away from the model, and adding it to the scene.
    camera = new THREE.PerspectiveCamera(45, WIDTH / HEIGHT,1,1000);
    camera.position.z = 100;
    scene.add(camera);


 // Adding an event listener that resizes the renderer based on the browser window.
    window.addEventListener('resize', function() {
      var WIDTH = window.innerWidth,
          HEIGHT = window.innerHeight;
      renderer.setSize(WIDTH, HEIGHT);
      camera.aspect = WIDTH / HEIGHT;
      camera.updateProjectionMatrix();
    });

// Setting the background color of the scene.
    renderer.setClearColor(0x333F47, 1);

    // Creating a light, setting its position, and adding it to the scene.
    var pointLight = new THREE.PointLight(0xffffff, 0.6);
    pointLight.position.set(80,90,150);
    scene.add(pointLight);


 // Loading the mesh and including it in the scene.
    var loader = new THREE.ColladaLoader();
    loader.options.convertUpAxis = true;
    loader.load( "models/water.dae", function(result){
      result.scene.scale.x = .01;
      result.scene.scale.y = .01;
      result.scene.scale.z = .01;
      result.scene.position.z = 20; 
      result.scene.updateMatrix();
      result.scene.matrixAutoUpdate = false;
      scene.add(result.scene);
    });


 // Adding OrbitControls for navigating with the mouse.
    controls = new THREE.OrbitControls(camera, renderer.domElement);
    controls.enableDamping = true;
    controls.dampingFactor = 0.25;
    controls.enableZoom = true;
    controls.target.z = 20;

  }


// Function to render the scene and update as needed.
  function animate() {

    requestAnimationFrame(animate);

    renderer.render(scene, camera);
    controls.update();

  }
</script>
</body>
</html>

Any comments or suggestions are greatly appreciated.

Answer №1

When it comes to adjusting the sensitivity of mouse movement, there are a few ways you can approach it. If the rotation is moving too quickly or not smoothly enough, one solution is to implement an easing equation. In my experience with 2D applications, I often use the following formula:

rotation += (targetRotation - rotation) / speed;

For example, if you want an object to rotate towards the position of the mouse at a slower pace, you could write something like this:

object.rotation += (mouseRotation - object.rotation) / 15;

This method can be adjusted and applied to a 3D system as well for achieving smoother and more controlled rotations.

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

After loading Google Maps, initiate an AJAX request

Is there a way to determine if Google Maps has finished loading? I need to send an Ajax request once the map is fully loaded. Currently, I am displaying a group of users on a map with info windows. However, the browser becomes unresponsive due to the larg ...

Dispensing guarantees with Protractor

q library offers a unique feature that allows resolving and spreading multiple promises into separate arguments: If you have a promise for an array, you can utilize spread instead of then. The spread function distributes the values as arguments in the f ...

My code seems to be malfunctioning - why can't I keep the aspect ratio?

UPDATE: Can someone please assist me with maintaining the aspect ratio of a drawn image? I am struggling to achieve this and would appreciate any help. I have been attempting to upload an image, draw it using the imageDraw() function, and fit it within a ...

Get a URL from the JSON data returned by the Wikipedia API

How can I retrieve the image URL from a JSON response and store it in a variable? I found helpful information on the MediaWiki API help page Following this example to extract image information from a page: https://commons.wikimedia.org/w/api.php?action= ...

Alert: Parser error in JSONP!

$.ajax({ type: "GET", dataType: "jsonp", jsonpCallback: "jsoncallback", //async: true , data: { // some other data here }, url: "http://mywebsite.com/getRequest.php", success: function(response ...

Setting a random number as an id in the constructor in Next JS can be achieved by generating a

What steps can be taken to resolve the error message displayed below? Error: The text content does not match the HTML rendered by the server. For more information, visit: https://nextjs.org/docs/messages/react-hydration-error Provided below is the code i ...

Executing a jQuery function only once per click on a select element - how can it be done?

I have a form with a select element, and I want some code to run when I click on it, but not when I choose an option. The issue is that the code is running twice, preventing me from selecting an option as it resets itself each time. Here is the HTML: &l ...

Error message: Unspecified service injected

I am currently working with 2 separate javascript files for my project. One is being used as a controller, while the other serves as a service. However, when I attempt to inject the service into the controller and access its function, an error message pops ...

Anchoring HTTP headers in HTML tags

Currently, I am working on some code to enable dragging files from a web app to the desktop by utilizing Chrome's anchor element dragging support. The challenge I am facing is that certain file links require more than a simple GET request - they nece ...

How can we customize HTML images without allowing third-party JavaScript to enlarge them?

I am using Blogger to host my website and I have a basic knowledge of HTML and CSS. I want to incorporate a collaborative add-your-link feature using SimplyLinked. However... They provided me with the following HTML: <script type="text/javascript" src ...

The mobile devices are not showing my HTML website

I have implemented the following CSS link code on my website: <link rel="stylesheet" href="index_files/front.css" media="all" type="text/css" > Additionally, I have included the following code <meta name="HandheldFriendly" content="True"> & ...

What is the reason for the return of undefined with getElementsByClassName() in puppeteer?

Currently, I am utilizing puppeteer to fetch certain elements from a webpage, specifically class items (divs). Although I understand that getElementsByClassName returns a list that needs to be looped through, the function always returns undefined for me, e ...

Guide for transferring information from JavaScript to PHP on the same page

My dilemma lies in transferring data from my JavaScript script to PHP code for use within a query. Despite numerous attempts, I have been unsuccessful in achieving this. Below is my script for uploading files using an input type: file, where the URL is sto ...

What is preventing me from altering the array one element at a time?

I am working with an array and a class let questions = [ { questionText: '', answerOptions: [], }, ]; class Questions { constructor(questionText,answerOptions) { this.questionText = questionText; this.answerOptio ...

Transforming a function into its string representation | 'function(){...}'

func=function() {foo=true} alert(JSON.stringify(func)); alerts "undefined" obj={foo: true} alert (JSON.stringify(obj)); alerts: "{foo: true}" Have you ever wondered why JSON.stringify() doesn't work for a "function object"? It seems that when tryi ...

Clicked button redirects user to the parent URL

When the application is running on page http://localhost:3000/login, clicking a button should redirect to http://localhost:3000/. This is how I attempted it: import React from 'react'; import { Redirect } from 'react-router-dom'; impor ...

What is the best way to showcase a div on top of all other elements in an HTML page?

As a newcomer to html and css, I have successfully created a div that contains city names. The issue I am currently facing is when I click on a button to display the div, it gets hidden behind the next section of my page. Take a look at the image below for ...

Angular-material's Md-dialog popup box is displayed as a separate view within the Yeoman framework

I have recently created a project using Yeoman (angular-fullstack, angular-material) and encountered an issue with triggering the md-dialog box. When clicking on a div element, the dialog box is supposed to appear. However, instead of showing the popup the ...

Combining data from various JSON files to create dynamic charts with Highcharts

At this moment, my Highchart code is set up in a way where I want to replace the static data-series values within the HTML file with information loaded from a JSON file. The current code appears as follows: <!doctype html> <script type="text/jav ...

Cannot locate module: Unable to resolve 'encoding' in '/Users/dev/node_modules/node-fetch/lib'

Currently, I am working with next.js version 13.4.5 and firebase version 10.1.0. Every time I execute npm run dev, a warning is displayed initially. Eventually, an error message pops up in the terminal after the warning persists for some time. I am u ...