Last active
October 1, 2021 02:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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