-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilter1.js
58 lines (50 loc) · 1.65 KB
/
filter1.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
export default function(text) {
// Get the filter ID from the element's data attribute
const filterId = text.getAttribute('data-filter');
// Select the feGaussianBlur element within the filter
const feBlur = document.querySelector(`#${filterId} feGaussianBlur`);
if (!feBlur) {
console.warn(`Filter with ID ${filterId} not found for element`, text);
return;
}
// Apply the filter to the text element
text.style.filter = `url(#${filterId})`;
// Set initial opacity to 0
gsap.set(text, { opacity: 0 });
// Object to store the stdDeviation value for the blur filter
let primitiveValues = { stdDeviation: 0 };
// Create the animation timeline
const animationTimeline = gsap.timeline({
defaults: {
duration: 2,
ease: 'expo',
},
// On every update, set the stdDeviation attribute in the feGaussianBlur filter
onUpdate: () => {
feBlur.setAttribute('stdDeviation', primitiveValues.stdDeviation);
},
scrollTrigger: {
trigger: text, // Trigger the animation when the text enters the viewport
start: 'center bottom'
}
})
.to(primitiveValues, {
startAt: { stdDeviation: 50 },
stdDeviation: 0
}, 0)
.to(text, {
startAt: {
opacity: 0
},
opacity: 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();
});
}
}