演示效果

http://www.9696e.com/demo/snow/

春节之前新一博客也会一直挂着的。

加载链接

<script src="http://www.9696e.com/demo/snow/snow.js" type="text/javascript"></script>

源码奉上

1
(function(){var t,e,n,a,o,i,r=[],s=[],u=0;requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame||window.oRequestAnimationFrame||function(e){var n=(new Date).getTime();var a=Math.max(0,16-(n-u));t=setTimeout(function(){e(n+a)},a);u=n+a};cancelAnimationFrame=window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame||window.msCancelAnimationFrame||window.oCancelAnimationFrame||function(){clearTimeout(t)};function l(){if(!(this instanceof l)){throw new Error("please use new() create instance");return}if(m.call(this)==false){return}this.status=0;this.start()}function m(){var t=document.createElement("canvas");this.canvas=t;var e=document.createElement("x");e.style.cssText="pointer-events:auto";return!!(t.getContext&&t.getContext("2d")&&e.style.pointerEvents==="auto")}l.prototype.start=function(){var t=this;if(this.status===1){return false}this.status=1;this.snowCanvas();this.createFlakes();requestAnimationFrame(c)};l.prototype.stop=function(){if(this.status===2)return;this.status=2;cancelAnimationFrame()};l.prototype.snowCanvas=function(){var t=this.canvas;e=window.innerWidth;n=window.innerHeight;t.id="J_snowfall";t.width=e;t.height=n;t.setAttribute("style","position: fixed; top: 0; left: 0; z-index: 9000; pointer-events: none;");document.body.appendChild(t);o=t;i=t.getContext("2d");i.strokeStyle="none";window.onresize=function(){e=window.innerWidth;n=window.innerHeight;t.width=e;t.height=n;flakeCount=e*n/6e3;h.createFlakes()}};l.prototype.createFlakes=function(){var t,n,o,r,u="#FFF",l=[.45,.55,.8,.9,1];r=[40,50,60,80,100];for(var m=0;m<5;m++){t=document.createElement("canvas");t.width=r[m];t.height=r[m];n=t.getContext("2d");n.font=r[m]/2+"px sumson";o=n.createRadialGradient(r[m]/2,r[m]/2,0,r[m]/2,r[m]/2,r[m]/2);o.addColorStop(0,u);o.addColorStop(.1,u);o.addColorStop(.85,f(u,l[m]));o.addColorStop(1,f(u,0));n.fillStyle=o;n.fillText("*",r[m]/2,r[m]/2);s.push(t)}a=i.createLinearGradient(0,0,0,e);d()};function d(){var t;var a=(e-940)/2;flakeCount=e*n/6e3;for(var o=0;o<flakeCount;o++){t=o<flakeCount*.6?0:o<flakeCount*.8?1:o<flakeCount*.9?2:o<flakeCount*.98?3:4;var i=Math.floor(Math.random()*10)>=5?a+940:a;var s=i<940?Math.random()*i:940+a+Math.random()*a;r[o]=[s,Math.random()*n,t]}}function c(t){i.fillStyle=a,i.clearRect(0,0,e,n),i.beginPath();for(var o=0;o<flakeCount;o++){var u=r[o][0];var l=r[o][1];var m=s[r[o][2]];i.drawImage(m,u,l)}i.fill();w();requestAnimationFrame(c)}function f(t,e){var n,a,o;t=t.replace(/^s*#|s*$/g,"");t.length===3&&(t=t.replace(/([0-9a-fA-F])/g,"$1$1"));n=parseInt(t.substr(0,2),16);a=parseInt(t.substr(2,2),16);o=parseInt(t.substr(4,2),16);return"rgba("+n+", "+a+", "+o+", "+e+")"}function w(){var t,a,o,i,s,u,l;p=.01;a=Math.sin(p);l=(e-940)/2;for(var m=0;m<flakeCount;m++){u=r[m];o=Math.sin(4*p+m);u[1]+=(u[2]/2+(2+o))/2;u[0]+=6*(a+o/2)/(10/u[2]);s=Math.floor(Math.random()*10)>=5?l+940:l;i=s<940?Math.random()*s:940+l+Math.random()*l;u[1]>n&&(u[1]=-20,u[0]=i);if(u[0]>l&&u[0]<940+l||u[0]>e||u[0]<-20){u[0]>e?u[0]=-20:u[0]=e-20}r[m]=u}}var h=new l})();

来自:http://outofmemory.cn/wr/?u=http%3A%2F%2Fwww.9696e.com%2Farchives%2F1573

JS实现雪花效果的更多相关文章

  1. js实现雪花飘落效果的代码

    使用js实现雪花飘落的效果,用html5绘布加js写的雪花飘效果 . 代码: <html> <head> <script> /** * js与html5实现的雪花飘 ...

  2. js之雪花飘落

    有很多网站都有雪花或花瓣飘落的特效,看上去很好看.我来用js实现这个效果. 在写代码之前可以先引入bass.css对样式做下处理: 1.html部分 先建一个文件夹,在body中插入如下代码 < ...

  3. Html页面雪花效果的实现

    简单介绍 昨天修改了一下博客所用的模板,冬天来了,给自己的博客加点雪花,感觉更有意境. 百度找到了非常多的结果,最终还是选用了cfs.snow.js,很赞压缩之后只有1kb左右,而且不会影响页面使用, ...

  4. React.js实现原生js拖拽效果及思考

    一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...

  5. 用js实现动画效果核心方式

    为了做好导航菜单,有时候需要在菜单下拉的时候实现动画效果,所以这几天就研究了研究如何用js实现动画效果,实现动画核心要用到两个函数,一个是setTimeOut,另一个是setInterval. 下边我 ...

  6. CAEmitterLayer实现雪花效果

    CAEmitterLayer 简介 在iOS5.0中,Apple引入了CAEmitterLayer层,CAEmitterLayer是一个高性能的粒子效果引擎,被用来创建实时粒子动画,如:烟雾,火,雨等 ...

  7. js拖拽效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 原生JS实现分页效果2.0(新增了上一页和下一页,添加当前元素样式)

    虽然写的很烂,但至少全部都是自己写的,因为这个没有固定的顺序,所以就没有封装,如果你技术好的话,可以你写的分享给我,谢谢. <!DOCTYPE html><html lang=&qu ...

  9. 原生JS实现分页效果1.0

    不太完整,写的太急,等等加上完整注释,写起来还是有些难度的,写的有点水,后面再改进改进. <!DOCTYPE html><html lang="en">&l ...

随机推荐

  1. NGUI 屏幕自适应大屏与小屏(初始设定宽高为1280x720,能适应比其小或者更大的屏)

    具体细节可以参考另外一篇随笔! 以下提供的算法完成的事: 1.自适应1280x720分辨率以下的屏幕 2.自适应1280x720分辨率以上的屏幕 在我设定的要求内包括的分辨率大部分都测过了,背景图.全 ...

  2. 【go语言】RSA加解密

    关于go语言的RSA加解密的介绍,这里有一篇文章,已经介绍的很完整了. 对应的go语言的加解密代码,参考git. 因为原文跨语言是跟php,我这里要跟c语言进行交互,所以,这里贴上c语言的例子. 参考 ...

  3. redis 在linux下的安装

    背景:在项目互联网项目开发中我们经常需要缓存热点数据,在做热点数据缓存时有2个常用的方案 redis简介redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型 ...

  4. ABAP 动态内表添加单元格颜色字段

    *动态内表alv显示时要求某些单元格显示颜色 *wa_fldcat-datatype不能添加LVC_T_SCOL类型,在创建好内表之后,再添加颜色列. DATA: wa_fldcat TYPE lvc ...

  5. How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)

    Question: On CentOS 7, I want to switch from DHCP to static IP address configuration with one of my ...

  6. NES模拟器开发-CPU笔记

    我的项目XNES已经开始动手编码了,目前的进度大概是cpu的模拟完成了大概10~20%左右.简单记录一下CPU模拟过程中遇到的问题和思考. 原理: cpu模拟实际就是模拟cpu处理opcode的过程, ...

  7. RHEL7关于时间的学习笔记

    当你发现时间是贼了,它早已偷光你的选择. 一,GMT.UTC.CST GMT:(Greenwich Mean Time)格林威治时间 ,太阳通过格林威治那一刻来作为计时标准. UTC:(Coordin ...

  8. 解决WampServer中MySQL数据库中文乱码的问题

    原文地址:http://blog.csdn.net/qq756703833/article/details/37971057 左键点击托盘区的WampServer图标,选择MySQL--my.ini, ...

  9. ZOJ3792_Romantic Value

    给出图,使得两点无流量,剩余其他边的总容量与删除边数的比值. 要机智啊... 因为原图给的边数不超过1000,容量也不超过1000,可以这样把边的容量变为2000*c+1.这样跑出最大流后,最大流除以 ...

  10. python核心编程笔记

    1.python的对象是通过引用传递的. 2.多元赋值: x, y = y, x