1 Star 0 Fork 0

江山录/canvas学习笔记

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
雪花飘落.html 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
江山录 提交于 2024-06-30 16:18 . 新增了几个canvas图标
<!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>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/panws/canvas-learning-notes.git
[email protected]:panws/canvas-learning-notes.git
panws
canvas-learning-notes
canvas学习笔记
master

搜索帮助