The Three.js tweened property fails to reach its target value and ends up completing the animation while remaining stuck at its original value

I've been experimenting with implementing Tween in my WebGL project at to smoothly move the camera to a new position. Unfortunately, I'm facing challenges getting Tween to function as desired.

When attempting to tween the camera.position.x value from -3 to -10 over 3 seconds, instead of transitioning smoothly, the camera's position jumps to x=-3 and stops there. Even after confirming through Tween's .onComplete() that the task is complete (though I have since removed this alert), the issue persists.

The code I am using can be accessed at this link:

function zoomOutCamera()
{
    var position, target;

    // app.camera.position.z = 10;

    position = -3.0;
    target = -10.0;
    myTween = new TWEEN.Tween(position).to(target, 3000);

    myTween.onUpdate(function(){
        // alert(position);
        app.camera.position.x = position;
    });

    // myTween.onComplete(bananaphone(position));

    myTween.start();
}


function myAnimate()
{


    if(!myTween.onComplete())
    {
    requestAnimationFrame( myAnimate);
    }
    myTween.update();


}

Below is a summary of how I'm testing the code and what actually occurs:

Current outcome when checking in browser's JavaScript console:

input: camera.position.x
output: 0.5
input: myAnimate()
output: undefined
input: camera.position.x
output: -3

Expected outcome according to my understanding:

input: camera.position.x
output: 0.5
input: myAnimate()
output: undefined
input: camera.position.x
output: -10

Despite trying different approaches to the tween and consulting online resources and my O'Reilly book, the problem persists. I would appreciate any suggestions on how to improve the accuracy of my question.

Thank you in advance for any assistance provided.

Answer №1

It takes time for a Tween's update function to work properly! If you are using it with the 3D render function "myAnimate", make sure to pass the time parameter.

function myAnimate(time)
{ 
    if(!myTween.onComplete())
    {
        requestAnimationFrame(myAnimate);
    }
    myTween.update(time);
} 

Answer №2

Tween function does not apply to whole numbers

function zoomOutCamera()
{
    var position, target;

    // app.camera.position.z = 10;

    position = { x:-3.0};
    target = {x:-10.0};
    myTweeter = new TWEEN.Tween(position).to(target, 3000);

    myTweeter.onUpdate(function(){
        // alert(position);
        app.camera.position.x = this.x;
    });

    // myTweeter.onComplete(bananaPhone(position));

    myTweeter.start(+new Date());
}


function myAnimate()
{


    if(!myTweener.onComplete())
    {
    requestAnimationFrame(myAnimate);
    }
    myTweener.update(+new Date());


} 

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

Guide to setting up the redux-form with initial values pulled from the store

I am looking to preload a Redux Form with values obtained from the store. Currently, I can populate the redux form with data directly from the reducer. However, when submitting the form, it only accepts data for clickable elements to generate the payload. ...

Does .NET MVC provide the necessary separation of HTML/CSS/JS for my project?

Currently, I am collaborating with my ASP.NET development team in an effort to improve the quality of our HTML output. We are facing challenges due to ASP.NET's tendency to insert JavaScript directly into the page, creating dependencies on JS for form ...

Lending a hand in deselecting a rule within the jcf-hidden add-on using Selenium

Currently, I am working on automating a website that utilizes the jcf-hidden class (a form of jQuery class) which hides the select element I want to access. By removing the display: block !important; property, I was able to successfully interact with the ...

Verify if item is currently on wishlist

Is there a way to verify if products have been added to the wishlist for logged in customers? I would like to display the result on a twig file, showing whether the product is already on the wishlist. button color=red else button color=gray In addition, ...

Modify the background color of a list with jQuery

I have a list where I am adding data through Ajax and I want to change the background color of the selected list item. Here is my list: <div class="widget-content" > <ul class="news-items" id='jq_friendList'> ...

Ways to retrieve the identifier of the uploaded document in GridFs for linking with another model

After creating a GridFS and uploading an image using the code snippet below: app.post("/upload", upload.single("photo"), function(req, res) { res.redirect("/images"); }) I also have a separate model for users: var userSchema = new mongoose.Schema({ ...

Using React.render to render an empty subcomponent within a jsdom environment

Experimenting with React in node using jsdom has been an interesting challenge for me. I encountered an issue when attempting to render a component that includes another component with content, only to have the subcomponent's content fail to render. F ...

Using a .NET Web-API controller variable as a parameter in a JavaScript function

I am looking to send a "variable" from the controller to a JavaScript function. The code I have implemented is as below: <div ng-controller="faqController"> <div ng-repeat="c in categories"> <h2 onclick="toggle_visibility(&apos ...

javascript method to retrieve specific value from row column upon button click

Take a look at my HTML code below: <div class="container"> <table class="table table-bordered"> <thead> <td>Name</td> <td>Address</td> <td>Ci ...

Executing code after finishing a fetch in Next.js

Here is the JavaScript code snippet under question: async function checkAuth() { console.log("3") await fetch(apiUrl+'/auth', { method: 'POST' }).then(response => response.json()).then(result => { ...

Python and JavaScript fundamental interaction

My current setup involves having a local HTML page named leaflet.html, which is being shown in an embedded browser within a python-tkinter application. Within the leaflet.html file, there exists a straightforward JavaScript code snippet that includes a fu ...

A guide on updating a MySQL table using a JSON object in Node.js

I have a JSON Object and need to UPDATE a mySQL table without listing all of the keys individually For an INSERT operation, I use the following code: var arrayValue = Object.keys(obj).map(function(key) { return String("'"+obj[key]+"'"); ...

Switch polygon setting during event on Vue Google Map

Looking for a way to change the stroke color of a polygon when hovering over it. Struggling to locate proper documentation on how to access the setOptions method from events. <GmapMap ref="google_map" :center="{ lat: 0, lng: -0 }"> ...

Different techniques for retrieving elements generated by ng-repeat from their containing parent

Let's keep it simple - imagine we have a directive called headSlides. This directive's template includes an image that is being repeated using ng-repeat: <img class="bg" ng-repeat="image in images" ng-src="{{image.src}}"> I need to access ...

Calculate the total value of selected checkboxes in every row of the table

I am looking to calculate the total sum of checkboxes for each row in a table Javascript : For Sum $('input[type="checkbox"]').change(function() { var total = 0; $('#mytable tr').each(functi ...

As we hover over a specific div, a secret div emerges and shifts its position

Looking to create a hidden div that appears and moves when hovering over another div. My current code is functional, but the hidden div does not move as the mouse moves on the div. This is likely due to the hover function being used. How can I address thi ...

How can I quickly send a flash logout notification through Express or verify if a redirection has occurred

Hey everyone, I'm currently dealing with an issue in my server.js setup where I'm trying to display the message "logged out successfully" on the /login page when a user logs out. The problem stems from using multiple middleware and feeling overwh ...

Utilizing Javascript to retrieve the current Controller and Action in Rails

Is it possible for JavaScript to determine the current controller and action in Rails? I had the idea of creating a hidden container to store the current controller and action, but I'm curious if there is a specific JavaScript function for this purpo ...

Access the Express server by connecting to its IP address

Is it feasible to connect to an express server using the server's UP address? If so, how would one go about doing this? (Examples of code snippets would be greatly appreciated) ...

Unable to reach the sub-component of the JSON object that was returned

For some reason, I can't seem to access a sub object of a returned $Resource object that fetched a group of JSON objects. It's baffling me. Resource > $resolved: true > $then: function (b, g) {var j=e(),h= > data: Object > 519bc5f6 ...