Skip to content

Instantly share code, notes, and snippets.

@Daniel-Walsh
Last active October 1, 2021 02:28
Show Gist options
  • Save Daniel-Walsh/39fa523dbfa19015add3492d27f53e98 to your computer and use it in GitHub Desktop.
Save Daniel-Walsh/39fa523dbfa19015add3492d27f53e98 to your computer and use it in GitHub Desktop.
Calculate the animation time of an element, based on the distance and speed it should travel #javascript #animation
// set your global speed
const speed = 50;
// original position
const x1 = 334;
const y1 = 644;
// new position
const x2 = 182;
const y2 = 598;
// calculate your distance
const distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
// calculate your time in milliseconds
const time = (distance / speed) * 1000;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment