圣诞树代码html飘雪花代码
时间: 2023-12-16 07:28:39 浏览: 214
html实现圣诞树代码
以下是圣诞树代码和HTML飘雪花代码的示例:
1. 圣诞树代码
```python
def print_tree(height):
for i in range(height):
print(" " * (height - i - 1) + "*" * (2 * i + 1))
print_tree(5)
```
2. HTML飘雪花代码
```html
<!DOCTYPE html>
<html>
<head>
<style>
canvas { display: block; }
</style>
</head>
<body>
<canvas id="snowCanvas"></canvas>
<script>
const canvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
let snowflakes = [];
function createSnowflakes() {
const { innerWidth, innerHeight } = window;
canvas.width = innerWidth;
canvas.height = innerHeight;
for (let i = 0; i < 100; i++) {
snowflakes.push({
x: Math.random() * innerWidth,
y: Math.random() * innerHeight,
radius: Math.random() * 5 + 2,
speedX: Math.random() * 3 - 1.5,
speedY: Math.random() * 3 + 1,
});
}
}
function drawSnowflakes() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.beginPath();
for (let i = 0; i < snowflakes.length; i++) {
const snowflake = snowflakes[i];
ctx.moveTo(snowflake.x, snowflake.y); ctx.arc(snowflake.x, snowflake.y, snowflake.radius, 0, Math.PI * 2, true);
}
ctx.fill();
moveSnowflakes();
}
function moveSnowflakes() {
for (let i = 0; i < snowflakes.length; i++) {
const snowflake = snowflakes[i];
snowflake.x += snowflake.speedX;
snowflake.y += snowflake.speedY;
if (snowflake.y > canvas.height) {
snowflake.y = 0;
}
}
}
createSnowflakes();
setInterval(drawSnowflakes, 50);
</script>
</body>
</html>
```
阅读全文