Personalized Alien Planet Explorer

${planet.lifeforms || 'No specific data available.'}

`; // Populate Features Div featuresDiv.innerHTML = `

Key Landmarks / Hazards

${planet.landmarksHazards || 'Standard terrain.'}

`; summaryP.textContent = planet.summary; outputDiv.style.display = 'block'; outputDiv.scrollIntoView({ behavior: 'smooth', block: 'start' }); }; // --- Event Listeners --- generateBtn.addEventListener('click', () => { console.log("Generate button clicked"); try { const inputs = { explorerName: explorerNameInput.value.trim(), planetType: planetTypeSelect.value, atmospherePref: atmosphereSelect.value, lifeformDensity: lifeDensitySelect.value, featureInterest: featureSelect.value }; currentPlanet = generatePlanet(inputs); console.log("Planet generated:", currentPlanet); displayPlanet(currentPlanet); } catch(error) { console.error("Error in generate click handler:", error); summaryP.textContent = "Error generating planet. Check console (F12)."; outputDiv.style.display = 'block'; } }); downloadBtn.addEventListener('click', () => { console.log("Download button clicked"); if (!currentPlanet || !currentPlanet.planetName) { alert("Please generate a planet profile first!"); return; } try { const { jsPDF } = window.jspdf; const { autoTable } = window.jspdfAutotable; const doc = new jsPDF(); // --- PDF Styling --- const primaryColorPDF = '#4facfe'; const secondaryColorPDF = '#a4508b'; const accentColorPDF = '#b0a46c'; // Adjusted gold for better contrast on white const textColorPDF = '#333333'; // Dark text const headerColorPDF = '#1b263b'; // Dark blue header bg const headingFontSize = 18; const subHeadingFontSize = 13; const normalFontSize = 10; const planetNameFontSize = 22; let yPos = 15; const leftMargin = 15; const textWidth = doc.internal.pageSize.getWidth() - (leftMargin * 2); const lineSpacing = 6; const sectionSpacing = 8; // --- PDF Content --- // Title doc.setFontSize(headingFontSize); doc.setFont('helvetica', 'bold'); doc.setTextColor(primaryColorPDF); doc.text("Alien Planet Exploration Profile", doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' }); yPos += sectionSpacing * 1.5; // Explorer Name (if provided) if (currentPlanet.explorerName && currentPlanet.explorerName !== "Unspecified Explorer") { doc.setFontSize(normalFontSize); doc.setFont('helvetica', 'italic'); doc.setTextColor(textColorPDF); doc.text(`Prepared for: ${currentPlanet.explorerName}`, doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' }); yPos += lineSpacing * 1.5; } // Planet Name & System doc.setFontSize(planetNameFontSize); doc.setFont('helvetica', 'bold'); // Use standard fonts doc.setTextColor(headerColorPDF); // Dark blue name doc.text(currentPlanet.planetName, doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' }); yPos += lineSpacing; doc.setFontSize(subHeadingFontSize); doc.setFont('helvetica', 'normal'); doc.setTextColor(accentColorPDF); // Gold system doc.text(currentPlanet.starSystem, doc.internal.pageSize.getWidth() / 2, yPos, { align: 'center' }); yPos += sectionSpacing * 2; // Planetary Data Table doc.setFontSize(subHeadingFontSize); doc.setFont('helvetica', 'bold'); doc.setTextColor(textColorPDF); doc.text("Planetary Data:", leftMargin, yPos); yPos += lineSpacing; autoTable(doc, { startY: yPos, theme: 'grid', headStyles: { fillColor: headerColorPDF, textColor: '#ffffff', fontStyle: 'bold' }, styles: { fontSize: normalFontSize, cellPadding: 2 }, body: [ ['Planet Type', currentPlanet.type], ['Atmosphere', currentPlanet.atmosphere], ['Lifeform Density', currentPlanet.inputs.lifeformDensity], // Show input choice ['Dominant Feature', currentPlanet.inputs.featureInterest], // Show input choice ], didDrawPage: (data) => { yPos = data.cursor.y + sectionSpacing; } // Update yPos }); // yPos is updated by hook // Helper to add text sections with page break check const addTextSection = (title, textContent) => { const titleHeight = subHeadingFontSize * 0.5; const textLines = doc.splitTextToSize(textContent || "N/A", textWidth); const textHeight = textLines.length * (normalFontSize * 0.4); // Adjust multiplier as needed const requiredHeight = titleHeight + textHeight + sectionSpacing + lineSpacing; if (yPos + requiredHeight > doc.internal.pageSize.getHeight() - 15) { // 15mm bottom margin doc.addPage(); yPos = 15; } doc.setFontSize(subHeadingFontSize); doc.setFont('helvetica', 'bold'); doc.setTextColor(textColorPDF); doc.text(title + ":", leftMargin, yPos); yPos += lineSpacing; doc.setFontSize(normalFontSize); doc.setFont('helvetica', 'normal'); doc.text(textLines, leftMargin, yPos); yPos += textHeight + sectionSpacing; // Move yPos down }; // Lifeforms Section addTextSection("Notable Lifeforms", currentPlanet.lifeforms); // Landmarks/Hazards Section addTextSection("Key Landmarks / Hazards", currentPlanet.landmarksHazards); // Exploration Summary Section if (yPos + (subHeadingFontSize * 0.5) + (normalFontSize * 0.4 * 3) + sectionSpacing > doc.internal.pageSize.getHeight() - 15) { // Estimate height for summary doc.addPage(); yPos = 15; } doc.setFontSize(subHeadingFontSize); doc.setFont('helvetica', 'bold'); doc.setTextColor(textColorPDF); doc.text("Exploration Summary:", leftMargin, yPos); yPos += lineSpacing; doc.setFontSize(normalFontSize); doc.setFont('helvetica', 'italic'); // Italic summary const summaryLines = doc.splitTextToSize(currentPlanet.summary || "Standard procedures recommended.", textWidth); doc.text(summaryLines, leftMargin, yPos); // Save PDF const safeName = currentPlanet.planetName.replace(/[^a-z0-9]/gi, '_').toLowerCase(); doc.save(`planet_profile_${safeName}.pdf`); } catch(error) { console.error("Error generating PDF:", error); alert("An error occurred while creating the PDF. Please check the console (F12)."); } }); }); // End DOMContentLoaded
Scroll to Top