Getting started with Tween JS on a three-dimensional cube in Three JS

I'm venturing into the world of Tween JS and attempting to create a basic animation that moves objects to the right using Tween.

Here is the code snippet from my init function (utilizing Three JS):

var geometry = new THREE.CylinderGeometry(200, 200, 200, 4, 0);
var material = new THREE.MeshPhongMaterial({ambient: 0xf0f0f0, color: 0x006699, specular: 0x006699, shininess: 60, shading: THREE.FlatShading});
var mesh = new THREE.Mesh(geometry, material);
mesh.position.x = 0;
mesh.position.y = 0;
mesh.position.z = 0;
mesh.updateMatrix();
mesh.matrixAutoUpdate = false;
scene.add(mesh);

var tween = new TWEEN.Tween({ x: 0, y: 0 })
.to({ x: 400 }, 2000)
.easing(TWEEN.Easing.Elastic.InOut)
.onUpdate(function() {
mesh.position.x = Math.round(this.x);
}).start();

Below is my animate function:

requestAnimationFrame(animate);        
renderer.render(scene, camera);
TWEEN.update();
update();

The cube renders but the tween effect doesn't seem to be functioning. Could there be something I've overlooked?

Answer №1

Here are the steps I took to enhance my scene:

  1. Develop a separate render() function

  2. Organize the TWEEN code into a function that can be easily called. Remember to clear all previous TWEENS within this function. This practice was adopted from tutorial examples, although the exact reason behind it is not crystal clear.

  3. Within the TWEEN function, invoke the render function during updates.

  4. Integrate TWEEN.update into your ongoing animation loop. It's essential to note that the render() function will be triggered each time a TWEEN is updated.

Below is a snippet of skeleton code that might align with your project:

//TWEEN function
function moveObject( ) {
    TWEEN.removeAll();
    new TWEEN.Tween( { x: 0, y: 0 } )
    .to( { x: 400 }, 2000 )
    .easing( TWEEN.Easing.Elastic.InOut )
    .onUpdate( render )
    .start();   
};
//NON-STOP animation loop
function animation(){
    requestAnimationFrame(animate);  
    TWEEN.update();
}
//Render function
function render(){
    renderer.render(scene, camera);
}

This guide proves beneficial in refining your setup.

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

Can dates in the form of a String array be transmitted from the server to the client?

Struggling to send a String array from the server side to the client using Nodejs and Pug. Encounter errors like "SyntaxError: expected expression, got '&'" or "SyntaxError: identifier starts immediately after numeric literal". Server runs o ...

Expression fragment in Thymeleaf

In splitting my templates into head/main/footer parts using thymeleaf, I have found a method to include stylesheets and javascript on certain pages while excluding them from others. This is achieved through the use of fragment expressions outlined here. M ...

How can I trigger an event to append a UL element using

Can anyone suggest a more elegant solution for handling an append/remove event from an element without having to manually trigger an event or resorting to other methods? It would be great if there was a way to do something like this: $('#id').o ...

Using urllib.urlopen in place of javascript

I'm curious if there is a Javascript module that functions similarly to Python's urllib. Specifically, I am looking for something along the lines of: urllib.urlopen(url, data) This function would return an object that allows for a blocking fetc ...

Sorting complex strings in Typescript based on the dates contained within them

How can I sort a list of 2 strings with dates inside them so that the earlier one always comes first? The date is always after the second comma. For example, const example = ["AAA,5,2020-09-17T21:14:09.0545516Z", "AAA,0,2020-09-03T20:38:08. ...

Learn how to achieve a sleek animation similar to the famous "Ken Burns effect" by utilizing the CSS property "transform" instead of "object-position". Check out the demo to see it in action!

I am currently exploring how to create an animation similar to the "Ken Burns" effect using CSS transform properties. While I have been using object-position to animate, I am facing challenges with the fluidity of the movement. I am seeking help to achiev ...

Manage SPA forms using Dataform Javascript - Safeguarding form information by avoiding inclusion in the URL

I have been using express, mongoose, and JavaScript to build a single page application. Everything seems to be working fine, except for one thing - I do not want the form data to show up in the URL when making a post request. I attempted to use a redirect, ...

What is the process of retrieving a property value from a database using selected values from cascaded drop-down lists?

I am facing a challenge where I need to extract a property of an entity by passing the IDs of selected items from a cascaded dropdown list. The requirement is to update the price every time there is a change in either level 1 or level 2 of the cascading dr ...

Assign Texture to Plane with Clarity

How can I adjust the way a texture is mapped onto a plane in Three.js? Assuming we have a 1x1 plane and a 16:9 image, how can we ensure the image maintains its aspect ratio and any overlap is trimmed off? Currently, the image is "squished" by default. Is ...

What is the best method for effectively eliminating duplicate objects with the same value from an array?

Let's say we have a collection of jqlite objects, and using the angular.equals function, we can determine if they are equal. How can we utilize this function to eliminate duplicate items from an array of jQlite objects? This is my attempted solution: ...

What method is the most effective for preloading images and stylesheets?

One of the main concerns I have is optimizing the load time of my website. I would like to preload images, style sheets, and scripts to ensure a faster loading speed and to prevent users from seeing images loading right before them. Can someone suggest the ...

Cross-site communication with Ajax using both POST and GET requests

As a beginner in JavaScript, I'm facing challenges with implementing ajax POST and GET requests. While I can successfully do it using Google Postman as shown here https://i.sstatic.net/Cegxj.pnghttps://i.sstatic.net/ovJT0.png, the problem arises when ...

Express server unable to process Fetch POST request body

I'm currently developing a React app and I've configured a basic Express API to store user details in the database app.post("/register", jsonParser, (req, res) => { console.log("body is ", req.body); let { usern ...

Accessing and parsing an XML file in Webstorm IDE with the use of node.js and ajax

I've recently upgraded to the newest Webstorm IDE to work on my webpage development with Ajax. 1. When attempting to read a local xml file, I started the server and accessed localhost with Chrome, but unfortunately, I couldn't read the local xml ...

Use Vuejs to add the details of the selected item to an array and then dynamically update the array when items are checked or unchecked

My products json contains various details similar to the examples below. products.json products: [ { "id": "6", "name": "Cake-WhiteForest", "manufacturer": " ...

Is it possible for a webgl shader to operate within a three.js environment?

I have created a webgl shader in three.js to process a webgl texture. When I run the code with "use_three" set to false, everything works fine. However, if I set it to true, the result is a black screen. Any suggestions on what might be causing this issu ...

Automatically launch a popup window specified in JavaScript

Aim:- I am trying to automatically open a radwindow from the server-side based on a specific IF condition. Snippet Used:- In the aspx page, I have defined the radwindow as follows: <telerik:RadWindowManager Skin="WBDA" ID="AssetPreviewManager" Modal= ...

Error handling in Realm-js can result in exceptions being thrown

Currently, I am attempting to test realm-js using node.js console. However, upon inserting the first string, I encounter an exception: > var Realm = require('realm') TypeError: utf8 is not a function at Function.from (native) at Function.from ...

Top recommendations for implementing private/authentication routes in NextJS 13

When working with routes affected by a user's authentication status in NextJS 13, what is the most effective approach? I have two specific scenarios that I'm unsure about implementing: What is the best method for redirecting an unauthenticated ...

Reviewing user input for any inappropriate characters using jQuery's functionality

When a username is inputted into the input box, I want to make sure that only valid characters are accepted. The following code shows what I have so far; but what should I replace "SOMETHING" with in the regular expression? var numbers = new RegExp( ...