Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconFundamentals of Web Animation with GSAP
Fundamentals of Web Animation with GSAP

Chapter 4: Advanced Animation Technique

Practical Exercises for Chapter 4: Advanced Animation Technique

Congratulations on completing Chapter 4! To solidify your understanding of the advanced animation techniques discussed, here are some practical exercises. These will challenge you to apply the skills you've learned in real-world scenarios. After attempting each exercise, review the provided solutions to compare your approach.

Exercise 1: Create a ScrollTrigger Animation

Implement an animation where elements fade in and move up as you scroll down the page.

Solution:
HTML:

<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

CSS:

.box {
    opacity: 0;
    transform: translateY(100px);
    margin: 50px;
    height: 100px;
    background-color: blue;
}

JavaScript:

gsap.registerPlugin(ScrollTrigger);

gsap.utils.toArray(".box").forEach(box => {
  gsap.to(box, {
    scrollTrigger: box,
    opacity: 1,
    translateY: 0,
    duration: 1
  });
});

Exercise 2: Morphing SVG Paths with GSAP

Create an animation where one SVG path smoothly transitions into another.

Solution:
HTML:

<svg width="200" height="200">
  <path id="startPath" d="M10 80 Q 95 10 180 80 T 350 80" stroke="blue" fill="transparent"/>
  <path id="endPath" d="M10 180 Q 95 110 180 180 T 350 180" stroke="red" fill="transparent" visibility="hidden"/>
</svg>

JavaScript:

gsap.registerPlugin(MorphSVGPlugin);

gsap.to("#startPath", {
  duration: 2,
  morphSVG: "#endPath"
});

Exercise 3: Responsive Animation Based on Screen Width

Design an animation where the distance of element movement changes based on the screen width.

Solution:
JavaScript:

let distance = window.innerWidth < 768 ? "50px" : "100px";
gsap.to(".box", {duration: 2, x: distance});

Exercise 4: Interactive SVG Hover Animation

Create an interactive hover effect on an SVG element where it scales up on mouseover and returns to its original size on mouseout.

Solution:
HTML:

<svg width="100" height="100">
  <circle id="circle" cx="50" cy="50" r="40" fill="blue" />
</svg>

JavaScript:

document.getElementById("circle").addEventListener("mouseover", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1.2});
});

document.getElementById("circle").addEventListener("mouseout", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1});
});

These exercises are designed to help you practice and refine your skills in advanced GSAP animations, including responsive design, SVG morphing, and interactive animations. By working through these challenges, you'll gain a deeper understanding of how to apply GSAP in various scenarios, enhancing your ability to create sophisticated and engaging web animations. Keep experimenting with different techniques, and you'll continue to grow as an accomplished web animator.

Practical Exercises for Chapter 4: Advanced Animation Technique

Congratulations on completing Chapter 4! To solidify your understanding of the advanced animation techniques discussed, here are some practical exercises. These will challenge you to apply the skills you've learned in real-world scenarios. After attempting each exercise, review the provided solutions to compare your approach.

Exercise 1: Create a ScrollTrigger Animation

Implement an animation where elements fade in and move up as you scroll down the page.

Solution:
HTML:

<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

CSS:

.box {
    opacity: 0;
    transform: translateY(100px);
    margin: 50px;
    height: 100px;
    background-color: blue;
}

JavaScript:

gsap.registerPlugin(ScrollTrigger);

gsap.utils.toArray(".box").forEach(box => {
  gsap.to(box, {
    scrollTrigger: box,
    opacity: 1,
    translateY: 0,
    duration: 1
  });
});

Exercise 2: Morphing SVG Paths with GSAP

Create an animation where one SVG path smoothly transitions into another.

Solution:
HTML:

<svg width="200" height="200">
  <path id="startPath" d="M10 80 Q 95 10 180 80 T 350 80" stroke="blue" fill="transparent"/>
  <path id="endPath" d="M10 180 Q 95 110 180 180 T 350 180" stroke="red" fill="transparent" visibility="hidden"/>
</svg>

JavaScript:

gsap.registerPlugin(MorphSVGPlugin);

gsap.to("#startPath", {
  duration: 2,
  morphSVG: "#endPath"
});

Exercise 3: Responsive Animation Based on Screen Width

Design an animation where the distance of element movement changes based on the screen width.

Solution:
JavaScript:

let distance = window.innerWidth < 768 ? "50px" : "100px";
gsap.to(".box", {duration: 2, x: distance});

Exercise 4: Interactive SVG Hover Animation

Create an interactive hover effect on an SVG element where it scales up on mouseover and returns to its original size on mouseout.

Solution:
HTML:

<svg width="100" height="100">
  <circle id="circle" cx="50" cy="50" r="40" fill="blue" />
</svg>

JavaScript:

document.getElementById("circle").addEventListener("mouseover", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1.2});
});

document.getElementById("circle").addEventListener("mouseout", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1});
});

These exercises are designed to help you practice and refine your skills in advanced GSAP animations, including responsive design, SVG morphing, and interactive animations. By working through these challenges, you'll gain a deeper understanding of how to apply GSAP in various scenarios, enhancing your ability to create sophisticated and engaging web animations. Keep experimenting with different techniques, and you'll continue to grow as an accomplished web animator.

