zhangsongcui

Char Rain (JS ver)

Jul 20th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.50 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Charactor Rain</title>
  6. </head>
  7.  
  8. <body>
  9. <canvas id="canvas" width="640" height="480" style="background: black">ooch!</canvas>
  10. <script>
  11.     "use strict";
  12.  
  13.     function getRand(max, min) {
  14.         min = min || 0;
  15.         return (min + Math.random() * (max - min)) | 0;
  16.     }
  17.  
  18.     function Char( x, n) {
  19.         this.X = x;
  20.         this.Y = 0;
  21.         this.clr = n === 0 ? 'white' : 'rgba(0,' + ((255 * (1 - n / 25)) | 0) +',0,1)';
  22.         this.shadow = n === 0;
  23.     }
  24.  
  25.     Char.prototype.draw = function Draw(ctx) {
  26.         if (this.shadow) {
  27.             ctx.shadowBlur = 8;
  28.         } else {
  29.             ctx.shadowBlur = 0;
  30.         }
  31.         ctx.fillStyle = this.clr;
  32.         ctx.fillText(String.fromCharCode(getRand(126, 33)), this.X, this.Y);
  33.         this.Y += 15;
  34.         if (this.Y >= ctx.canvas.height) {
  35.             this.Y = 0;
  36.         }
  37.     };
  38.  
  39.     (function main() {
  40.         var columnCount = 24;
  41.         var columns = [], startTimes = [];
  42.         for (var t = columnCount; t--; ) {
  43.             columns.push([]);
  44.         }
  45.         for (var t = columnCount; t--; ) {
  46.             startTimes.push(getRand(0, -30));
  47.         }
  48.         var canvas = document.getElementById("canvas");
  49.         var canvasCtx = canvas.getContext("2d");
  50.         var buffer = document.createElement("canvas");
  51.         buffer.width = canvas.width;
  52.         buffer.height = canvas.height;
  53.         var ctx = buffer.getContext("2d");
  54.  
  55.         function Repaint() {
  56.             ctx.fillStyle = "Black";
  57.             ctx.clearRect(0, 0, buffer.width, buffer.height);
  58.             columns.forEach(function(column, idx) {
  59.                 if (startTimes[idx] < 25) {
  60.                    if (startTimes[idx] >= 0) {
  61.                         column.push(new Char(25 * (idx + 1), startTimes[idx]));
  62.                     }
  63.                     ++startTimes[idx];
  64.                 }
  65.             });
  66.             columns.forEach(function(column) {
  67.                 column.forEach(function (char) {
  68.                     char.draw(ctx);
  69.                 });
  70.             });
  71.             canvasCtx.clearRect(0, 0, canvas.width, canvas.height);
  72.             canvasCtx.drawImage(buffer, 0, 0);
  73.         }
  74.  
  75.         ctx.font = 'bold 15px "Helvetica Neue", Arial';
  76.         ctx.textAlign = 'center';
  77.         ctx.shadowColor = "yellow";
  78.         ctx.shadowOffsetX = 0;
  79.         ctx.shadowOffsetY = 0;
  80.         setInterval(Repaint, 50);
  81.     })();
  82. </script>
  83. </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment