-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro.js
More file actions
140 lines (113 loc) · 3.66 KB
/
intro.js
File metadata and controls
140 lines (113 loc) · 3.66 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
// --- CONFIG ---
const cellSize = 20;
const characters = 'KH+/';
const animationDuration = 1.2; // seconds per update
const neighborInfluence = 0.09;
const randomness = 0.01;
const introDuration = 3000; // 3 seconds animation
let phase = "intro";
const gridContainer = document.getElementById('grid');
let gridCols = Math.ceil(window.innerWidth / cellSize);
let gridRows = Math.ceil(window.innerHeight / cellSize);
gridContainer.style.gridTemplateColumns = `repeat(${gridCols}, ${cellSize}px)`;
gridContainer.style.gridTemplateRows = `repeat(${gridRows}, ${cellSize}px)`;
// --- CREATE GRID ---
const grid = [];
for (let y = 0; y < gridRows; y++) {
const row = [];
for (let x = 0; x < gridCols; x++) {
const cellDiv = document.createElement('div');
cellDiv.className = 'cell';
const charIndex = Math.random() * characters.length;
cellDiv.textContent = characters[Math.floor(charIndex)];
gridContainer.appendChild(cellDiv);
row.push({
element: cellDiv,
charIndex,
velocity: Math.random() * 0.3
});
}
grid.push(row);
}
// --- UPDATE GRID FUNCTION ---
function updateGrid() {
for (let y = 0; y < gridRows; y++) {
for (let x = 0; x < gridCols; x++) {
const cell = grid[y][x];
// --- Neighbor influence ---
const neighbors = [];
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
if (dx === 0 && dy === 0) continue;
const ny = y + dy;
const nx = x + dx;
if (ny >= 0 && ny < gridRows && nx >= 0 && nx < gridCols) {
neighbors.push(grid[ny][nx]);
}
}
}
const avgVelocity = neighbors.reduce((sum, n) => sum + n.velocity, 0) / neighbors.length;
cell.velocity += (avgVelocity - cell.velocity) * neighborInfluence + (Math.random() - 0.5) * randomness;
// --- Update char ---
cell.charIndex = (cell.charIndex + cell.velocity) % characters.length;
const newChar = characters[Math.floor(cell.charIndex)];
// --- Animate with GSAP ---
gsap.to(cell.element, {
duration: animationDuration,
textContent: newChar,
ease: "power1.inOut"
});
}
}
}
// --- SLOW DOWN ---
function slowDown() {
grid.forEach(row =>
row.forEach(cell => {
cell.velocity *= 0.9;
})
);
}
// // --- TRANSITION TO WORDS ---
function transitionToWords(lines) {
phase = "freeze";
grid.forEach((row, y) => {
row.forEach((cell, x) => {
// // blank spaces above and below
const blockWidth = Math.max(...lines.map(l => l.length));
const blockHeight = lines.length;
const blockX = Math.floor((gridCols - blockWidth) / 2);
const startY = Math.floor((gridRows - blockHeight) / 2);
let targetChar = cell.element.textContent;
// main word lines
lines.forEach((line, i) => {
if (y === startY + i && x >= blockX && x < blockX + line.length) {
targetChar = line[x - blockX];
cell.isWord = true;
cell.element.classList.add("word");
} else {
cell.isWord = false;
cell.element.classList.remove("word");
}
});
gsap.to(cell.element, {
duration: 2,
textContent: targetChar,
ease: "power2.out"
});
});
});
}
// --- LOOP ---
gsap.ticker.add(() => {
if (phase === "intro") updateGrid();
});
// --- TIMING ---
setTimeout(() => {
gsap.ticker.add(slowDown);
}, introDuration - 1000);
setTimeout(() => {
transitionToWords([
"KAMRYN HANLEY",
]);
}, introDuration);