Advertisement
Bungeetaco

Matrix JS

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