1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
   | 
 
  function snowFall(snow) {          snow = snow || {};     this.maxFlake = snow.maxFlake || 200;        this.flakeSize = snow.flakeSize || 10;       this.fallSpeed = snow.fallSpeed || 1;    }
  requestAnimationFrame = window.requestAnimationFrame ||     window.mozRequestAnimationFrame ||     window.webkitRequestAnimationFrame ||     window.msRequestAnimationFrame ||     window.oRequestAnimationFrame ||     function(callback) { setTimeout(callback, 1000 / 60); };
  cancelAnimationFrame = window.cancelAnimationFrame ||     window.mozCancelAnimationFrame ||     window.webkitCancelAnimationFrame ||     window.msCancelAnimationFrame || 	window.oCancelAnimationFrame;
  snowFall.prototype.start = function(){          snowCanvas.apply(this);          createFlakes.apply(this);          drawSnow.apply(this) }
  function snowCanvas() {          var snowcanvas = document.createElement("canvas");     snowcanvas.id = "snowfall";     snowcanvas.width = window.innerWidth;     snowcanvas.height = document.body.clientHeight;     snowcanvas.setAttribute("style", "position:absolute; top: 0; left: 0; z-index: 1; pointer-events: none;");     document.getElementsByTagName("body")[0].appendChild(snowcanvas);     this.canvas = snowcanvas;     this.ctx = snowcanvas.getContext("2d");          window.onresize = function() {         snowcanvas.width = window.innerWidth;              } }
  function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {     this.x = Math.floor(Math.random() * canvasWidth);        this.y = Math.floor(Math.random() * canvasHeight);       this.size = Math.random() * flakeSize + 2;               this.maxSize = flakeSize;                                this.speed = Math.random() * 1 + fallSpeed;              this.fallSpeed = fallSpeed;                              this.velY = this.speed;                                  this.velX = 0;                                           this.stepSize = Math.random() / 30;                      this.step = 0                                        } flakeMove.prototype.update = function() {     var x = this.x,         y = this.y;          this.velX *= 0.98;     if (this.velY <= this.speed) {         this.velY = this.speed     }     this.velX += Math.cos(this.step += .05) * this.stepSize;
      this.y += this.velY;     this.x += this.velX;          if (this.x >= canvas.width || this.x <= 0 || this.y >= canvas.height || this.y <= 0) {         this.reset(canvas.width, canvas.height)     } };
  flakeMove.prototype.reset = function(width, height) {     this.x = Math.floor(Math.random() * width);     this.y = 0;     this.size = Math.random() * this.maxSize + 2;     this.speed = Math.random() * 1 + this.fallSpeed;     this.velY = this.speed;     this.velX = 0; };
  flakeMove.prototype.render = function(ctx) {     var snowFlake = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);     snowFlake.addColorStop(0, "rgba(255, 255, 255, 0.9)");       snowFlake.addColorStop(.5, "rgba(255, 255, 255, 0.5)");      snowFlake.addColorStop(1, "rgba(255, 255, 255, 0)");         ctx.save();     ctx.fillStyle = snowFlake;     ctx.beginPath();     ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);     ctx.fill();     ctx.restore(); };
  function createFlakes() {     var maxFlake = this.maxFlake,         flakes = this.flakes = [],         canvas = this.canvas;     for (var i = 0; i < maxFlake; i++) {         flakes.push(new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed))     } }
  function drawSnow() {     var maxFlake = this.maxFlake,         flakes = this.flakes;     ctx = this.ctx, canvas = this.canvas, that = this;          ctx.clearRect(0, 0, canvas.width, canvas.height);     for (var e = 0; e < maxFlake; e++) {         flakes[e].update();         flakes[e].render(ctx);     }          this.loop = requestAnimationFrame(function() {         drawSnow.apply(that);     }); }
  var snow = new snowFall({maxFlake:60}); snow.start();
 
  |