xxxxxxxxxx
72
async function loadJSON(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('Error loading JSON:', error);
return {}; // Return an empty object in case of error
}
}
async function setup() {
try {
const data = await loadJSON('instructions_db.json'); // Adjust the path if needed
if (!data["Everyday Items"] || !data["Activities"] || !data["Words"]) {
throw new Error('Missing expected data fields in JSON');
}
const everydayItems = data["Everyday Items"];
const activities = data["Activities"];
const words = data["Words"];
const container = document.getElementById('content');
function updateContent() {
const item = getRandomItem(everydayItems);
const activity = getRandomItem(activities);
const word = getRandomItem(words);
const output = `${item} ${activity} ${word}`;
container.innerHTML = output;
adjustFontSize(container);
}
// Initial content update
updateContent();
// Update content every 3 seconds
setInterval(updateContent, 5000);
// Adjust font size on window resize
window.addEventListener('resize', () => adjustFontSize(container));
} catch (error) {
console.error('Error in setup:', error);
}
}
function getRandomItem(array) {
if (array.length === 0) return ''; // Return empty string if array is empty
return array[Math.floor(Math.random() * array.length)];
}
function adjustFontSize(container) {
const windowWidth = window.innerWidth;
const containerWidth = windowWidth * 0.6; // 60% of the window width
const textWidth = container.scrollWidth;
if (textWidth === 0) return; // Prevent division by zero
// Calculate font size as a proportion of the container width
const newSize = 44;// (containerWidth / textWidth) * 100; // Adjust the scaling factor as needed
// Apply the new font size
container.style.fontSize = `${newSize}px`;
}
// Initialize
setup();