Personalized Fantasy Animal Creator

Weave magic into life! Describe the creature of your dreams by selecting its traits below.

${generatedAnimalData.appearance}

Notable Feature:

${generatedAnimalData.specialFeature}

Diet:

${generatedAnimalData.diet}

Sounds / Communication:

${generatedAnimalData.sounds}.

`; resultsDiv.style.display = 'block'; pdfBtn.disabled = false; // Enable PDF button } // --- PDF Download Function --- function downloadPDF() { if (!generatedAnimalData || Object.keys(generatedAnimalData).length === 0) { alert("Please create an animal first."); return; } if (typeof jspdf === 'undefined') { console.error("jsPDF library is not loaded."); alert("Error: PDF generation library failed to load. Please check internet or try again."); return; } const { jsPDF } = jspdf; const doc = new jsPDF(); // --- PDF Styling --- const primaryColor = '#2e7d32'; // Green const secondaryColor = '#6a1b9a'; // Purple const accentColor = '#ff8f00'; // Amber const textColor = '#5d4037'; // Brown const pageMargin = 15; const lineSpacing = 7; let currentY = pageMargin; // Helper to add text and manage Y position + page breaks function addTextToPDF(text, size, color, isBold = false, isItalic = false) { const pageHeight = doc.internal.pageSize.height; // Using a standard serif font for PDF let fontStyle = "normal"; if (isBold && isItalic) fontStyle = "bolditalic"; else if (isBold) fontStyle = "bold"; else if (isItalic) fontStyle = "italic"; doc.setFont("times", fontStyle); const textLines = doc.splitTextToSize(text, doc.internal.pageSize.width - pageMargin * 2); if (currentY + size / 2 > pageHeight - pageMargin) { doc.addPage(); currentY = pageMargin; } doc.setFontSize(size); doc.setTextColor(color); textLines.forEach(line => { if (currentY + lineSpacing > pageHeight - pageMargin) { doc.addPage(); currentY = pageMargin; // Re-apply font settings doc.setFont("times", fontStyle); doc.setFontSize(size); doc.setTextColor(color); } // Remove HTML tags for PDF line = line.replace(//g, '').replace(/<\/span>/g, ''); doc.text(line, pageMargin, currentY); currentY += lineSpacing; }); } function addHeadingPDF(text) { addTextToPDF(text, 14, secondaryColor, true); // Purple heading currentY += lineSpacing * 0.3; } function addParagraphPDF(text, isHighlight = false) { // Use accent color for highlighted feature text in PDF addTextToPDF(text, 11, isHighlight ? accentColor : textColor, isHighlight); currentY += lineSpacing * 0.5; } // --- PDF Content --- addTextToPDF(`Fantasy Creature: ${generatedAnimalData.name}`, 18, primaryColor, true); // Green title currentY += lineSpacing * 1.5; addHeadingPDF("Habitat:"); addParagraphPDF(generatedAnimalData.habitat); addHeadingPDF("Nature / Role:"); addParagraphPDF(generatedAnimalData.nature); addHeadingPDF("Typical Size:"); addParagraphPDF(generatedAnimalData.size); addHeadingPDF("Appearance:"); addParagraphPDF(generatedAnimalData.appearance); addHeadingPDF("Notable Feature:"); // Pass true to highlight this paragraph in PDF addParagraphPDF(generatedAnimalData.specialFeature, true); addHeadingPDF("Diet:"); addParagraphPDF(generatedAnimalData.diet); addHeadingPDF("Sounds / Communication:"); addParagraphPDF(generatedAnimalData.sounds + "."); // --- Save PDF --- doc.save(`fantasy_animal_${generatedAnimalData.name.replace(/\s+/g, '_')}.pdf`); } // --- Event Listeners --- createBtn.addEventListener('click', createAnimal); pdfBtn.addEventListener('click', downloadPDF); })(); // Immediately invoke the function
Scroll to Top