Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions 下雨.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>下雨</title>
<style>
body {
background-color: black;
overflow: hidden;
}
</style>
</head>

<body>
<canvas></canvas>
<script>
const cvs = document.querySelector('canvas');
const ctx = cvs.getContext('2d');
let rains = [];
const count = 1000;
initCvs();
window.addEventListener('resize', () => initCvs());
addRain();
render();
function addRain() {
while (rains.length < count) {
const length = Math.random() * 30 + 10;
rains.push({
x: Math.random() * cvs.width,
y: -length - Math.random() * cvs.height,
length,
speed: Math.random() * 15 + 15
});
}
requestAnimationFrame(addRain);
}

function render() {
if (rains.length > 0) {
ctx.clearRect(0, 0, cvs.width, cvs.height);
rains.forEach((v, i) => {
if (v.y > cvs.height)
rains.splice(i, 1);
ctx.beginPath();
ctx.moveTo(v.x, v.y);
ctx.lineTo(v.x, v.y + v.length);
ctx.stroke();
ctx.closePath();
v.y += v.speed;
});
}
rainAnimation = requestAnimationFrame(render);
}

function initCvs() {
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
ctx.strokeStyle = '#fff';
}
</script>
</body>

</html>