🧠 Description
This project showcases an elegant scroll-triggered animation powered by GSAP (GreenSock Animation Platform) and its MotionPathPlugin.
The animation allows an element to smoothly travel along a custom motion path defined by multiple waypoint markers positioned throughout the page.
As users scroll down the page, the animated element follows the motion path in real-time, creating a seamless visual connection between scroll progress and element movement.
The design uses GSAP's ScrollTrigger to synchronize the animation with scroll position and MotionPathPlugin to calculate smooth curves connecting all waypoints.
Perfect for interactive storytelling, guided tours, and creative scroll experiences.
💻 HTML Code
HTML Structure Explanation
The HTML is structured around a scrollable layout with motion path markers:
Key Elements:
.spacer- Top and bottom spacer sections to enable scrolling.main- Container with 300vh height to provide scrollable area.container.initial- Starting position containing the animated.boxelement.container.second/third/fourth/fifth/sixth- Waypoint markers that define the motion path.marker- Visual indicators for each waypoint.box- The element that will animate along the motion path
The layout uses absolute positioning to place markers at strategic locations throughout the scrollable area.
GSAP plugins (ScrollTrigger and MotionPathPlugin) are loaded via CDN for animation functionality.
🎨 CSS Code
CSS Breakdown
Grid Background
Multiple layered gradients create a subtle grid pattern for visual interest.
Custom CSS variables store reusable color values.
Responsive Layout
Flexbox centers spacer content for visual cues.
Absolute positioning allows precise marker placement.
Visual Indicators
Dashed borders on .container elements show waypoint locations.
Subtle opacity values (0.1, 0.4, 0.7) create depth perception.
Animated Element Styling
.box uses background-image for visual representation.
Border-radius creates rounded corners for polish.
Z-index ensures the animated element stays above markers.
Spacer Sections
20vh height provides visual breathing room and scrollable space.
The grid background fills the entire viewport for continuity.
⚙️ JavaScript Code
JavaScript Breakdown
GSAP Plugin Registration
gsap.registerPlugin(ScrollTrigger, MotionPathPlugin) - Enables scroll-triggered animations and motion path calculations.
Context Management
gsap.context() - Creates a scoped context for animations, allowing proper cleanup with ctx.revert().
Prevents memory leaks by reverting animations on resize or page updates.
Element Selection & Position Calculation
Retrieves the animated .box element and calculates its viewport position.
Selects all waypoint .container elements except the initial one.
Motion Points Array
Dynamically calculates relative positions of each waypoint using getBoundingClientRect().
Converts absolute viewport coordinates to relative coordinates (offset from box's starting position).
The points array stores x, y coordinates for each waypoint the element will visit.
ScrollTrigger Configuration
trigger: ".container.initial" - Animation starts when initial container enters viewport.
start: "clamp(top center)" - Prevents over-scrolling; clamps to visible area.
endTrigger: ".final" - Animation ends when final spacer section is reached.
scrub: 1 - Directly syncs animation progress with scroll position (1 = 1 second delay).
MotionPath Animation
motionPath: { path: points, curviness: 1.5 } - Animates the box along the calculated path.
curviness: 1.5 - Smoothness of curve (0 = straight lines, higher = more curved).
ease: "none" - Linear easing ensures consistent motion tied to scroll speed.
Responsive Behavior
window.addEventListener("resize", createTimeline) - Recalculates waypoints on window resize.
Ensures motion path stays accurate as viewport dimensions change.
The timeline automatically updates to track new marker positions.
This combination creates a smooth, synchronized scroll experience where the element follows a perfectly curved path through all waypoints as users scroll the page.
