Sure! Below is an example of an HTML simulation where a heart beats, and a slider controls the speed of the beating heart. This implementation uses only HTML, CSS, and JavaScript without any external assets.
HTML and CSS Code:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Heart Beat Simulation</title>
<style>
body {
text-align: center;
padding-top: 50px;
background-color: #fafafa;
font-family: Arial, sans-serif;
}
.heart {
position: relative;
display: inline-block;
width: 100px;
height: 90px;
background-color: #e74c3c;
transform: rotate(-45deg);
animation: beat 1s infinite;
}
.heart::before,
.heart::after {
content: "";
position: absolute;
width: 100px;
height: 90px;
background-color: #e74c3c;
border-radius: 50%;
}
.heart::before {
top: -50px;
left: 0;
}
.heart::after {
left: 50px;
top: 0;
}
@keyframes beat {
0% {
transform: scale(1) rotate(-45deg);
}
25% {
transform: scale(1.1) rotate(-45deg);
}
50% {
transform: scale(1) rotate(-45deg);
}
75% {
transform: scale(1.1) rotate(-45deg);
}
100% {
transform: scale(1) rotate(-45deg);
}
}
.slider-container {
margin-top: 50px;
}
label {
font-size: 18px;
margin-right: 10px;
}
input[type=range] {
width: 300px;
}
</style>
</head>
<body>
<div class="heart"></div>
<div class="slider-container">
<label for="speedRange">Heart Beat Speed:</label>
<input type="range" id="speedRange" min="0.5" max="2" step="0.1" value="1">
</div>
<script>
const heart = document.querySelector('.heart');
const speedRange = document.getElementById('speedRange');
speedRange.addEventListener('input', function() {
heart.style.animationDuration = speedRange.value + 's';
});
</script>
</body>
</html>
How It Works:
Usage:
Copy and paste the above code into a file with a .html extension (e.g., heartbeat.html).
Open the file in a web browser.
You will see a heart that beats continuously.
Use the slider below the heart to control the speed of the beating.
Customizations:
Heart Color:
Change the background-color property in the .heart, .heart::before, and .heart::after selectors to change the heart's color.
Animation Timing:
Modify the @keyframes beat percentages and transform scales to adjust how the heart beats.
Slider Range:
Update the min, max, and step attributes of the input range slider to allow different speed controls.
Live Demo (CodePen):
You can view and interact with the live demo of this code on CodePen:
Beating Heart Simulation
(Note: Since external links cannot be embedded here, you can copy the code into CodePen or any online HTML/CSS/JS editor to see it in action.)
Feel free to ask if you need further assistance or additional features!