<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas魔幻线条动画特效</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
.info {
position: absolute;
bottom: 20px;
color: rgba(255, 255, 255, 0.5);
font-size: 14px;
text-align: center;
}
.info a {
color: rgba(255, 255, 255, 0.8);
text-decoration: none;
}
.title {
position: absolute;
top: 20px;
color: rgba(255, 255, 255, 0.8);
font-size: 24px;
text-align: center;
text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}
</style>
</head>
<body>
<div class="title">Canvas魔幻线条动画</div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// 设置canvas大小为窗口大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 线条数组
const lines = [];
const lineCount = 50;
const maxDistance = 200;
const colors = [
'rgba(255, 0, 255, 0.5)',
'rgba(0, 255, 255, 0.5)',
'rgba(255, 255, 0, 0.5)',
'rgba(0, 255, 0, 0.5)',
'rgba(255, 0, 0, 0.5)',
'rgba(0, 0, 255, 0.5)'
];
// 线条类
class Line {
constructor() {
this.reset();
this.z = Math.random() * 4 + 1;
this.color = colors[Math.floor(Math.random() * colors.length)];
}
reset() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.vx = Math.random() * 2 - 1;
this.vy = Math.random() * 2 - 1;
this.life = 0;
this.maxLife = Math.random() * 100 + 100;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.life++;
// 边界检查
if (this.x < 0 || this.x > canvas.width || this.y < 0 || this.y > canvas.height || this.life >= this.maxLife) {
this.reset();
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.z, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
}
}
// 初始化线条
for (let i = 0; i < lineCount; i++) {
lines.push(new Line());
}
// 鼠标位置
const mouse = {
x: null,
y: null
};
// 跟踪鼠标位置
window.addEventListener('mousemove', (e) => {
mouse.x = e.clientX;
mouse.y = e.clientY;
});
// 窗口大小改变时重置canvas大小
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// 动画循环
function animate() {
// 半透明背景制造拖尾效果
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 更新和绘制所有线条
for (let i = 0; i < lines.length; i++) {
lines[i].update();
lines[i].draw();
// 绘制线条之间的连接
for (let j = i + 1; j < lines.length; j++) {
const dx = lines[i].x - lines[j].x;
const dy = lines[i].y - lines[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < maxDistance) {
const opacity = 1 - distance / maxDistance;
ctx.beginPath();
ctx.strokeStyle = `rgba(255, 255, 255, ${opacity * 0.2})`;
ctx.lineWidth = 0.5;
ctx.moveTo(lines[i].x, lines[i].y);
ctx.lineTo(lines[j].x, lines[j].y);
ctx.stroke();
}
}
// 绘制与鼠标的连接
if (mouse.x && mouse.y) {
const dx = lines[i].x - mouse.x;
const dy = lines[i].y - mouse.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < maxDistance * 1.5) {
const opacity = 1 - distance / (maxDistance * 1.5);
ctx.beginPath();
ctx.strokeStyle = `rgba(255, 255, 255, ${opacity * 0.3})`;
ctx.lineWidth = opacity * 2;
ctx.moveTo(lines[i].x, lines[i].y);
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
}
}
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
Canvas魔幻线条动画特效
于 2025-06-23 14:10:41 首次发布