新年倒计时烟花爱心代码
时间: 2024-01-02 17:22:43 浏览: 42
以下是一个新年倒计时烟花爱心代码的示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>New Year Countdown</title>
<style>
body {
background-color: black;
color: white;
text-align: center;
font-family: Arial, sans-serif;
}
#countdown {
font-size: 100px;
margin-top: 200px;
}
#fireworks {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
</style>
</head>
<body>
<h1>Happy New Year!</h1>
<div id="countdown"></div>
<canvas id="fireworks"></canvas>
<script>
// 设置倒计时的目标日期和时间
var countdownDate = new Date("Jan 1, 2023 00:00:00").getTime();
// 更新倒计时的时间
var countdown = setInterval(function() {
var now = new Date().getTime();
var distance = countdownDate - now;
// 计算剩余的天、小时、分钟和秒
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// 显示倒计时
document.getElementById("countdown").innerHTML = days + "天 " + hours + "小时 "
+ minutes + "分钟 " + seconds + "秒 ";
// 如果倒计时结束,显示祝福语
if (distance < 0) {
clearInterval(countdown);
document.getElementById("countdown").innerHTML = "Happy New Year!";
}
}, 1000);
// 创建烟花效果
var canvas = document.getElementById("fireworks");
var ctx = canvas.getContext("2d");
var particles = [];
var particleCount = 100;
function createParticle() {
var particle = {
x: canvas.width / 2,
y: canvas.height,
size: Math.random() * 7 + 3,
speedX: Math.random() * 3 - 1.5,
speedY: Math.random() * -15 - 10,
color: "hsl(" + Math.random() * 360 + ", 100%, 50%)"
};
particles.push(particle);
}
function drawParticles() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < particles.length; i++) {
var particle = particles[i];
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2);
ctx.fillStyle = particle.color;
ctx.fill();
particle.x += particle.speedX;
particle.y += particle.speedY;
if (particle.size > 0.2) particle.size -= 0.1;
}
}
function updateParticles() {
for (var i = 0; i < particleCount; i++) {
createParticle();
}
drawParticles();
if (particles.length > 200) {
for (var j = 0; j < 20; j++) {
particles.shift();
}
}
}
setInterval(updateParticles, 1000 / 60);
</script>
</body>
</html>
```
请注意,这段代码包含了HTML、CSS和JavaScript,可以在浏览器中直接运行。它会显示一个倒计时,直到2023年新年,同时还会产生烟花效果。