forked from rslashplace2/rslashplace2.github.io
-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvas-snow.min.js
6 lines (6 loc) · 3.9 KB
/
canvas-snow.min.js
1
2
3
4
5
6
/*!
* @cycjimmy/canvas-snow v3.0.2
* Homepage: https://github.com/cycjimmy/canvas-snow#readme
* Released under the MIT License.
*/
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).CanvasSnow=i()}(this,(function(){"use strict";function t(t,i){if(!(t instanceof i))throw new TypeError("Cannot call a class as a function")}function i(t,i){for(var n=0;n<i.length;n++){var e=i[n];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}function n(t,n,e){return n&&i(t.prototype,n),e&&i(t,e),Object.defineProperty(t,"prototype",{writable:!1}),t}var e=t=>"string"==typeof t&&t.constructor===String,s=function(){function i(n,e,s,a,h,r){t(this,i),this.x=n,this.y=e,this.r=s,this.fn=a,this.canvasW=h,this.canvasH=r}return n(i,[{key:"update",value:function(){this.y>this.canvasH?(this.x=Math.random()*this.canvasW,this.y=0):(this.x=this.fn.x(this.x,this.y),this.y=this.fn.y(this.y,this.y))}},{key:"draw",value:function(t){var i=t,n=i.createRadialGradient(this.x,this.y,0,this.x,this.y,this.r);n.addColorStop(0,"rgba(255, 255, 255, ".concat(this.r/6,")")),n.addColorStop(.5,"rgba(255, 255, 255, ".concat(this.r/6*.5,")")),n.addColorStop(1,"rgba(255, 255, 255, 0)"),i.fillStyle=n,i.fillRect(this.x-this.r,this.y-this.r,2*this.r,2*this.r)}}]),i}(),a=function(){function i(){t(this,i),this.list=[]}return n(i,[{key:"update",value:function(){for(var t=0,i=this.size();t<i;t+=1)this.list[t].update()}},{key:"push",value:function(t){this.list.push(t)}},{key:"draw",value:function(t){for(var i=0,n=this.size();i<n;i+=1)this.list[i].draw(t)}},{key:"get",value:function(t){return this.list[t]}},{key:"size",value:function(){return this.list.length}}]),i}(),h=function(t,i){return-1!==t.toString().indexOf("%")?t.split("%")[0]/100*i:e(t)?Number(t):t},r=function(t){var i=t.el,n=t.tip;switch(void 0===n?"horizontal":n){case"horizontal":return i.getBoundingClientRect().width;case"vertical":return i.getBoundingClientRect().height;default:return i.getBoundingClientRect()}};return function(){function i(n){var s,a=n.context,o=n.width,c=void 0===o?"100%":o,u=n.height,l=void 0===u?"100%":u,f=n.cell;t(this,i),this.context=e(a)?document.querySelector(a):a,this.width=(s=this.context,h(c,r({el:s,tip:"horizontal"}))),this.height=function(t,i){return h(i,r({el:t,tip:"vertical"}))}(this.context,l),this.cell=f,this.state="stop",this.snowList=null,this.canvas=null,this.cxt=null,this.interval=null}return n(i,[{key:"init",value:function(){return this.canvas||this._initCanvas(),this.snowList||(this.snowList=new a,this._newSnow()),this}},{key:"_initCanvas",value:function(){this.canvas=document.createElement("canvas"),this.context.appendChild(this.canvas),this.canvas.height=this.height,this.canvas.width=this.width,this.cxt=this.canvas.getContext("2d")}},{key:"start",value:function(){var t=this;this.snowList||this.init(),this.state="play",this.interval=setInterval((function(){t.cxt.clearRect(0,0,t.canvas.width,t.canvas.height),t.snowList.update(),t.snowList.draw(t.cxt)}),13)}},{key:"stop",value:function(){this.state="stop",clearInterval(this.interval),this.interval=null,this.cxt.clearRect(0,0,this.canvas.width,this.canvas.height)}},{key:"clear",value:function(){this.stop(),this.snowList=null}},{key:"_newSnow",value:function(){for(var t=0;t<this.cell;t+=1){var i=this._getRandom("x"),n=this._getRandom("y"),e=this._getRandom("r"),a=this._getRandom("fnx"),h=this._getRandom("fny"),r=new s(i,n,e,{x:a,y:h},this.width,this.height);r.draw(this.cxt),this.snowList.push(r)}}},{key:"_getRandom",value:function(t){var i,n;switch(t){case"x":i=Math.random()*this.width;break;case"y":i=(Math.random()-1)*this.height;break;case"r":i=2+4*Math.random();break;case"fnx":n=27+100*Math.random(),i=function(t,i){return t+.5*Math.sin(i/n)};break;case"fny":n=.4+1.4*Math.random(),i=function(t,i){return i+n}}return i}}]),i}()}));