Heart.js123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110// Declare the hearts array using constconst hearts = [];// Animate the falling hearts using an arrow functionconst animateHearts = () => { for (let i = 0; i < hearts.length; i++) { const heart = hearts[i]; // Remove hearts that have faded out if (heart.alpha <= 0) { heart.el.remove(); hearts.splice(i, 1); i--; } // Animate existing hearts else { heart.y--; heart.scale += 0.004; heart.alpha -= 0.013; heart.el.style.cssText = `left:${heart.x}px;top:${heart.y}px;opacity:${heart.alpha};transform:scale(${heart.scale},${heart.scale}) rotate(45deg);background:${heart.color};z-index:99999`; } } requestAnimationFrame(animateHearts);};// Handle click events on the page using an arrow functionconst handlePageClick = () => { const previousClickHandler = window.onclick; window.onclick = (e) => { // Call any previous click handler if (previousClickHandler) { previousClickHandler(e); } // Add a new heart where the user clicked const heart = document.createElement("div"); heart.className = "heart"; const newHeart = { el: heart, x: e.clientX - 5, y: e.clientY - 5, scale: 1, alpha: 1, color: getRandomColor(), }; hearts.push(newHeart); document.body.appendChild(heart); };};// Add a click event handler to the page to start the animationhandlePageClick();// Start the animation loopanimateHearts();// Inject CSS rules to style the heart elementconst css = ` .heart { width: 10px; height: 10px; position: fixed; background: #f00; transform: rotate(45deg); -webkit-transform: rotate(45deg); -moz-transform: rotate(45deg); } .heart:after, .heart:before { content: ''; width: inherit; height: inherit; background: inherit; border-radius: 50%; -webkit-border-radius: 500%; -moz-border-radius: 50%; position: fixed; } .heart:after { top: -5px; } .heart:before { left: -5px; }`;const injectHeartStyles = (css) => { const style = document.createElement("style"); style.type = "text/css"; try { style.appendChild(document.createTextNode(css)); } catch (e) { style.styleSheet.cssText = css; } document.head.appendChild(style);};injectHeartStyles(css);// Generate a random RGB color string using an arrow functionconst getRandomColor = () => { return `rgb(${~~(255 * Math.random())},${~~(255 * Math.random())},${~~(255 * Math.random())})`;};