Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:29:46+00:00 2026-06-14T13:29:46+00:00

I have written a small animate function using three.js for 3D rendering(full test application

  • 0

I have written a small animate function using three.js for 3D rendering(full test application below):

each time I click on the screen the cube rotates based on the animation descibed in the function call. But I was expecting that if the cube was already animating under one animation adding another would cause it to flicker about as the same properties are being animated by multiple animation calls. but this is not what happens the last animation is halted and the new one takes over, even though my callback function at the end shows that the old animation function was still running! So why does the cube not flicker about when multiple clicks are sent?

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <script src="three-mini.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <script>
        var animate = function(obj, prop, targetValue, length, callback) {

            var startTime = Date.now(), inverseLength = 1 / length, startValue = obj[prop];

            if( typeof targetValue === "string") {
                if(targetValue.substring(0, 2) === "+=") {
                    targetValue = obj[prop] + Number(targetValue.substring(2));
                } else {
                    targetValue = obj[prop] - Number(targetValue.substring(2));
                }
            }

            var animateProp = function() {

                var elapsed = (Date.now() - startTime) * inverseLength;

                if(elapsed >= 1) {

                    obj[prop] = targetValue;

                    if( callback instanceof Function) {
                        requestAnimationFrame(callback);
                    }
                    return;

                } else {

                    obj[prop] = (startValue - targetValue) * (Math.cos(elapsed * Math.PI) + 1) * 0.5 + targetValue;

                    requestAnimationFrame(animateProp);

                }

            };
            requestAnimationFrame(animateProp);

        };
        var camera, scene, renderer;
        var geometry, material, mesh;

        init();

        function init() {
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
            camera.position.z = 1000;
            scene = new THREE.Scene();
            geometry = new THREE.CubeGeometry(200, 200, 200);
            material = new THREE.MeshBasicMaterial({
                color : 0xff0000,
                wireframe : true
            });
            mesh = new THREE.Mesh(geometry, material);
            scene.add(mesh);
            renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);

            document.body.appendChild(renderer.domElement);
            renderer.domElement.addEventListener('click', function() {
                animate(mesh.rotation, "x", "-=" + Math.PI * 2 * 10, 5000, function() {
                    alert("CALLED BACK!")
                });
                animate(mesh.rotation, "y", "-=" + Math.PI * 2 * 10, 15000, function() {
                });
            });
            window.addEventListener('load', render);
            window.addEventListener('resize', function() {
                renderer.setSize(window.innerWidth, window.innerHeight);
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
            });
        }

        function render() {

            // note: three.js includes requestAnimationFrame shim
            requestAnimationFrame(render);

            renderer.render(scene, camera);

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

UPDATE 1:

In an attempt to find out what’s going on I add more meshes and animated them all differently, the code is below, it animates each mesh for each subsequent click and it animates them first by rotation then it moves them forwards then backwards, then back to rotation. You can animate position without halting the animation on rotation and you can animate one mesh at the same time as another mesh without halting previous animations, so why doesn’t the animation flicker when multiple animations are running on the same mesh and on the same properties?

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <script src="three-mini.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <script>
        var animate = function(obj, prop, targetValue, length, callback) {

            var startTime = Date.now(), inverseLength = 1 / length, startValue = obj[prop];

            if( typeof targetValue === "string") {
                if(targetValue.substring(0, 2) === "+=") {
                    targetValue = obj[prop] + Number(targetValue.substring(2));
                } else {
                    targetValue = obj[prop] - Number(targetValue.substring(2));
                }
            }

            var animateProp = function() {

                var elapsed = (Date.now() - startTime) * inverseLength;

                if(elapsed >= 1) {

                    obj[prop] = targetValue;

                    if( callback instanceof Function) {
                        requestAnimationFrame(callback);
                    }
                    return;

                } else {

                    obj[prop] = (startValue - targetValue) * (Math.cos(elapsed * Math.PI) + 1) * 0.5 + targetValue;

                    requestAnimationFrame(animateProp);

                }

            };
            requestAnimationFrame(animateProp);

        };
        var camera, scene, renderer, geometry, material, mesh1, mesh2, mesh3, mesh4, mesh5, i = 0, j = 0;

        init();

        function init() {
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
            camera.position.z = 1000;
            scene = new THREE.Scene();
            geometry = new THREE.CubeGeometry(200, 200, 200);
            material = new THREE.MeshBasicMaterial({
                color : 0xff0000,
                wireframe : true
            });
            mesh1 = new THREE.Mesh(geometry, material);
            scene.add(mesh1);
            mesh2 = new THREE.Mesh(geometry, material);
            mesh2.position.x = mesh2.position.y = mesh2.position.z = 200;
            scene.add(mesh2);
            mesh3 = new THREE.Mesh(geometry, material);
            mesh3.position.x = mesh3.position.z = 200;
            mesh3.position.y = -200;
            scene.add(mesh3);
            mesh4 = new THREE.Mesh(geometry, material);
            mesh4.position.y = mesh4.position.z = 200;
            mesh4.position.x = -200;
            scene.add(mesh4);
            mesh5 = new THREE.Mesh(geometry, material);
            mesh5.position.y = mesh5.position.x = -200;
            mesh5.position.z = 200;
            scene.add(mesh5);
            renderer = new THREE.CanvasRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);

            document.body.appendChild(renderer.domElement);
            renderer.domElement.addEventListener('click', function() {
                var mesh;
                if(i === 5) {
                    i = 0;
                    j++;
                    if(j === 3) {
                        j = 0;
                    }
                }
                i++;

                var mesh = window['mesh' + i];
                if(j === 1) {
                    animate(mesh.position, "z", "+=" + 500, 2000, function() {
                        //alert("CALLED BACK!")
                    });
                    return;
                }
                if(j === 2) {
                    animate(mesh.position, "z", "-=" + 500, 3000, function() {
                        //alert("CALLED BACK!")
                    }); retunr;
                }
                animate(mesh.rotation, "x", "-=" + Math.PI * 2 * 5, 5000, function() {
                    //alert("CALLED BACK!")
                });
                animate(mesh.rotation, "y", "-=" + Math.PI * 2 * 6, 10000, function() {
                });
            });
            window.addEventListener('load', render);
            window.addEventListener('resize', function() {
                renderer.setSize(window.innerWidth, window.innerHeight);
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
            });
        }

        function render() {

            // note: three.js includes requestAnimationFrame shim
            requestAnimationFrame(render);

            renderer.render(scene, camera);

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

comment for WestLangley:
let me try and explain a bit more, I apologise for not being clear. lets say I click once and it starts an animation from position.x = 0 to targetValue: '+=200'. and then halfway through that animation I click again which this time animates in the negative x direction which will start with position.x = 100 (as it’s starting halfway through the first animation) and has targetValue: '-=200'. Now I expect that the first animation is still running so it is continuing to animate from x=100 to x=200, and the second animation is now also running from x=100 to x=-100, so as each animation function is called I would expect to see the cube jump left and right until the first animation comes to an end and then the second animation can continue unhindered. This is what I was expecting to happen, thus I was expecting it to flicker. But apparently multiple animation functions are running at the same time but only the latest one is having any effect updating the mesh properties. :S at least as far as I can tell.

My main worry about this is that I can tell from the experimenting that the “hidden” animation calls are still being processed and so throwing away precious processor cycles, it also leads to problems with adding animation calls in the callback function. To this end my main concern is how can I stop these “hidden” animation calls?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:29:47+00:00Added an answer on June 14, 2026 at 1:29 pm

    I’m still not entirely certain on what is happening, but what I do know is that multiple animations are running at the same time even though the earlier ones are not being shown. My main concern about this is that it’s using up processing power that is not necessary. So I have added in a suffix notation and I now add the animating functions to the animated property owning object itself, so if I am animating position.x i create my animation function and assign it to position.x_animate and position.x_animate_alt alternatively. this is so I can cancel the previous, still running animation so I’m not wasting processor cycles. full snippet just for animate function below:

    var animate = function(obj, prop, targetValue, length, callback) {
    
                var suffix = '_animate', altSuffix = '_animate_alt', thisSuffix = suffix, startTime = Date.now(), inverseLength = 1 / length, startValue = obj[prop];
    
                if( typeof targetValue === "string") {
                    if(targetValue.substring(0, 2) === "+=") {
                        targetValue = obj[prop] + Number(targetValue.substring(2));
                    } else {
                        targetValue = obj[prop] - Number(targetValue.substring(2));
                    }
                }
    
                if(obj[prop+suffix] instanceof Function){
                    obj[prop+suffix].cancelled = true;
                    thisSuffix = altSuffix;
                }
                if(obj[prop+altSuffix] instanceof Function){
                    obj[prop+altSuffix].cancelled = true;
                    thisSuffix = suffix;
                }
    
                obj[prop+thisSuffix] = function() {
    
                    var elapsed;
                    if(obj[prop+thisSuffix].cancelled){
                        delete obj[prop+thisSuffix];
                        return;
                    }
                    elapsed = (Date.now() - startTime) * inverseLength;
    
                    if(elapsed >= 1) {
    
                        obj[prop] = targetValue;
                        delete obj[prop+thisSuffix];
                        if( callback instanceof Function) {
                            requestAnimationFrame(callback);
                        }
                        return;
    
                    } else {
    
                        obj[prop] = (startValue - targetValue) * (Math.cos(elapsed * Math.PI) + 1) * 0.5 + targetValue;
    
                        requestAnimationFrame(obj[prop+thisSuffix]);
    
                    }
    
                };
    
                requestAnimationFrame(obj[prop+thisSuffix]);
    
            };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a small application in C# using Visual Studio 2010. It is
I have written a small Asp.net application using Entity Framework. A Stored Procedure accepts
i have written a small Rails Application where i can upload three Files which
I have written a small .net Windows Forms application. And now I decided to
I have written a small function, which uses ElementTree to parse xml file,but it
Hi I am learning JQuery and I have written a small function where I
I have written a small unit test for my django view .My project structure
I have written a small Python application where I use PyGame for displaying some
i have written a small form application, which contains textbox only. I have enabled
I have written a small application which will be used in my work environment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.