canvas+js 实现黑客帝国代码雨效果

JS or jQuery 4 个月前 回复

, ,

使用canvas+js实现黑客帝国代码雨效果

html代码:

<canvas width="1272" height="275"></canvas>

js代码:

let canvas = document.querySelector('canvas');
let ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let str = "hjyl.org".split('');
let arr = Array(Math.ceil(canvas.width / 10)).fill(0);
let run = () => {
    ctx.fillStyle = 'rgba(0,0,0,0.05)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = '#0f0';
    arr.forEach((item, index) => {
        ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, item + 10);
        arr[index] = item > canvas.height || item > 10000 * Math.random() ? 0 : item + 10;
    });
};
// run()
setInterval(run, 40);
支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)