<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>漩涡速度控制特效</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
font-family: Arial, sans-serif;
color: white;
}
canvas {
display: block;
}
.controls {
position: absolute;
bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}
.credit {
position: absolute;
top: 10px;
left: 10px;
font-size: 12px;
color: rgba(255, 255, 255, 0.5);
}
</style>
</head>
<body>
<canvas id="swirlCanvas"></canvas>
<div class="controls">
<label for="speedControl">漩涡速度:</label>
<input type="range" id="speedControl" min="0" max="10" value="3" step="0.1">
<span id="speedValue">3.0</span>
</div>
<script>
const canvas = document.getElementById('swirlCanvas');
const ctx = canvas.getContext('2d');
const speedControl = document.getElementById('speedControl');
const speedValue = document.getElementById('speedValue');
// 设置画布大小为窗口大小
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// 漩涡参数
let time = 0;
let speed = 3;
// 更新速度显示
speedControl.addEventListener('input', function() {
speed = parseFloat(this.value);
speedValue.textContent = speed.toFixed(1);
});
// 颜色生成函数
function getColor(angle, distance) {
const r = Math.floor(Math.sin(angle * 0.1 + time * 0.5) * 127 + 128);
const g = Math.floor(Math.sin(angle * 0.2 + time * 0.7) * 127 + 128);
const b = Math.floor(Math.sin(angle * 0.3 + time * 0.9) * 127 + 128);
return `rgb(${r}, ${g}, ${b})`;
}
// 动画循环
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const maxRadius = Math.sqrt(centerX * centerX + centerY * centerY);
// 绘制漩涡
for (let i = 0; i < 1000; i++) {
const angle = time + i * 0.1;
const distance = (i / 1000) * maxRadius;
const x = centerX + Math.cos(angle) * distance;
const y = centerY + Math.sin(angle) * distance;
const size = (1 - distance / maxRadius) * 10 + 2;
ctx.fillStyle = getColor(angle, distance);
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fill();
}
time += speed * 0.01;
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>