-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
wordcloud.html
398 lines (351 loc) · 13.6 KB
/
wordcloud.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stable Word Cloud Overlay - Social Stream Ninja</title>
<link rel="icon" href="./icons/favicon.ico" />
<link rel="preload" href="./thirdparty/NotoColorEmoji.ttf" as="font" type="font/ttf" crossorigin="anonymous">
<script src="./thirdparty/d3.min.js"></script>
<style>
body {
font-family: 'Sora', Arial, sans-serif;
background-color: rgba(0, 0, 0, 0);
margin: 0;
overflow: hidden;
}
#word-cloud {
width: 100vw;
height: 100vh;
}
.word {
transition: all 2.5s ease;
}
.word-inner {
fill: white;
font-weight: 700;
}
.word-outline {
stroke: #000;
stroke-width: 3px;
stroke-linejoin: round;
font-weight: 700;
}
.word-glow {
fill: none;
stroke: rgba(255,255,255,0.5);
stroke-width: 2px;
filter: url(#glow);
font-weight: 700;
}
/* latin-ext */
@font-face {
font-family: 'Sora';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/sora/v12/xMQ9uFFYT72X5wkB_18qmnndmSdSnh2BAfO5mnuyOo1lfiQwWa-xsaQ.woff2) format('woff2');
unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Sora';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/sora/v12/xMQ9uFFYT72X5wkB_18qmnndmSdSnh2BAfO5mnuyOo1lfiQwV6-x.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/* latin-ext */
@font-face {
font-family: 'Sora';
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(https://fonts.gstatic.com/s/sora/v12/xMQ9uFFYT72X5wkB_18qmnndmSdSnh2BAfO5mnuyOo1lfiQwWa-xsaQ.woff2) format('woff2');
unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Sora';
font-style: normal;
font-weight: 700;
font-display: swap;
src: url(https://fonts.gstatic.com/s/sora/v12/xMQ9uFFYT72X5wkB_18qmnndmSdSnh2BAfO5mnuyOo1lfiQwV6-x.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
</style>
</head>
<body>
<div id="word-cloud"></div>
<script>
(function() {
const urlParams = new URLSearchParams(window.location.search);
const styles = {
default: {
fontSizes: { base: 18, maxIncrease: 16 },
colors: d3.schemeCategory10,
glow: { stdDeviation: 2.5 },
rotate: d => (~~(Math.random() * 6) - 3) * 10,
forces: {
collide: 1.2, // Increased from 0.6
charge: -150, // Strengthened repulsion
xStrength: 0.1,
yStrength: 0.1
}
},
neon: {
fontSizes: { base: 20, maxIncrease: 20 },
colors: ['#ff00ff', '#00ffff', '#ff3377', '#00ff88'],
glow: { stdDeviation: 5 },
rotate: () => 0,
forces: {
collide: 1.2,
charge: -180,
xStrength: 0.1,
yStrength: 0.1
}
},
minimal: {
fontSizes: { base: 16, maxIncrease: 12 },
colors: ['#000000'],
glow: { stdDeviation: 0 },
rotate: () => 0,
forces: {
collide: 1.2,
charge: -130,
xStrength: 0.12,
yStrength: 0.12
}
},
retro: {
fontSizes: { base: 18, maxIncrease: 18 },
colors: ['#E94F37', '#1C77C3', '#39A9DB', '#40BCD8'],
glow: { stdDeviation: 1 },
rotate: d => (~~(Math.random() * 4) - 2) * 15,
forces: {
collide: 1.2,
charge: -160,
xStrength: 0.11,
yStrength: 0.11
}
}
};
function getTextWidth(text, fontSize) {
const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
const context = canvas.getContext("2d");
context.font = `${fontSize}px Sora`;
return context.measureText(text).width;
}
const activeStyle = styles[urlParams.get('style')] || styles.default;
const scale = parseFloat(urlParams.get('scale')) || 1;
function loadGoogleFont(fontName) {
const formattedFontName = fontName.replace(/\s/g, "+");
const link = document.createElement("link");
link.href = `https://fonts.googleapis.com/css2?family=${formattedFontName}&display=swap`;
link.rel = "stylesheet";
document.head.appendChild(link);
var currentFont = getComputedStyle(document.documentElement).getPropertyValue("--font-family").trim();
document.documentElement.style.setProperty("--font-family", "'" + fontName.replaceAll("+", " ") + "', " + currentFont);
}
if (urlParams.get("font")) {
document.documentElement.style.setProperty("--font-family", urlParams.get("font") + ", Avenir Next, Sora, Roboto, Helvetica, Geneva, Verdana, Arial, sans-serif");
}
if (urlParams.get("googlefont")) {
loadGoogleFont(urlParams.get("googlefont"));
}
const maxWords = 100;
const colors = d3.schemeCategory10;
let words = {};
let svg, wordGroup, simulation;
let nodes = [];
let width, height;
function initializeWordCloud() {
width = window.innerWidth;
height = window.innerHeight;
svg = d3.select("#word-cloud")
.append("svg")
.attr("width", width)
.attr("height", height);
svg.append("defs")
.append("filter")
.attr("id", "glow")
.append("feGaussianBlur")
.attr("stdDeviation", activeStyle.glow.stdDeviation)
.attr("result", "coloredBlur");
wordGroup = svg.append("g")
.attr("transform", `translate(${width / 2},${height / 2})`);
simulation = d3.forceSimulation()
.force("collide", d3.forceCollide().radius(d => {
// Calculate actual text dimensions for more accurate collision
const textWidth = getTextWidth(d.text, d.size);
const textHeight = d.size;
return Math.max(textWidth, textHeight) / 2 + 15; // Added padding
}).iterations(3)) // Increased iterations for better collision detection
.force("charge", d3.forceManyBody().strength(activeStyle.forces.charge))
.force("center", d3.forceCenter(0, 0))
.force("x", d3.forceX(0).strength(activeStyle.forces.xStrength))
.force("y", d3.forceY(0).strength(activeStyle.forces.yStrength))
.alphaDecay(0.005) // Slower decay for better settling
.velocityDecay(0.4) // Increased for more stability
.on("tick", ticked);
}
function updateWordCloud() {
const wordEntries = Object.entries(words)
.sort((a, b) => b[1] - a[1])
.slice(0, maxWords);
const maxFrequency = d3.max(wordEntries, d => d[1]);
const minFrequency = d3.min(wordEntries, d => d[1]);
const fontSize = d => {
const baseSize = activeStyle.fontSizes.base * scale;
const maxSizeIncrease = activeStyle.fontSizes.maxIncrease * scale;
const normalizedFrequency = (d - minFrequency) / (maxFrequency - minFrequency || 1);
const sizeIncrease = Math.log1p(normalizedFrequency) / Math.log1p(1) * maxSizeIncrease;
return baseSize + sizeIncrease;
};
// Update collision detection for existing nodes
nodes.forEach(node => {
const entry = wordEntries.find(([text]) => text === node.text);
if (entry) {
node.size = fontSize(entry[1]);
node.frequency = entry[1];
node.width = getTextWidth(node.text, node.size);
node.height = node.size;
}
});
// Create new nodes with proper dimensions
const newNodes = wordEntries
.filter(([text]) => !nodes.some(node => node.text === text))
.map(([text, frequency]) => {
const size = fontSize(frequency);
return {
text,
size,
frequency,
rotate: activeStyle.rotate(),
x: (Math.random() - 0.5) * width * 0.6, // Reduced spread for initial placement
y: (Math.random() - 0.5) * height * 0.6,
width: getTextWidth(text, size),
height: size,
opacity: 0
};
});
nodes = nodes.concat(newNodes);
// Update force simulation with new collision parameters
simulation.force("collide").radius(d => Math.max(d.width, d.height) / 2 + 15);
simulation.nodes(nodes);
simulation.alpha(1).restart();
const wordElements = wordGroup.selectAll("g.word")
.data(nodes, d => d.text);
const enter = wordElements.enter()
.append("g")
.attr("class", "word")
.attr("transform", d => `translate(${d.x || 0},${d.y || 0})rotate(${d.rotate || 0})`)
.style("opacity", d => d.opacity);
enter.append("text")
.attr("class", "word-outline")
.style("font-size", d => `${d.size}px`)
.style("font-family", "'Sora', sans-serif")
.attr("text-anchor", "middle")
.text(d => d.text);
enter.append("text")
.attr("class", "word-inner")
.style("font-size", d => `${d.size}px`)
.style("font-family", "'Sora', sans-serif")
.style("fill", (d, i) => activeStyle.colors[i % activeStyle.colors.length])
.attr("text-anchor", "middle")
.text(d => d.text);
enter.append("text")
.attr("class", "word-glow")
.style("font-size", d => `${d.size}px`)
.style("font-family", "'Sora', sans-serif")
.attr("text-anchor", "middle")
.text(d => d.text);
wordElements.merge(enter)
.transition()
.duration(500)
.attr("transform", d => `translate(${d.x || 0},${d.y || 0})rotate(${d.rotate || 0})`)
.style("opacity", 1)
.selectAll("text")
.style("font-size", d => `${d.size}px`);
wordElements.exit().remove();
}
function ticked() {
// Add padding to the bounds to prevent words from touching the edges
const padding = 20;
const boundedNodes = nodes.map(d => {
const halfWidth = d.width / 2;
const halfHeight = d.height / 2;
d.x = Math.max(-width / 2 + halfWidth + padding,
Math.min(width / 2 - halfWidth - padding, d.x || 0));
d.y = Math.max(-height / 2 + halfHeight + padding,
Math.min(height / 2 - halfHeight - padding, d.y || 0));
return d;
});
wordGroup.selectAll("g.word")
.data(boundedNodes)
.attr("transform", d => `translate(${d.x || 0},${d.y || 0})rotate(${d.rotate || 0})`);
}
function handleResize() {
width = window.innerWidth;
height = window.innerHeight;
svg.attr("width", width).attr("height", height);
wordGroup.attr("transform", `translate(${width / 2},${height / 2})`);
simulation.force("x", d3.forceX(0).strength(0.05));
simulation.force("y", d3.forceY(0).strength(0.05));
simulation.alpha(1).restart();
}
function clearWordCloud() {
words = {};
nodes = [];
wordGroup.selectAll("g.word").remove();
simulation.nodes(nodes);
simulation.alpha(1).restart();
console.log("Word cloud cleared");
}
function processData(data) {
if (data.content) {
data = data.content;
}
if (data.chatmessage) {
const message = data.chatmessage.trim().toLowerCase();
if (/^\w+$/.test(message) && !message.match(/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F1E0}-\u{1F1FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/u)) {
words[message] = (words[message] || 0) + 1;
updateWordCloud();
}
}
if ("state" in data) {
clearWordCloud();
}
}
function setupIframe() {
const iframe = document.createElement("iframe");
const filename = new URL(window.location.href).pathname.split('/').pop().split('.')[0] || "dock";
iframe.src = "https://vdo.socialstream.ninja/?ln&salt=vdo.ninja¬mobile¬mobile&password=false&solo&view=" + urlParams.get('session') + "&novideo&noaudio&label=wordcloud&cleanoutput&room=" + urlParams.get('session');
iframe.style.width = "0px";
iframe.style.height = "0px";
iframe.style.position = "fixed";
iframe.style.left = "-100px";
iframe.style.top = "-100px";
iframe.id = "frame1";
document.body.appendChild(iframe);
const eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
const eventer = window[eventMethod];
const messageEvent = eventMethod === "attachEvent" ? "onmessage" : "message";
eventer(messageEvent, function (e) {
if (e.source != iframe.contentWindow) return;
if ("dataReceived" in e.data) {
if ("overlayNinja" in e.data.dataReceived) {
processData(e.data.dataReceived.overlayNinja);
}
}
});
}
// Initialize everything
initializeWordCloud();
setupIframe();
window.addEventListener('resize', handleResize);
})();
</script>
</body>
</html>