moytrage

matrix_rain.html

Oct 8th, 2017
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <body>
  2.     <canvas id="c"></canvas>
  3.     <style type="text/css">
  4.         /*basic reset*/
  5.         * {margin: 0; padding: 0;}
  6.         /*adding a black bg to the body to make things clearer*/
  7.         body {background: black;}
  8.         canvas {display: block;}
  9.     </style>
  10.     <script>
  11.         var c = document.getElementById("c");
  12.         var ctx = c.getContext("2d");
  13.  
  14.         //making the canvas full screen
  15.         c.height = window.innerHeight;
  16.         c.width = window.innerWidth;
  17.  
  18.         //chinese characters - taken from the unicode charset
  19.         var chinese = "田由甲申甴电甶男甸甹町画甼甽甾甿畀畁畂畃畄畅畆畇畈畉畊畋界畍畎畏畐畑";
  20.         //converting the string into an array of single characters
  21.         chinese = chinese.split("");
  22.  
  23.         var font_size = 10;
  24.         var columns = c.width/font_size; //number of columns for the rain
  25.         //an array of drops - one per column
  26.         var drops = [];
  27.         //x below is the x coordinate
  28.         //1 = y co-ordinate of the drop(same for every drop initially)
  29.         for(var x = 0; x < columns; x++)
  30.             drops[x] = 1;
  31.  
  32.         //drawing the characters
  33.         function draw()
  34.         {
  35.             //Black BG for the canvas
  36.             //translucent BG to show trail
  37.             ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
  38.             ctx.fillRect(0, 0, c.width, c.height);
  39.            
  40.             ctx.fillStyle = "#0F0"; //green text
  41.             ctx.font = font_size + "px arial";
  42.             //looping over drops
  43.             for(var i = 0; i < drops.length; i++)
  44.             {
  45.                 //a random chinese character to print
  46.                 var text = chinese[Math.floor(Math.random()*chinese.length)];
  47.                 //x = i*font_size, y = value of drops[i]*font_size
  48.                 ctx.fillText(text, i*font_size, drops[i]*font_size);
  49.                
  50.                 //sending the drop back to the top randomly after it has crossed the screen
  51.                 //adding a randomness to the reset to make the drops scattered on the Y axis
  52.                 if(drops[i]*font_size > c.height && Math.random() > 0.975)
  53.                     drops[i] = 0;
  54.                
  55.                 //incrementing Y coordinate
  56.                 drops[i]++;
  57.             }
  58.         }
  59.  
  60.         setInterval(draw, 33);
  61.     </script>
  62. </body>
Advertisement
Add Comment
Please, Sign In to add comment