<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>html5线条传输能量动画特效</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
}
canvas {
display: block;
}
.link {
position: fixed;
bottom: 10px;
right: 10px;
color: rgba(255, 255, 255, 0.5);
font-family: Arial, sans-serif;
font-size: 12px;
text-decoration: none;
z-index: 100;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const lines = [];
const lineCount = 20;
class Line {
constructor() {
this.reset();
}
reset() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.length = Math.random() * 100 + 50;
this.speed = Math.random() * 2 + 1;
this.angle = Math.random() * Math.PI * 2;
this.width = Math.random() * 2 + 1;
this.alpha = Math.random() * 0.5 + 0.1;
this.hue = Math.random() * 60 + 180; // Blue to green range
}
update() {
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed;
if (this.x < 0 || this.x > canvas.width || this.y < 0 || this.y > canvas.height) {
this.reset();
}
}
draw() {
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(
this.x + Math.cos(this.angle) * this.length,
this.y + Math.sin(this.angle) * this.length
);
ctx.strokeStyle = `hsla(${this.hue}, 100%, 50%, ${this.alpha})`;
ctx.lineWidth = this.width;
ctx.stroke();
}
}
function init() {
for (let i = 0; i < lineCount; i++) {
lines.push(new Line());
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
lines.forEach(line => {
line.update();
line.draw();
});
requestAnimationFrame(animate);
}
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
init();
animate();
</script>
</body>
</html>