帮我写一个星空html页面
时间: 2024-06-04 14:12:39 浏览: 125
<!DOCTYPE html>
<html>
<head>
<title>星空</title>
<style>
body {
background-color: #000;
color: #fff;
font-family: Arial, sans-serif;
}
canvas {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<h1>星空</h1>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// 设置canvas宽高
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 生成随机星星
var stars = [];
for (var i = 0; i < 100; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: Math.random() * 3 + 1,
color: "#fff"
});
}
// 绘制星星
function drawStars() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < stars.length; i++) {
var star = stars[i];
ctx.beginPath();
ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2);
ctx.fillStyle = star.color;
ctx.fill();
}
}
// 动画循环
function animate() {
requestAnimationFrame(animate);
drawStars();
}
animate();
</script>
</body>
</html>
阅读全文