当前位置: 首页 > news >正文

canvas实现代码雨

 学习抖音: @渡一前端必修课

 效果图:

 全部代码:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Title</title><style>* {padding: 0;margin: 0;}#view {max-width: 100vw;max-height: 100vh;overflow: hidden;display: block;}</style>
</head><body><canvas id="view"></canvas>
</body>
<script>// 获取canvas元素const cvs = document.getElementById("view");// 获取窗口大小  window.devicePixelRatio// window.devicePixelRatio为一个双精度浮点值,指示显示器的物理像素分辨率与CSS像素分辨率之比。简单来说,它告诉浏览器应使用多少屏幕物理像素来绘制单个CSS像素。const width = window.innerWidth * window.devicePixelRatio,height = window.innerHeight * window.devicePixelRatioconsole.log(width)console.log(height)// 设置 canvas尺寸cvs.width = widthcvs.height = height// 获取绘制上下文const ctx = cvs.getContext("2d")// 字体大小const fontSize = 20 * window.devicePixelRatio;// 定义列宽和多少列const columnWidth = 20;const columnCount = Math.floor(width / 20);// 没一列下一个文字是第几个const nextChar = new Array(columnCount).fill(0)// 绘制function draw() {ctx.fillStyle = "rgba(255,255,255,0.2)";ctx.fillRect(0, 0, width, height)for (let i = 0; i < columnCount; i++) {ctx.fillStyle = getRandomColor();let char = getRandomChar();ctx.font = `${fontSize}px "Roboto Mono"`let x = i * columnWidth; // x 轴的位置const s = nextChar[i]let y = (s + 1) * fontSize; //y 轴的位置ctx.fillText(char, x, y)if (y > height && Math.random() > 0.95) {nextChar[i] = 0} else {nextChar[i]++}}}setInterval(draw, 30)// 获取随机颜色function getRandomColor() {let color = ["#4150d8","#28bf7e","#ed7c2f","#ff0000","#f9cf36","#4a5bdc","#7b04f4","#ee04f4","#04a0f4","#1af404","#d4f404","#f404f1",];return color[Math.floor(Math.random() * color.length)]}// 获取随机文字function getRandomChar() {const str = "qwertyuioplkjhgfdabzxcmv"return str[Math.floor(Math.random() * str.length)]}
</script></html>

http://www.lryc.cn/news/115966.html

相关文章:

  • 基于MFCC特征提取和HMM模型的语音合成算法matlab仿真
  • 多重网格算法的cuda编程
  • DP(状态机模型)
  • 按照指定的文件顺序进行scp传输
  • 小红书数据分析丨现实版模拟人生,这届网友热衷于“云开店”?
  • 休闲卤味强势崛起:卤味零食成为新一代热门美食
  • 自除数-C语言
  • -bash: ./startup.sh: Permission denied解决
  • Java课题笔记~ AOP 概述
  • 真我V3 5G(RMX2200 RMX2201)解锁刷机全过程
  • springCache-缓存
  • 【solon生态】- solon.cloud.micrometer插件使用指南及micrometer详解
  • 【Spring Boot】Thymeleaf模板引擎 — Thymeleaf的高级用法
  • 用html+javascript打造公文一键排版系统13:增加半角字符和全角字符的相互转换功能
  • 元宇宙3D数字虚拟客服打造年轻化、数字化营销新品牌
  • micromamba快速安装(windows版本)
  • HTML <source> 标签
  • 香港第一金:加息预期仍令贵金属承压,黄金仍需关注破位情况
  • C语言学习笔记 vscode使用外部console-11
  • 96 | Python 小项目—— 学生成绩管理系统
  • 【uniapp使用web-view点击返回报错后返回不了】
  • Map Reduce教程_编程入门自学教程_菜鸟教程-免费教程分享
  • 吉利科技携手企企通,打造集团化数智供应链系统
  • 2023河南萌新联赛第(四)场:河南大学 F - 小富的idea
  • 总结线程池
  • 基础的 lftp 使用方法
  • python之prettytable库的使用
  • google PGS 下一代id
  • 【elasticsearch】关于elasticsearch的max_result_window限制问题的解决方式思考
  • 音频光耦合器