Canvas万花筒动画特效

<!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: #000;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: Arial, sans-serif;
        }
        
        canvas {
            display: block;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1;
        }
        
        .controls {
            position: fixed;
            bottom: 80px;
            left: 0;
            width: 100%;
            text-align: center;
            z-index: 2;
        }
        
        button {
            padding: 8px 15px;
            background: rgba(255,255,255,0.1);
            color: white;
            border: 1px solid rgba(255,255,255,0.3);
            border-radius: 4px;
            cursor: pointer;
            margin: 0 5px;
            transition: all 0.3s ease;
        }
        
        button:hover {
            background: rgba(255,255,255,0.2);
        }
        
        /* 插入的链接样式 */
        .custom-link {
            position: fixed;
            bottom: 20px;
            right: 20px;
            color: rgba(255,255,255,0.7);
            text-decoration: none;
            font-size: 14px;
            background: rgba(0,0,0,0.3);
            padding: 8px 15px;
            border-radius: 20px;
            z-index: 10;
            transition: all 0.3s ease;
        }
        
        .custom-link:hover {
            color: white;
            background: rgba(0,0,0,0.5);
        }
    </style>
</head>
<body>
    <canvas id="kaleidoscopeCanvas"></canvas>
    
    <div class="controls">
        <button id="changePattern">变换图案</button>
        <button id="changeColor">变换颜色</button>
    </div>
    

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const canvas = document.getElementById('kaleidoscopeCanvas');
            const ctx = canvas.getContext('2d');
            const changePatternBtn = document.getElementById('changePattern');
            const changeColorBtn = document.getElementById('changeColor');
            
            // 设置canvas尺寸为窗口大小
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            
            // 万花筒参数
            let segments = 6;
            let colorScheme = 0;
            const colorSchemes = [
                ['#FF5252', '#FF4081', '#E040FB', '#7C4DFF', '#536DFE', '#448AFF'],
                ['#40C4FF', '#18FFFF', '#64FFDA', '#69F0AE', '#B2FF59', '#EEFF41'],
                ['#FFFF00', '#FFD740', '#FFAB40', '#FF6E40', '#FF3D00', '#DD2C00'],
                ['#F8BBD0', '#F48FB1', '#F06292', '#EC407A', '#E91E63', '#D81B60']
            ];
            
            // 当前图案参数
            let patternType = 0;
            let time = 0;
            
            // 动画循环
            function animate() {
                requestAnimationFrame(animate);
                
                // 清除画布
                ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                
                // 设置中心点
                const centerX = canvas.width / 2;
                const centerY = canvas.height / 2;
                const radius = Math.min(canvas.width, canvas.height) * 0.4;
                
                // 绘制万花筒
                ctx.save();
                ctx.translate(centerX, centerY);
                
                for (let i = 0; i < segments; i++) {
                    ctx.save();
                    ctx.rotate(i * Math.PI * 2 / segments);
                    
                    // 绘制镜像部分
                    drawPattern(radius);
                    
                    ctx.restore();
                }
                
                ctx.restore();
                
                time += 0.01;
            }
            
            // 绘制图案
            function drawPattern(radius) {
                const colors = colorSchemes[colorScheme];
                
                switch (patternType) {
                    case 0:
                        // 螺旋图案
                        for (let j = 0; j < 50; j++) {
                            const angle = time + j * 0.1;
                            const r = radius * (j / 50);
                            const x = Math.cos(angle) * r;
                            const y = Math.sin(angle) * r;
                            const size = 5 + Math.sin(time + j * 0.2) * 3;
                            
                            ctx.fillStyle = colors[j % colors.length];
                            ctx.beginPath();
                            ctx.arc(x, y, size, 0, Math.PI * 2);
                            ctx.fill();
                        }
                        break;
                        
                    case 1:
                        // 波纹图案
                        for (let j = 0; j < 30; j++) {
                            const r = radius * (j / 30);
                            const size = 10 + Math.sin(time * 2 + j * 0.5) * 8;
                            
                            ctx.strokeStyle = colors[j % colors.length];
                            ctx.lineWidth = 2;
                            ctx.beginPath();
                            ctx.arc(0, 0, r, 0, Math.PI * 2);
                            ctx.stroke();
                            
                            for (let k = 0; k < 6; k++) {
                                const angle = time + k * Math.PI / 3;
                                const x = Math.cos(angle) * r;
                                const y = Math.sin(angle) * r;
                                
                                ctx.fillStyle = colors[(j + k) % colors.length];
                                ctx.beginPath();
                                ctx.arc(x, y, size * 0.5, 0, Math.PI * 2);
                                ctx.fill();
                            }
                        }
                        break;
                        
                    case 2:
                        // 几何图案
                        for (let j = 1; j <= 10; j++) {
                            const points = [];
                            const sides = 3 + j % 5;
                            const r = radius * (j / 10);
                            
                            for (let k = 0; k < sides; k++) {
                                const angle = time + k * Math.PI * 2 / sides;
                                points.push({
                                    x: Math.cos(angle) * r,
                                    y: Math.sin(angle) * r
                                });
                            }
                            
                            ctx.fillStyle = colors[j % colors.length];
                            ctx.globalAlpha = 0.6;
                            ctx.beginPath();
                            ctx.moveTo(points[0].x, points[0].y);
                            
                            for (let k = 1; k < points.length; k++) {
                                ctx.lineTo(points[k].x, points[k].y);
                            }
                            
                            ctx.closePath();
                            ctx.fill();
                        }
                        break;
                }
            }
            
            // 变换图案
            changePatternBtn.addEventListener('click', function() {
                patternType = (patternType + 1) % 3;
            });
            
            // 变换颜色
            changeColorBtn.addEventListener('click', function() {
                colorScheme = (colorScheme + 1) % colorSchemes.length;
            });
            
            // 窗口大小改变时调整canvas尺寸
            window.addEventListener('resize', function() {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
            });
            
            // 初始化动画
            animate();
        });
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值