Here's the full source to a plugin I'm developing, the relevant code I want to refactor is:
function measure($element, $bubble){
var scrollTop = $window.scrollTop(),
viewportHeight = $window.height(),
readableTop = $element.position().top,
readableHeight = $element.height(),
readableBottom = readableTop + readableHeight,
scrollHeight = viewportHeight / readableHeight * viewportHeight,
progress = (scrollTop - readableTop) / (readableHeight - viewportHeight),
total = getTotalMinutes($element),
remaining = Math.ceil(total - (total * progress)),
distanceProgress = (scrollTop - readableTop) / readableHeight,
distanceLiteral = readableTop + distanceProgress * readableHeight + viewportHeight / 2 - $bubble.height() / 2,
distance = Math.max(readableTop, Math.min(distanceLiteral, readableBottom));
return {
remaining: remaining,
progress: progress,
distance: distance
};
}
How might I make this calculation more understandable? I'm not terribly good at math, so I got there by trial and error, pretty much.
Now I'd like to refactor into something more readable or at least trim the repetitive nonsense, but I have no clue where to start.
Could you lend me a hand or give me any advice on how to tackle this?