前端瀑布流效果

先看效果

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>
相关推荐
金八梭13 小时前
格斗对决v4.5
前端·css·自动化·html·css3
CappuccinoRose2 天前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互
zhanlang02174 天前
HTML 零基础入门到企业实战【第八章 8.6】HTML5 本地存储、拖拽 API、视频音频新特性
html·音视频·html5
inksci4 天前
用飞帆做了个小网页
前端·javascript·css·html
星云_byto5 天前
CSS面试题
前端·css
zhanlang02175 天前
HTML 零基础入门到企业实战全套教程【连载 8.4】HTML5 新增表单属性与表单控件
前端·html·html5
科研邪修法5 天前
PDF新增页面文字与文本批注怎么区分?以双版本培训讲义为例
前端·css·pdf
狮子座的男孩5 天前
html+css基础:31、响应式布局(媒体类型,媒体特性,运算符,常用阈值)
前端·css·经验分享·html·响应式布局·媒体类型/特性·常用阈值
狮子座的男孩5 天前
html+css基础:32、BFC(父子元素防止 margin 塌陷,父子元素清除浮动,兄弟元素两栏布局)
前端·css·经验分享·html·bfc·margin塌陷·清除浮动
zhanlang02175 天前
HTML 零基础入门到企业实战全套教程【第八章 8.3】HTML5 语义化标签|SEO 优化、页面布局实战应用
前端·html·html5