<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>几何图形发光变换特效</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background: #000;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: 'Arial', sans-serif;
}
canvas {
position: fixed;
top: 0;
left: 0;
z-index: 1;
}
.content {
position: relative;
z-index: 2;
text-align: center;
color: white;
padding: 20px;
max-width: 800px;
}
h1 {
font-size: 3em;
margin-bottom: 20px;
text-shadow: 0 0 10px rgba(255, 255, 255, 0.7);
}
p {
font-size: 1.2em;
margin-bottom: 30px;
line-height: 1.6;
}
.link {
margin-top: 40px;
}
a {
color: #fff;
text-decoration: none;
font-size: 1.2em;
padding: 12px 25px;
border: 2px solid rgba(255, 255, 255, 0.5);
border-radius: 30px;
background: rgba(255, 255, 255, 0.1);
transition: all 0.3s ease;
display: inline-block;
}
a:hover {
background: rgba(255, 255, 255, 0.3);
box-shadow: 0 0 15px rgba(255, 255, 255, 0.5);
transform: translateY(-3px);
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="content">
<h1>几何图形发光变换</h1>
<p>体验动态几何图形的奇妙变换,感受光与形的完美结合</p>
</div>
<script>
// 初始化画布
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// 设置画布大小为窗口大小
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// 几何图形参数
const shapes = [];
const shapeCount = 15;
const colors = [
'rgba(255, 100, 100, 0.7)',
'rgba(100, 255, 100, 0.7)',
'rgba(100, 100, 255, 0.7)',
'rgba(255, 255, 100, 0.7)',
'rgba(100, 255, 255, 0.7)',
'rgba(255, 100, 255, 0.7)'
];
// 初始化图形
function initShapes() {
for (let i = 0; i < shapeCount; i++) {
shapes.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: 30 + Math.random() * 70,
rotation: Math.random() * Math.PI * 2,
rotationSpeed: (Math.random() - 0.5) * 0.02,
color: colors[Math.floor(Math.random() * colors.length)],
sides: 3 + Math.floor(Math.random() * 5), // 3-7边形
targetSides: 3 + Math.floor(Math.random() * 5),
sideChangeSpeed: 0.005 + Math.random() * 0.005,
glow: 0,
pulseSpeed: 0.01 + Math.random() * 0.02,
moveX: (Math.random() - 0.5) * 2,
moveY: (Math.random() - 0.5) * 2
});
}
}
// 绘制多边形
function drawPolygon(x, y, radius, sides, rotation) {
ctx.beginPath();
for (let i = 0; i < sides; i++) {
const angle = rotation + (i * 2 * Math.PI / sides);
const px = x + Math.cos(angle) * radius;
const py = y + Math.sin(angle) * radius;
if (i === 0) {
ctx.moveTo(px, py);
} else {
ctx.lineTo(px, py);
}
}
ctx.closePath();
}
// 动画循环
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制所有图形
shapes.forEach(shape => {
// 更新位置
shape.x += shape.moveX;
shape.y += shape.moveY;
// 边界检查
if (shape.x < 0 || shape.x > canvas.width) shape.moveX *= -1;
if (shape.y < 0 || shape.y > canvas.height) shape.moveY *= -1;
// 更新旋转
shape.rotation += shape.rotationSpeed;
// 更新边数变换
if (Math.abs(shape.sides - shape.targetSides) < 0.1) {
shape.targetSides = 3 + Math.floor(Math.random() * 5);
}
if (shape.sides < shape.targetSides) {
shape.sides += shape.sideChangeSpeed;
} else if (shape.sides > shape.targetSides) {
shape.sides -= shape.sideChangeSpeed;
}
// 更新发光效果
shape.glow += shape.pulseSpeed;
if (shape.glow > 1 || shape.glow < 0) {
shape.pulseSpeed *= -1;
}
// 绘制图形
ctx.save();
// 绘制发光效果
const glowSize = shape.size * (1 + shape.glow * 0.3);
const gradient = ctx.createRadialGradient(
shape.x, shape.y, 0,
shape.x, shape.y, glowSize
);
gradient.addColorStop(0, shape.color);
gradient.addColorStop(1, 'rgba(0,0,0,0)');
ctx.fillStyle = gradient;
drawPolygon(shape.x, shape.y, glowSize, shape.sides, shape.rotation);
ctx.fill();
// 绘制图形主体
ctx.fillStyle = shape.color;
drawPolygon(shape.x, shape.y, shape.size, shape.sides, shape.rotation);
ctx.fill();
ctx.restore();
});
requestAnimationFrame(animate);
}
// 启动动画
initShapes();
animate();
</script>
</body>
</html>
7982

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



