-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords.html
334 lines (288 loc) · 11.1 KB
/
words.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Word Cloud & Visualizer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wordcloud2.js/1.0.1/wordcloud2.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(135deg, #6a11cb, #2575fc);
background-size: 400% 400%;
animation: backgroundShift 10s ease infinite;
font-family: 'Arial', sans-serif;
overflow: hidden;
position: relative;
}
@keyframes backgroundShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
#word-cloud-container {
width: 80vw;
height: 60vh;
overflow: hidden;
border-radius: 15px;
background: rgba(255, 255, 255, 0.05);
box-shadow: 0 12px 50px rgba(0, 0, 0, 0.3);
position: absolute;
top: 15%;
left: 50%;
transform: translateX(-50%);
z-index: 10;
mix-blend-mode: screen;
}
#word-cloud-canvas {
width: 100%;
height: 100%;
}
.controls {
position: fixed;
bottom: 5vh;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 20px;
z-index: 15;
}
.control-button {
background: rgba(255, 255, 255, 0.2);
padding: 20px;
border-radius: 50%;
border: none;
cursor: pointer;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
transition: box-shadow 0.4s ease, transform 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
aspect-ratio: 1 / 1;
overflow: hidden;
position: relative;
}
.control-button.active {
box-shadow: 0 0 60px rgba(255, 255, 255, 0.9);
background-color: rgba(255, 255, 255, 0.3);
}
.control-button:active {
transform: scale(0.9);
}
.control-icon {
width: 35px;
height: 35px;
transition: filter 1.5s ease;
filter: drop-shadow(0 0 15px rgba(255, 255, 255, 0.7));
}
.particle {
position: absolute;
border-radius: 50%;
pointer-events: none;
animation: particleFloat 4s ease-in-out infinite alternate;
opacity: 0.8;
z-index: 5;
mix-blend-mode: difference;
}
@keyframes particleFloat {
0% { transform: translateY(0) translateX(0) scale(1); opacity: 0.5; }
50% { transform: translateY(-20px) translateX(20px) scale(1.3); opacity: 1; }
100% { transform: translateY(-40px) translateX(-20px) scale(1); opacity: 0.5; }
}
</style>
</head>
<body>
<div class="controls">
<button id="unmute-button" class="control-button" aria-label="Unmute Visualizer" title="Toggle Visualizer">
<img id="unmute-icon" class="control-icon" src="https://cdn-icons-png.flaticon.com/512/5539/5539962.png" alt="Mic Access Icon" />
</button>
<button id="toggle-speech-button" class="control-button" aria-label="Speech Recognition" title="Toggle Speech Recognition">
<img id="toggle-speech-icon" class="control-icon" src="https://cdn-icons-png.flaticon.com/512/4049/4049754.png" alt="Add Word Icon" />
</button>
</div>
<div id="word-cloud-container">
<canvas id="word-cloud-canvas"></canvas>
</div>
<script>
const wordCloudCanvas = document.getElementById('word-cloud-canvas');
const unmuteButton = document.getElementById('unmute-button');
const toggleSpeechButton = document.getElementById('toggle-speech-button');
const unmuteIcon = document.getElementById('unmute-icon');
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.interimResults = false;
recognition.maxAlternatives = 1;
let isUnmuted = false;
let isAddingWords = false;
let wordsArray = [];
let wordCloudData = [];
let audioContext = null;
let analyser = null;
let microphone = null;
let mediaStream = null;
// Word cloud shapes for randomness
const shapes = ['circle', 'star', 'triangle', 'diamond'];
function updateWordCloud(words) {
if (WordCloud.isSupported) {
const wordFrequency = words.reduce((freq, word) => {
freq[word] = (freq[word] || 0) + 1;
return freq;
}, {});
wordCloudData = Object.entries(wordFrequency).map(([word, count]) => [word, count]);
const randomShape = shapes[Math.floor(Math.random() * shapes.length)];
WordCloud(wordCloudCanvas, {
list: wordCloudData,
gridSize: Math.floor(10 * (window.innerWidth / 1024)),
weightFactor: 15,
color: () => `hsl(${Math.random() * 360}, 100%, 50%)`,
rotateRatio: 0.4,
minRotation: -Math.PI / 6,
maxRotation: Math.PI / 6,
backgroundColor: 'transparent',
shape: randomShape,
drawOutOfBound: false,
shrinkToFit: true,
clearCanvas: true
});
}
}
// Unmute button logic for music visualizer activation
unmuteButton.addEventListener('click', () => {
if (!isUnmuted) {
isUnmuted = true;
unmuteButton.classList.add('active');
unmuteIcon.src = "https://cdn-icons-png.flaticon.com/512/5539/5539899.png"; // Unmuted mic icon
deactivateSpeechRecognition(); // Ensure speech recognition is turned off before starting visualizer
activateVisualizer();
} else {
isUnmuted = false;
unmuteButton.classList.remove('active');
unmuteIcon.src = "https://cdn-icons-png.flaticon.com/512/5539/5539962.png"; // Muted mic icon
deactivateVisualizer();
}
});
function activateVisualizer() {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioContext.createAnalyser();
analyser.fftSize = 256;
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
mediaStream = stream;
microphone = audioContext.createMediaStreamSource(stream);
microphone.connect(analyser);
visualize();
}).catch(error => {
console.error("Error accessing microphone for visualizer:", error);
deactivateVisualizer(); // Clean up in case of error
});
}
function visualize() {
const dataArray = new Uint8Array(analyser.frequencyBinCount);
function draw() {
if (!isUnmuted) return;
analyser.getByteFrequencyData(dataArray);
const maxVolume = Math.max(...dataArray);
updateWordCloudWithVisualizer(maxVolume);
createParticleEffects(maxVolume);
requestAnimationFrame(draw);
}
draw();
}
function deactivateVisualizer() {
if (audioContext) {
audioContext.close();
audioContext = null;
analyser = null;
microphone = null;
// Explicitly stop all tracks of the media stream
if (mediaStream) {
mediaStream.getTracks().forEach(track => track.stop());
mediaStream = null;
}
}
document.querySelectorAll('.particle').forEach(p => p.remove());
}
function updateWordCloudWithVisualizer(volume) {
if (WordCloud.isSupported && wordCloudData.length) {
WordCloud(wordCloudCanvas, {
list: wordCloudData.map(([word, count]) => [word, count * (1 + volume / 256)]),
gridSize: Math.floor(10 * (window.innerWidth / 1024)),
weightFactor: 15,
color: () => `hsl(${Math.random() * 360}, 100%, ${50 + volume / 10}%)`,
rotateRatio: 0.4,
minRotation: -Math.PI / 6,
maxRotation: Math.PI / 6,
backgroundColor: 'transparent',
shape: 'circle',
drawOutOfBound: false,
shrinkToFit: true,
clearCanvas: true
});
}
}
function createParticleEffects(volume) {
document.querySelectorAll('.particle').forEach(p => p.remove());
const particleCount = Math.min(volume / 10, 10);
for (let i = 0; i < particleCount; i++) {
const particle = document.createElement('div');
particle.className = 'particle';
particle.style.width = `${Math.random() * 8 + 3}px`;
particle.style.height = particle.style.width;
particle.style.left = `${Math.random() * 90}vw`;
particle.style.top = `${Math.random() * 80}vh`;
particle.style.background = `hsl(${Math.random() * 360}, 100%, 75%)`;
document.body.appendChild(particle);
}
}
// Toggle speech button logic for speech detection
toggleSpeechButton.addEventListener('click', () => {
if (!isAddingWords) {
isAddingWords = true;
toggleSpeechButton.classList.add('active');
deactivateVisualizer(); // Ensure visualizer is turned off before starting speech recognition
recognition.start();
} else {
isAddingWords = false;
toggleSpeechButton.classList.remove('active');
recognition.stop();
}
});
function deactivateSpeechRecognition() {
if (isAddingWords) {
isAddingWords = false;
toggleSpeechButton.classList.remove('active');
recognition.stop();
}
}
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
const newWords = transcript.split(' ');
wordsArray = wordsArray.concat(newWords);
if (wordsArray.length > 1000) {
wordsArray = wordsArray.slice(wordsArray.length - 1000);
}
updateWordCloud(wordsArray);
};
recognition.onspeechend = () => {
if (isAddingWords) {
recognition.start(); // Keep listening as long as the toggle is active
} else {
recognition.stop();
}
};
recognition.onerror = (event) => {
console.error("Speech recognition error:", event.error);
if (isAddingWords) {
recognition.start();
}
};
// Initial word cloud rendering for visibility testing
updateWordCloud(wordsArray);
</script>
</body>
</html>