Spaces:
Sleeping
Sleeping
| document.addEventListener('DOMContentLoaded', function() { | |
| // Poem data | |
| const poems = [ | |
| { | |
| text: "In the quiet of the evening, under skies so wide, I find my heart beating close to yours, my love. The stars above whisper secrets of our bond, eternal as the night, deep as the ocean's song.", | |
| keywords: ["evening", "love", "stars"] | |
| }, | |
| { | |
| text: "Autumn leaves dance in crimson and gold, mirroring the fire that burns within my soul. Each leaf that falls is a promise made, that in your arms is where I'll fade.", | |
| keywords: ["autumn", "leaves", "fire"] | |
| }, | |
| { | |
| text: "Your eyes are the moonlight that guides me through the dark, your touch the spark that ignites the fire in my heart. In this vast universe, you are my constant star, my love, my life, no matter near or far.", | |
| keywords: ["moon", "eyes", "universe"] | |
| }, | |
| { | |
| text: "Like the river finds the sea, my heart has found its way to thee. Through storm and sunshine, come what may, in your love I'll always stay.", | |
| keywords: ["river", "sea", "love"] | |
| }, | |
| { | |
| text: "In the garden of your heart, I plant my love so true. With every beat it grows, a flower just for you. The petals soft as whispers, the scent so sweet and light, blooming in the sunshine of your love so bright.", | |
| keywords: ["garden", "heart", "flower"] | |
| } | |
| ]; | |
| // DOM elements | |
| const poemPrompt = document.getElementById('poem-prompt'); | |
| const generateBtn = document.getElementById('generate-btn'); | |
| const poemText = document.getElementById('poem-text'); | |
| const poemContainer = document.getElementById('poem-container'); | |
| const prevBgBtn = document.getElementById('prev-bg'); | |
| const nextBgBtn = document.getElementById('next-bg'); | |
| const bgSelect = document.getElementById('bg-select'); | |
| // Background images | |
| const backgrounds = [ | |
| { id: 'autumn', path: 'assets/images/bg-autumn.svg' }, | |
| { id: 'stars', path: 'assets/images/bg-stars.svg' }, | |
| { id: 'hearts', path: 'assets/images/bg-hearts.svg' } | |
| ]; | |
| let currentBgIndex = 0; | |
| // Set initial background | |
| updateBackground(); | |
| // Event listeners | |
| generateBtn.addEventListener('click', generatePoem); | |
| poemPrompt.addEventListener('keypress', function(e) { | |
| if (e.key === 'Enter') { | |
| generatePoem(); | |
| } | |
| }); | |
| prevBgBtn.addEventListener('click', showPreviousBackground); | |
| nextBgBtn.addEventListener('click', showNextBackground); | |
| bgSelect.addEventListener('change', function() { | |
| const selectedId = this.value; | |
| const index = backgrounds.findIndex(bg => bg.id === selectedId); | |
| if (index !== -1) { | |
| currentBgIndex = index; | |
| updateBackground(); | |
| } | |
| }); | |
| // Functions | |
| function generatePoem() { | |
| const input = poemPrompt.value.toLowerCase(); | |
| // Simple filtering based on input keywords | |
| let filteredPoems = poems; | |
| if (input.trim()) { | |
| const keywords = input.split(/\s+/); | |
| filteredPoems = poems.filter(poem => { | |
| return keywords.some(keyword => | |
| poem.keywords.some(kw => kw.includes(keyword)) | |
| ); | |
| }); | |
| } | |
| // If no matching poems, use all poems | |
| if (filteredPoems.length === 0) { | |
| filteredPoems = poems; | |
| } | |
| // Select random poem | |
| const randomIndex = Math.floor(Math.random() * filteredPoems.length); | |
| const selectedPoem = filteredPoems[randomIndex]; | |
| // Display poem with animation | |
| poemText.style.opacity = 0; | |
| setTimeout(() => { | |
| poemText.textContent = selectedPoem.text; | |
| poemText.style.opacity = 1; | |
| // Change background | |
| showNextBackground(); | |
| }, 300); | |
| } | |
| function updateBackground() { | |
| const currentBg = backgrounds[currentBgIndex]; | |
| document.body.style.backgroundImage = `url('${currentBg.path}')`; | |
| bgSelect.value = currentBg.id; | |
| } | |
| function showNextBackground() { | |
| currentBgIndex = (currentBgIndex + 1) % backgrounds.length; | |
| updateBackground(); | |
| } | |
| function showPreviousBackground() { | |
| currentBgIndex = (currentBgIndex - 1 + backgrounds.length) % backgrounds.length; | |
| updateBackground(); | |
| } | |
| }); |