benshepherd

Snow particles

Jun 23rd, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <html>
  3.     <head>
  4.         <title>HTML5 Canvas</title>
  5.         <style>
  6.            
  7.             * { padding:0; margin:0; }
  8.             body { overflow:hidden; }
  9.            
  10.         </style>
  11.         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  12.         <script>
  13.            
  14.             $(function() {
  15.                
  16.                var canvas = document.getElementById("canvas");
  17.                var ctx = canvas.getContext("2d");
  18.                
  19.                var W = $(document).width();
  20.                var H = $(document).height();
  21.                
  22.                canvas.width = W;
  23.                canvas.height = H;
  24.                
  25.                var parts = [];
  26.                var dir_x = ranInt(-10,10);
  27.                
  28.                function Rain() {
  29.                    this.x = ranInt(0,W);
  30.                    this.y = 0;
  31.                    this.s = ranInt(5,10);
  32.                    this.c = "rgba(255,255,255,1)";
  33.                    this.sx = 0;
  34.                    this.sy = ranInt(2,4) * ranInt(1,10);
  35.                }
  36.                
  37.                function Draw() {
  38.                    
  39.                    ctx.globalCompositeOperation = "source-over";
  40.                    ctx.fillStyle = "rgba(0,0,0,1)";
  41.                    ctx.fillRect(0,0,W,H);
  42.                    ctx.globalCompositeOperation = "lighter";
  43.                    
  44.                    for(var i = 0; i < parts.length; i++) {
  45.                        
  46.                        var p = parts[i];
  47.                        
  48.                        //draw particle
  49.                        ctx.beginPath();
  50.                        ctx.arc(p.x, p.y, p.s, 0, Math.PI * 2);
  51.                        ctx.fillStyle = p.c;
  52.                        ctx.fill();
  53.                        
  54.                        //update particle
  55.                        parts[i].x += dir_x;
  56.                        parts[i].y += p.sy;
  57.                        
  58.                        if(p.y > H - p.s || p.x > W - p.s || p.x < 0) {
  59.                            console.log(i + " is over the border, removing index");
  60.                            parts[i] = new Rain();
  61.                        }
  62.                        
  63.                    }
  64.                    
  65.                    /* debug purposes */
  66.                    ctx.fillStyle = "rgba(255,255,255,1)";
  67.                    ctx.font = "24px Arial";
  68.                    var debug = [
  69.                        "parts.length: " + parts.length,
  70.                        "dir_x: " + dir_x
  71.                        ];
  72.                    var h = 40;
  73.                    for(i = 0; i < debug.length; i++) {
  74.                        ctx.fillText(debug[i], 20, h*(i+1));
  75.                    }
  76.                    
  77.                }
  78.                
  79.                function CreateParticle() {
  80.                    
  81.                    if(parts.length != 100)
  82.                    parts.push(new Rain());
  83.                    
  84.                }
  85.                
  86.                setInterval(Draw, 33);
  87.                setInterval(CreateParticle, 100);
  88.                setInterval(function() {
  89.                    
  90.                    dir_x += ranInt(-2,2);
  91.                    
  92.                    if(dir_x > 10) {
  93.                        dir_x -= ranInt(3,7);
  94.                    }
  95.                    if(dir_x < -10) {
  96.                        dir_x += ranInt(3,7)
  97.                    }
  98.                    
  99.                }, 500);
  100.                
  101.                
  102.                /* Other important functions */
  103.                function ranInt(Min, Max) {
  104.                    return Math.floor(Math.random() * (Max-Min) + Min);
  105.                }
  106.             });
  107.            
  108.         </script>
  109.        
  110.     </head>
  111.    
  112.     <body>
  113.         <canvas id="canvas"></canvas>
  114.     </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment