In HTML, CSS, and JS, write a heartbeat simulator, where there's a range input with a…
- User
In HTML, CSS, and JS, write a heartbeat simulator, where there's a range input with a min-max of the human average heartbeat in BPM, above, there is a heart that simulates the heartbeat, make sure to make a sleek dark UI design with accurate human heartbeat simulation
- Assistant
Certainly! Below is a complete implementation of a heartbeat simulator using HTML, CSS, and JavaScript. The simulator features:
- A heart that simulates the human heartbeat with an accurate "lub-dub" pattern.
- A range input to adjust the heartbeat rate (BPM) within the average human range.
- A sleek dark UI design with a responsive and interactive interface.
HTML (
index.html):<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Heartbeat Simulator</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <div class="heart"></div> <div class="controls"> <input type="range" id="bpmRange" min="40" max="180" value="80"> <label for="bpmRange">BPM: <span id="bpmValue">80</span></label> </div> </div> <script src="script.js"></script> </body> </html>CSS (
style.css):/* General Styles */ body { background-color: #121212; color: #ffffff; font-family: Arial, sans-serif; margin: 0; padding: 0; } .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } /* Heart Styles */ .heart { position: relative; width: 100px; height: 90px; transform: rotate(-45deg); transform-origin: center; background-color: #e60000; animation: beat 1s infinite; box-shadow: 0 0 20px rgba(230, 0, 0, 0.5); } .heart::before, .heart::after { content: ""; position: absolute; width: 100px; height: 90px; background-color: #e60000; border-radius: 50%; } .heart::before { top: -50px; left: 0; } .heart::after { left: 50px; top: 0; } /* Heartbeat Animation */ @keyframes beat { 0% { transform: scale(1) rotate(-45deg); box-shadow: 0 0 20px rgba(230, 0, 0, 0.5); } 5% { transform: scale(1.3) rotate(-45deg); box-shadow: 0 0 30px rgba(230, 0, 0, 0.7); } 10% { transform: scale(1) rotate(-45deg); box-shadow: 0 0 20px rgba(230, 0, 0, 0.5); } 15% { transform: scale(1.3) rotate(-45deg); box-shadow: 0 0 30px rgba(230, 0, 0, 0.7); } 20% { transform: scale(1) rotate(-45deg); box-shadow: 0 0 20px rgba(230, 0, 0, 0.5); } 100% { transform: scale(1) rotate(-45deg); box-shadow: 0 0 20px rgba(230, 0, 0, 0.5); } } /* Controls Styles */ .controls { margin-top: 40px; display: flex; align-items: center; } input[type=range] { -webkit-appearance: none; width: 300px; background: transparent; margin-right: 15px; } input[type=range]:focus { outline: none; } /* Range Track */ input[type=range]::-webkit-slider-runnable-track { width: 100%; height: 10px; cursor: pointer; background: #444; border-radius: 5px; } input[type=range]:focus::-webkit-slider-runnable-track { background: #555; } /* Range Thumb */ input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; height: 24px; width: 24px; border-radius: 12px; background: #e60000; margin-top: -7px; cursor: pointer; box-shadow: 0 0 5px #e60000; } /* Firefox */ input[type=range]::-moz-range-track { width: 100%; height: 10px; cursor: pointer; background: #444; border-radius: 5px; } input[type=range]::-moz-range-thumb { height: 24px; width: 24px; border-radius: 12px; background: #e60000; cursor: pointer; box-shadow: 0 0 5px #e60000; } /* IE */ input[type=range]::-ms-track { width: 100%; height: 10px; cursor: pointer; background: transparent; border-color: transparent; color: transparent; } input[type=range]::-ms-fill-lower { background: #444; border-radius: 5px; } input[type=range]::-ms-fill-upper { background: #444; border-radius: 5px; } input[type=range]::-ms-thumb { height: 24px; width: 24px; border-radius: 12px; background: #e60000; cursor: pointer; box-shadow: 0 0 5px #e60000; } input[type=range]:focus::-ms-fill-lower { background: #555; } input[type=range]:focus::-ms-fill-upper { background: #555; } /* Label Styles */ label { font-size: 1.2em; } #bpmValue { font-weight: bold; color: #e60000; text-shadow: 0 0 5px #e60000; }JavaScript (
script.js):const bpmRange = document.getElementById('bpmRange'); const bpmValue = document.getElementById('bpmValue'); const heart = document.querySelector('.heart'); function updateBPM() { const bpm = bpmRange.value; bpmValue.textContent = bpm; // Calculate animation duration based on BPM // Total duration for one heartbeat cycle (in seconds) const duration = 60 / bpm; // Update the heart's animation duration heart.style.animationDuration = duration + 's'; } bpmRange.addEventListener('input', updateBPM); // Initialize on page load updateBPM();
Explanation:
-
Heart Animation:
- The heart is created using pure CSS. By positioning and rotating squares and circles, we form a heart shape.
- The
@keyframes beatanimation simulates the heartbeat with a "lub-dub" pattern. The heart scales up slightly and glows at specific keyframes to mimic the quick double beat followed by a pause. - The
animation-durationis dynamically adjusted based on the BPM to speed up or slow down the heartbeat.
-
BPM Control:
- The range input (
input[type=range]) allows users to adjust the BPM between 40 and 180, which covers typical resting and active heart rates for humans. - The current BPM value is displayed next to the slider and updates in real-time as the slider is moved.
- The range input (
-
Dark UI Design:
- A sleek dark theme is applied using dark background colors and contrasting text and element colors.
- The heart and slider thumb are accented with a vibrant red color (#e60000) to draw attention.
- Subtle shadows and glow effects are added to enhance the visual appeal.
How to Use:
-
Open
index.htmlin a modern web browser. -
Adjust the BPM:
- Use the slider to set the desired BPM.
- The heart animation will adjust accordingly, simulating the heartbeat at the selected BPM.
Notes:
-
Cross-Browser Compatibility:
- The CSS includes prefixes and styles for WebKit (Chrome, Safari), Mozilla (Firefox), and Microsoft (Edge, IE) browsers to ensure consistent appearance.
-
Responsive Design:
- The interface is centered and scales well on different screen sizes.
-
Further Customization:
- You can adjust the colors, sizes, and animation effects in the CSS to suit your preferences.
- Additional elements or information (e.g., labels, pulse graphs) can be added to enhance the simulator.
Feel free to copy and paste this code into your own files to test and run the heartbeat simulator. Enjoy exploring the simulation of the human heartbeat!