<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5 Canvas彩虹圈动画特效</title>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: #121212;
overflow: hidden;
font-family: Arial, sans-serif;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
.title {
position: relative;
color: white;
font-size: 36px;
text-align: center;
z-index: 2;
text-shadow: 0 0 10px rgba(255,255,255,0.5);
}
.link {
position: absolute;
bottom: 20px;
right: 20px;
color: white;
font-size: 14px;
text-decoration: none;
font-weight: bold;
z-index: 3;
}
</style>
</head>
<body>
<h1 class="title">彩虹圈动画特效</h1>
<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 colors = [
'#FF0000', '#FF7F00', '#FFFF00',
'#00FF00', '#0000FF', '#4B0082',
'#9400D3'
];
// 圆环数组
let rings = [];
// 圆环类
class Ring {
constructor(x, y) {
this.x = x;
this.y = y;
this.radius = 5;
this.maxRadius = Math.random() * 100 + 50;
this.lineWidth = Math.random() * 10 + 5;
this.color = colors[Math.floor(Math.random() * colors.length)];
this.velocity = Math.random() * 2 + 1;
this.opacity = 1;
this.life = 100;
}
update() {
this.radius += this.velocity;
this.life--;
this.opacity = this.life / 100;
if (this.radius > this.maxRadius) {
this.velocity = -this.velocity * 0.5;
}
if (this.life <= 0) {
this.reset();
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.strokeStyle = this.color;
ctx.lineWidth = this.lineWidth;
ctx.globalAlpha = this.opacity;
ctx.stroke();
ctx.globalAlpha = 1;
}
reset() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.radius = 5;
this.maxRadius = Math.random() * 100 + 50;
this.lineWidth = Math.random() * 10 + 5;
this.color = colors[Math.floor(Math.random() * colors.length)];
this.velocity = Math.random() * 2 + 1;
this.opacity = 1;
this.life = 100;
}
}
// 初始化圆环
function initRings() {
for (let i = 0; i < 20; i++) {
setTimeout(() => {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
rings.push(new Ring(x, y));
}, i * 300);
}
}
// 动画循环
function animate() {
requestAnimationFrame(animate);
ctx.fillStyle = 'rgba(18, 18, 18, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
rings.forEach(ring => {
ring.update();
ring.draw();
});
}
// 鼠标移动事件
canvas.addEventListener('mousemove', (e) => {
rings.push(new Ring(e.clientX, e.clientY));
// 限制圆环数量
if (rings.length > 50) {
rings = rings.slice(rings.length - 50);
}
});
// 窗口大小调整
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// 点击事件
canvas.addEventListener('click', (e) => {
for (let i = 0; i < 5; i++) {
setTimeout(() => {
rings.push(new Ring(e.clientX, e.clientY));
}, i * 100);
}
});
// 启动动画
initRings();
animate();
</script>
</body>
</html>
HTML5 Canvas彩虹圈动画特效
于 2025-06-13 13:52:43 首次发布
1240

被折叠的 条评论
为什么被折叠?



