前端瀑布流效果

先看效果

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>
相关推荐
AAA阿giao15 小时前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
掘金安东尼1 天前
用 CSS 打造完美的饼图
前端·css
掘金安东尼1 天前
纯 CSS 实现弹性文字效果
前端·css
前端Hardy2 天前
HTML&CSS&JS:打造丝滑的3D彩纸飘落特效
前端·javascript·css
前端Hardy2 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
parade岁月3 天前
Tailwind CSS v4 — 当框架猜不透你的心思
前端·css
前端Hardy3 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html
程序员阿耶3 天前
5 个让 CSS 起飞的新特性,设计师看了直呼内行
css
前端Hardy3 天前
HTML&CSS:纯CSS实现随机转盘抽奖机——无JS,全靠现代CSS黑科技!
css·html
漂流瓶jz3 天前
BEM、OOCSS、SMACSS、ITCSS、AMCSS、SUITCSS:CSS命名规范简介
前端·css·代码规范