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 6128485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:37:56+00:00 2026-05-23T16:37:56+00:00

Im trying to understand how to make a javascript animation run smoothly and I’ve

  • 0

Im trying to understand how to make a javascript animation run smoothly and I’ve been reading some answers around here and I found something I don’t understand.

Here is the link to the question Smooth javascript animation

In the answer with most votes it says “which is why generally it’s a good idea to base position/frame on the amount of time that has elapsed since the start of the animation (using new Date().getTime()) rather than moving/changing a fixed amount each frame.”

Can anyone show me a very very simple example that uses the method from this answer, and explain how you then control the speed of the animation?

  • 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-05-23T16:37:57+00:00Added an answer on May 23, 2026 at 4:37 pm

    The basic idea is the following:

    • You want to move a DIV from 0,0 to 100,0 in 1 second.
    • You’d like it to have 50 frames per second (using a setTimeout(fun, 20))
    • However, since the callback is not guaranteed to run in exactly 20ms, your callback needs to figure out when it was actually run. For example, let’s say your animation started at time X but your first animation callback wasn’t called until X+5ms. You need to calculate the position that the div should be at 25ms of the animation, your animation callback can’t assume 50 even steps.

    Here’s a very simple code sample. I don’t usually code with globals but this is the easiest way to show the technique.

    http://jsfiddle.net/MWWm6/2/

    var duration = 1000; // in ms
    var startTime; // in ms
    var startX = 0;
    var endX = 500;
    // 0 means try to get as many frames as possible
    // > 1: Remember that this is not guaranteed to run as often as requested
    var refreshInterval = 0;
    var div = document.getElementById('anim');
    
    function updatePosition() {
        var now = (new Date()).getTime();
        var msSinceStart = now - startTime;
        var percentageOfProgress = msSinceStart / duration;
        var newX = (endX - startX) * percentageOfProgress;
        div.style.left = Math.min(newX, endX) + "px";
        if (window.console) {
            console.log('Animation Frame - percentageOfProgress: ' + percentageOfProgress + ' newX = ' + newX);
        }
        if (newX < endX) {
            scheduleRepaint();
        }
    }
    
    function scheduleRepaint() {
        setTimeout(updatePosition, refreshInterval);
    }
    
    div.onclick = function() {
        startTime = (new Date()).getTime();
        scheduleRepaint();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand some scripts that I have inherited and make use
I'm trying to make some tests on a JavaScript application and someone advised me
I am trying to apply some javascript to a pdf to make it print
I've been trying to understand how Ruby blocks work, and to do that I've
I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is
I am trying to understand how to best make use of blocks in my
im trying to understand the get and set properties for fields, and run in
not sure if this make sense at all im trying to understand how C#
I'm trying to understand OOP and having some trouble with a couple of concepts
This is a function taken from Pro JavaScript techniques that I'm trying to understand,

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.