代码 黑客帝国代码雨效果

2024-11-19 01:49:31 +0800 CST views 563

#黑客帝国代码雨效果

该文本展示了一个实现代码雨效果的HTML页面。使用JavaScript在全屏画布上绘制随机字符,字符从上到下不断下落,形成动态效果。代码中包含了画布的初始化、字符集的定义、绘制函数以及动画的设置,适合用于学习Canvas和动画效果的实现。

、<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>代码雨效果</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background-color: black;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="matrixCanvas"></canvas>
    <script>
// 获取画布和绘图上下文
const canvas = document.getElementById('matrixCanvas');
const ctx = canvas.getContext('2d');

// 设置画布全屏
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// 当窗口大小改变时,调整画布尺寸
window.addEventListener('resize', () => {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
});

// 定义字符集,可以根据需要添加更多字符
const katakana = 'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズヅブプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン';
const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const nums = '0123456789';

const alphabet = katakana + latin + nums;

// 每个字符的字体大小
const fontSize = 16;
// 计算列数 = 画布宽度 / 字体大小
const columns = Math.floor(canvas.width / fontSize);

// 用于存储每个列中字符的下落位置
const drops = [];
// 初始化每个列的起始位置
for (let x = 0; x < columns; x++) {
    drops[x] = canvas.height / fontSize;
}

// 绘制函数
function draw() {
    // 让画布稍微变暗,以达到渐隐效果
    ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // 设置文本颜色为绿色和字体样式
    ctx.fillStyle = '#0F0';
    ctx.font = fontSize + 'px monospace';

    // 遍历所有的列
    for (let i = 0; i < drops.length; i++) {
        // 随机选择一个字符
        const text = alphabet.charAt(Math.floor(Math.random() * alphabet.length));

        // 绘制字符,位置为 x = i * 字体大小, y = drops[i] * 字体大小
        ctx.fillText(text, i * fontSize, drops[i] * fontSize);

        // 让字符往下移动一格
        drops[i]++;

        // 重新随机重置下落位置,以制造持续下落的效果
        if (drops[i] * fontSize > canvas.height && Math.random() > 0.975) {
            drops[i] = 0;
        }
    }
}

// 设置动画间隔,调用绘制函数
setInterval(draw, 33); // 大约每秒30帧

    </script>
</body>
</html>

images

复制全文 生成海报 前端开发 动画效果 Canvas

推荐文章

thinkphp swoole websocket 结合的demo
2024-11-18 10:18:17 +0800 CST
Redis和Memcached有什么区别?
2024-11-18 17:57:13 +0800 CST
网络数据抓取神器 Pipet
2024-11-19 05:43:20 +0800 CST
Go语言中实现RSA加密与解密
2024-11-18 01:49:30 +0800 CST
Go 协程上下文切换的代价
2024-11-19 09:32:28 +0800 CST
CSS 中的 `scrollbar-width` 属性
2024-11-19 01:32:55 +0800 CST
Vue中的表单处理有哪几种方式?
2024-11-18 01:32:42 +0800 CST
Rust async/await 异步运行时
2024-11-18 19:04:17 +0800 CST
Go语言中的mysql数据库操作指南
2024-11-19 03:00:22 +0800 CST
CSS 特效与资源推荐
2024-11-19 00:43:31 +0800 CST
FcDesigner:低代码表单设计平台
2024-11-19 03:50:18 +0800 CST
thinkphp分页扩展
2024-11-18 10:18:09 +0800 CST
基于Flask实现后台权限管理系统
2024-11-19 09:53:09 +0800 CST
nginx反向代理
2024-11-18 20:44:14 +0800 CST
php客服服务管理系统
2024-11-19 06:48:35 +0800 CST
一文详解回调地狱
2024-11-19 05:05:31 +0800 CST
一键压缩图片代码
2024-11-19 00:41:25 +0800 CST
程序员茄子在线接单