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

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



