SVG Draw
Each <path> gets strokeDasharray and strokeDashoffset both set to path.getTotalLength(). This makes the entire path invisible (the dash offset shifts the dash completely out of view). GSAP tweens strokeDashoffset to 0 with scrub: 1, drawing the path progressively as the user scrolls.
각 <path>의 strokeDasharray와 strokeDashoffset을 path.getTotalLength()로 설정합니다. 이렇게 하면 전체 경로가 보이지 않게 됩니다. GSAP가 scrub: 1로 strokeDashoffset을 0으로 트윈해 사용자가 스크롤할수록 경로가 그려집니다.
Source Code script.js
gsap.registerPlugin(ScrollTrigger);
// Animate all .draw-path elements via scroll
document.querySelectorAll('.section').forEach((section) => {
const paths = section.querySelectorAll('.draw-path');
paths.forEach((path) => {
const length = path.getTotalLength();
// Set up the dash
gsap.set(path, {
strokeDasharray: length,
strokeDashoffset: length,
});
// Animate dash offset as section scrolls in
gsap.to(path, {
strokeDashoffset: 0,
ease: 'none',
scrollTrigger: {
trigger: section,
start: 'top 80%',
end: 'bottom 40%',
scrub: 1,
},
});
});
});