码迷,mamicode.com
首页 > Web开发 > 详细

黑客帝国雨效果JS

时间:2019-03-28 23:20:43      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:屏幕   title   margin   dex   draw   text   pre   over   文字   

黑客帝国雨效果JS。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    *{margin: 0;padding: 0;}
    body{overflow: hidden;}
    canvas{background: #111;}
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script>
        var canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");

        var W = window.innerWidth;
        var H = window.innerHeight;

        canvas.width = W;
        canvas.height = H;

        var fontSize = 16;
        //计算列
        var colunm = Math.floor(W/fontSize);
        //console.log(colunm);
        //存储Y值
        var drops = [];

        for(var i = 0;i<colunm;i++){
            drops[i] = 1;
        }
        //console.log(...drops)
        var str = "Welcome JavaScript";

        //let tempRandom = Math.random()*str.length;
        //console.log(tempRandom,Math.floor(tempRandom))
        //把文字画到屏幕上
        function draw(){
            context.fillStyle = "rgba(0,0,0,0.05)";//文字随机出现的背景
            context.fillRect( 0, 0, W, H);
            context.font = "700 " + fontSize + "px ARIAL";
            context.fillStyle = "#00cc33";
            //context.fillStyle = randColor();
            //console.log("你好");
            for(var i = 0; i<colunm; i++){
                //让字符串中的内容随机出现
                var index = Math.floor(Math.random()*str.length);
                var x = i*fontSize;
                var y = drops[i]*fontSize;
                context.fillText(str[index],x,y);
                console.log(str[index],x,y);
                if(y>=canvas.height && Math.random()>0.99){
                    drops[i] = 0;
                }
            drops[i]++;
            }
        }

        function randColor(){
            var r = Math.floor(Math.random()*256);
            var g = Math.floor(Math.random()*256);
            var b = Math.floor(Math.random()*256);
            return "rgb("+r+","+g+","+b+")";
        }

        draw();
        setInterval(draw,20);
    </script>
</body>
</html>

效果截图:

技术图片

 

黑客帝国雨效果JS

标签:屏幕   title   margin   dex   draw   text   pre   over   文字   

原文地址:https://www.cnblogs.com/liubeimeng/p/10618388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!