-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartikel-animation-work.html
More file actions
231 lines (204 loc) · 9.95 KB
/
Copy pathpartikel-animation-work.html
File metadata and controls
231 lines (204 loc) · 9.95 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Die Finale, Perfektionierte Partikel-Intro</title>
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<style>
body {
background-color: #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
overflow: hidden;
}
.app-container {
width: 90%;
max-width: 900px;
text-align: center;
}
.headline-wrapper {
/* Dieser Wrapper ist das Positionierungs-Fundament */
position: relative;
display: inline-block;
margin: 0 auto;
}
.headline {
font-size: 5rem;
font-weight: 700;
letter-spacing: 0.02em;
margin: 0;
padding: 0;
white-space: nowrap;
/* Der Schlüssel zum Enthüllen: Die Farbe wird animiert */
transition: color 400ms ease-in-out;
}
.canvas-effect {
/* Das Canvas ist nur eine temporäre Schicht darüber */
position: fixed;
pointer-events: none;
z-index: 10;
transition: opacity 400ms ease-in-out;
}
</style>
</head>
<body>
<div id="root" class="app-container"></div>
<script type="text/javascript">
const { useState, useCallback, useRef, useEffect, useLayoutEffect, createElement } = React;
const HOLD_DURATION = 100; // ms
const FADE_DURATION = 400; // ms, muss mit CSS-Transition übereinstimmen
// ========================================================================================
// PARTIKEL-KOMPONENTE: FOKUSSIERT SICH NUR NOCH AUF DAS ZEICHNEN
// ========================================================================================
const AssembleEffect = ({ text, rect, onAnimationComplete }) => {
const canvasRef = useRef(null);
const PADDING = 80;
useEffect(() => {
const config = { startRadius: 60, easeFactor: 0.07, targetRadius: 1.0 };
const canvas = canvasRef.current;
const ctx = canvas.getContext('2d');
const particles = [];
ctx.font = `700 5rem -apple-system, BlinkMacSystemFont, "Segoe UI"`;
const fontMetrics = ctx.measureText(text);
const baselineY = PADDING + fontMetrics.actualBoundingBoxAscent;
ctx.fillText(text, PADDING, baselineY);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width; x++) {
if (imageData[(y * canvas.width + x) * 4 + 3] > 128) {
const randomAngle = Math.random() * Math.PI * 2;
const randomDist = Math.random() * config.startRadius;
particles.push({
targetX: x, targetY: y,
x: x + Math.cos(randomAngle) * randomDist,
y: y + Math.sin(randomAngle) * randomDist,
radius: 0, targetRadius: config.targetRadius,
isActive: false,
delay: Math.random() * 12,
});
}
}
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
let animationFrameId;
let frameCount = 0;
function animate() {
let allAssembled = true;
animationFrameId = requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
const startPos = frameCount * 8;
for (const p of particles) {
if (!p.isActive && p.targetX < startPos + PADDING) p.isActive = true;
if (p.isActive && p.delay > 0) p.delay--;
if (p.isActive && p.delay <= 0) {
p.x += (p.targetX - p.x) * config.easeFactor;
p.y += (p.targetY - p.y) * config.easeFactor;
p.radius += (p.targetRadius - p.radius) * config.easeFactor;
if (Math.hypot(p.targetX - p.x, p.targetY - p.y) > 0.05 || p.radius < p.targetRadius - 0.05) {
allAssembled = false;
}
ctx.beginPath();
ctx.arc(p.x, p.y, Math.max(0, p.radius), 0, Math.PI * 2);
ctx.fillStyle = "#FFF";
ctx.fill();
} else if (!p.isActive) {
allAssembled = false;
}
}
frameCount++;
if (allAssembled) {
cancelAnimationFrame(animationFrameId);
onAnimationComplete();
}
}
animate();
return () => cancelAnimationFrame(animationFrameId);
}, [text, rect, onAnimationComplete]);
// Das Canvas wird jetzt exakt über die Position des SPANs zentriert.
return createElement('canvas', {
ref: canvasRef,
width: rect.width + PADDING * 2,
height: rect.height + PADDING * 2,
className: 'canvas-effect',
style: {
top: `${rect.top - PADDING}px`,
left: `${rect.left - PADDING}px`,
}
});
};
// ========================================================================================
// DIE HAUPT-APP: ORCHESTRIERT DEN "OVERLAY & SWAP COLOR"-TRICK
// ========================================================================================
const App = () => {
const text = "HELLO WORLD";
const [rect, setRect] = useState(null);
// Die neue, vereinfachte State-Machine
const [phase, setPhase] = useState('preparing'); // 'preparing', 'animating', 'fading'
const headlineRef = useRef(null);
useLayoutEffect(() => {
// Misst den Span, sobald er da ist.
if (headlineRef.current && phase === 'preparing') {
setRect(headlineRef.current.getBoundingClientRect());
}
}, [phase]);
useEffect(() => {
// Startet die Animation, sobald der Text gemessen wurde.
if (rect && phase === 'preparing') {
const timer = setTimeout(() => {
setPhase('animating');
}, 300);
return () => clearTimeout(timer);
}
}, [rect, phase]);
// Leitet den "Hold and Fade"-Übergang ein.
const handleAnimationComplete = useCallback(() => {
setTimeout(() => setPhase('fading'), HOLD_DURATION);
}, []);
// Definiert die dynamischen Stile basierend auf der Phase.
const headlineStyle = {
// Der echte Text wird sichtbar, wenn der Fade beginnt.
color: phase === 'fading' ? '#FFFFFF' : 'transparent'
};
const canvasOpacity = {
// Das Canvas wird ausgeblendet, wenn der Fade beginnt.
opacity: phase === 'fading' ? 0 : 1
};
return createElement('div', { className: 'headline-wrapper' },
createElement('h1', { ref: headlineRef, className: 'headline', style: headlineStyle }, text),
// Das Canvas wird nur in der Vorbereitungs- und Animationsphase gerendert.
(phase === 'animating' && rect) && createElement(AssembleEffect, {
text,
rect,
onAnimationComplete: handleAnimationComplete,
}),
// Ein temporäres, statisches Canvas, das während des Fades sichtbar ist, um den Übergang zu ermöglichen.
(phase === 'fading' && rect) && createElement('canvas', {
className: 'canvas-effect',
style: { ...canvasOpacity, top: `${rect.top - 80}px`, left: `${rect.left - 80}px` },
width: rect.width + 160,
height: rect.height + 160,
ref: (node) => { // Zeichnet den finalen Text auf dieses Fade-Canvas
if (node) {
const ctx = node.getContext('2d');
ctx.clearRect(0, 0, node.width, node.height);
ctx.font = `700 5rem -apple-system, BlinkMacSystemFont, "Segoe UI"`;
const fontMetrics = ctx.measureText(text);
const baselineY = 80 + fontMetrics.actualBoundingBoxAscent;
ctx.fillText(text, 80, baselineY);
}
}
})
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(createElement(App));
</script>
</body>
</html>