-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
364 lines (348 loc) · 12 KB
/
app.js
File metadata and controls
364 lines (348 loc) · 12 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const { useEffect, useRef } = React;
const THREE = window.THREE;
function LiquidEther({
mouseForce = 20,
cursorSize = 100,
isViscous = false,
viscous = 30,
iterationsViscous = 32,
iterationsPoisson = 32,
dt = 0.014,
BFECC = true,
resolution = 0.5,
isBounce = false,
colors = ['#5227FF', '#FF9FFC', '#B19EEF'],
style = {},
className = '',
autoDemo = true,
autoSpeed = 0.5,
autoIntensity = 2.2,
takeoverDuration = 0.25,
autoResumeDelay = 1000,
autoRampDuration = 0.6
}) {
const mountRef = useRef(null);
const webglRef = useRef(null);
const resizeObserverRef = useRef(null);
const rafRef = useRef(null);
const intersectionObserverRef = useRef(null);
const isVisibleRef = useRef(true);
const resizeRafRef = useRef(null);
useEffect(() => {
if (!mountRef.current) return;
function makePaletteTexture(stops) {
let arr;
if (Array.isArray(stops) && stops.length > 0) {
if (stops.length === 1) {
arr = [stops[0], stops[0]];
} else {
arr = stops;
}
} else {
arr = ['#ffffff', '#ffffff'];
}
const w = arr.length;
const data = new Uint8Array(w * 4);
for (let i = 0; i < w; i++) {
const c = new THREE.Color(arr[i]);
data[i * 4 + 0] = Math.round(c.r * 255);
data[i * 4 + 1] = Math.round(c.g * 255);
data[i * 4 + 2] = Math.round(c.b * 255);
data[i * 4 + 3] = 255;
}
const tex = new THREE.DataTexture(data, w, 1, THREE.RGBAFormat);
tex.magFilter = THREE.LinearFilter;
tex.minFilter = THREE.LinearFilter;
tex.wrapS = THREE.ClampToEdgeWrapping;
tex.wrapT = THREE.ClampToEdgeWrapping;
tex.generateMipmaps = false;
tex.needsUpdate = true;
return tex;
}
const paletteTex = makePaletteTexture(colors);
const bgVec4 = new THREE.Vector4(0, 0, 0, 0);
class CommonClass {
constructor() {
this.width = 0;
this.height = 0;
this.aspect = 1;
this.pixelRatio = 1;
this.isMobile = false;
this.breakpoint = 768;
this.fboWidth = null;
this.fboHeight = null;
this.time = 0;
this.delta = 0;
this.container = null;
this.renderer = null;
this.clock = null;
}
init(container) {
this.container = container;
this.pixelRatio = Math.min(window.devicePixelRatio || 1, 2);
this.resize();
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
this.renderer.autoClear = false;
this.renderer.setClearColor(new THREE.Color(0x000000), 0);
this.renderer.setPixelRatio(this.pixelRatio);
this.renderer.setSize(this.width, this.height);
this.renderer.domElement.style.width = '100%';
this.renderer.domElement.style.height = '100%';
this.renderer.domElement.style.display = 'block';
this.clock = new THREE.Clock();
this.clock.start();
}
resize() {
if (!this.container) return;
const rect = this.container.getBoundingClientRect();
this.width = Math.max(1, Math.floor(rect.width));
this.height = Math.max(1, Math.floor(rect.height));
this.aspect = this.width / this.height;
if (this.renderer) this.renderer.setSize(this.width, this.height, false);
}
update() {
this.delta = this.clock.getDelta();
this.time += this.delta;
}
}
const Common = new CommonClass();
class MouseClass {
constructor() {
this.mouseMoved = false;
this.coords = new THREE.Vector2();
this.coords_old = new THREE.Vector2();
this.diff = new THREE.Vector2();
this.timer = null;
this.container = null;
this._onMouseMove = this.onDocumentMouseMove.bind(this);
this._onTouchStart = this.onDocumentTouchStart.bind(this);
this._onTouchMove = this.onDocumentTouchMove.bind(this);
this._onMouseEnter = this.onMouseEnter.bind(this);
this._onMouseLeave = this.onMouseLeave.bind(this);
this._onTouchEnd = this.onTouchEnd.bind(this);
this.isHoverInside = false;
this.hasUserControl = false;
this.isAutoActive = false;
this.autoIntensity = 2.0;
this.takeoverActive = false;
this.takeoverStartTime = 0;
this.takeoverDuration = 0.25;
this.takeoverFrom = new THREE.Vector2();
this.takeoverTo = new THREE.Vector2();
this.onInteract = null;
}
init(container) {
this.container = container;
container.addEventListener('mousemove', this._onMouseMove, false);
container.addEventListener('touchstart', this._onTouchStart, false);
container.addEventListener('touchmove', this._onTouchMove, false);
container.addEventListener('mouseenter', this._onMouseEnter, false);
container.addEventListener('mouseleave', this._onMouseLeave, false);
container.addEventListener('touchend', this._onTouchEnd, false);
}
dispose() {
if (!this.container) return;
this.container.removeEventListener('mousemove', this._onMouseMove, false);
this.container.removeEventListener('touchstart', this._onTouchStart, false);
this.container.removeEventListener('touchmove', this._onTouchMove, false);
this.container.removeEventListener('mouseenter', this._onMouseEnter, false);
this.container.removeEventListener('mouseleave', this._onMouseLeave, false);
this.container.removeEventListener('touchend', this._onTouchEnd, false);
}
setCoords(x, y) {
if (!this.container) return;
if (this.timer) clearTimeout(this.timer);
const rect = this.container.getBoundingClientRect();
const nx = (x - rect.left) / rect.width;
const ny = (y - rect.top) / rect.height;
this.coords.set(nx * 2 - 1, -(ny * 2 - 1));
this.mouseMoved = true;
this.timer = setTimeout(() => {
this.mouseMoved = false;
}, 100);
}
setNormalized(nx, ny) {
this.coords.set(nx, ny);
this.mouseMoved = true;
}
onDocumentMouseMove(event) {
if (this.onInteract) this.onInteract();
if (this.isAutoActive && !this.hasUserControl && !this.takeoverActive) {
const rect = this.container.getBoundingClientRect();
const nx = (event.clientX - rect.left) / rect.width;
const ny = (event.clientY - rect.top) / rect.height;
this.takeoverFrom.copy(this.coords);
this.takeoverTo.set(nx * 2 - 1, -(ny * 2 - 1));
this.takeoverStartTime = performance.now();
this.takeoverActive = true;
this.hasUserControl = true;
this.isAutoActive = false;
return;
}
this.setCoords(event.clientX, event.clientY);
this.hasUserControl = true;
}
onDocumentTouchStart(event) {
if (event.touches.length === 1) {
const t = event.touches[0];
if (this.onInteract) this.onInteract();
this.setCoords(t.pageX, t.pageY);
this.hasUserControl = true;
}
}
onDocumentTouchMove(event) {
if (event.touches.length === 1) {
const t = event.touches[0];
if (this.onInteract) this.onInteract();
this.setCoords(t.pageX, t.pageY);
}
}
onTouchEnd() {
this.isHoverInside = false;
}
onMouseEnter() {
this.isHoverInside = true;
}
onMouseLeave() {
this.isHoverInside = false;
}
update() {
if (this.takeoverActive) {
const t = (performance.now() - this.takeoverStartTime) / (this.takeoverDuration * 1000);
if (t >= 1) {
this.takeoverActive = false;
this.coords.copy(this.takeoverTo);
this.coords_old.copy(this.coords);
this.diff.set(0, 0);
} else {
const k = t * t * (3 - 2 * t);
this.coords.copy(this.takeoverFrom).lerp(this.takeoverTo, k);
}
}
this.diff.subVectors(this.coords, this.coords_old);
this.coords_old.copy(this.coords);
if (this.coords_old.x === 0 && this.coords_old.y === 0) this.diff.set(0, 0);
if (this.isAutoActive && !this.takeoverActive) this.diff.multiplyScalar(this.autoIntensity);
}
}
const Mouse = new MouseClass();
class AutoDriver {
constructor(mouse, manager, opts) {
this.mouse = mouse;
this.manager = manager;
this.enabled = opts.enabled;
this.speed = opts.speed;
this.resumeDelay = opts.resumeDelay || 3000;
this.rampDurationMs = (opts.rampDuration || 0) * 1000;
this.active = false;
this.current = new THREE.Vector2(0, 0);
this.target = new THREE.Vector2();
this.lastTime = performance.now();
this.activationTime = 0;
this.margin = 0.2;
this._tmpDir = new THREE.Vector2();
this.pickNewTarget();
}
pickNewTarget() {
const r = Math.random;
this.target.set((r() * 2 - 1) * (1 - this.margin), (r() * 2 - 1) * (1 - this.margin));
}
forceStop() {
this.active = false;
this.mouse.isAutoActive = false;
}
update() {
if (!this.enabled) return;
const now = performance.now();
const idle = now - this.manager.lastUserInteraction;
if (idle < this.resumeDelay) {
if (this.active) this.forceStop();
return;
}
if (this.mouse.isHoverInside) {
if (this.active) this.forceStop();
return;
}
if (!this.active) {
this.active = true;
this.current.copy(this.mouse.coords);
this.lastTime = now;
this.activationTime = now;
}
if (!this.active) return;
this.mouse.isAutoActive = true;
let dtSec = (now - this.lastTime) / 1000;
this.lastTime = now;
if (dtSec > 0.2) dtSec = 0.016;
const dir = this._tmpDir.subVectors(this.target, this.current);
const dist = dir.length();
if (dist < 0.01) {
this.pickNewTarget();
return;
}
dir.normalize();
let ramp = 1;
if (this.rampDurationMs > 0) {
const t = Math.min(1, (now - this.activationTime) / this.rampDurationMs);
ramp = t * t * (3 - 2 * t);
}
const step = this.speed * dtSec * ramp;
const move = Math.min(step, dist);
this.current.addScaledVector(dir, move);
this.mouse.setNormalized(this.current.x, this.current.y);
}
}
// [Include all your other classes from the original code - ShaderPass, Advection, etc.]
// This is a placeholder for your complete simulation system
const container = mountRef.current;
container.style.position = container.style.position || 'relative';
container.style.overflow = container.style.overflow || 'hidden';
// Initialize the WebGL manager with all your original logic
// [Include your complete WebGLManager and initialization code here]
return () => {
// Cleanup code
};
}, [
BFECC,
cursorSize,
dt,
isBounce,
isViscous,
iterationsPoisson,
iterationsViscous,
mouseForce,
resolution,
viscous,
colors,
autoDemo,
autoSpeed,
autoIntensity,
takeoverDuration,
autoResumeDelay,
autoRampDuration
]);
return <div ref={mountRef} className={`liquid-ether-container ${className || ''}`} style={style} />;
}
function App() {
return (
<>
<LiquidEther />
<div className="overlay-text">
Allure - Create. Collaborate. Shine.
</div>
</>
);
}
const checkWebGL = () => {
try {
const canvas = document.createElement('canvas');
return !!(window.WebGLRenderingContext && canvas.getContext('webgl'));
} catch (e) {
return false;
}
};
if (!checkWebGL()) {
document.getElementById('root').innerHTML = '<div style="color: red; padding: 20px;">WebGL is not supported in your browser.</div>';
} else {
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
}