Retro Game Idea Generator 🕹️
Parameters Selected
Genre:
Theme:
Character:
Goal:
Synopsis
Gameplay Mechanics
Potential Enemies/Obstacles
${enemies}
`; // Populate PDF content elements *before* showing output/enabling download pdfTitle.textContent = gameTitle; pdfGenre.textContent = genre; pdfTheme.textContent = theme; pdfCharacter.textContent = character; pdfGoal.textContent = goal; pdfSynopsis.textContent = synopsis; pdfGameplay.textContent = gameplay; pdfEnemies.textContent = enemies; console.log("PDF content elements populated."); // Show results outputArea.style.display = 'block'; downloadButton.disabled = false; generateButton.disabled = false; console.log("Game concept generated and displayed."); }); // --- PDF Download --- downloadButton.addEventListener('click', () => { // Ensure content has been generated and displayed if (outputArea.style.display === 'none' || !pdfSynopsis.textContent) { console.error("Cannot download PDF: No concept generated yet."); alert("Please generate a game concept first!"); return; } console.log("Download PDF button clicked."); const element = document.getElementById('concept-pdf-content'); // Verify element exists if (!element) { console.error("PDF content container element not found!"); return; } // Verify content exists within the PDF container before proceeding console.log('Verifying PDF content innerHTML:', element.innerHTML.substring(0, 500) + "..."); // Log snippet if (!pdfTitle.textContent || !pdfSynopsis.textContent) { console.error("PDF content appears empty despite UI display. Aborting PDF generation."); alert("Error: PDF content is missing. Please try generating again."); return; } const gameTitle = (titleDisplay.textContent || 'Untitled').replace(/[^a-z0-9]/gi, '_').replace(/_{2,}/g, '_'); const filename = `Retro_Game_Concept_${gameTitle}.pdf`; const opt = { margin: [15, 12, 15, 12], // mm T, L, B, R filename: filename, image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2, logging: false, letterRendering: true, // Important for pixel fonts useCORS: true // Usually needed for web fonts }, jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' } }; // --- Refined Visibility Toggle for PDF Generation --- // Temporarily make the element visible but keep it off-screen const originalVisibility = element.style.visibility; const originalPosition = element.style.position; const originalLeft = element.style.left; const originalTop = element.style.top; element.style.visibility = 'visible'; // Make it visible to the renderer element.style.position = 'absolute'; // Keep absolute positioning element.style.left = '0px'; // Bring into viewport context for measurement element.style.top = '0px'; element.style.zIndex = '-1'; // Keep it behind everything visually console.log("Starting PDF generation for element:", element); html2pdf().set(opt).from(element).save().then(() => { console.log("PDF Generated:", filename); }).catch(err => { console.error("PDF generation failed:", err); alert("Sorry, there was an error creating the PDF. Please check the console (F12) for details."); }).finally(() => { // --- Always restore original styles --- console.log("Restoring original styles for PDF content element."); element.style.visibility = originalVisibility; element.style.position = originalPosition; element.style.left = originalLeft; element.style.top = originalTop; element.style.zIndex = ''; // Reset z-index if needed }); }); } // End initializeGenerator })(); // IIFE