-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilter3.js
64 lines (56 loc) · 1.96 KB
/
filter3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
export default function(text) {
// Get the filter ID from the element's data attribute
const filterId = text.getAttribute('data-filter');
// Select the necessary SVG filter elements
const feBlur = document.querySelector(`#${filterId} feGaussianBlur`);
const feDisplacementMap = document.querySelector(`#${filterId} feDisplacementMap`);
// Check if required elements exist
if (!feBlur || !feDisplacementMap) {
console.warn(`Filter with ID ${filterId} not found for element`, text);
return;
}
// Apply the filter to the text element
text.style.filter = `url(#${filterId})`;
// Object to store the values for blur and displacement
let primitiveValues = { stdDeviation: 0, scale: 0 };
// Set initial opacity to 0
gsap.set(text, { opacity: 0 });
// Create the animation timeline
const animationTimeline = gsap.timeline({
defaults: {
duration: 2,
ease: 'power4',
},
// On every update, set the appropriate attributes in the SVG filters
onUpdate: () => {
feBlur.setAttribute('stdDeviation', primitiveValues.stdDeviation);
feDisplacementMap.setAttribute('scale', primitiveValues.scale);
},
scrollTrigger: {
trigger: text, // Trigger the animation when the text enters the viewport
start: 'center bottom',
}
})
.to(primitiveValues, {
startAt: { stdDeviation: 40, scale: 150 }, // Start with a strong blur and high displacement
stdDeviation: 0,
scale: 0
}, 0)
.to(text, {
startAt: {
opacity: 0,
scale: 0.9
},
opacity: 1,
scale: 1
}, 0);
// Find the corresponding replay button (assumes button is the next sibling)
const replayButton = text.nextElementSibling;
// Add click event listener to the replay button
if (replayButton && replayButton.classList.contains('replay')) {
replayButton.addEventListener('click', () => {
// Restart the timeline when the button is clicked
animationTimeline.restart();
});
}
}