<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5绘制彩色圆圈动画特效</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
display: block;
}
.controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
}
button {
padding: 8px 16px;
background: rgba(255, 255, 255, 0.1);
color: white;
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 4px;
cursor: pointer;
transition: all 0.3s ease;
}
button:hover {
background: rgba(255, 255, 255, 0.2);
}
.hidden-link {
position: absolute;
bottom: 60px;
left: 50%;
transform: translateX(-50%);
color: rgba(255, 255, 255, 0.3);
font-size: 12px;
text-decoration: none;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<canvas id="circleCanvas"></canvas>
<div class="controls">
<button id="addCircle">添加圆圈</button>
<button id="clearCircles">清除所有</button>
</div>
<script>
const canvas = document.getElementById('circleCanvas');
const ctx = canvas.getContext('2d');
const addBtn = document.getElementById('addCircle');
const clearBtn = document.getElementById('clearCircles');
// 设置画布大小为窗口大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 圆圈数组
const circles = [];
const colors = [
'#FF5F5F', '#FF9F5F', '#FFDF5F',
'#5FFF5F', '#5FDFFF', '#5F5FFF',
'#DF5FFF', '#FF5FDF'
];
// 圆圈类
class Circle {
constructor(x, y) {
this.x = x || Math.random() * canvas.width;
this.y = y || Math.random() * canvas.height;
this.radius = 5;
this.maxRadius = 50 + Math.random() * 100;
this.color = colors[Math.floor(Math.random() * colors.length)];
this.growthRate = 0.5 + Math.random() * 2;
this.opacity = 0;
this.targetOpacity = 0.7 + Math.random() * 0.3;
this.lifePhase = 0; // 0: 出现, 1: 稳定, 2: 消失
this.speedX = (Math.random() - 0.5) * 2;
this.speedY = (Math.random() - 0.5) * 2;
this.rotation = Math.random() * Math.PI * 2;
this.rotationSpeed = (Math.random() - 0.5) * 0.02;
}
update() {
// 移动
this.x += this.speedX;
this.y += this.speedY;
// 边界检查
if (this.x < 0 || this.x > canvas.width) this.speedX *= -1;
if (this.y < 0 || this.y > canvas.height) this.speedY *= -1;
// 旋转
this.rotation += this.rotationSpeed;
// 生命周期处理
if (this.lifePhase === 0) {
// 出现阶段
this.opacity += 0.02;
if (this.opacity >= this.targetOpacity) {
this.lifePhase = 1;
}
} else if (this.lifePhase === 2) {
// 消失阶段
this.opacity -= 0.02;
if (this.opacity <= 0) {
const index = circles.indexOf(this);
if (index > -1) {
circles.splice(index, 1);
}
return false;
}
}
// 生长
if (this.radius < this.maxRadius && this.lifePhase === 0) {
this.radius += this.growthRate;
}
return true;
}
draw() {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
// 绘制圆圈
ctx.beginPath();
ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.globalAlpha = this.opacity;
ctx.fill();
// 绘制内部装饰
ctx.beginPath();
ctx.arc(0, 0, this.radius * 0.7, 0, Math.PI * 2);
ctx.strokeStyle = 'rgba(255, 255, 255, 0.5)';
ctx.lineWidth = 2;
ctx.globalAlpha = this.opacity * 0.8;
ctx.stroke();
// 绘制中心点
ctx.beginPath();
ctx.arc(0, 0, this.radius * 0.2, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.globalAlpha = this.opacity * 0.6;
ctx.fill();
ctx.restore();
}
}
// 动画循环
function animate() {
// 半透明背景创造拖尾效果
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 更新和绘制所有圆圈
circles.forEach(circle => {
circle.update();
circle.draw();
});
requestAnimationFrame(animate);
}
// 添加新圆圈
function addCircle(x, y) {
circles.push(new Circle(x, y));
// 限制最大圆圈数量
if (circles.length > 50) {
// 找到最早添加的圆圈并标记为消失
for (let i = 0; i < circles.length; i++) {
if (circles[i].lifePhase === 1) {
circles[i].lifePhase = 2;
break;
}
}
}
}
// 清除所有圆圈
function clearAllCircles() {
circles.forEach(circle => {
circle.lifePhase = 2;
});
}
// 处理窗口大小变化
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// 点击添加圆圈
canvas.addEventListener('click', (e) => {
addCircle(e.clientX, e.clientY);
});
// 按钮事件
addBtn.addEventListener('click', () => {
addCircle();
});
clearBtn.addEventListener('click', clearAllCircles);
// 初始添加一些圆圈
for (let i = 0; i < 10; i++) {
addCircle();
}
// 开始动画
animate();
</script>
</body>
</html>
5475

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