Practical Exercises for Chapter 4: Advanced Animation Technique

Congratulations on completing Chapter 4! To solidify your understanding of the advanced animation techniques discussed, here are some practical exercises. These will challenge you to apply the skills you've learned in real-world scenarios. After attempting each exercise, review the provided solutions to compare your approach.

Exercise 1: Create a ScrollTrigger Animation

Implement an animation where elements fade in and move up as you scroll down the page.

Solution:
HTML:

<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

CSS:

.box {
    opacity: 0;
    transform: translateY(100px);
    margin: 50px;
    height: 100px;
    background-color: blue;
}

JavaScript:

gsap.registerPlugin(ScrollTrigger);

gsap.utils.toArray(".box").forEach(box => {
  gsap.to(box, {
    scrollTrigger: box,
    opacity: 1,
    translateY: 0,
    duration: 1
  });
});

Exercise 2: Morphing SVG Paths with GSAP

Create an animation where one SVG path smoothly transitions into another.

Solution:
HTML:

<svg width="200" height="200">
  <path id="startPath" d="M10 80 Q 95 10 180 80 T 350 80" stroke="blue" fill="transparent"/>
  <path id="endPath" d="M10 180 Q 95 110 180 180 T 350 180" stroke="red" fill="transparent" visibility="hidden"/>
</svg>

JavaScript:

gsap.registerPlugin(MorphSVGPlugin);

gsap.to("#startPath", {
  duration: 2,
  morphSVG: "#endPath"
});

Exercise 3: Responsive Animation Based on Screen Width

Design an animation where the distance of element movement changes based on the screen width.

Solution:
JavaScript:

let distance = window.innerWidth < 768 ? "50px" : "100px";
gsap.to(".box", {duration: 2, x: distance});

Exercise 4: Interactive SVG Hover Animation

Create an interactive hover effect on an SVG element where it scales up on mouseover and returns to its original size on mouseout.

Solution:
HTML:

<svg width="100" height="100">
  <circle id="circle" cx="50" cy="50" r="40" fill="blue" />
</svg>

JavaScript:

document.getElementById("circle").addEventListener("mouseover", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1.2});
});

document.getElementById("circle").addEventListener("mouseout", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1});
});

These exercises are designed to help you practice and refine your skills in advanced GSAP animations, including responsive design, SVG morphing, and interactive animations. By working through these challenges, you'll gain a deeper understanding of how to apply GSAP in various scenarios, enhancing your ability to create sophisticated and engaging web animations. Keep experimenting with different techniques, and you'll continue to grow as an accomplished web animator.

Practical Exercises for Chapter 4: Advanced Animation Technique

Congratulations on completing Chapter 4! To solidify your understanding of the advanced animation techniques discussed, here are some practical exercises. These will challenge you to apply the skills you've learned in real-world scenarios. After attempting each exercise, review the provided solutions to compare your approach.

Exercise 1: Create a ScrollTrigger Animation

Implement an animation where elements fade in and move up as you scroll down the page.

Solution:
HTML:

<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

CSS:

.box {
    opacity: 0;
    transform: translateY(100px);
    margin: 50px;
    height: 100px;
    background-color: blue;
}

JavaScript:

gsap.registerPlugin(ScrollTrigger);

gsap.utils.toArray(".box").forEach(box => {
  gsap.to(box, {
    scrollTrigger: box,
    opacity: 1,
    translateY: 0,
    duration: 1
  });
});

Exercise 2: Morphing SVG Paths with GSAP

Create an animation where one SVG path smoothly transitions into another.

Solution:
HTML:

<svg width="200" height="200">
  <path id="startPath" d="M10 80 Q 95 10 180 80 T 350 80" stroke="blue" fill="transparent"/>
  <path id="endPath" d="M10 180 Q 95 110 180 180 T 350 180" stroke="red" fill="transparent" visibility="hidden"/>
</svg>

JavaScript:

gsap.registerPlugin(MorphSVGPlugin);

gsap.to("#startPath", {
  duration: 2,
  morphSVG: "#endPath"
});

Exercise 3: Responsive Animation Based on Screen Width

Design an animation where the distance of element movement changes based on the screen width.

Solution:
JavaScript:

let distance = window.innerWidth < 768 ? "50px" : "100px";
gsap.to(".box", {duration: 2, x: distance});

Exercise 4: Interactive SVG Hover Animation

Create an interactive hover effect on an SVG element where it scales up on mouseover and returns to its original size on mouseout.

Solution:
HTML:

<svg width="100" height="100">
  <circle id="circle" cx="50" cy="50" r="40" fill="blue" />
</svg>

JavaScript:

document.getElementById("circle").addEventListener("mouseover", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1.2});
});

document.getElementById("circle").addEventListener("mouseout", () => {
  gsap.to("#circle", {duration: 0.5, scale: 1});
});

These exercises are designed to help you practice and refine your skills in advanced GSAP animations, including responsive design, SVG morphing, and interactive animations. By working through these challenges, you'll gain a deeper understanding of how to apply GSAP in various scenarios, enhancing your ability to create sophisticated and engaging web animations. Keep experimenting with different techniques, and you'll continue to grow as an accomplished web animator.