代码雨(h5+css+js实现)小特效案例

 以上为效果图

代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>代码雨</title>
        <style type="text/css">
            * {
                /* 将边框线和padding当作内容使用 */
                box-sizing: border-box;
            }

            body {
                background: #000000;
                margin: 0;
                padding: 0;
                /* 隐藏滚动条 */
                overflow: hidden;
                text-shadow: 0 0 80px;
            }
        </style>

    </head>
    <body>
        <!-- 画布 -->
        <canvas id="canvas"></canvas>

        <script type="text/javascript">
            var canvar = document.getElementById('canvas');
            var ctx = canvas.getContext('2d');
            canvas.height = window.innerHeight;
            canvas.width = window.innerWidth;
            // 代码雨的内容
            var texts = '+-'.split('');
            var fontSize = 14;
            var colums = canvas.width / fontSize;
            // 定义一个数组
            var drops = [];
            for (var x = 0; x < colums; x++) {
                drops[x] = 1;
            }

            function draw() {
                // 设置画布背景
                ctx.fillStyle = 'rgba(0,0,0,0.05)';
                // 在画布中设置一个矩形
                ctx.fillRect(0, 0, canvas.width, canvar.height);
                // 设置文字颜色
                ctx.fillStyle = '#0f0';
                // 设置文字大小和样式
                ctx.font = fontSize + 'px arial';
                // 逐行输出文字
                for (var i = 0; i < drops.length; i++) {
                    var text = texts[Math.floor(Math.random() * texts.length)]
                    ctx.fillText(text, fontSize * i, drops[i] * fontSize);
                    if (drops[i] * fontSize > canvas.height || Math.random() > 0.95) {
                        drops[i] = 0;
                    }
                    drops[i]++;
                }
            }
            setInterval(draw, 33);
        </script>
    </body>
</html>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值