Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const textInput = document.getElementById('textInput');
+ const generateBtn = document.getElementById('generateBtn');
+ const downloadBtn = document.getElementById('downloadBtn');
+ const canvas = document.getElementById('cloudCanvas');
+ const ctx = canvas.getContext('2d');
+
+ function getWordFrequency(text) {
+ const words = text.toLowerCase()
+ .replace(/[^\w\s]/g, '')
+ .split(/\s+/)
+ .filter(word => word.length > 3);
+
+ const frequency = {};
+ words.forEach(word => {
+ frequency[word] = (frequency[word] || 0) + 1;
+ });
+
+ return Object.entries(frequency)
+ .sort((a, b) => b[1] - a[1])
+ .slice(0, 50);
+ }
+
+ function generateCloud() {
+ const text = textInput.value;
+ if (!text) return;
+
+ const words = getWordFrequency(text);
+
+ canvas.width = canvas.parentElement.offsetWidth - 40;
+ canvas.height = 400;
+
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ ctx.textAlign = 'center';
+ ctx.textBaseline = 'middle';
+
+ const maxFreq = Math.max(...words.map(w => w[1]));
+
+ words.forEach(([word, freq], i) => {
+ const fontSize = Math.max(12, (freq / maxFreq) * 48);
+ const angle = Math.random() < 0.5 ? 0 : Math.PI / 2;
+
+ ctx.font = `${fontSize}px system-ui, sans-serif`;
+ ctx.fillStyle = `hsl(${230 + Math.random() * 60}, ${70 + Math.random() * 30}%, ${40 + Math.random() * 20}%)`;
+
+ const x = canvas.width/2 + (Math.random() - 0.5) * canvas.width/2;
+ const y = canvas.height/2 + (Math.random() - 0.5) * canvas.height/2;
+
+ ctx.save();
+ ctx.translate(x, y);
+ ctx.rotate(angle);
+ ctx.fillText(word, 0, 0);
+ ctx.restore();
+ });
+ }
+
+ function downloadCloud() {
+ const link = document.createElement('a');
+ link.download = 'wordcloud.png';
+ link.href = canvas.toDataURL();
+ link.click();
+ }
+
+ generateBtn.addEventListener('click', generateCloud);
+ downloadBtn.addEventListener('click', downloadCloud);
+
+ // Responsive canvas resize
+ window.addEventListener('resize', generateCloud);
+ });