前端瀑布流效果

先看效果

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>
相关推荐
银安36 分钟前
CSS排版布局篇(8):Grid 二维布局
前端·css
华仔啊2 小时前
Vue3 登录页还能这么丝滑?这个 hover 效果太惊艳了
前端·css·vue.js
牧杉-惊蛰11 小时前
disable-devtool 网络安全 禁止打开控制台
前端·css·vue.js
CoderYanger11 小时前
前端基础-HTML入门保姆级课堂笔记
前端·javascript·css·html
蓝胖子的多啦A梦13 小时前
低版本Chrome导致弹框无法滚动的解决方案
前端·css·html·chrome浏览器·版本不同造成问题·弹框页面无法滚动
tryCbest14 小时前
Html5实现弹出表单
html5
ะัี潪ิื15 小时前
精灵图(雪碧图)的生成和使用
java·css
iuuia16 小时前
02--CSS基础
前端·css
苦夏木禾16 小时前
css实现表格中最后一列固定
前端·javascript·css
Dcc21 小时前
纯 css 实现前端主题切换+自定义方案
前端·css