1. var path = {top: [0, 100, 300, 400], left: [0, 100, 300, 400], distance:[], percent:[]};
  2. var totalDistance = 0;
  3.  
  4. // Calculate distance between points using Pythagoras theorem
  5. var distanceX;
  6. var distanceY;
  7. for(var i = 1; i< path.top.length; ++i) {
  8. distanceX = path.top[i] - path.top[i-1];
  9. distanceY = path.left[i] - path.left[i-1];
  10.  
  11. path.distance[i] = Math.sqrt((distanceX * distanceX) + (distanceY * distanceY));
  12.  
  13. totalDistance += path.distance[i];
  14. }
  15.  
  16. // Calculate and add property for how many percent part of the animation should take up
  17. for(i = 1; i < path.distance.length; ++i) {
  18. path.percent[i] = path.distance[i] / totalDistance;
  19. console.log(i, path.percent[i]);
  20.  
  21. }
  22.  
  23. var frameCounter = 0;
  24. var fps = 10;
  25. var animationDurationInSeconds = 10;
  26.  
  27. //setInterval(updatePosition, 1000 / fps);
  28.  
  29. function updatePosition() {
  30. if((frameCounter / fps) <= animationDurationInSeconds) {
  31. var animationPercent = frameCounter / (fps * animationDurationInSeconds);
  32.  
  33. ++frameCounter;
  34. }
  35. }