圣诞树带圣诞歌有雪花的html代码
时间: 2023-12-26 13:27:56 浏览: 92
圣诞树html网页代码.zip
以下是一个带有圣诞歌和雪花效果的HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>圣诞树</title>
<style>
body {
background-color: blue;
}
canvas {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<canvas id="snowCanvas"></canvas>
<h1>圣诞树</h1>
<pre>
We wish you a Merry Christmas,
We wish you a Merry Christmas,
We wish you a Merry Christmas,
And a Happy New Year!
</pre>
<script>
// 雪花效果
var canvas = document.getElementById("snowCanvas");
var ctx = canvas.getContext("2d");
var width = window.innerWidth;
var height = window.innerHeight;
canvas.width = width;
canvas.height = height;
var snowflakes = [];
function Snowflake() {
this.x = Math.random() * width;
this.y = Math.random() * height;
this.radius = Math.random() * 3 + 1;
this.speed = Math.random() * 3 + 1;
this.opacity = Math.random();
}
Snowflake.prototype.update = function() {
this.y += this.speed;
if (this.y > height) {
this.y = -this.radius;
}
}
Snowflake.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = "white";
ctx.globalAlpha = this.opacity;
ctx.fill();
}
function createSnowflakes() {
for (var i = 0; i < 100; i++) {
snowflakes.push(new Snowflake());
}
}
function drawSnowflakes() {
ctx.clearRect(0, 0, width, height);
for (var i = 0; i < snowflakes.length; i++) {
snowflakes[i].update();
snowflakes[i].draw();
}
requestAnimationFrame(drawSnowflakes);
}
createSnowflakes();
drawSnowflakes();
</script>
</body>
</html>
```
阅读全文