代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Snowfall with Canvas</title>
<style>
body,
html {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #2c3e50;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="snowCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById("snowCanvas");
const ctx = canvas.getContext("2d");
class Snowflake {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 3 + 2; // Snowflake sizes between 2 and 5
this.speed = Math.random() * 1 + 0.5; // Falling speeds
}
update() {
this.y += this.speed;
if (this.y > canvas.height) {
this.y = 0;
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = "white";
ctx.fill();
}
}
const snowflakes = [];
for (let i = 0; i < 200; i++) {
snowflakes.push(new Snowflake());
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowflakes.forEach((snowflake) => {
snowflake.update();
snowflake.draw();
});
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。