After reading up on all of the buzz around animating SVGs with the strokeDashArray trick I finally found a little bit of code to connect this functionality to the user's scroll position on the Cable Codes blog.
This idea is great, but I have a wavy, curvy path that actually loops at one point. So I divided the calculated total length of the path to get a good middle ground but it still falls short with the line lagging behind or getting ahead as you scroll.
Here's an example of the snippet with some adjustments:
$(document).ready(function(){
$(window).scroll(function() {
drawLine( $('#route'), document.getElementById('path') );
});
//draw the line
function drawLine(container, line){
var pathLength = line.getTotalLength(),
distanceFromTop = container.offset().top - $(window).scrollTop(),
percentDone = 1 - (distanceFromTop / $(window).height()),
length = percentDone * pathLength / 30;
line.style.strokeDasharray = [ length ,pathLength].join(' ');
}
});
I made a CodePen with the path and the snippet of code with my adjustments. Any ideas as to how I could keep the end of this line in the centre of the viewport as the user scrolls?
Updated: The SVG is, in fact, responsive. This changes a few things, eh? Here's an updated CodePen.