Carbohydrate Counting Tool

Carbohydrate Counting Tool

Build your meal and track your carbohydrate intake with ease.

Add Foods to Your Meal

Current Meal

Your meal is empty.

Total Carbs: 0g

Your meal is empty.

`; } else { currentMeal.forEach((food, index) => { totalCarbs += food.carbs; const el = document.createElement('div'); el.className = 'flex justify-between items-center p-2 border-b'; el.innerHTML = `

${food.name}

${food.carbs}g carbs

`; el.querySelector('.remove-food-btn').addEventListener('click', () => removeFoodFromMeal(index)); mealListContainer.appendChild(el); }); } document.getElementById('total-carbs').textContent = `${totalCarbs.toFixed(1)}g`; }; const renderCustomFoodList = () => { const listContainer = document.getElementById('custom-food-list'); listContainer.innerHTML = ''; if (customFoods.length === 0) { listContainer.innerHTML = `

You haven't added any custom foods yet.

`; return; } customFoods.forEach((food, index) => { const el = document.createElement('div'); el.className = 'flex justify-between items-center p-2 border-b'; el.innerHTML = `

${food.name}

${food.carbs}g carbs per ${food.serving}

`; el.querySelector('.remove-custom-food-btn').addEventListener('click', () => { customFoods.splice(index, 1); saveData(); renderCustomFoodList(); renderSearchResults(document.getElementById('food-search').value); }); listContainer.appendChild(el); }); }; const handleAddCustomFood = () => { const name = document.getElementById('food-name').value.trim(); const serving = document.getElementById('serving-size').value.trim(); const carbs = parseFloat(document.getElementById('food-carbs').value); if (name && serving && !isNaN(carbs)) { customFoods.push({ name, serving, carbs }); saveData(); renderCustomFoodList(); renderSearchResults(document.getElementById('food-search').value); // Clear inputs document.getElementById('food-name').value = ''; document.getElementById('serving-size').value = ''; document.getElementById('food-carbs').value = ''; } else { alert('Please fill in all fields with valid data.'); } }; const renderSummary = () => { const container = document.getElementById('summary-container'); container.innerHTML = ''; let totalCarbs = 0; if (currentMeal.length === 0) { container.innerHTML = `

No items in your meal to summarize.

`; return; } const table = document.createElement('table'); table.className = 'w-full text-left'; table.innerHTML = ` Food Item Carbs (g) Total `; const tbody = table.querySelector('tbody'); currentMeal.forEach(food => { totalCarbs += food.carbs; const row = document.createElement('tr'); row.innerHTML = `${food.name}${food.carbs.toFixed(1)}`; tbody.appendChild(row); }); table.querySelector('#summary-total').textContent = `${totalCarbs.toFixed(1)}g`; container.appendChild(table); }; const generatePdf = () => { const elementToCapture = document.getElementById('results-content'); if (!elementToCapture || currentMeal.length === 0) { alert("Please build a meal first before downloading a summary."); return; } document.body.classList.add('pdf-capture'); html2canvas(elementToCapture, { scale: 2, useCORS: true, logging: false }) .then(canvas => { document.body.classList.remove('pdf-capture'); const imgData = canvas.toDataURL('image/png'); const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const margin = 15; const contentWidth = pdfWidth - margin * 2; const aspectRatio = canvas.width / canvas.height; const contentHeight = contentWidth / aspectRatio; pdf.setFontSize(18); pdf.setTextColor(40); pdf.text("Carbohydrate Meal Summary", pdfWidth / 2, margin, { align: 'center' }); pdf.addImage(imgData, 'PNG', margin, margin + 5, contentWidth, contentHeight); pdf.save('Carb-Summary.pdf'); }).catch(err => console.error("html2canvas error:", err)); }; // --- Event Listeners --- Object.entries(tabs.buttons).forEach(([num, btn]) => btn.addEventListener('click', () => switchTab(parseInt(num)))); navButtons.prev.addEventListener('click', () => { if (currentTab > 1) switchTab(currentTab - 1); }); navButtons.next.addEventListener('click', () => { if (currentTab < 4) switchTab(currentTab + 1); }); document.getElementById('food-search').addEventListener('input', (e) => renderSearchResults(e.target.value)); document.getElementById('add-custom-food-btn').addEventListener('click', handleAddCustomFood); document.getElementById('download-pdf-btn').addEventListener('click', generatePdf); // --- Initial Setup --- loadData(); updateNavButtons(); });
Scroll to Top