🍣

【GSAP】GSAP Practice【#9 GSAP Sample2 SVG Animation1】

2024/12/20に公開

【#9 GSAP Sample2 SVG Animation1】

YouTube: https://youtu.be/yz2X0bSG2iE
https://youtu.be/yz2X0bSG2iE

今回からSVGにGsapのアニメーションを
設定するサンプルの作成を行います。

今回はSVGの描画と事前準備を行います。

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Gsap practice</title>
    <link rel="stylesheet" href="css/main.css" />
  </head>
  <body>
    <div class="section-1">
      <div id="loading">Loading</div>
    </div>
    <div class="section-2">
      <div id="stroke-wrapper">
        <svg height="400" width="1000">
          <path
            d="M 20 150 Q 480 150 980 150"
            stroke="white"
            fill="transparent"
          />
        </svg>
      </div>
    </div>

    <script src="https://unpkg.com/lenis@1.1.16/dist/lenis.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/ScrollTrigger.min.js"></script>
    <script src="js/main.js"></script>
  </body>
</html>
css/main.css
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,
body {
  width: 100%;
  height: 100%;
}
.section-1,
.section-2 {
  width: 100%;
  height: 100vh;
}
.section-1 {
  background-color: salmon;
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: center;
  justify-content: center;
}

#loading {
  color: white;
  font-size: 60px;
  display: flex;
}

#loading span {
  display: block;
}

.section-2 {
  background-color: skyblue;
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: center;
  justify-content: center;
}
#stroke-wrapper {
  width: 1000px;
  margin: 0 auto;
  background-color: gray;
}
.box {
  width: 160px;
  height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}
.box-1 {
  background-color: red;
}
.box-2 {
  background-color: blue;
}
.box-3 {
  background-color: green;
}
.box-4 {
  background-color: purple;
}
js/main.js
// use a script tag or an external JS file
document.addEventListener("DOMContentLoaded", (event) => {
  gsap.registerPlugin(ScrollTrigger);
  new App();
});

class App {
  constructor() {
    this._initialize();
    this._renderLenis();
  }

  _initialize() {
    this._createLenis();
    this._sec1Anime();
    this._sec2Anime();
  }

  _createLenis() {
    this.lenis = new Lenis();
  }

  _renderLenis(time) {
    this.lenis.raf(time);
    requestAnimationFrame(this._renderLenis.bind(this));
  }

  _sec1Anime() {
    const loadingEl = document.querySelector("#loading");
    const loadingTexts = loadingEl.textContent.split("");
    let innerText = "";

    loadingTexts.forEach((text) => {
      innerText += `<span>${text}</span>`;
    });

    // console.log(loadingTexts);
    // console.log(innerText);

    loadingEl.innerHTML = innerText;

    const tl = gsap.timeline({ repeat: -1 });

    tl.to("#loading span", {
      duration: 1,
      y: -50,
      stagger: {
        each: 0.2,
        from: "left",
      },
    }).to("#loading span", {
      duration: 1,
      y: 0,
      stagger: {
        each: 0.2,
        from: "left",
      },
      ease: "bounce.out",
    });
  }

  _sec2Anime() {
    const strokeEl = document.querySelector("#stroke-wrapper");
    const strokeElHeight = strokeEl.clientHeight;
    const rectX = strokeEl.getBoundingClientRect().left;
    const rectY = strokeEl.getBoundingClientRect().top;

    let orgPath = `M 20 ${strokeElHeight / 2} Q 480 ${strokeElHeight / 2} 980 ${
      strokeElHeight / 2
    }`;

    gsap.set("svg path", {
      attr: {
        d: orgPath,
      },
    });
  }
}

Discussion