JS实现雪花效果
演示效果
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实现雪花效果的更多相关文章
- js实现雪花飘落效果的代码
使用js实现雪花飘落的效果,用html5绘布加js写的雪花飘效果 . 代码: <html> <head> <script> /** * js与html5实现的雪花飘 ...
- js之雪花飘落
有很多网站都有雪花或花瓣飘落的特效,看上去很好看.我来用js实现这个效果. 在写代码之前可以先引入bass.css对样式做下处理: 1.html部分 先建一个文件夹,在body中插入如下代码 < ...
- Html页面雪花效果的实现
简单介绍 昨天修改了一下博客所用的模板,冬天来了,给自己的博客加点雪花,感觉更有意境. 百度找到了非常多的结果,最终还是选用了cfs.snow.js,很赞压缩之后只有1kb左右,而且不会影响页面使用, ...
- React.js实现原生js拖拽效果及思考
一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...
- 用js实现动画效果核心方式
为了做好导航菜单,有时候需要在菜单下拉的时候实现动画效果,所以这几天就研究了研究如何用js实现动画效果,实现动画核心要用到两个函数,一个是setTimeOut,另一个是setInterval. 下边我 ...
- CAEmitterLayer实现雪花效果
CAEmitterLayer 简介 在iOS5.0中,Apple引入了CAEmitterLayer层,CAEmitterLayer是一个高性能的粒子效果引擎,被用来创建实时粒子动画,如:烟雾,火,雨等 ...
- js拖拽效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 原生JS实现分页效果2.0(新增了上一页和下一页,添加当前元素样式)
虽然写的很烂,但至少全部都是自己写的,因为这个没有固定的顺序,所以就没有封装,如果你技术好的话,可以你写的分享给我,谢谢. <!DOCTYPE html><html lang=&qu ...
- 原生JS实现分页效果1.0
不太完整,写的太急,等等加上完整注释,写起来还是有些难度的,写的有点水,后面再改进改进. <!DOCTYPE html><html lang="en">&l ...
随机推荐
- 03 Apache Solr: 安装和运行
前面介绍了Solr在项目中的使用和构建高度可用.高度可扩展的Solr服务器的一些想法.但是光说不练假把式,现在开始,把Solr运行起来继续深入了解吧! 安装 安装JAVA Apache So ...
- 合并两个排好序的链表(c++)
#include<iostream> struct node{ int payload; node* next; node(int payload){this->payload=pa ...
- TCP协议学习记录 (二) Ping程序
简单的实现了一个ping程序,没有做icmp差错报文的检查. 支持自定义字节数,支持发包个数 #pragma pack(4) #define ECHO_REQUEST 8 #define DATASI ...
- yarn container启动失败
在yarn资源管理的集群上运行spark程序,无法读取的数据多与少,都会报这个错误,但是其他程序在集群上能够正常运行. 16/11/14 00:13:44 WARN cluster.YarnSched ...
- solr多词匹配搜索问题及解决
使用solr进行某较长词搜索时出现了一些问题,及解决方案. 1.问题:solr默认使用OR方式搜索,当搜索一个很长的次,比如“XX集团股份有限公司”,分词器分词后,使用OR方式匹配,会匹配到很多结果. ...
- windows 下ssh 客户端
今天用 xshell 和 bitvise ssh 在 windows 登录SSH ,发现 vi 一些中文内容的文件会出现乱码,配置无效,在网上找到一款不会中文乱码的SSH客户端 MobaXterm , ...
- 强大的Spring缓存技术(上)
缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 cache 配置方法,展现了 ...
- 建站阿里云、amh主机面版
阿里云 Nginx+tomcat7+Mencached负载均衡集群配置 http://blog.csdn.net/zht666/article/details/38515147 apache2.2.1 ...
- 转载:java 中对类中的属性使用set/get方法的意义和用法
经常看到有朋友提到类似:对类中的属性使用set/get方法的作用?理论的回答当然是封闭性之类的,但是这样对我们有什么作用呢?为什么要这样设计?我直接使用属性名来访问不是更直接,代码更简洁明了吗?下面我 ...
- 最小生成树算法——Kruskal算法
#include<stdio.h> #include<algorithm> #include<windows.h> using namespace std; str ...