🦔
【GSAP】GSAP Practice【#10 GSAP Sample2 SVG Animation2】
【#10 GSAP Sample2 SVG Animation2】
YouTube: https://youtu.be/aoE-vWFvNLE
今回はマウスカーソルの動きに合わせたアニメーションの設定を行います。
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,
},
});
strokeEl.addEventListener("mousemove", (e) => {
let strokeQY = strokeElHeight / 2;
let strokeQX = e.x - rectX;
let strokeRectY = e.y - rectY;
if (strokeRectY > strokeElHeight / 2) {
strokeQY = strokeRectY - strokeElHeight / 2;
} else {
strokeQY = strokeRectY + strokeElHeight / 2;
}
let path = `M 20 ${strokeElHeight / 2} Q ${strokeQX} ${strokeQY} 980 ${
strokeElHeight / 2
}`;
gsap.to("svg path", {
attr: {
d: path,
},
duration: 0.3,
ease: "power3.out",
});
});
strokeEl.addEventListener("mouseleave", () => {
gsap.to("svg path", {
attr: {
d: orgPath,
},
duration: 1.5,
ease: "elastic.out(1,0.3)",
});
});
}
}
Discussion