What may be causing the lag in the translation within ThreeJS?

Just started diving into ThreeJS and discovering its capabilities.

Playing around with a simple example of creating a white dot and applying a basic translation.

let dotGeometry = new THREE.Geometry();
let dotMaterial = new THREE.PointsMaterial({
  size: 10,
  color: 0xffffff,
});
dotGeometry.vertices.push(new THREE.Vector3(1, 0, -1));
let dot = new THREE.Points(dotGeometry, dotMaterial);
scene.add(dot);

function animate() {
  requestAnimationFrame(animate);
  dot.position.x += 0.01;
  renderer.render(scene, camera);
}

animate();

Noticed that the movement is a bit choppy even with just one point displayed on the screen.

Any suggestions on how to achieve smoother transformations or if I'm missing something?

Thanks in advance for your help. Andrea

Answer №1

It is important to always consider the time delta of a single animation step when transforming objects. This approach gives more meaning to values like 0.1, representing 0.1 world units per second. Such implementation not only enhances the smoothness of animations but also makes them independent of the framerate. You can experiment with this modified code:

const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
camera.position.z = 5;

const scene = new THREE.Scene();

const clock = new THREE.Clock();

const dotGeometry = new THREE.Geometry();
const dotMaterial = new THREE.PointsMaterial({
  size: 10,
  color: 0xffffff,
});
dotGeometry.vertices.push(new THREE.Vector3(1, 0, -1));
const dot = new THREE.Points(dotGeometry, dotMaterial);
scene.add(dot);

const renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

function animate() {
  requestAnimationFrame(animate);
  const delta = clock.getDelta();
  dot.position.x += 0.1 * delta;
  renderer.render(scene, camera);
}

animate();
body {
  margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32465a40575772021c030001">[email protected]</a>/build/three.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

Changing a variable's value from within a Highmaps chart in Vue.js

Allow me to present my current project which involves the creation of a web application that displays data about devices spread across a country. To achieve this, I utilized Vue.js and HighCharts (employing HighMaps for the map component). The image below ...

Error encountered with the OffsetWidth in the Jq.carousel program

I am encountering an issue that I cannot seem to figure out. Unexpected error: Cannot read property 'offsetWidth' of undefined To view the code in question, click on this link - http://jsfiddle.net/2EFsd/1/ var $carousel = $(' #carouse& ...

How to implement a cyclic item generation feature in React.js

My function is meant to draw multiple items in a cycle, but it is only drawing the first item when I attempt to draw 5. This is my function: export default function CinemaHole() { const items = []; for(let i = 0; i < 5; i++) { item ...

Vue.js - The error message "$slots have 'el' is null" indicates a problem with the element in

When trying to access the Vuejs $slots instance, I encounter el = null, but type = "div" Here is the template: <slot name="head"> <h1> {{ text }} </h1> </slot> And in the script section: ... ...

Surprising color values are produced by three.js when utilizing points with custom shading and blending techniques

I am working with a simple setup that involves using a custom shader and a custom blend mode: A Scene and a PerspectiveCamera An IcosahedronBufferGeometry A custom new THREE.ShaderMaterial with unique shaders Some new THREE.Points with the icosahedron an ...

Shutting down the jQuery pop-up

I am struggling with trying to display this code as a popup on my website. Here is the code I have: <div id="myDialog" title="myTitle"> <div class="table_cell"> <div class="message"></div> </div> <div class="tabl ...

Resizing without altering the placement of a THREE.Line object

Currently, I have a line connecting two vertices drawn using LineBasicMaterial. My goal is to increase the length of this line. To achieve this, I tried scaling the line object by adding 0.1 to its z-axis scale: line.scale.z += 0.1; While this method did ...

The TextArea element is experiencing issues with the Jquery function

In my asp.net web application, I have implemented a JQuery function to restrict user input characters. $(document).ready(function () { $('input').on('input', function () { var c = this.selectionStart, ...

Shadowing jQuery variables involves declaring a new variable with the

There seems to be an unusual pattern used in jQuery: var jQuery = (function() { // This local copy of jQuery is defined within a closure var jQuery = function( selector, context ) { ... return jQuery; })(); Why was this approach chosen? Instead of exp ...

Using the transform property with the scale function causes elements positioned in the bottom right corner to vanish

Issue specific to Google Chrome and Windows 10 I'm currently working on a flipbook that adjusts content size using transform:scale() based on the user's screen size. There is also a zoom feature that allows users to adjust the scale factor. I ha ...

Iframes are not compatible with JQuery UI dialogs

After attempting to open a URL within an Iframe in a JQuery dialog box, I encountered the following issue: Here is the code snippet that I used: http://pastebin.com/Wqf0mGhZ I am looking for a solution to make sure that the dialog displays all of the con ...

Share firebase reference as a prop in Vue

Is it possible to pass a Firebase reference as a prop from the top-level component in Vue.js to a child component? <template> <div id="app"> <NavBar /> <router-view db="pass the db ref here" storage="pass the storage ref h ...

What is the best method for transferring a string from JavaScript to a JSON file?

Is there a way to update the value of "JSValue" in my JSON (JSValue) using JavaScript? Specifically, how can I assign JSValue to a variable called Value using JavaScript? JavaScript var Value = 1 + 1 JSON { "DATA": [ { "JSValue": "0" } ...

Extremely sluggish change identification in combination Angular application

We are encountering consistent issues with slow change detection in our hybrid AngularJS / Angular 8 app, especially when dealing with components from different versions of the framework. The problem seems to arise when using older AngularJS components wit ...

How can I prevent list items in jQuery Mobile from being truncated with ellipses?

Here is the list I am working with: <ul id="linksList" data-role="listview" data-inset="true" data-filter="true"> <!-- Dynamic contents! --> </ul> This list pulls its data from a local XML file (RSS feed). I am looking f ...

TypeScript: creating an interface property that relies on the value of another

Is it feasible to have an interface property that relies on another? For instance, consider the following: const object = { foo: 'hello', bar: { hello: '123', }, } I wish to ensure that the key in bar corresponds to the value of f ...

VueJS Error: Unable to access the 'className' property of an undefined variable

I'm currently working on a menu design project where I need to highlight the active tab/page that the user is on by adding a color class and utilizing JavaScript to update the active link. Here's a snippet of my template: <div class="menu ...

Issue with the reload animation jumping too quickly in Framer Motion on NextJS

While creating an animation for my landing page using NextJS with framer motion, I encountered a strange behavior after deploying the site. The animations started to happen too quickly and it felt a bit off. During development on localhost using Chrome, ev ...

Dynamic HTML element

I created an input number field and I want to dynamically display the value of this field in a container without having to refresh the page. I am currently using ajax for this purpose, but unfortunately, I still need to reload the page. HTML: < ...

Transform an array into an object with assigned key names

How can we transform the following array: [2019,2020,2021] into the format: { 0: {year:2019}, 1: {year:2020}, 2: {year:2021} } ...