Snow fall without writing code

just save it snowfall.js and

/** @license
 *
 SnowStorm.dg.js
 Snow Fall Effect - JavaScript-based Snow for web pages
 * --------------------------------------------------------
 * Copyright (c) 2012, Dhiraj Kumar. All rights reserved.
 */

/*global window, document, navigator, clearInterval, setInterval */

var snowStorm = (function(window, document) {

  // --- common properties ---

  this.autoStart = true;          // Whether the snow should start automatically or not.
  this.flakesMax = 150;           // Limit total amount of snow made (falling + sticking)
  this.flakesMaxActive = 80;      // Limit amount of snow falling at once (less = lower CPU use)
  this.animationInterval = 60;    // Theoretical "miliseconds per frame" measurement. 20 = fast + smooth, but high CPU use. 50 = more conservative, but slower
  this.excludeMobile = true;      // Snow is likely to be bad news for mobile phones' CPUs (and batteries.) By default, be nice.
  this.flakeBottom = null;        // Integer for Y axis snow limit, 0 or null for "full-screen" snow effect
  this.followMouse = true;        // Snow movement can respond to the user's mouse
  this.snowColor = '#fff';        // Don't eat (or use?) yellow snow.
  this.snowCharacter = '*';        // • = bullet, · is square on some systems etc.
  this.snowStick = true;          // Whether or not snow should "stick" at the bottom. When off, will never collect.
  this.targetElement = null;      // element which snow will be appended to (null = document.body) - can be an element ID eg. 'myDiv', or a DOM node reference
  this.useMeltEffect = true;      // When recycling fallen snow (or rarely, when falling), have it "melt" and fade out if browser supports it
  this.useTwinkleEffect = false;  // Allow snow to randomly "flicker" in and out of view while falling
  this.usePositionFixed = false;  // true = snow does not shift vertically when scrolling. May increase CPU load, disabled by default - if enabled, used only where supported

  // --- less-used bits ---

  this.freezeOnBlur = true;       // Only snow when the window is in focus (foreground.) Saves CPU.
  this.flakeLeftOffset = 0;       // Left margin/gutter space on edge of container (eg. browser window.) Bump up these values if seeing horizontal scrollbars.
  this.flakeRightOffset = 0;      // Right margin/gutter space on edge of container
  this.flakeWidth = 60;            // Max pixel width reserved for snow element
  this.flakeHeight = 60;           // Max pixel height reserved for snow element
  this.vMaxX = 5;                 // Maximum X velocity range for snow
  this.vMaxY = 4;                 // Maximum Y velocity range for snow
  this.zIndex = 0;                // CSS stacking order applied to each snowflake

  // --- End of user section ---

function doStart(){if(!s.excludeMobile||!isMobile){if(s.freezeOnBlur){s.events.add(isIE?document:window,"mousemove",doDelayedStart)}else{doDelayedStart()}}s.events.remove(window,"load",doStart)}function doDelayedStart(){window.setTimeout(function(){s.start(true)},20);s.events.remove(isIE?document:window,"mousemove",doDelayedStart)}function plusMinus(a){return parseInt(rnd(2),10)===1?a*-1:a}function rnd(a,b){if(isNaN(b)){b=0}return Math.random()*a+b}var s=this,storm=this,i,isIE=navigator.userAgent.match(/msie/i),isIE6=navigator.userAgent.match(/msie 6/i),isWin98=navigator.appVersion.match(/windows 98/i),isMobile=navigator.userAgent.match(/mobile|opera m(ob|in)/i),isBackCompatIE=isIE&&document.compatMode==="BackCompat",noFixed=isMobile||isBackCompatIE||isIE6,screenX=null,screenX2=null,screenY=null,scrollY=null,vRndX=null,vRndY=null,windOffset=1,windMultiplier=2,flakeTypes=6,fixedForEverything=false,opacitySupported=function(){try{document.createElement("div").style.opacity="0.5"}catch(a){return false}return true}(),didInit=false,docFrag=document.createDocumentFragment();this.timers=[];this.flakes=[];this.disabled=false;this.active=false;this.meltFrameCount=20;this.meltFrames=[];this.events=function(){function g(){e(d(arguments),"remove")}function f(){e(d(arguments),"add")}function e(b,d){var e=b.shift(),f=[c[d]];if(a){e[f](b[0],b[1])}else{e[f].apply(e,b)}}function d(c){var d=b.call(c),e=d.length;if(a){d[1]="on"+d[1];if(e>3){d.pop()}}else if(e===3){d.push(false)}return d}var a=!window.addEventListener&&window.attachEvent,b=Array.prototype.slice,c={add:a?"attachEvent":"addEventListener",remove:a?"detachEvent":"removeEventListener"};return{add:f,remove:g}}();this.randomizeWind=function(){var a;vRndX=plusMinus(rnd(s.vMaxX,.2));vRndY=rnd(s.vMaxY,.2);if(this.flakes){for(a=0;a=0&&e.vX<.2){e.vX=.2}else if(e.vX<0 amp="" e.vx="">-.2){e.vX=-.2}if(e.vY>=0&&e.vY<.2){e.vY=.2}};this.move=function(){var a=e.vX*windOffset,b;e.x+=a;e.y+=e.vY*e.vAmp;if(e.x>=screenX||screenX-e.x.998){e.melting=true;e.melt()}if(f.useTwinkleEffect){if(!e.twinkleFrame){if(Math.random()>.9){e.twinkleFrame=parseInt(Math.random()*20,10)}}else{e.twinkleFrame--;e.o.style.visibility=e.twinkleFrame&&e.twinkleFrame%2===0?"hidden":"visible"}}}};this.animate=function(){e.move()};this.setVelocities=function(){e.vX=vRndX+rnd(f.vMaxX*.12,.1);e.vY=vRndY+rnd(f.vMaxY*.12,.1)};this.setOpacity=function(a,b){if(!opacitySupported){return false}a.style.opacity=b};this.melt=function(){if(!f.useMeltEffect||!e.melting){e.recycle()}else{if(e.meltFrames.flakesMaxActive){s.flakes[s.flakes.length-1].active=-1}}storm.targetElement.appendChild(docFrag)};this.timerInit=function(){s.timers=!isWin98?[setInterval(s.snow,s.animationInterval)]:[setInterval(s.snow,s.animationInterval*3),setInterval(s.snow,s.animationInterval)]};this.init=function(){var a;for(a=0;a  return this;

}(window, document));

Comments