Hello! I need help implementing animation into an SVG image I made. Its just a line that I would like to animate as you scroll down the page (the line forming down the page while scrolling)
So far, I've only managed to have the SVG appear but it simply sits on top of all the elements without moving or animating.
Does anyone know a code to have the SVG animate ? I've attached the current code I am working with. I am not too advanced with JavaScript, so animating the svg is proving to be a challenge for me. Any help on this would be greatly appreciated! Thank you!
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title› Document </title>
</head>
<body>
<div class="line-container">
<svg viewBox="0 0 215 2074" fill="none" preserveAspectRatio="xMidYMax meet">
<path d="M127 0V886V1020C76.6 1070.4 63.3334 1057 63 1044C74 1055 89.4 1083.6 63 1110C36.6 1136.4 117.333 1121 161 1110C192.333 1099.33 242.2 1078 191 1078C139.8 1078 84.3334 1163.33 63 1206L115 1150V1064L147 1174C121 1169.33 78.2 1169.2 115 1206C151.8 1242.8 151.667 1276 147 1288V1642V1786C146.333 1796.67 139 1818 115 1818C91 1818 90.3333 1832.67 93 1840C89.6667 1847.33 89.4 1862 115 1862C140.6 1862 111 1876.67 93 1884L127 1918V1974V1884H147L161 1870H175V1840H1L201 1918L147 1974L115 1992V1946H161L127 2002V2074" stroke="#F55D5D" stroke-width="5"/>
</svg>
</div>
</body>
</html>
</html>
<style>
body {
height: 500vh;
margin: 0;
z-index:9999999;
}
.line-container {
position:fixed;
top:0;
left:0;
width: 100%;
height: 100%;
text-align: center;
overflow: hidden;
z-index:9999999;
}
svg{
display: inline-block;
height:100%;
z-index:9999999;
}
</style>
<script>
let path = document.querySelector('path')
let pathLength = path.getTotalLength()
path.style.strokeDasharray = pathLength + '' + pathLength;
path.style.strokeDashoffset = pathLength;
window.addEventListener('scroll',()=>{
var scrollPercentage = (document.documentElement.scrollTop + document.body.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);
var drawLength = pathLength * scrollPercentage;
path.style.strokeDashoffset = pathLength - drawLength;
}
</script>