-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflake.min.js
6 lines (5 loc) · 3.23 KB
/
snowflake.min.js
1
2
3
4
5
6
/*!
Made With JOY by What Cheer
http://whatcheer.com/
*/
function SnowFlake(b){var a=this;this.options={parent:document.body,img:"small.png",weight:1,sway:10,sway_speed:0.15,roll_delay:0};this.width=0;this.height=0;this.bottom_buffer=0;this.max_dimension=0;this.ready=false;this.error=false;for(var c in b){this.options[c]=b[c]}this.img=document.createElement("img");this.img.onload=function(){a.width=this.width;a.height=this.height;a.max_dimension=(a.width>a.height)?a.width:a.height;a.bottom_buffer=Math.floor(a.max_dimension/4);a.ready=true};this.img.onerror=function(){a.error=true};this.img.setAttribute("src",this.options.img);this.img.style.position="absolute";this.img.style.zIndex=99;this.timer=null;this.top=this.options.top;this.left=this.options.left;this.ctr=Math.floor(Math.random()*this.options.sway)+1;this.y_accel=0;this.x_accel=0;this.start=function(){var d=this;if(!this.ready){if(!d.error){this.timer=setTimeout(function(){d.start()},250)}return}this.draw();this.options.parent.appendChild(this.img);this.timer=setInterval(function(){d.tick()},100);if("undefined"==typeof this.img.addEventListener){this.img.attachEvent("onmouseover",function(f){return d.mouseover(f)})}else{this.img.addEventListener("mouseover",function(f){return d.mouseover(f)})}};this.mouseover=function(f){var d=this.left+(this.img.clientWidth/2);if(f.x<d){this.x_accel=10}else{this.x_accel=-10}this.y_accel=-10};this.stop=function(){clearInterval(this.timer)};this.tick=function(){var d=this.top+this.max_dimension+this.options.weight+this.y_accel;if(d>this.minHeight()-this.bottom_buffer){this.top=this.options.top;this.left=this.options.left;this.y_accel=0;this.x_accel=0}else{this.top+=this.options.weight+this.y_accel;this.ctr++}if(this.y_accel<0){this.y_accel=this.y_accel+1}if(this.y_accel>0){this.y_accel=this.y_accel-1}if(this.x_accel>0){this.options.left+=--this.x_accel}if(this.x_accel<0){this.options.left+=++this.x_accel}this.left=this.options.left+Math.sin(this.ctr*this.options.sway_speed)*this.options.sway;if(this.left+this.max_dimension+1>=this.minWidth()){this.left=this.maxWidth()-this.max_dimension-1;this.options.left=this.left-this.max_dimension}this.draw()};this.draw=function(){this.img.style.top=this.top+"px";this.img.style.left=this.left+"px";var f=this.maxHeight()-(this.top+this.height+this.bottom_buffer),g=Math.floor(this.maxHeight()/4),e=1;e=(f<g)?f/g:1;this.img.filter="alpha(opacity="+Math.floor(e*100)+")";this.img.style.opacity=e;var d="rotate("+(this.ctr*this.options.roll_delay)%360+"deg)";this.img.style.webkitTransform=d;this.img.style.mozTransform=d;this.img.style.msTransform=d;this.img.style.oTransform=d};this.maxWidth=function(){return(this.options.parent.clientWidth>this.options.parent.offsetWidth)?this.options.parent.offsetWidth:this.options.parent.clientWidth};this.minWidth=function(){return(this.options.parent.clientWidth<this.options.parent.offsetWidth)?this.options.parent.offsetWidth:this.options.parent.clientWidth};this.maxHeight=function(){return(this.options.parent.clientHeight>this.options.parent.offsetHeight)?this.options.parent.offsetHeight:this.options.parent.clientHeight};this.minHeight=function(){return(this.options.parent.clientHeight<this.options.parent.offsetHeight)?this.options.parent.offsetHeight:this.options.parent.clientHeight}};