倒计时跨年烟花代码html
时间: 2023-12-08 14:03:02 浏览: 184
以下是一个简单的倒计时跨年烟花代码的HTML示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>New Year Countdown with Fireworks</title>
<style>
body {
background-color: black;
color: white;
font-size: 50px;
text-align: center;
margin-top: 200px;
}
canvas {
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
</style>
</head>
<body>
<div id="countdown"></div>
<canvas id="fireworks"></canvas>
<script>
// 设置倒计时时间
var countDownDate = new Date("Jan 1, 2023 00:00:00").getTime();
// 更新倒计时
var x = 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 + "d " + hours + "h " + minutes + "m " + seconds + "s ";
// 如果倒计时结束,显示祝福语
if (distance < 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "Happy New Year!";
}
}, 1000);
// 烟花效果
var canvas = document.getElementById("fireworks");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(0, 0, 0, 0.2)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
var particles = [];
var particleCount = 100;
var particleSpeed = 5;
function Particle() {
this.x = canvas.width / 2;
this.y = canvas.height / 2;
this.vx = Math.random() * particleSpeed - particleSpeed / 2;
this.vy = Math.random() * particleSpeed - particleSpeed / 2;
this.gravity = 0.3;
this.color = "hsla(" + parseInt(Math.random() * 360, 10) + ", 100%, 50%, 1)";
this.size = Math.random() * 3;
}
Particle.prototype.update = function() {
this.x += this.vx;
this.y += this.vy;
this.vy += this.gravity;
this.size -= 0.05;
};
Particle.prototype.draw = function() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
};
for (var i = 0; i < particleCount; i++) {
particles.push(new Particle());
}
function loop() {
ctx.fillStyle = "rgba(0, 0, 0, 0.2)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
if (particles[i].size <= 0.2) {
particles.splice(i, 1);
}
}
if (particles.length < particleCount) {
particles.push(new Particle());
}
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>
```
阅读全文