[go: up one dir, main page]

DEV Community

Cover image for πŸŽ‰ Day 3: Mastering SVG Animations with GSAP 🎨✨
Ashish prajapati
Ashish prajapati

Posted on

πŸŽ‰ Day 3: Mastering SVG Animations with GSAP 🎨✨

Today, I dived into the mesmerizing world of SVG Animations πŸ–ΌοΈ using GSAP. SVG (Scalable Vector Graphics) is the secret sauce for creating detailed, scalable visuals, and GSAP takes it to the next level with smooth and intuitive animations. Here's what I learned and implemented. πŸš€


πŸ’‘ Key Concepts Covered

  1. πŸ–‹οΈ SVG Paths: SVG paths are defined using a series of commands and coordinates (e.g., M for move, Q for quadratic Bezier curves). These paths are the foundation for dynamic animations.
  2. πŸ–±οΈ Mouse Events: Leveraging JavaScript's mousemove and mouseleave events to track user interactions.
  3. πŸ› οΈ GSAP's attr Plugin: The attr property in GSAP allows seamless animation of SVG attributes, like d for paths.

✨ Code Breakdown

Here’s the magic code for today’s lesson πŸ§™β€β™‚οΈ:

var Path = `M 10 100 Q 500 100 990 100`; // Initial path
var finalPath = `M 10 100 Q 500 100 990 100`; // Path to revert to on mouse leave

var string = document.querySelector("#string"); // Select the interactive area

// 🎯 Mousemove Event: Dynamically adjust the SVG path
string.addEventListener("mousemove", function(dets) {
    path = `M 10 80 Q ${dets.x} ${dets.y} 990 80`; // Adjust path based on mouse position
    gsap.to("svg path", {
       attr: { d: path }, // Animate the 'd' attribute of the path
       duration: 0.3,
       ease: "power1.out" // Smooth easing effect ✨
    });
});

// πŸ”„ Mouseleave Event: Revert the path to its original form
string.addEventListener("mouseleave", function(dets) {
    gsap.to("svg path", {
       attr: { d: finalPath }, // Revert to original path
       duration: 0.3,
       ease: "elastic.out(1, 0.3)" // Bouncy easing effect 🎒
    });
});
Enter fullscreen mode Exit fullscreen mode

🧐 What Happens in the Code?

  • 🎯 Mousemove Animation:

    • Captures the mouse's x and y coordinates πŸ–±οΈ.
    • Adjusts the control point of the quadratic Bezier curve (Q ${dets.x} ${dets.y}) dynamically.
    • GSAP's attr animates the d attribute of the path, creating fluid motion 🌊.
  • πŸ”„ Mouseleave Animation:

    • Reverts the path to its original shape when the mouse leaves the area.
    • Uses an elastic bounce effect for an engaging experience πŸͺ„.

πŸŽ₯ Visual Demo

Imagine an SVG path that reacts to your mouse movement, bending and flexing like a string 🎻. When you move away, it gracefully snaps back to its original form with a satisfying bounce! πŸͺ„πŸ’«


πŸ’ͺ Challenges Faced

  • Wrapping my head around the d attribute for SVG paths, especially understanding how quadratic Bezier curves (Q) work πŸŒ€.
  • Fine-tuning easing functions (power1.out, elastic.out) to achieve the perfect look and feel πŸŽ›οΈ.

🌟 What I Loved

The interactivity of SVG animations combined with GSAP’s ease of use unlocks limitless creative possibilities. This project gave me a hands-on understanding of how SVG and GSAP work together to produce captivating animations. βœ¨πŸ’‘


πŸ› οΈ Next Steps

  • Experiment with animating stroke paths and fills 🎨.
  • Combine SVG animations with scroll-triggered events for even more immersive effects πŸ”—.

πŸ”— Project Links

Stay tuned for more GSAP adventures! πŸš€πŸ’₯

Top comments (0)