🎉 Day 3: Mastering SVG Animations with GSAP 🎨✨
Ashish prajapati

Ashish prajapati @anticoder03

About: Development isn’t about big projects; it’s about learning small things daily. Each concept you master builds your skills, shaping you into a better, more confident developer. Keep going!

Location:
Gujarat, India
Joined:
Aug 26, 2024

🎉 Day 3: Mastering SVG Animations with GSAP 🎨✨

Publish Date: Dec 11 '24
1 0

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! 🚀💥

Comments 0 total

    Add comment