Canvas实现黑客帝国字符雨

 利用Canvas的fillText(),隔一定时间在画布上作画

<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            overflow: hidden
        }
    </style>
</head>

<body>
    <canvas id='canvas' style='background:#111'></canvas>
    <script>
        canvas = document.getElementById('canvas')
        context = canvas.getContext('2d')

        //定义画布大小,字体,颜色
        var W = window.innerWidth
        var H = window.innerHeight
        canvas.height = H
        canvas.width = W
        var fontSize = 16
        var colunms = Math.floor(W / fontSize)
        var drops = [] //drops中是每列字母y坐标的倍数
        for (let i = 0; i < colunms; i++) {
            drops.push(1000)
        }
        var str = 'qwertyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM0123456789'

        function draw() {
            //每次draw(),设置透明度可以显示上一次draw()的图像,形成残影
            context.fillStyle = 'rgba(0, 0, 0, 0.05)'
            context.fillRect(0, 0, W, H)
            context.font = '1000 ' + fontSize + 'px Consolas'
            context.fillStyle = '#00cc33'
            context.textAlign = 'center'
            for (let i = 0; i < colunms; i++) {
                let index = Math.floor(Math.random() * str.length)
                let x = i * fontSize
                let y = drops[i] * fontSize
                //在x,y坐标中随机画str中的一个字符
                context.fillText(str[index], x, y)
                //y大于画布高度后,设置随机数,规定是否从顶点重新开始画
                if (y >= H && Math.random() > 0.99) {
                    drops[i] = 0
                }
                //每次draw()后,倍数加一,下一个字符画在它的下方
                drops[i]++
            }
        }

        window.onload = function () {
            setInterval(draw, 50)
        }
    </script>
</body>

还可以设置字体的随机颜色,使动画更绚丽

添加randColor函数,并更改draw

function draw() {
            context.fillStyle = 'rgba(0, 0, 0, 0.05)'
            context.fillRect(0, 0, W, H)
            context.font = '1000 ' + fontSize + 'px Consolas'
            context.textAlign = 'center'
            for (let i = 0; i < colunms; i++) {
                context.fillStyle = randColor()
                let index = Math.floor(Math.random() * str.length)
                let x = i * fontSize
                let y = drops[i] * fontSize
                context.fillText(str[index], x, y)
                if (y >= H && Math.random() > 0.99) {
                    drops[i] = 0
                }
                drops[i]++
            }
        }

        function randColor() {
            let r = Math.floor(Math.random() * 256)
            let g = Math.floor(Math.random() * 256)
            let b = Math.floor(Math.random() * 256)
            return `rgb(${r},${g},${b})`
        }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值