前端瀑布流效果

先看效果

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body style="background-color: #000;padding: 0; margin: 0;">
    <canvas id="canvas"></canvas>
</body>
    <script>
        let cvs = document.querySelector('canvas');
        let ctx = cvs.getContext('2d');
        function init() {
            cvs.width = window.innerWidth * devicePixelRatio;
            cvs.height = window.innerHeight * devicePixelRatio;
            console.log(cvs.width);
            console.log(cvs.height);
        }
        init()
        
        let fontsize = 20 * devicePixelRatio;
        ctx.fillStyle="#000"
        let columCount = Math.floor(cvs.width/fontsize);//获取列
        let charIndex = new Array(columCount).fill(0);

        function draw() {
            ctx.fillStyle='rgba(0,0,0,0.1)'
            ctx.fillRect(0,0,cvs.width,cvs.height)
            ctx.fillStyle='#6be445'
            ctx.textBaseline='top'
            for (let i = 0; i < columCount; i++) {
                let x = i *fontsize;
                let y = charIndex[i] *fontsize;
                ctx.fillText(getRandomText(),x,y)
                if(y>cvs.height && Math.random()>0.99){
                    charIndex[i] = 0;
                }else{
                    charIndex[i]++
                }
            }
        }
        draw()
        function getRandomText() {
            let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            return str[Math.floor(Math.random()*str.length)]
        }
        
        setInterval(draw, 50);
    </script>
</html>
相关推荐
用户059540174463 小时前
asyncio 踩坑实录:这个问题坑了我3小时,差点让线上服务崩掉
前端·css
凯瑟琳.奥古斯特4 小时前
Bootstrap快速上手指南
开发语言·前端·css·bootstrap·html
ZC跨境爬虫5 小时前
跟着 MDN 学 HTML day_2:(表单分组与高级输入控件实战)
前端·javascript·css·ui·html
FlyWIHTSKY7 小时前
Vue 3 + 原生 CSS Float
前端·css·vue.js
ZC跨境爬虫7 小时前
跟着 MDN 学 HTML day_1:(全套原生Input+表单结构拆解)
前端·css·ui·html
ZC跨境爬虫8 小时前
Apple官网复刻第二阶段day_6:(统一页脚模块封装+CSS公共复用体系落地)
前端·css·ui·重构·html
琹箐8 小时前
今天吃什么干什么随机生成
javascript·css·css3
yqcoder8 小时前
CSS 布局双雄:浮动 (Float) vs 绝对定位 (Absolute) 深度解析
前端·css
ZC跨境爬虫9 小时前
跟着 MDN 学 HTML day_3:(表单CSS美化实战与盒子模型三大核心属性详解)
前端·javascript·css·html
玩嵌入式的菜鸡18 小时前
网页访问单片机设备---基于mqtt
前端·javascript·css