惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
W
WeLiveSecurity
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
I
InfoQ
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
Hacker News - Newest:
Hacker News - Newest: "LLM"
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Help Net Security
Help Net Security
博客园_首页
博客园 - 叶小钗
O
OpenAI News
PCI Perspectives
PCI Perspectives
月光博客
月光博客
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
GbyAI
GbyAI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Last Watchdog
The Last Watchdog
C
CXSECURITY Database RSS Feed - CXSecurity.com
有赞技术团队
有赞技术团队
D
Darknet – Hacking Tools, Hacker News & Cyber Security
腾讯CDC
Hacker News: Ask HN
Hacker News: Ask HN
I
Intezer
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
量子位
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
N
News and Events Feed by Topic
P
Proofpoint News Feed
Scott Helme
Scott Helme
D
Docker
Know Your Adversary
Know Your Adversary
Recent Commits to openclaw:main
Recent Commits to openclaw:main
TaoSecurity Blog
TaoSecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tor Project blog

Coding Your Life

lib库开发的一款PDF处理小工具 | Coding Your Life mirror-cli | Coding Your Life 最长递增子序列算法 | Coding Your Life webpack5 模块联邦技术 | Coding Your Life webpack基础配置详解 | Coding Your Life 使用MessageChannel模拟React优先级执行队列 | Coding Your Life vue3 和 react 虚拟dom | Coding Your Life ES6中Reflect对象与Proxy结合实现代理和响应式编程 | Coding Your Life 前端技术分享MediaRecord实现运动相机 | Coding Your Life react基础概念 | Coding Your Life 微信小程序使用canvas创建像素头像 | Coding Your Life 前端基础概念 | Coding Your Life 中高级前端须注意的40条移动端H5坑位指南 | Coding Your Life native-code-push 热更新配置 | Coding Your Life Git 常用命令速查表 | Coding Your Life push-server 热更新常用命令速查表 | Coding Your Life 高效的js片段 | Coding Your Life
canvas实现代码雨效果 | Coding Your Life
Sir_Liu · 2025-05-25 · via Coding Your Life

前言

canvas绘图是一种比较常用的前端技术,适用场景包括:画板、游戏、动画、图表、视频等。下面我们使用该技术完成一个代码雨效果。

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const canvas = document.getElementById('canvas');
const animationInterval: number | null = null;


function rainCode(rainCodeStr="TSDJXHS1010",fillStyle='#22C55E') {
const width = window.innerWidth;
const height = window.innerHeight;
rainCodeStr = rainCodeStr?.toUpperCase() ||'TSDJXHS1010' ;
const rainCodeArr = rainCodeStr.split('');
canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
let positionY = Array.from({ length: width / 15 }, () => 0);
const colors = ['#rgb(234, 5, 250)', 'rgb(106, 6, 194)', 'rgb(19, 6, 194)', 'rgb(6, 194, 62)', 'rgb(168, 218, 19)'];
animationInterval = setInterval(() => {

ctx!.fillStyle = 'rgba(0,0,0,0.05)';
ctx!.fillRect(0, 0, width, height);
ctx!.fillStyle = fillStyle ||'#0f0'

ctx!.font = '15px Arial';
for (let i = 0; i < positionY.length; i++) {
ctx!.fillText(rainCodeArr[Math.floor(Math.random() * rainCodeArr.length)], i * 15, positionY[i]);
positionY[i] = positionY[i] > height || positionY[i] > Math.random() * 10000 ? 0 : positionY[i] + 15;
}
}, 50);

document.body.appendChild(canvas);
}


document.addEventListener('DOMContentLoaded', () => {
document.body.style.margin = "0px";
document.body.style.padding = "0px";
rainCode();
});

document.addEventListener("beforeunload", () => {
animationInterval && clearInterval(animationInterval);
canvas && canvas.remove();
});

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Coding Your Life