-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (137 loc) · 4.18 KB
/
script.js
File metadata and controls
157 lines (137 loc) · 4.18 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
document.addEventListener("DOMContentLoaded", () => {
gsap.registerPlugin(ScrollTrigger);
// --- Canvas Image Sequence Logic ---
const canvas = document.getElementById("scrolly-canvas");
const ctx = canvas.getContext("2d");
const loadingProgress = document.getElementById("loading-progress");
const loadingHud = document.getElementById("loading-hud");
const FRAME_COUNT = 88;
const images = [];
let imagesLoaded = 0;
// Preload Images
for (let i = 0; i < FRAME_COUNT; i++) {
const img = new Image();
const paddedIndex = i.toString().padStart(2, "0");
img.src = `sequence/frame_${paddedIndex}_delay-0.09s.png`;
img.onload = () => {
imagesLoaded++;
loadingProgress.innerText = `${Math.round((imagesLoaded / FRAME_COUNT) * 100)}%`;
if (imagesLoaded === FRAME_COUNT) {
// Hide loading HUD
gsap.to(loadingHud, { opacity: 0, duration: 1 });
renderFrame(0);
}
};
images.push(img);
}
const renderFrame = (index) => {
if (images[index] && images[index].complete) {
// Object-fit cover calculations
const img = images[index];
const canvasRatio = canvas.width / canvas.height;
const imgRatio = img.width / img.height;
let drawWidth = canvas.width;
let drawHeight = canvas.height;
let offsetX = 0;
let offsetY = 0;
if (canvasRatio > imgRatio) {
drawHeight = canvas.width / imgRatio;
offsetY = (canvas.height - drawHeight) / 2;
} else {
drawWidth = canvas.height * imgRatio;
offsetX = (canvas.width - drawWidth) / 2;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
}
};
const handleResize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Determine which frame to render based on current scroll
const scrollContainer = document.querySelector('.scroll-container');
const scrollMax = scrollContainer.offsetHeight - window.innerHeight;
const progress = window.scrollY / scrollMax;
const frameIndex = Math.min(FRAME_COUNT - 1, Math.max(0, Math.round(progress * (FRAME_COUNT - 1))));
renderFrame(frameIndex || 0);
};
window.addEventListener('resize', handleResize);
handleResize();
// Link Canvas rendering to scroll via GSAP
gsap.to({ frame: 0 }, {
frame: FRAME_COUNT - 1,
snap: "frame",
ease: "none",
scrollTrigger: {
trigger: ".scroll-container",
start: "top top",
end: "bottom bottom",
scrub: 0.5 // Smoothing
},
onUpdate: function() {
renderFrame(Math.round(this.targets()[0].frame));
}
});
// --- Cinematic Overlays (Scrollytelling Scenes) ---
// Scene 1
gsap.timeline({
scrollTrigger: {
trigger: ".scroll-container",
start: "top top",
end: "15% top",
scrub: 1
}
})
.fromTo("#scene-1", { opacity: 1, y: 0, position: "fixed" }, { opacity: 0, y: -100 });
// Scene 2
gsap.timeline({
scrollTrigger: {
trigger: ".scroll-container",
start: "25% top",
end: "50% top",
scrub: 1
}
})
.fromTo("#scene-2", { opacity: 0, y: 50, position: "fixed" }, { opacity: 1, y: 0 })
.to("#scene-2", { opacity: 0, y: -50 }, "+=0.5");
// Scene 3
gsap.timeline({
scrollTrigger: {
trigger: ".scroll-container",
start: "60% top",
end: "85% top",
scrub: 1
}
})
.fromTo("#scene-3", { opacity: 0, y: 50, position: "fixed" }, { opacity: 1, y: 0 })
.to("#scene-3", { opacity: 0, y: -50 }, "+=0.5");
// Global Darkening overlay near the end
gsap.fromTo("#dark-overlay",
{ opacity: 0 },
{
opacity: 1,
ease: "none",
scrollTrigger: {
trigger: ".scroll-container",
start: "85% top",
end: "bottom bottom",
scrub: true
}
}
);
// --- Projects Grid Animations ---
const projectCards = document.querySelectorAll('.project-card');
projectCards.forEach((card, i) => {
gsap.to(card, {
scrollTrigger: {
trigger: card,
start: "top 90%",
},
opacity: 1,
y: 0,
duration: 0.8,
ease: "power3.out",
delay: i * 0.1
});
});
});