Update static/js/background.js
Browse files- static/js/background.js +175 -175
static/js/background.js
CHANGED
@@ -1,175 +1,175 @@
|
|
1 |
-
// Import Three.js library (already included in index.html)
|
2 |
-
// const THREE = window.THREE;
|
3 |
-
|
4 |
-
class Background {
|
5 |
-
constructor() {
|
6 |
-
// Initialize core Three.js components
|
7 |
-
this.scene = new THREE.Scene();
|
8 |
-
this.camera = new THREE.PerspectiveCamera(
|
9 |
-
75, // Field of view
|
10 |
-
window.innerWidth / window.innerHeight, // Aspect ratio
|
11 |
-
0.1, // Near plane
|
12 |
-
1000 // Far plane
|
13 |
-
);
|
14 |
-
|
15 |
-
// Setup renderer with transparency and anti-aliasing
|
16 |
-
this.renderer = new THREE.WebGLRenderer({
|
17 |
-
canvas: document.querySelector('#webgl-background'),
|
18 |
-
alpha: true,
|
19 |
-
antialias: true
|
20 |
-
});
|
21 |
-
this.renderer.setSize(window.innerWidth, window.innerHeight);
|
22 |
-
this.renderer.setClearColor(0x0a0a0a, 1); // Dark background color
|
23 |
-
|
24 |
-
// Initialize particle system properties
|
25 |
-
this.particles = [];
|
26 |
-
this.particleCount = 100;
|
27 |
-
this.particleGeometry = new THREE.BufferGeometry();
|
28 |
-
this.particleMaterial = new THREE.PointsMaterial({
|
29 |
-
size: 2,
|
30 |
-
color: 0xffffff,
|
31 |
-
transparent: true,
|
32 |
-
opacity: 0.5,
|
33 |
-
blending: THREE.AdditiveBlending
|
34 |
-
});
|
35 |
-
|
36 |
-
// Set up camera position
|
37 |
-
this.camera.position.z = 100;
|
38 |
-
|
39 |
-
// Initialize the background
|
40 |
-
this.init();
|
41 |
-
|
42 |
-
// Bind event listeners
|
43 |
-
this.bindEvents();
|
44 |
-
}
|
45 |
-
|
46 |
-
init() {
|
47 |
-
// Create particle positions array
|
48 |
-
const positions = new Float32Array(this.particleCount * 3);
|
49 |
-
|
50 |
-
// Generate random positions for particles
|
51 |
-
for (let i = 0; i < this.particleCount; i++) {
|
52 |
-
const i3 = i * 3; // Index for x, y, z coordinates
|
53 |
-
positions[i3] = (Math.random() - 0.5) * window.innerWidth; // X coordinate
|
54 |
-
positions[i3 + 1] = (Math.random() - 0.5) * window.innerHeight; // Y coordinate
|
55 |
-
positions[i3 + 2] = (Math.random() - 0.5) * 500; // Z coordinate
|
56 |
-
|
57 |
-
// Store particle data for animation
|
58 |
-
this.particles.push({
|
59 |
-
velocity: (Math.random() - 0.5) * 0.2,
|
60 |
-
baseX: positions[i3],
|
61 |
-
baseY: positions[i3 + 1]
|
62 |
-
});
|
63 |
-
}
|
64 |
-
|
65 |
-
// Set particle positions in geometry
|
66 |
-
this.particleGeometry.setAttribute(
|
67 |
-
'position',
|
68 |
-
new THREE.BufferAttribute(positions, 3)
|
69 |
-
);
|
70 |
-
|
71 |
-
// Create particle system and add to scene
|
72 |
-
this.particleSystem = new THREE.Points(
|
73 |
-
this.particleGeometry,
|
74 |
-
this.particleMaterial
|
75 |
-
);
|
76 |
-
this.scene.add(this.particleSystem);
|
77 |
-
|
78 |
-
// Start animation loop
|
79 |
-
this.animate();
|
80 |
-
}
|
81 |
-
|
82 |
-
// Handle window resize events
|
83 |
-
bindEvents() {
|
84 |
-
window.addEventListener('resize', () => {
|
85 |
-
// Update camera aspect ratio and projection matrix
|
86 |
-
this.camera.aspect = window.innerWidth / window.innerHeight;
|
87 |
-
this.camera.updateProjectionMatrix();
|
88 |
-
|
89 |
-
// Update renderer size
|
90 |
-
this.renderer.setSize(window.innerWidth, window.innerHeight);
|
91 |
-
});
|
92 |
-
}
|
93 |
-
|
94 |
-
// Animation loop for particle movement
|
95 |
-
animate() {
|
96 |
-
requestAnimationFrame(() => this.animate());
|
97 |
-
|
98 |
-
const positions = this.particleGeometry.attributes.position.array;
|
99 |
-
const time = Date.now() * 0.0005;
|
100 |
-
|
101 |
-
// Update particle positions with wave-like motion
|
102 |
-
for (let i = 0; i < this.particleCount; i++) {
|
103 |
-
const i3 = i * 3;
|
104 |
-
const particle = this.particles[i];
|
105 |
-
|
106 |
-
// Create wave-like motion using sine waves
|
107 |
-
positions[i3] = particle.baseX + Math.sin(time + i) * 2;
|
108 |
-
positions[i3 + 1] = particle.baseY + Math.cos(time + i) * 2;
|
109 |
-
|
110 |
-
// Add slight drift to z-position
|
111 |
-
positions[i3 + 2] += particle.velocity;
|
112 |
-
|
113 |
-
// Reset particles that drift too far
|
114 |
-
if (Math.abs(positions[i3 + 2]) > 250) {
|
115 |
-
positions[i3 + 2] = -250;
|
116 |
-
}
|
117 |
-
}
|
118 |
-
|
119 |
-
// Mark particle positions for update
|
120 |
-
this.particleGeometry.attributes.position.needsUpdate = true;
|
121 |
-
|
122 |
-
// Add subtle camera movement
|
123 |
-
this.camera.position.x = Math.sin(time) * 10;
|
124 |
-
this.camera.position.y = Math.cos(time) * 10;
|
125 |
-
this.camera.lookAt(this.scene.position);
|
126 |
-
|
127 |
-
// Render the scene
|
128 |
-
this.renderer.render(this.scene, this.camera);
|
129 |
-
}
|
130 |
-
|
131 |
-
// Method to add dramatic effects during game events
|
132 |
-
addDramaticEffect(type) {
|
133 |
-
switch(type) {
|
134 |
-
case 'impostor_reveal':
|
135 |
-
// Create a dramatic red flash effect
|
136 |
-
this.particleMaterial.color.setHex(0xff0000);
|
137 |
-
setTimeout(() => {
|
138 |
-
this.particleMaterial.color.setHex(0xffffff);
|
139 |
-
}, 1000);
|
140 |
-
break;
|
141 |
-
|
142 |
-
case 'round_start':
|
143 |
-
// Increase particle movement temporarily
|
144 |
-
const originalVelocities = this.particles.map(p => p.velocity);
|
145 |
-
this.particles.forEach(p => p.velocity *= 2);
|
146 |
-
setTimeout(() => {
|
147 |
-
this.particles.forEach((p, i) => p.velocity = originalVelocities[i]);
|
148 |
-
}, 2000);
|
149 |
-
break;
|
150 |
-
|
151 |
-
case 'voting':
|
152 |
-
// Create a pulsing effect
|
153 |
-
const pulseAnimation = () => {
|
154 |
-
this.particleMaterial.size = 2 + Math.sin(Date.now() * 0.005) * 1;
|
155 |
-
};
|
156 |
-
const pulseInterval = setInterval(pulseAnimation, 16);
|
157 |
-
setTimeout(() => {
|
158 |
-
clearInterval(pulseInterval);
|
159 |
-
this.particleMaterial.size = 2;
|
160 |
-
}, 3000);
|
161 |
-
break;
|
162 |
-
}
|
163 |
-
}
|
164 |
-
}
|
165 |
-
|
166 |
-
// Initialize the background when the DOM is loaded
|
167 |
-
document.addEventListener('DOMContentLoaded', () => {
|
168 |
-
const background = new Background();
|
169 |
-
|
170 |
-
// Expose background instance for game events
|
171 |
-
window.gameBackground = background;
|
172 |
-
});
|
173 |
-
|
174 |
-
// Export the Background class for potential module usage
|
175 |
-
export default Background
|
|
|
1 |
+
// Import Three.js library (already included in index.html)
|
2 |
+
// const THREE = window.THREE;
|
3 |
+
|
4 |
+
class Background {
|
5 |
+
constructor() {
|
6 |
+
// Initialize core Three.js components
|
7 |
+
this.scene = new THREE.Scene();
|
8 |
+
this.camera = new THREE.PerspectiveCamera(
|
9 |
+
75, // Field of view
|
10 |
+
window.innerWidth / window.innerHeight, // Aspect ratio
|
11 |
+
0.1, // Near plane
|
12 |
+
1000 // Far plane
|
13 |
+
);
|
14 |
+
|
15 |
+
// Setup renderer with transparency and anti-aliasing
|
16 |
+
this.renderer = new THREE.WebGLRenderer({
|
17 |
+
canvas: document.querySelector('#webgl-background'),
|
18 |
+
alpha: true,
|
19 |
+
antialias: true
|
20 |
+
});
|
21 |
+
this.renderer.setSize(window.innerWidth, window.innerHeight);
|
22 |
+
this.renderer.setClearColor(0x0a0a0a, 1); // Dark background color
|
23 |
+
|
24 |
+
// Initialize particle system properties
|
25 |
+
this.particles = [];
|
26 |
+
this.particleCount = 100;
|
27 |
+
this.particleGeometry = new THREE.BufferGeometry();
|
28 |
+
this.particleMaterial = new THREE.PointsMaterial({
|
29 |
+
size: 2,
|
30 |
+
color: 0xffffff,
|
31 |
+
transparent: true,
|
32 |
+
opacity: 0.5,
|
33 |
+
blending: THREE.AdditiveBlending
|
34 |
+
});
|
35 |
+
|
36 |
+
// Set up camera position
|
37 |
+
this.camera.position.z = 100;
|
38 |
+
|
39 |
+
// Initialize the background
|
40 |
+
this.init();
|
41 |
+
|
42 |
+
// Bind event listeners
|
43 |
+
this.bindEvents();
|
44 |
+
}
|
45 |
+
|
46 |
+
init() {
|
47 |
+
// Create particle positions array
|
48 |
+
const positions = new Float32Array(this.particleCount * 3);
|
49 |
+
|
50 |
+
// Generate random positions for particles
|
51 |
+
for (let i = 0; i < this.particleCount; i++) {
|
52 |
+
const i3 = i * 3; // Index for x, y, z coordinates
|
53 |
+
positions[i3] = (Math.random() - 0.5) * window.innerWidth; // X coordinate
|
54 |
+
positions[i3 + 1] = (Math.random() - 0.5) * window.innerHeight; // Y coordinate
|
55 |
+
positions[i3 + 2] = (Math.random() - 0.5) * 500; // Z coordinate
|
56 |
+
|
57 |
+
// Store particle data for animation
|
58 |
+
this.particles.push({
|
59 |
+
velocity: (Math.random() - 0.5) * 0.2,
|
60 |
+
baseX: positions[i3],
|
61 |
+
baseY: positions[i3 + 1]
|
62 |
+
});
|
63 |
+
}
|
64 |
+
|
65 |
+
// Set particle positions in geometry
|
66 |
+
this.particleGeometry.setAttribute(
|
67 |
+
'position',
|
68 |
+
new THREE.BufferAttribute(positions, 3)
|
69 |
+
);
|
70 |
+
|
71 |
+
// Create particle system and add to scene
|
72 |
+
this.particleSystem = new THREE.Points(
|
73 |
+
this.particleGeometry,
|
74 |
+
this.particleMaterial
|
75 |
+
);
|
76 |
+
this.scene.add(this.particleSystem);
|
77 |
+
|
78 |
+
// Start animation loop
|
79 |
+
this.animate();
|
80 |
+
}
|
81 |
+
|
82 |
+
// Handle window resize events
|
83 |
+
bindEvents() {
|
84 |
+
window.addEventListener('resize', () => {
|
85 |
+
// Update camera aspect ratio and projection matrix
|
86 |
+
this.camera.aspect = window.innerWidth / window.innerHeight;
|
87 |
+
this.camera.updateProjectionMatrix();
|
88 |
+
|
89 |
+
// Update renderer size
|
90 |
+
this.renderer.setSize(window.innerWidth, window.innerHeight);
|
91 |
+
});
|
92 |
+
}
|
93 |
+
|
94 |
+
// Animation loop for particle movement
|
95 |
+
animate() {
|
96 |
+
requestAnimationFrame(() => this.animate());
|
97 |
+
|
98 |
+
const positions = this.particleGeometry.attributes.position.array;
|
99 |
+
const time = Date.now() * 0.0005;
|
100 |
+
|
101 |
+
// Update particle positions with wave-like motion
|
102 |
+
for (let i = 0; i < this.particleCount; i++) {
|
103 |
+
const i3 = i * 3;
|
104 |
+
const particle = this.particles[i];
|
105 |
+
|
106 |
+
// Create wave-like motion using sine waves
|
107 |
+
positions[i3] = particle.baseX + Math.sin(time + i) * 2;
|
108 |
+
positions[i3 + 1] = particle.baseY + Math.cos(time + i) * 2;
|
109 |
+
|
110 |
+
// Add slight drift to z-position
|
111 |
+
positions[i3 + 2] += particle.velocity;
|
112 |
+
|
113 |
+
// Reset particles that drift too far
|
114 |
+
if (Math.abs(positions[i3 + 2]) > 250) {
|
115 |
+
positions[i3 + 2] = -250;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
|
119 |
+
// Mark particle positions for update
|
120 |
+
this.particleGeometry.attributes.position.needsUpdate = true;
|
121 |
+
|
122 |
+
// Add subtle camera movement
|
123 |
+
this.camera.position.x = Math.sin(time) * 10;
|
124 |
+
this.camera.position.y = Math.cos(time) * 10;
|
125 |
+
this.camera.lookAt(this.scene.position);
|
126 |
+
|
127 |
+
// Render the scene
|
128 |
+
this.renderer.render(this.scene, this.camera);
|
129 |
+
}
|
130 |
+
|
131 |
+
// Method to add dramatic effects during game events
|
132 |
+
addDramaticEffect(type) {
|
133 |
+
switch(type) {
|
134 |
+
case 'impostor_reveal':
|
135 |
+
// Create a dramatic red flash effect
|
136 |
+
this.particleMaterial.color.setHex(0xff0000);
|
137 |
+
setTimeout(() => {
|
138 |
+
this.particleMaterial.color.setHex(0xffffff);
|
139 |
+
}, 1000);
|
140 |
+
break;
|
141 |
+
|
142 |
+
case 'round_start':
|
143 |
+
// Increase particle movement temporarily
|
144 |
+
const originalVelocities = this.particles.map(p => p.velocity);
|
145 |
+
this.particles.forEach(p => p.velocity *= 2);
|
146 |
+
setTimeout(() => {
|
147 |
+
this.particles.forEach((p, i) => p.velocity = originalVelocities[i]);
|
148 |
+
}, 2000);
|
149 |
+
break;
|
150 |
+
|
151 |
+
case 'voting':
|
152 |
+
// Create a pulsing effect
|
153 |
+
const pulseAnimation = () => {
|
154 |
+
this.particleMaterial.size = 2 + Math.sin(Date.now() * 0.005) * 1;
|
155 |
+
};
|
156 |
+
const pulseInterval = setInterval(pulseAnimation, 16);
|
157 |
+
setTimeout(() => {
|
158 |
+
clearInterval(pulseInterval);
|
159 |
+
this.particleMaterial.size = 2;
|
160 |
+
}, 3000);
|
161 |
+
break;
|
162 |
+
}
|
163 |
+
}
|
164 |
+
}
|
165 |
+
|
166 |
+
// Initialize the background when the DOM is loaded
|
167 |
+
document.addEventListener('DOMContentLoaded', () => {
|
168 |
+
const background = new Background();
|
169 |
+
|
170 |
+
// Expose background instance for game events
|
171 |
+
window.gameBackground = background;
|
172 |
+
});
|
173 |
+
|
174 |
+
// Export the Background class for potential module usage
|
175 |
+
export default Background
|