HTML5可以控制漩涡速度的特效

<!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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值