forked from xvvvyz/tilde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
207 lines (181 loc) · 7.87 KB
/
background.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
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
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.js';
class AnimatedBackground {
constructor() {
this.container = document.querySelector('bgelement');
this.camera = new THREE.PerspectiveCamera(34, window.innerWidth / window.innerHeight, 0.1, 1000);
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
this.clock = new THREE.Clock();
this.particles = new THREE.Group();
this.particleCount = 10000;
this.seed = Math.random() * 10000; // Seed for this page load
this.animationParams = this.generateAnimationParams();
this.timeScale = 3.0; // Increase this value to speed up the entire animation
this.init();
this.createParticles();
this.addEvents();
this.animate();
}
init() {
this.camera.position.z = 100;
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.container.appendChild(this.renderer.domElement);
this.scene.add(this.particles);
}
random() {
const x = Math.sin(this.seed++) * 10000;
return x - Math.floor(x);
}
generateAnimationParams() {
const baseShape = Math.floor(this.random() * 4); // 0: cube, 1: sphere, 2: torus, 3: spiral
return {
baseShape: baseShape,
scaleFreq: 0.2 + this.random() * 0.8,
scaleAmp: 0.05 + this.random() * 0.2,
twistFreq: 0.3 + this.random() * 0.7,
twistAmp: 0.02 + this.random() * 0.08,
moveFreq: {
x: 0.4 + this.random() * 0.8,
y: 0.45 + this.random() * 0.8,
z: 0.5 + this.random() * 0.8
},
moveAmp: 0.3 + this.random() * 0.7,
sphereFreq: {
x: 0.3 + this.random() * 0.7,
y: 0.35 + this.random() * 0.7,
z: 0.4 + this.random() * 0.7
},
rotationSpeed: {
y: 0.0005 + this.random() * 0.002,
x: 0.00025 + this.random() * 0.001
},
colorShift: this.random() * 0.5,
pulseFreq: 0.1 + this.random() * 0.4
};
}
createParticles() {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(this.particleCount * 3);
const colors = new Float32Array(this.particleCount * 3);
for (let i = 0; i < this.particleCount; i++) {
let x, y, z;
switch (this.animationParams.baseShape) {
case 0: // Cube
x = (this.random() - 0.5) * 50;
y = (this.random() - 0.5) * 50;
z = (this.random() - 0.5) * 50;
break;
case 1: // Sphere
const theta = this.random() * Math.PI * 2;
const phi = Math.acos(2 * this.random() - 1);
const radius = 25 + this.random() * 25;
x = radius * Math.sin(phi) * Math.cos(theta);
y = radius * Math.sin(phi) * Math.sin(theta);
z = radius * Math.cos(phi);
break;
case 2: // Torus
const u = this.random() * Math.PI * 2;
const v = this.random() * Math.PI * 2;
const R = 30;
const r = 10;
x = (R + r * Math.cos(v)) * Math.cos(u);
y = (R + r * Math.cos(v)) * Math.sin(u);
z = r * Math.sin(v);
break;
case 3: // Spiral
const t = this.random() * 10;
x = t * Math.cos(t * 2);
y = t * 4;
z = t * Math.sin(t * 2);
break;
}
positions[i * 3] = x;
positions[i * 3 + 1] = y;
positions[i * 3 + 2] = z;
const color = new THREE.Color();
color.setHSL(this.random(), 0.7, 0.5);
colors[i * 3] = color.r;
colors[i * 3 + 1] = color.g;
colors[i * 3 + 2] = color.b;
}
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
const material = new THREE.PointsMaterial({
size: 0.5,
vertexColors: true,
transparent: true,
opacity: 0.8,
sizeAttenuation: true,
depthWrite: false,
blending: THREE.AdditiveBlending,
});
this.particleSystem = new THREE.Points(geometry, material);
this.particles.add(this.particleSystem);
}
addEvents() {
window.addEventListener('resize', this.onWindowResize.bind(this), false);
}
onWindowResize() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
animate() {
requestAnimationFrame(this.animate.bind(this));
const time = this.clock.getElapsedTime() * this.timeScale;
const params = this.animationParams;
const positions = this.particleSystem.geometry.attributes.position.array;
const colors = this.particleSystem.geometry.attributes.color.array;
for (let i = 0; i < positions.length; i += 3) {
const x = positions[i];
const y = positions[i + 1];
const z = positions[i + 2];
const scale = Math.sin(time * params.scaleFreq) * params.scaleAmp + 1.05;
const twist = Math.sin(time * params.twistFreq) * Math.PI * params.twistAmp;
let newX = Math.sin(twist) * y * scale + Math.cos(twist) * x * scale;
let newY = Math.sin(twist) * x * scale + Math.cos(twist) * y * scale;
let newZ = z * scale;
// Reduce the movement amplitude
const moveAmp = params.moveAmp * 0.3;
newX += Math.sin(time * params.moveFreq.x + i * 0.01) * moveAmp;
newY += Math.cos(time * params.moveFreq.y + i * 0.01) * moveAmp;
newZ += Math.sin(time * params.moveFreq.z + i * 0.01) * moveAmp;
// Add a force that pulls particles back to the center
const pullStrength = 0.02;
newX -= x * pullStrength;
newY -= y * pullStrength;
newZ -= z * pullStrength;
const maxDistance = 28;
const minDistance = 9;
const distance = Math.sqrt(newX * newX + newY * newY + newZ * newZ);
if (distance > maxDistance) {
const factor = maxDistance / distance;
newX *= factor;
newY *= factor;
newZ *= factor;
} else if (distance < minDistance) {
const factor = minDistance / distance;
newX *= factor;
newY *= factor;
newZ *= factor;
}
positions[i] = newX;
positions[i + 1] = newY;
positions[i + 2] = newZ;
// Color shifting
const hue = (colors[i] + colors[i + 1] + colors[i + 2]) / 3 + params.colorShift * Math.sin(time * params.pulseFreq + i * 0.01);
const color = new THREE.Color();
color.setHSL(hue % 1, 0.7, 0.5);
colors[i] = color.r;
colors[i + 1] = color.g;
colors[i + 2] = color.b;
}
this.particleSystem.geometry.attributes.position.needsUpdate = true;
this.particleSystem.geometry.attributes.color.needsUpdate = true;
this.particles.rotation.y += params.rotationSpeed.y;
this.particles.rotation.x += params.rotationSpeed.x;
this.renderer.render(this.scene, this.camera);
}
}
new AnimatedBackground();