Improve function for calculation animation speed