生成代码雨
代码雨效果实现
我将创建一个类似《黑客帝国》风格的代码雨动画,使用HTML5 Canvas实现,并添加一些交互功能。
设计思路
- 使用Canvas绘制绿色字符雨效果
- 每个字符有不同的下落速度
- 添加鼠标交互效果
- 实现字符颜色渐变效果
- 添加开始/暂停和速度控制功能
下面是完整实现代码:
<!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>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #000;
font-family: 'Courier New', monospace;
color: #0f0;
overflow: hidden;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.container {
position: relative;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
canvas {
display: block;
background-color: #000;
}
.title {
position: absolute;
top: 20px;
left: 0;
width: 100%;
text-align: center;
z-index: 10;
text-shadow: 0 0 10px #0f0;
font-size: 2.8rem;
letter-spacing: 8px;
}
.subtitle {
position: absolute;
top: 80px;
left: 0;
width: 100%;
text-align: center;
z-index: 10;
font-size: 1.2rem;
opacity: 0.8;
letter-spacing: 3px;
}
.controls {
position: absolute;
bottom: 30px;
left: 0;
width: 100%;
display: flex;
justify-content: center;
gap: 20px;
z-index: 10;
flex-wrap: wrap;
padding: 0 20px;
}
.btn {
background-color: rgba(0, 30, 0, 0.7);
border: 1px solid #0f0;
color: #0f0;
padding: 10px 20px;
font-family: 'Courier New', monospace;
font-size: 1rem;
cursor: pointer;
border-radius: 4px;
transition: all 0.3s;
text-transform: uppercase;
letter-spacing: 2px;
}
.btn:hover {
background-color: rgba(0, 60, 0, 0.9);
box-shadow: 0 0 15px #0f0;
}
.btn.active {
background-color: rgba(0, 100, 0, 0.9);
box-shadow: 0 0 10px #0f0;
}
.speed-control {
display: flex;
align-items: center;
gap: 10px;
color: #0f0;
}
.speed-slider {
width: 150px;
height: 6px;
-webkit-appearance: none;
background-color: #003000;
border-radius: 3px;
outline: none;
}
.speed-slider::-webkit-slider-thumb {
-webkit-appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #0f0;
cursor: pointer;
border: 1px solid #0a0;
}
.footer {
position: absolute;
bottom: 10px;
left: 0;
width: 100%;
text-align: center;
font-size: 0.9rem;
opacity: 0.6;
letter-spacing: 1px;
}
@media (max-width: 768px) {
.title {
font-size: 1.8rem;
letter-spacing: 4px;
}
.subtitle {
font-size: 1rem;
top: 60px;
}
.controls {
gap: 10px;
}
.btn {
padding: 8px 15px;
font-size: 0.9rem;
}
}
</style>
</head>
<body>
<div class="container">
<h1 class="title">CODE RAIN</h1>
<p class="subtitle">黑客帝国风格代码雨模拟</p>
<canvas id="codeRain"></canvas>
<div class="controls">
<button id="startBtn" class="btn active">开始代码雨</button>
<button id="pauseBtn" class="btn">暂停动画</button>
<button id="resetBtn" class="btn">重置效果</button>
<div class="speed-control">
<span>速度:</span>
<input type="range" min="1" max="10" value="5" class="speed-slider" id="speedSlider">
<span id="speedValue">5</span>
</div>
<button id="colorBtn" class="btn">切换颜色</button>
</div>
<div class="footer">
移动鼠标与代码雨交互 | 点击按钮控制动画效果
</div>
</div>
<script>
// 获取Canvas元素和上下文
const canvas = document.getElementById('codeRain');
const ctx = canvas.getContext('2d');
// 设置Canvas尺寸为窗口尺寸
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 代码字符集 - 包括英文字母、数字和符号
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$+-*/=%\"'#&_(),.;:?!\\|{}<>[]^~";
const charArray = chars.split("");
// 代码雨列数组
let columns = [];
// 动画状态
let animationId = null;
let isRunning = true;
// 颜色模式 (0: 绿色, 1: 蓝色, 2: 紫色, 3: 红色)
let colorMode = 0;
// 颜色配置
const colorSchemes = [
{ main: "#0f0", light: "#9f9", dark: "#090" }, // 绿色
{ main: "#0af", light: "#7df", dark: "#08a" }, // 蓝色
{ main: "#f0f", light: "#f9f", dark: "#909" }, // 紫色
{ main: "#f00", light: "#f99", dark: "#900" } // 红色
];
// 速度控制 (1-10)
let speed = 5;
// 鼠标位置
let mouseX = -100;
let mouseY = -100;
// 鼠标影响半径
const mouseRadius = 100;
// 初始化代码列
function initColumns() {
// 根据Canvas宽度计算列数 (每列20像素宽)
const columnCount = Math.floor(canvas.width / 20);
columns = [];
for (let i = 0; i < columnCount; i++) {
columns[i] = {
x: i * 20,
y: Math.random() * -canvas.height, // 从画布上方随机位置开始
speed: 5 + Math.random() * 10, // 随机速度
chars: [],
charCount: Math.floor(10 + Math.random() * 30) // 每列字符数量
};
// 为每列生成字符
for (let j = 0; j < columns[i].charCount; j++) {
columns[i].chars.push({
char: charArray[Math.floor(Math.random() * charArray.length)],
color: 1.0所有内容均由人工智能模型生成,其生成内容的准确性和完整性无法保证,不代表我们的态度或观点。